Skip to content

Commit

Permalink
fix: seeder and test bugs for ddd first entities
Browse files Browse the repository at this point in the history
  • Loading branch information
pdevito3 committed Dec 6, 2021
1 parent 4acef12 commit 3d982ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 7 additions & 3 deletions Craftsman/Builders/Seeders/SeederFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
using Craftsman.Helpers;
using Craftsman.Models;
using System;
using Enums;

public static class SeederFunctions
{
public static string GetEntitySeederFileText(string classNamespace, Entity entity, string dbContextName, string solutionDirectory, string projectBaseName)
{
var entitiesClassPath = ClassPathHelper.EntityClassPath(solutionDirectory, "", entity.Plural, projectBaseName);
var dtoClassPath = ClassPathHelper.DtoClassPath(solutionDirectory, "", entity.Name, projectBaseName);
var dbContextClassPath = ClassPathHelper.DbContextClassPath(solutionDirectory, "", projectBaseName);
var entityForCreationDto = $"Fake{Utilities.GetDtoName(entity.Name, Dto.Creation)}";
if (dbContextName is null)
{
throw new ArgumentNullException(nameof(dbContextName));
Expand All @@ -19,6 +22,7 @@ public static string GetEntitySeederFileText(string classNamespace, Entity entit
using AutoBogus;
using {entitiesClassPath.ClassNamespace};
using {dtoClassPath.ClassNamespace};
using {dbContextClassPath.ClassNamespace};
using System.Linq;
Expand All @@ -28,9 +32,9 @@ public static class {Utilities.GetSeederName(entity)}
{{
if (!context.{entity.Plural}.Any())
{{
context.{entity.Plural}.Add(new AutoFaker<{entity.Name}>());
context.{entity.Plural}.Add(new AutoFaker<{entity.Name}>());
context.{entity.Plural}.Add(new AutoFaker<{entity.Name}>());
context.{entity.Plural}.Add({entity.Name}.Create(new AutoFaker<{entityForCreationDto}>()));
context.{entity.Plural}.Add({entity.Name}.Create(new AutoFaker<{entityForCreationDto}>()));
context.{entity.Plural}.Add({entity.Name}.Create(new AutoFaker<{entityForCreationDto}>()));
context.SaveChanges();
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ private static string CreateEntityTestUnauthorized(Entity entity)
{
var fakeEntity = Utilities.FakerName(entity.Name);
var fakeEntityVariableName = $"fake{entity.Name}";
var fakeCreationDto = Utilities.FakerName(Utilities.GetDtoName(entity.Name, Dto.Creation));

return $@"
[Test]
public async Task create_{entity.Name.ToLower()}_returns_unauthorized_without_valid_token()
{{
// Arrange
var {fakeEntityVariableName} = new {fakeEntity} {{ }}.Generate();
var {fakeEntityVariableName} = {fakeEntity}.Generate(new {fakeCreationDto}().Generate());
await InsertAsync({fakeEntityVariableName});
Expand All @@ -98,14 +99,14 @@ private static string CreateEntityTestForbidden(Entity entity)
{
var fakeEntity = Utilities.FakerName(entity.Name);
var fakeEntityVariableName = $"fake{entity.Name}";
var pkName = Entity.PrimaryKeyProperty.Name;
var fakeCreationDto = Utilities.FakerName(Utilities.GetDtoName(entity.Name, Dto.Creation));

return $@"
[Test]
public async Task create_{entity.Name.ToLower()}_returns_forbidden_without_proper_scope()
{{
// Arrange
var {fakeEntityVariableName} = new {fakeEntity} {{ }}.Generate();
var {fakeEntityVariableName} = {fakeEntity}.Generate(new {fakeCreationDto}().Generate());
_client.AddAuth();
await InsertAsync({fakeEntityVariableName});
Expand Down

0 comments on commit 3d982ef

Please sign in to comment.