Skip to content

Commit f3a5adb

Browse files
authored
Merge pull request #31 from g4-api/development
Development
2 parents 0ed1a18 + 210c9c0 commit f3a5adb

131 files changed

Lines changed: 15471 additions & 3994 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
using G4.Extensions;
2+
using G4.IntegrationTests.Framework;
3+
using G4.Models;
4+
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
using System.Collections.Generic;
8+
9+
namespace G4.IntegrationTests.Plugins.Common.ExportData
10+
{
11+
internal class C0006(TestContext context) : TestCaseBase(context)
12+
{
13+
protected override IEnumerable<G4RuleModelBase> OnActions(AutomationEnvironment environment)
14+
{
15+
// Return a collection of action rule models
16+
return
17+
[
18+
// No-action action: Export Data Container
19+
new ActionRuleModel()
20+
{
21+
PluginName = "NoAction",
22+
Argument = "Export Data Container",
23+
Rules = OnExtractions(environment)
24+
}
25+
];
26+
}
27+
28+
protected override IEnumerable<ExtractionRuleModel> OnExtractions(AutomationEnvironment environment)
29+
{
30+
// Get the file name from the test parameters
31+
var fileName = environment.TestParameters["fileName"];
32+
33+
// Check if trimming is requested in the test parameters.
34+
var trim = environment.TestParameters.Get(key: "trim", defaultValue: false);
35+
36+
// Check if clearing line breaks is requested in the test parameters.
37+
var clearLinesBreak = environment.TestParameters.Get(key: "clearLinesBreak", defaultValue: false);
38+
39+
// Check if the test parameters contain a flag to export for entity
40+
var forEntity = environment.TestParameters.Get(key: "forEntity", defaultValue: false);
41+
42+
// Check if the extraction scope is specified in the test parameters.
43+
// If specified, use the provided extraction scope.
44+
// If not specified, use the default "Elements" scope.
45+
var extractionScope = environment.TestParameters.Get(key: "extractionScope", defaultValue: "Elements");
46+
47+
// Check if the content type is specified in the test parameters.
48+
var contentType = environment.TestParameters.Get(key: "contentType", defaultValue: "WebElementContent");
49+
50+
// Create a G4DataProviderModel for collecting SQLight data.
51+
var dataCollector = new G4DataProviderModel
52+
{
53+
Type = "SqLiteDataCollector", // Specify the type of data collector.
54+
ForEntity = forEntity, // Indicate that it's for an entity.
55+
Source = $"{fileName}.db", // Specify the data source (SQLight file).
56+
Repository = "TestData" // The table name in the database to store the extracted data.
57+
};
58+
59+
// Define an extraction rule for hotel information
60+
var extractionRule = new ExtractionRuleModel
61+
{
62+
Argument = "{{$ --Scope:" + extractionScope + "}}", // Argument to specify the extraction scope
63+
OnElement = "//div[@class='hotel']", // XPath expression to find hotel elements
64+
DataCollector = dataCollector, // Data collector for saving the extracted data
65+
Rules =
66+
[
67+
// Content rule for extracting 'Location' information
68+
new ContentRuleModel
69+
{
70+
OnElement = ".//p[starts-with(.,'Location:')]", // XPath for 'Location' element
71+
Key = "Location", // Key to identify this extraction
72+
PluginName = contentType, // Plugin to use for extraction
73+
RegularExpression = @"(?<=\w+:).*", // Regular expression to extract text
74+
Transformers = NewTransformers("Location", trim, clearLinesBreak) // Transformers to apply to the extracted text
75+
},
76+
// Content rule for extracting 'Price' information
77+
new ContentRuleModel
78+
{
79+
OnElement = ".//p[starts-with(.,'Price:')]",
80+
Key = "Price",
81+
PluginName = contentType,
82+
RegularExpression = @"-?\d*\.?\d+",
83+
DataType = "number",
84+
Transformers = NewTransformers("Price", trim, clearLinesBreak)
85+
},
86+
// Content rule for extracting 'Rating' information
87+
new ContentRuleModel
88+
{
89+
OnElement = ".//p[starts-with(.,'Rating:')]",
90+
Key = "Rating",
91+
PluginName = contentType,
92+
RegularExpression = @"-?\d*\.?\d+",
93+
DataType = "number",
94+
Transformers = NewTransformers("Rating", trim, clearLinesBreak)
95+
},
96+
// Content rule for extracting 'Amenities' information
97+
new ContentRuleModel
98+
{
99+
OnElement = ".//p[starts-with(.,'Amenities:')]",
100+
Key = "Amenities",
101+
PluginName = contentType,
102+
RegularExpression = @"(?<=\w+:\s+).*",
103+
Transformers = NewTransformers("Amenities", trim, clearLinesBreak)
104+
},
105+
// Content rule for extracting 'Description' information
106+
new ContentRuleModel
107+
{
108+
OnElement = ".//p[starts-with(.,'Description:')]",
109+
Key = "Description",
110+
PluginName = contentType,
111+
RegularExpression = @"(?<=\w+:\s+).*",
112+
Transformers = NewTransformers("Description", trim, clearLinesBreak)
113+
},
114+
// Content rule for extracting 'Pet Friendly' information
115+
new ContentRuleModel
116+
{
117+
OnElement = ".",
118+
OnAttribute = "data-pet-friendly",
119+
Key = "PetFriendly",
120+
PluginName = contentType,
121+
DataType = "bool",
122+
Transformers = NewTransformers("PetFriendly", trim, clearLinesBreak)
123+
},
124+
// Content rule for extracting 'Last Update' information
125+
new ContentRuleModel
126+
{
127+
OnElement = ".",
128+
OnAttribute = "data-last-updated",
129+
Key = "LastUpdate",
130+
PluginName = contentType,
131+
DataType = "datetime",
132+
Transformers = NewTransformers("LastUpdate", trim, clearLinesBreak)
133+
},
134+
// Content rule for extracting 'Pre' information
135+
new ContentRuleModel
136+
{
137+
OnElement = ".//pre",
138+
Key = "Pre",
139+
PluginName = contentType,
140+
Transformers = NewTransformers("Pre", trim, clearLinesBreak)
141+
}
142+
]
143+
};
144+
145+
// Return the extraction rule as an array
146+
return [extractionRule];
147+
}
148+
149+
// Creates an array of TransformerRuleModel instances based on the specified parameters.
150+
private static TransformerRuleModel[] NewTransformers(string field, bool trim, bool clearLinesBreak)
151+
{
152+
// Initialize a list to hold the transformer rule models.
153+
var transformers = new List<TransformerRuleModel>();
154+
155+
// If trimming is requested, add a Trim transformer for the specified field.
156+
if (trim)
157+
{
158+
transformers.Add(new TransformerRuleModel
159+
{
160+
PluginName = "Trim",
161+
OnElement = field
162+
});
163+
}
164+
165+
// If clearing line breaks is requested, add a ClearLines transformer for the specified field.
166+
if (clearLinesBreak)
167+
{
168+
transformers.Add(new TransformerRuleModel
169+
{
170+
PluginName = "ClearLines",
171+
OnElement = field
172+
});
173+
}
174+
175+
// Convert the list of transformers to an array and return it.
176+
return [.. transformers];
177+
}
178+
}
179+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using G4.Extensions;
2+
using G4.IntegrationTests.Framework;
3+
using G4.Models;
4+
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
using System.Collections.Generic;
8+
9+
namespace G4.IntegrationTests.Plugins.Common.SendOpenAiPrompt
10+
{
11+
internal class C0001(TestContext context) : TestCaseBase(context)
12+
{
13+
protected override IEnumerable<G4RuleModelBase> OnActions(AutomationEnvironment environment)
14+
{
15+
// Retrieve the OpenAI API key from the environment's context properties (defaulting to empty string if not found)
16+
var apiKey = environment.ContextProperties.Get(key: "OpenAi.ApiKey", defaultValue: string.Empty);
17+
18+
// Build and return the list of action rules to execute
19+
return
20+
[
21+
// Action: Send an OpenAI prompt asking for C developer guidance
22+
new ActionRuleModel
23+
{
24+
Argument = "{{$ " +
25+
"--SystemPrompt:You are an expert C developer who writes clear, efficient code and provides practical guidance on C programming tasks. " +
26+
"--Prompt:Hey there! How's your day going? " +
27+
"--ApiKey:" + apiKey + "}}",
28+
PluginName = "SendOpenAiPrompt"
29+
},
30+
31+
// Action: Send an OpenAI prompt requesting current weather conditions
32+
new ActionRuleModel
33+
{
34+
Argument = "{{$ " +
35+
"--Prompt:Could you please check and share the current weather conditions? " +
36+
"--ApiKey:" + apiKey + "}}",
37+
PluginName = "SendOpenAiPrompt"
38+
},
39+
40+
// Assertion: Ensure that the system response text is not empty or whitespace
41+
new ActionRuleModel
42+
{
43+
PluginName = "Assert",
44+
Argument = "{{$ --Condition:Text --Operator:Match --Expected:^(?!\\s*$).+}}",
45+
OnElement = "{{$Get-Parameter --Name:OpenAiSystemResponse --Scope:Session}}"
46+
},
47+
48+
// Assertion: Ensure that at least one completion token was used
49+
new ActionRuleModel
50+
{
51+
PluginName = "Assert",
52+
Argument = "{{$ --Condition:Text --Operator:Greater --Expected:0}}",
53+
OnElement = "{{$Get-Parameter --Name:OpenAiCompletionTokens --Scope:Session}}"
54+
},
55+
56+
// Assertion: Ensure that at 56 prompt token were used
57+
new ActionRuleModel
58+
{
59+
PluginName = "Assert",
60+
Argument = "{{$ --Condition:Text --Operator:Equal --Expected:56}}",
61+
OnElement = "{{$Get-Parameter --Name:OpenAiPromptTokens --Scope:Session}}"
62+
},
63+
64+
// Assertion: Ensure that the total token count is greater than zero
65+
new ActionRuleModel
66+
{
67+
PluginName = "Assert",
68+
Argument = "{{$ --Condition:Text --Operator:Greater --Expected:0}}",
69+
OnElement = "{{$Get-Parameter --Name:OpenAiTotalTokens --Scope:Session}}"
70+
},
71+
];
72+
}
73+
}
74+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using G4.Extensions;
2+
using G4.IntegrationTests.Framework;
3+
using G4.Models;
4+
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
using System.Collections.Generic;
8+
9+
namespace G4.IntegrationTests.Plugins.Common.SendOpenAiPrompt
10+
{
11+
internal class C0002(TestContext context) : TestCaseBase(context)
12+
{
13+
protected override IEnumerable<G4RuleModelBase> OnActions(AutomationEnvironment environment)
14+
{
15+
// Retrieve the OpenAI API key from the environment's context properties (defaulting to empty string if not found)
16+
var apiKey = environment.ContextProperties.Get(key: "OpenAi.ApiKey", defaultValue: string.Empty);
17+
18+
// Build and return the list of action rules to execute
19+
return
20+
[
21+
// Action: Send an OpenAI prompt asking for C developer guidance
22+
new ActionRuleModel
23+
{
24+
Argument = "{{$ " +
25+
"--SystemPrompt:You are an expert C developer who writes clear, efficient code and provides practical guidance on C programming tasks. " +
26+
"--Prompt:Hey there! How's your day going? " +
27+
"--ApiKey:" + apiKey + "}}",
28+
PluginName = "SendOpenAiPrompt"
29+
},
30+
31+
// Action: Send an OpenAI prompt requesting current weather conditions
32+
new ActionRuleModel
33+
{
34+
Argument = "{{$ " +
35+
"--Prompt:Could you please check and share the current weather conditions? " +
36+
"--ApiKey:" + apiKey + " " +
37+
"--NewChat}}",
38+
PluginName = "SendOpenAiPrompt"
39+
},
40+
41+
// Assertion: Ensure that the system response text is not empty or whitespace
42+
new ActionRuleModel
43+
{
44+
PluginName = "Assert",
45+
Argument = "{{$ --Condition:Text --Operator:Match --Expected:^(?!\\s*$).+}}",
46+
OnElement = "{{$Get-Parameter --Name:OpenAiSystemResponse --Scope:Session}}"
47+
},
48+
49+
// Assertion: Ensure that at least one completion token was used
50+
new ActionRuleModel
51+
{
52+
PluginName = "Assert",
53+
Argument = "{{$ --Condition:Text --Operator:Greater --Expected:0}}",
54+
OnElement = "{{$Get-Parameter --Name:OpenAiCompletionTokens --Scope:Session}}"
55+
},
56+
57+
// Assertion: Ensure that 28 prompt token were used
58+
new ActionRuleModel
59+
{
60+
PluginName = "Assert",
61+
Argument = "{{$ --Condition:Text --Operator:Equal --Expected:28}}",
62+
OnElement = "{{$Get-Parameter --Name:OpenAiPromptTokens --Scope:Session}}"
63+
},
64+
65+
// Assertion: Ensure that the total token count is greater than zero
66+
new ActionRuleModel
67+
{
68+
PluginName = "Assert",
69+
Argument = "{{$ --Condition:Text --Operator:Greater --Expected:0}}",
70+
OnElement = "{{$Get-Parameter --Name:OpenAiTotalTokens --Scope:Session}}"
71+
},
72+
];
73+
}
74+
}
75+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using G4.Extensions;
2+
using G4.IntegrationTests.Framework;
3+
using G4.Models;
4+
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
using System.Collections.Generic;
8+
9+
namespace G4.IntegrationTests.Plugins.Common.SendOpenAiPrompt
10+
{
11+
internal class C0003(TestContext context) : TestCaseBase(context)
12+
{
13+
protected override IEnumerable<G4RuleModelBase> OnActions(AutomationEnvironment environment)
14+
{
15+
// Retrieve the OpenAI API key from the environment's context properties (defaulting to empty string if not found)
16+
var apiKey = environment.ContextProperties.Get(key: "OpenAi.ApiKey", defaultValue: string.Empty);
17+
18+
// Build and return the list of action rules to execute
19+
return
20+
[
21+
// Action: Send an OpenAI prompt asking for C developer guidance
22+
new ActionRuleModel
23+
{
24+
Argument = "{{$ " +
25+
"--SystemPrompt:You are an expert C developer who writes clear, efficient code and provides practical guidance on C programming tasks. " +
26+
"--Prompt:Hey there! How's your day going? " +
27+
"--ApiKey:" + apiKey + " " +
28+
"--MaxTokens:5}}",
29+
PluginName = "SendOpenAiPrompt"
30+
},
31+
32+
// Assertion: Ensure that the system response text is not empty or whitespace
33+
new ActionRuleModel
34+
{
35+
PluginName = "Assert",
36+
Argument = "{{$ --Condition:Text --Operator:Match --Expected:^(?!\\s*$).+}}",
37+
OnElement = "{{$Get-Parameter --Name:OpenAiSystemResponse --Scope:Session}}"
38+
},
39+
40+
// Assertion: Ensure that 5 completion token was used
41+
new ActionRuleModel
42+
{
43+
PluginName = "Assert",
44+
Argument = "{{$ --Condition:Text --Operator:Equal --Expected:5}}",
45+
OnElement = "{{$Get-Parameter --Name:OpenAiCompletionTokens --Scope:Session}}"
46+
},
47+
48+
// Assertion: Ensure that 41 prompt token were used
49+
new ActionRuleModel
50+
{
51+
PluginName = "Assert",
52+
Argument = "{{$ --Condition:Text --Operator:Equal --Expected:41}}",
53+
OnElement = "{{$Get-Parameter --Name:OpenAiPromptTokens --Scope:Session}}"
54+
},
55+
56+
// Assertion: Ensure that the total token count is greater than zero
57+
new ActionRuleModel
58+
{
59+
PluginName = "Assert",
60+
Argument = "{{$ --Condition:Text --Operator:Greater --Expected:0}}",
61+
OnElement = "{{$Get-Parameter --Name:OpenAiTotalTokens --Scope:Session}}"
62+
},
63+
];
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)