public async Task GetSinglePlantLogType()
        {
            var logType = ValidObjectHelper.ValidPlantLogType();
            var id      = logType.Id;
            var name    = logType.Name;

            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(GetSinglePlantLogType));
            await context.PlantLogTypes.AddAsync(new PlantLogType { Id = id, Name = name });

            await context.SaveChangesAsync();

            var querySingle = new GetPlantLogTypeByIdQuery {
                Id = id
            };
            var queryHandler = new GetPlantLogTypeByIdQueryHandler(context);
            var result       = await queryHandler.Handle(querySingle, CancellationToken.None);

            Assert.True(result.Success);
            Assert.Equal(id, result.Data.Id);
            Assert.Equal(name, result.Data.Name);

            var queryNonExisting = new GetPlantLogTypeByIdQuery {
                Id = Guid.NewGuid()
            };
            var resultNonExisting = await queryHandler.Handle(queryNonExisting, CancellationToken.None);

            Assert.False(resultNonExisting.Success);
        }
Ejemplo n.º 2
0
        public PlantLogQueryTestsFixture()
        {
            context      = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(PlantLogQueryTests));
            logType1     = ValidObjectHelper.ValidPlantLogType();
            logType2     = ValidObjectHelper.ValidPlantLogType();
            plantSpecies = ValidObjectHelper.ValidPlantSpecies();
            plant1       = ValidObjectHelper.ValidPlant(plantSpecies);
            plant2       = ValidObjectHelper.ValidPlant(plantSpecies);

            var id1 = Guid.NewGuid();
            var id2 = Guid.NewGuid();
            var id3 = Guid.NewGuid();
            var id4 = Guid.NewGuid();
            var id5 = Guid.NewGuid();

            var log1 = "Hello";
            var log2 = "World";
            var log3 = "ab";
            var log4 = "world Hello abcdefgh";
            var log5 = "Töster";

            var dt1 = new DateTime(2020, 01, 01, 00, 00, 00);
            var dt2 = new DateTime(2020, 01, 01, 18, 00, 00);
            var dt3 = new DateTime(2020, 01, 02, 12, 00, 00);
            var dt4 = new DateTime(2020, 02, 01, 12, 00, 00);
            var dt5 = new DateTime(2019, 01, 01, 12, 00, 00);

            plantLog1 = new PlantLog {
                Id = id1, DateTime = dt1, Log = log1, Plant = plant1, PlantLogType = logType1
            };
            plantLog2 = new PlantLog {
                Id = id2, DateTime = dt2, Log = log2, Plant = plant1, PlantLogType = logType1
            };
            plantLog3 = new PlantLog {
                Id = id3, DateTime = dt3, Log = log3, Plant = plant2, PlantLogType = logType2
            };
            plantLog4 = new PlantLog {
                Id = id4, DateTime = dt4, Log = log4, Plant = plant2, PlantLogType = logType2
            };
            plantLog5 = new PlantLog {
                Id = id5, DateTime = dt5, Log = log5, Plant = plant2, PlantLogType = logType1
            };

            context.AddRange(logType1, logType2, plantSpecies, plant1, plant2, plantLog1, plantLog2, plantLog3, plantLog4, plantLog5);
            context.SaveChanges();
        }
Ejemplo n.º 3
0
        public async Task CreatePlantLogForPlant()
        {
            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(CreatePlantLogForPlant));

            var logType      = ValidObjectHelper.ValidPlantLogType();
            var plantSpecies = ValidObjectHelper.ValidPlantSpecies();
            var plant1       = ValidObjectHelper.ValidPlant(plantSpecies);
            await context.AddRangeAsync(logType, plantSpecies, plant1);

            await context.SaveChangesAsync();

            var validPlantLog = ValidObjectHelper.ValidPlantLog(plant1, logType);
            var id            = validPlantLog.Id;
            var plantId       = validPlantLog.PlantId;
            var logTypeId     = validPlantLog.PlantLogTypeId;
            var dateTime      = validPlantLog.DateTime;
            var log           = validPlantLog.Log;

            var cntBefore = await context.PlantLogs.CountAsync();

            var createCmd = new CreatePlantLogForPlantCommand {
                Id = id, Log = log, DateTime = dateTime, PlantId = plantId, PlantLogTypeId = logTypeId
            };
            var cmdHandler = new CreatePlantLogForPlantCommandHandler(context);
            var result     = await cmdHandler.Handle(createCmd, CancellationToken.None);

            Assert.True(result.Success);
            Assert.Equal(cntBefore + 1, await context.PlantLogs.CountAsync());
            var obj = await context.PlantLogs.FindAsync(id);

            Assert.Equal(id, obj.Id);
            Assert.Equal(plantId, obj.PlantId);
            Assert.Equal(logTypeId, obj.PlantLogTypeId);
            Assert.Equal(dateTime, obj.DateTime);
            Assert.Equal(log, obj.Log);
            var createCmdInvalidPlant = new CreatePlantLogForPlantCommand {
                Id = Guid.NewGuid(), PlantId = Guid.NewGuid(), Log = log, DateTime = dateTime, PlantLogTypeId = logTypeId
            };
            var res2 = await cmdHandler.Handle(createCmdInvalidPlant, CancellationToken.None);

            Assert.False(res2.Success);
        }
Ejemplo n.º 4
0
        public async Task GetSinglePlantLogForPlantTest()
        {
            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(GetSinglePlantLogForPlantTest));
            var logType      = ValidObjectHelper.ValidPlantLogType();
            var plantSpecies = ValidObjectHelper.ValidPlantSpecies();
            var plant1       = ValidObjectHelper.ValidPlant(plantSpecies);
            var plant2       = ValidObjectHelper.ValidPlant(plantSpecies);
            var logPlant1    = ValidObjectHelper.ValidPlantLog(plant1, logType);
            var logPlant2    = ValidObjectHelper.ValidPlantLog(plant2, logType);
            await context.AddRangeAsync(logType, plantSpecies, plant1, plant2, logPlant1, logPlant2);

            await context.SaveChangesAsync();

            var querySingle = new GetPlantLogForPlantByIdQuery {
                Id = logPlant1.Id, PlantId = plant1.Id
            };
            var queryHandler = new GetPlantLogForPlantByIdQueryHandler(context);
            var result       = await queryHandler.Handle(querySingle, CancellationToken.None);

            Assert.True(result.Success);
            Assert.Equal(logPlant1.Id, result.Data.Id);
            Assert.Equal(logPlant1.PlantId, result.Data.PlantId);
            Assert.Equal(logPlant1.PlantLogTypeId, result.Data.PlantLogTypeId);
            Assert.Equal(logPlant1.DateTime, result.Data.DateTime);
            Assert.Equal(logPlant1.Log, result.Data.Log);

            var querySingleWrongPlant = new GetPlantLogForPlantByIdQuery {
                Id = logPlant1.Id, PlantId = plant2.Id
            };
            var result2 = await queryHandler.Handle(querySingleWrongPlant, CancellationToken.None);

            Assert.False(result2.Success);

            var queryNonExisting = new GetPlantLogForPlantByIdQuery {
                Id = Guid.NewGuid(), PlantId = plant2.Id
            };
            var resultNonExisting = await queryHandler.Handle(queryNonExisting, CancellationToken.None);

            Assert.False(resultNonExisting.Success);
        }
        public async Task CreatePlantLogType()
        {
            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(CreatePlantLogType));

            var validLogType = ValidObjectHelper.ValidPlantLogType();
            var id           = validLogType.Id;
            var name         = validLogType.Name;
            var cntBefore    = await context.PlantLogTypes.CountAsync();

            var createCmd = new CreatePlantLogTypeCommand {
                Id = id, Name = name
            };
            var cmdHandler = new CreatePlantLogTypeCommandHandler(context);
            var result     = await cmdHandler.Handle(createCmd, CancellationToken.None);

            Assert.True(result.Success);
            Assert.Equal(cntBefore + 1, await context.PlantLogTypes.CountAsync());
            var obj = await context.PlantLogTypes.FindAsync(id);

            Assert.Equal(id, obj.Id);
            Assert.Equal(name, obj.Name);
        }
        public async Task DeletePlantLogType()
        {
            var logType = ValidObjectHelper.ValidPlantLogType();
            var id      = logType.Id;

            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(DeletePlantLogType));
            await context.PlantLogTypes.AddAsync(logType);

            await context.SaveChangesAsync();

            var deleteCmd = new DeletePlantLogTypeCommand {
                Id = id
            };
            var deleteHandler = new DeletePlantLogTypeCommandHandler(context);
            var deleteResult  = await deleteHandler.Handle(deleteCmd, CancellationToken.None);

            Assert.True(deleteResult.Success);
            Assert.Empty(context.PlantLogTypes);

            var deleteResult2 = await deleteHandler.Handle(deleteCmd, CancellationToken.None);

            Assert.False(deleteResult2.Success);
        }
Ejemplo n.º 7
0
        public async Task TestUpdateForPlantValidator()
        {
            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(TestUpdateForPlantValidator));
            // setup
            var logType      = ValidObjectHelper.ValidPlantLogType();
            var plantSpecies = ValidObjectHelper.ValidPlantSpecies();
            var plant1       = ValidObjectHelper.ValidPlant(plantSpecies);
            var plant2       = ValidObjectHelper.ValidPlant(plantSpecies);
            var logPlant1    = ValidObjectHelper.ValidPlantLog(plant1, logType);
            var logPlant2    = ValidObjectHelper.ValidPlantLog(plant2, logType);
            await context.AddRangeAsync(logType, plantSpecies, plant1, plant2, logPlant1, logPlant2);

            await context.SaveChangesAsync();


            var validator   = new UpdatePlantLogForPlantCommandValidator(context);
            var updateValid = new UpdatePlantLogForPlantCommand
            {
                Id             = logPlant1.Id,
                PlantId        = plant1.Id,
                PlantLogTypeId = logType.Id,
                DateTime       = DateTime.Now,
                Log            = LoremIpsum.Words(2)
            };

            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.Id, updateValid);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.PlantId, updateValid);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.PlantLogTypeId, updateValid);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.DateTime, updateValid);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.Log, updateValid);

            // wrong plant id
            var updateInvalidPlantId = new UpdatePlantLogForPlantCommand
            {
                Id             = logPlant1.Id,
                PlantId        = plant2.Id,
                PlantLogTypeId = logType.Id,
                DateTime       = DateTime.Now,
                Log            = LoremIpsum.Words(2)
            };

            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.Id, updateInvalidPlantId);
            validator.ShouldHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.PlantId, updateInvalidPlantId);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.PlantLogTypeId, updateInvalidPlantId);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.DateTime, updateInvalidPlantId);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.Log, updateInvalidPlantId);

            // wrong log type id
            var updateInvalidLogType = new UpdatePlantLogForPlantCommand
            {
                Id             = logPlant1.Id,
                PlantId        = plant1.Id,
                PlantLogTypeId = Guid.NewGuid(),
                DateTime       = DateTime.Now,
                Log            = LoremIpsum.Words(2)
            };

            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.Id, updateInvalidLogType);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.PlantId, updateInvalidLogType);
            validator.ShouldHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.PlantLogTypeId, updateInvalidLogType);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.DateTime, updateInvalidLogType);
            validator.ShouldNotHaveValidationErrorFor(updatePlantLogForPlantCommand => updatePlantLogForPlantCommand.Log, updateInvalidLogType);
        }
Ejemplo n.º 8
0
        public async Task TestCreateValidator()
        {
            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(TestCreateValidator));
            // setup
            var logType      = ValidObjectHelper.ValidPlantLogType();
            var plantSpecies = ValidObjectHelper.ValidPlantSpecies();
            var plant        = ValidObjectHelper.ValidPlant(plantSpecies);
            await context.AddRangeAsync(logType, plantSpecies, plant);

            await context.SaveChangesAsync();


            var validator      = new CreatePlantLogForPlantCommandValidator(context);
            var createValidCmd = new CreatePlantLogForPlantCommand
            {
                Id             = Guid.NewGuid(),
                PlantId        = plant.Id,
                PlantLogTypeId = logType.Id,
                DateTime       = DateTime.Now,
                Log            = LoremIpsum.Words(2),
            };

            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.Id, createValidCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.PlantId, createValidCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.PlantLogTypeId, createValidCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.DateTime, createValidCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.Log, createValidCmd);

            //invalid plant log type
            var createInvalidLogTypeCmd = new CreatePlantLogForPlantCommand
            {
                Id             = Guid.NewGuid(),
                PlantId        = plant.Id,
                PlantLogTypeId = Guid.NewGuid(),
                DateTime       = DateTime.Now,
                Log            = LoremIpsum.Words(2),
            };

            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.Id, createInvalidLogTypeCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.PlantId, createInvalidLogTypeCmd);
            validator.ShouldHaveValidationErrorFor(plantLogCmd => plantLogCmd.PlantLogTypeId, createInvalidLogTypeCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.DateTime, createInvalidLogTypeCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.Log, createInvalidLogTypeCmd);

            //invalid plant log type
            var createInvalidPlantCmd = new CreatePlantLogForPlantCommand
            {
                Id             = Guid.NewGuid(),
                PlantId        = Guid.NewGuid(),
                PlantLogTypeId = logType.Id,
                DateTime       = DateTime.Now,
                Log            = LoremIpsum.Words(2),
            };

            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.Id, createInvalidPlantCmd);
            validator.ShouldHaveValidationErrorFor(plantLogCmd => plantLogCmd.PlantId, createInvalidPlantCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.PlantLogTypeId, createInvalidPlantCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.DateTime, createInvalidPlantCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.Log, createInvalidPlantCmd);

            //invalid id
            var createInvalidIdCmd = new CreatePlantLogForPlantCommand
            {
                Id             = Guid.Empty,
                PlantId        = plant.Id,
                PlantLogTypeId = logType.Id,
                DateTime       = DateTime.Now,
                Log            = LoremIpsum.Words(2),
            };

            validator.ShouldHaveValidationErrorFor(plantLogCmd => plantLogCmd.Id, createInvalidIdCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.PlantId, createInvalidIdCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.PlantLogTypeId, createInvalidIdCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.DateTime, createInvalidIdCmd);
            validator.ShouldNotHaveValidationErrorFor(plantLogCmd => plantLogCmd.Log, createInvalidIdCmd);
        }