-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mg-diego
committed
Aug 20, 2023
1 parent
a63ee39
commit b554545
Showing
21 changed files
with
664 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
samples/TestWare.Samples.MongoDB/Features/Database.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@MongoDB | ||
Feature: Database | ||
|
||
|
||
Scenario: Insert at database | ||
When the following document is inserted in 'collection-example' collection at 'database-example' database | ||
| NAME | | ||
| Didac | | ||
Then the following document is saved in 'collection-example' collection at 'database-example' database | ||
| NAME | | ||
| Didac | | ||
|
||
|
||
Scenario: Delete at database | ||
Given the following document is saved in 'collection-example' collection at 'database-example' database | ||
| NAME | | ||
| Diego | | ||
When the following document is deleted in 'collection-example' collection at 'database-example' database | ||
| NAME | | ||
| Diego | | ||
Then no documents are saved in 'collection-example' collection at 'database-example' database with values | ||
| NAME | | ||
| Diego | | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using TestWare.Reporting.ExtentReport; | ||
using TestWare.Samples.MongoDB.Seeder; | ||
|
||
namespace TestWare.Samples.MongoDB; | ||
|
||
[Binding] | ||
public sealed class Hook | ||
{ | ||
private readonly TestContext _testContext; | ||
private int _stepCounter; | ||
private static readonly LifeCycle _lifeCycle = new(); | ||
private static ExtentReport _testReport; | ||
Check warning on line 16 in samples/TestWare.Samples.MongoDB/Hook.cs GitHub Actions / build (windows-latest)
Check warning on line 16 in samples/TestWare.Samples.MongoDB/Hook.cs GitHub Actions / build (ubuntu-latest)
|
||
|
||
public Hook(TestContext testContext) | ||
{ | ||
_testContext = testContext; | ||
} | ||
|
||
[BeforeFeature] | ||
public static void BeforeFeature(FeatureContext featureContext) | ||
{ | ||
var name = featureContext.FeatureInfo.Title; | ||
var tags = featureContext.FeatureInfo.Tags; | ||
|
||
_lifeCycle.BeginTestSuite(name); | ||
_testReport.CreateFeature(name, tags); | ||
} | ||
|
||
[AfterFeature] | ||
public static void AfterFeature(FeatureContext featureContext) | ||
{ | ||
_lifeCycle.EndTestSuite(); | ||
} | ||
|
||
[BeforeScenario] | ||
public void BeforeScenario(FeatureContext featureContext, ScenarioContext scenarioContext) | ||
{ | ||
var name = scenarioContext.ScenarioInfo.Arguments.Count > 0 | ||
? $"{DateTime.UtcNow.ToString("yyy-MM-dd HH-mm-ss", CultureInfo.InvariantCulture)} - {scenarioContext.ScenarioInfo.Title}" | ||
: scenarioContext.ScenarioInfo.Title; | ||
|
||
var description = scenarioContext.ScenarioInfo.Description ?? ""; | ||
var scenarioTags = scenarioContext.ScenarioInfo.Tags; | ||
_testReport.CreateTestCase(name, description, scenarioTags); | ||
|
||
_testContext.WriteLine("----------------------------------------- \r\n"); | ||
_testContext.WriteLine($"Feature: {featureContext.FeatureInfo.Title}"); | ||
_testContext.WriteLine($" Scenario: {scenarioContext.ScenarioInfo.Title} \r\n"); | ||
|
||
_stepCounter = 1; | ||
var tags = GetTags(featureContext, scenarioContext); | ||
_lifeCycle.BeginTestCase(name, tags); | ||
|
||
var databaseSeeder = new DatabaseSeeder(); | ||
databaseSeeder.InitializeDatabase(); | ||
} | ||
|
||
[AfterScenario] | ||
public void AfterScenario() | ||
{ | ||
_testReport.SetTestcaseOutcome(_testContext.CurrentTestOutcome); | ||
_lifeCycle.EndTestCase(); | ||
} | ||
|
||
[BeforeTestRun] | ||
public static void BeforeTestRun() | ||
{ | ||
_lifeCycle.BeginTestExecution(); | ||
_testReport = new ExtentReport(_lifeCycle.GetCurrentResultsDirectory()); | ||
} | ||
|
||
[AfterTestRun] | ||
public static void AfterTestRun() | ||
{ | ||
_lifeCycle.EndTestExecution(); | ||
_testReport.CreateTestReportFile(); | ||
} | ||
|
||
[BeforeStep] | ||
public void BeforeStep(ScenarioContext scenarioContext) | ||
{ | ||
var name = scenarioContext.CurrentScenarioBlock.ToString(); | ||
var description = scenarioContext.StepContext.StepInfo.Text; | ||
_testReport.CreateStep(name, description); | ||
|
||
var stepId = $"{_stepCounter:00} {description}"; | ||
_stepCounter++; | ||
_lifeCycle.BeginTestStep(stepId); | ||
} | ||
|
||
[AfterStep] | ||
public void AfterStep(ScenarioContext scenarioContext) | ||
{ | ||
_lifeCycle.EndTestStep(); | ||
var evidencesPath = _lifeCycle.GetStepEvidences(); | ||
|
||
foreach (var evidence in evidencesPath) | ||
{ | ||
_testReport.AddScreenshotToStep(evidence); | ||
_testContext.AddResultFile(evidence); | ||
} | ||
} | ||
|
||
private static List<string> GetTags(FeatureContext featureContext, ScenarioContext scenarioContext) | ||
{ | ||
var tags = featureContext.FeatureInfo.Tags.ToList(); | ||
tags.AddRange(scenarioContext.ScenarioInfo.Tags.ToList()); | ||
return tags; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
global using FluentAssertions; | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
global using TechTalk.SpecFlow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using TestWare.Core; | ||
using TestWare.Core.Configuration; | ||
using TestWare.Core.Interfaces; | ||
using TestWare.Engines.MongoDB; | ||
|
||
namespace TestWare.Samples.MongoDB; | ||
|
||
internal class LifeCycle : AutomationLifeCycleBase | ||
{ | ||
protected override IEnumerable<Assembly> GetTestWareComponentAssemblies() | ||
{ | ||
IEnumerable<Assembly> assemblies = new[] | ||
{ | ||
typeof(Hook).Assembly | ||
}; | ||
|
||
return assemblies; | ||
} | ||
|
||
protected override IEnumerable<IEngineManager> GetTestWareEngines() | ||
{ | ||
IEnumerable<IEngineManager> engines = new[] | ||
{ | ||
new MongoDbManager() | ||
}; | ||
|
||
return engines; | ||
} | ||
|
||
protected override TestConfiguration GetConfiguration() | ||
{ | ||
return ConfigurationManager.ReadConfigurationFile("TestConfiguration.MongoDB.json"); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
samples/TestWare.Samples.MongoDB/Scripts/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
version: '3.7' | ||
|
||
services: | ||
mongodb: | ||
image: mongo:latest | ||
container_name: mongodb | ||
restart: always | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: guest | ||
MONGO_INITDB_ROOT_PASSWORD: guest | ||
MONGO_INITDB_DATABASE: admin | ||
ports: | ||
- 27017:27017 | ||
volumes: | ||
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro |
1 change: 1 addition & 0 deletions
1
samples/TestWare.Samples.MongoDB/Scripts/initialize-mongodb-docker.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docker-compose up --build -d mongodb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
db.createUser( | ||
{ | ||
user: "guest", | ||
pwd: "guest", | ||
roles: [ | ||
{ | ||
role: "readWrite", | ||
db: "database-example" | ||
} | ||
] | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using MongoDB.Bson; | ||
using TestWare.Core; | ||
using TestWare.Engines.MongoDB.Factory; | ||
|
||
namespace TestWare.Samples.MongoDB.Seeder; | ||
|
||
internal class DatabaseSeeder | ||
{ | ||
private readonly IMongoDbClient _mongoDbClient; | ||
private const string DATABASE_NAME = "database-example"; | ||
private const string COLLECTION_NAME = "collection-example"; | ||
|
||
public DatabaseSeeder() | ||
{ | ||
_mongoDbClient = ContainerManager.GetTestWareComponent<IMongoDbClient>(); | ||
} | ||
|
||
public void InitializeDatabase() | ||
{ | ||
_mongoDbClient.DropDatabase(DATABASE_NAME); | ||
|
||
_mongoDbClient.CreateDatabase(DATABASE_NAME); | ||
_mongoDbClient.CreateCollection(DATABASE_NAME, COLLECTION_NAME); | ||
|
||
_mongoDbClient.InsertOne(new BsonDocument("name", "Diego"), DATABASE_NAME, COLLECTION_NAME); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
samples/TestWare.Samples.MongoDB/StepDefinitions/DatabaseStepDefinitions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using FluentAssertions.Execution; | ||
using MongoDB.Bson; | ||
using TestWare.Core; | ||
using TestWare.Engines.MongoDB.Factory; | ||
|
||
namespace TestWare.Samples.MongoDB.StepDefinitions; | ||
|
||
[Binding] | ||
public class DatabaseStepDefinitions | ||
{ | ||
private readonly IMongoDbClient _mongoDbClient; | ||
|
||
public DatabaseStepDefinitions() | ||
{ | ||
_mongoDbClient = ContainerManager.GetTestWareComponent<IMongoDbClient>(); | ||
} | ||
|
||
[When(@"the following document is inserted in '([^']*)' collection at '([^']*)' database")] | ||
public void TheFollowingDocumentIsInsertedInCollectionAtDatabase(string collectionName, string databaseName, Table table) | ||
{ | ||
var value = table.Rows[0]["NAME"].ToString(); | ||
|
||
_mongoDbClient.InsertOne(new BsonDocument("name", value), databaseName, collectionName); | ||
} | ||
|
||
[When(@"the following document is deleted in '([^']*)' collection at '([^']*)' database")] | ||
public void TheFollowingDocumentIsDeletedInCollectionAtDatabase(string collectionName, string databaseName, Table table) | ||
{ | ||
var value = table.Rows[0]["NAME"].ToString(); | ||
|
||
_mongoDbClient.DeleteOne(new BsonDocument("name", value), databaseName, collectionName); | ||
} | ||
|
||
|
||
[Given(@"the following document is saved in '([^']*)' collection at '([^']*)' database")] | ||
[Then(@"the following document is saved in '([^']*)' collection at '([^']*)' database")] | ||
public void TheFollowingDocumentIsSavedInCollectionAtDatabase(string collectionName, string databaseName, Table table) | ||
{ | ||
var value = table.Rows[0]["NAME"].ToString(); | ||
|
||
var result = _mongoDbClient.Find(new BsonDocument("name", value), databaseName, collectionName).Result; | ||
|
||
using (new AssertionScope()) | ||
{ | ||
result.Should().NotBeNull(); | ||
result.Count.Should().Be(1); | ||
result[0].GetValue("name").RawValue.Should().Be(value); | ||
Check warning on line 47 in samples/TestWare.Samples.MongoDB/StepDefinitions/DatabaseStepDefinitions.cs GitHub Actions / build (windows-latest)
Check warning on line 47 in samples/TestWare.Samples.MongoDB/StepDefinitions/DatabaseStepDefinitions.cs GitHub Actions / build (ubuntu-latest)
|
||
} | ||
} | ||
|
||
[Then(@"no documents are saved in '([^']*)' collection at '([^']*)' database with values")] | ||
public void ThenNoDocumentsAreSavedInCollectionAtDatabaseWithValues(string collectionName, string databaseName, Table table) | ||
{ | ||
var value = table.Rows[0]["NAME"].ToString(); | ||
|
||
var result = _mongoDbClient.Find(new BsonDocument("name", value), databaseName, collectionName).Result; | ||
|
||
result.Count.Should().Be(0); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
samples/TestWare.Samples.MongoDB/TestConfiguration.MongoDB.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"Configurations": [ | ||
{ | ||
"Tag": "MongoDB", | ||
"Capabilities": [ | ||
{ | ||
"Name": "MongoDB", | ||
"ConnectionString": "mongodb://guest:guest@localhost:27017/" | ||
} | ||
] | ||
} | ||
], | ||
"TestResultPath": "C:\\workspace\\ERNI\\results\\" | ||
} |
36 changes: 36 additions & 0 deletions
36
samples/TestWare.Samples.MongoDB/TestWare.Samples.MongoDB.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.11.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.0" /> | ||
<PackageReference Include="SpecFlow" Version="3.9.74" /> | ||
<PackageReference Include="SpecFlow.MsTest" Version="3.9.74" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Features\" /> | ||
<Folder Include="Scripts\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Core\TestWare.TestReport\TestWare.ExtentReport.csproj" /> | ||
<ProjectReference Include="..\..\src\Engines\TestWare.Engines.MongoDB\TestWare.Engines.MongoDB.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="TestConfiguration.MongoDB.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.