Skip to content

Instantly share code, notes, and snippets.

@JaimeStill
Last active August 18, 2022 20:10
Show Gist options
  • Select an option

  • Save JaimeStill/5ca02cfebd4380b177b7c21bf1f002c5 to your computer and use it in GitHub Desktop.

Select an option

Save JaimeStill/5ca02cfebd4380b177b7c21bf1f002c5 to your computer and use it in GitHub Desktop.

e2e Data Seeding

  1. Build an API that links to DbCli, but provides the data to be seeded.
  2. Build an Angular service that interfaces with the DbCli API.
  3. Integrate DbCli service into e2e testing so that:
    • A Test DB can be initialized and the provided data seeded
    • e2e tests are conducted
    • The Test DB can be destroyed when complete
{
"ConnectionStrings": {
"Dev": "Server=.\\DevSql;Trusted_Connection=True;Database=app-dev",
"Test": "Server=.\\DevSql;Trusted_Connection=True;Database=app-test"
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace App.Data;
public class DbManager : IDisposable
{
readonly bool destroy;
public AppDbContext Context { get; private set; }
static string GetConnectionString(string env)
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("connections.json")
.AddEnvironmentVariables()
.Build();
string connection = config.GetConnectionString(env);
Console.WriteLine($"Connection string: {connection}");
return connection;
}
static AppDbContext GetDbContext(string connection)
{
var builder = new DbContextOptionsBuilder<AppDbContext>()
.UseSqlServer(connection);
return new AppDbContext(builder.Options);
}
public DbManager(string env = "Dev", bool destroy = false)
{
this.destroy = destroy;
Context = GetDbContext(GetConnectionString(env));
}
public void Initialize()
{
if (destroy)
Context.Database.EnsureDeleted();
Context.Database.Migrate();
}
public async Task InitializeAsync()
{
if (destroy)
await Context.Database.EnsureDeletedAsync();
await Context.Database.MigrateAsync();
}
public void Dispose()
{
if (destroy)
Context.Database.EnsureDeleted();
Context.Dispose();
GC.SuppressFinalize(this);
}
}
using App.Data;
using App.Data.Extensions;
using System.Runtime.Versioning;
[assembly:SupportedOSPlatform("windows")]
try
{
string env = args.Length > 0
? args[0]
: "Dev";
bool destroy = args.Length > 1
&& bool.Parse(args[1]);
using DbManager manager = new (env, destroy);
await manager.InitializeAsync();
await manager.Context.Initialize();
}
catch (Exception ex)
{
throw new Exception("An error occurred while building the database", ex);
}
import Test from '../tester';
Test.classification();
export default class Classification {
private static selectField = (field: string) =>
cy.get('classification-dialog mat-dialog-content > section > mat-form-field')
.contains(field)
.parent()
.parent()
.click({ force: true });
private static inputField = (field: string, input: string) =>
this.selectField(field)
.clear()
.type(input);
private static add = (classification: string, abbreviation: string, color: string, textColor: string) =>
it(`add ${classification} Classification`, () => {
cy.get('[fxlayoutalign="start center"] > .mat-focus-indicator > .mat-button-wrapper')
.contains('Add Classification')
.click();
this.inputField('Classification', classification);
this.inputField('Abbreviation', abbreviation);
this.inputField('Color', color);
this.inputField('Text Color', textColor);
cy.get('classification-dialog mat-dialog-actions')
.contains('Save')
.click({ force: true });
cy.get('.mat-simple-snackbar > :nth-child(1)')
.should('be.visible')
.and('contain', 'successfully created');
})
private static edit = (classification: string, color: string) =>
it(`Should edit ${classification} (Color is ${color})`, () => {
cy.get('section > double-action-card > action-card')
.contains(classification)
.parentsUntil('action-card')
.find('button')
.prev()
.click();
this.inputField('Color', color);
cy.get('classification-dialog mat-dialog-actions')
.contains('Save')
.click({ force: true });
});
private static delete = (classification: string) =>
it(`Should Delete ${classification}`, () => {
cy.get('section > double-action-card > action-card')
.contains(classification)
.parentsUntil('action-card')
.find('button')
.next()
.click();
cy.get('.mat-warn')
.click()
.wait(1000);
});
static test = () =>
describe('CRUD Classification', () => {
it('goes to the Manager Admin Home page', () => {
cy.visit('localhost:3000/admin/home');
});
this.add('APPLE', 'APL', 'red', 'white');
this.add('ORANGE', 'OGE', 'orange', 'black');
this.add('GRAPE`, 'GPE', 'indigo', 'white');
this.edit('APPLE', 'green');
this.delete('APPLE');
this.delete('ORANGE');
this.delete('GRAPE');
});
}
import Classification from './classification';
export default class Test {
static classification = Classification.test;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment