public GoalFaker(IManageDatabase databaseManager, ILogger logger) { var random = new Random(); int randomNumber = random.Next(); Bogus.Randomizer.Seed = new Random(randomNumber); var dispositionFaker = new GoalDispositionFaker(databaseManager, logger); var dispositionFake = dispositionFaker.GetOne(); var dispositionRepository = new GoalDispositionRepository(databaseManager, new GoalDispositionRepositorySql(), logger); var dispositionFakeResult = dispositionRepository.Create(dispositionFake); dispositionFake = dispositionFakeResult.Model; var stateFaker = new GoalStateFaker(databaseManager, logger); var stateFake = stateFaker.GetOne(); var stateRepository = new GoalStateRepository(databaseManager, new GoalStateRepositorySql(), logger); var stateFakeResult = stateRepository.Create(stateFake); stateFake = stateFakeResult.Model; var statusFaker = new GoalStatusFaker(databaseManager, logger); var statusFake = statusFaker.GetOne(); var statusRepository = new GoalStatusRepository(databaseManager, new GoalStatusRepositorySql(), logger); var statusFakeResult = statusRepository.Create(statusFake); statusFake = statusFakeResult.Model; _faker = new Bogus.Faker <Goal>() .StrictMode(false) .Rules((f, m) => { m.GoalId = null; m.Description = f.Lorem.Sentence(10); m.Notes = f.Lorem.Sentence(10); m.DispositionIdRef = dispositionFake.GoalDispositionId; m.StateIdRef = stateFake.GoalStateId; m.StatusIdRef = statusFake.GoalStatusId; }); }
public void CreateUpdateDeleteFetch() { // file system var path = "/shop/randomsilo/modern-web/backends/GoalTracker"; var project = "GoalTracker"; var outputPath = $"{path}/{project}.Infrastructure.Test/TestOutput/"; var databaseFile = GetDatabase(outputPath, MethodBase.GetCurrentMethod()); // logger ILogger logger = GetLogger($"{outputPath}/{MethodBase.GetCurrentMethod().ReflectedType.Name}_{MethodBase.GetCurrentMethod().Name}.log"); // database setup // - context IDatabaseContext databaseContext = new DatabaseContext( $"Data Source={databaseFile}" , "TestDb" , "TestSchema" , $"{path}/sql/sqlite/ALL.sql" ); Assert.NotNull(databaseContext); // - manager IManageDatabase databaseManager = new DatabaseManager(databaseContext); Assert.NotNull(databaseManager); // - create tables databaseManager.CreateDatabase(); // - repository var goalStateRepository = new GoalStateRepository(databaseManager, new GoalStateRepositorySql(), logger); Assert.NotNull(goalStateRepository); // faker BrashActionResult <GoalState> result = null; var goalStateFaker = new GoalStateFaker(databaseManager, logger); Assert.NotNull(goalStateFaker); // create var goalStateCreateModel = goalStateFaker.GetOne(); result = goalStateRepository.Create(goalStateCreateModel); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); Assert.True(result.Model.GoalStateId > 0); // use model with id goalStateCreateModel = result.Model; // update var goalStateUpdateModel = goalStateFaker.GetOne(); goalStateUpdateModel.GoalStateId = goalStateCreateModel.GoalStateId; result = goalStateRepository.Update(goalStateUpdateModel); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); // delete result = goalStateRepository.Delete(goalStateCreateModel); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); // fetch // - make fakes var fakes = goalStateFaker.GetMany(10); // - add fakes to database List <int?> ids = new List <int?>(); foreach (var f in fakes) { result = goalStateRepository.Create(f); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); Assert.True(result.Model.GoalStateId >= 0); ids.Add(result.Model.GoalStateId); } // - get fakes from database foreach (var id in ids) { var model = new GoalState() { GoalStateId = id }; result = goalStateRepository.Fetch(model); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); Assert.True(result.Model.GoalStateId >= 0); } }