public async Task ThrowsEntityAlreadyExistsException_IfMeasureTypeExists()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "ThrowsEntityAlreadyExistsException_IfMeasureTypeExists")
                             .Options;

            var existingId                 = Guid.NewGuid().ToString();
            var existingMeasureUnit        = "Some existing measure unit";
            var existingSuitableSensorType = "Some existing sensor type";
            var softDeletedMeasureType     = new MeasureType
            {
                Id                 = existingId,
                MeasureUnit        = existingMeasureUnit,
                SuitableSensorType = existingSuitableSensorType,
                IsDeleted          = false
            };

            using (var arrangeContext = new SmartDormitoryContext(contextOptions))
            {
                await arrangeContext.MeasureTypes.AddAsync(softDeletedMeasureType);

                await arrangeContext.SaveChangesAsync();
            }

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut = new MeasureTypeService(assertContext);

                await Assert.ThrowsExceptionAsync <EntityAlreadyExistsException>(
                    () => sut.Create(existingMeasureUnit, existingSuitableSensorType), "\nMeasure type is already present in the database.");
            }
        }
        public async Task RestoreMeasureType_IfItIsSoftDeleted()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "RestoreMeasureType_IfItIsSoftDeleted")
                             .Options;

            var deletedId                 = Guid.NewGuid().ToString();
            var deletedMeasureUnit        = "Some soft deleted measure unit";
            var deletedSuitableSensorType = "Some soft deleted sensor type";

            using (var arrangeContext = new SmartDormitoryContext(contextOptions))
            {
                await arrangeContext.MeasureTypes.AddAsync(
                    new MeasureType
                {
                    Id                 = deletedId,
                    MeasureUnit        = deletedMeasureUnit,
                    SuitableSensorType = deletedSuitableSensorType,
                    IsDeleted          = true
                });

                await arrangeContext.SaveChangesAsync();
            }

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut = new MeasureTypeService(assertContext);

                await sut.Create(deletedMeasureUnit, deletedSuitableSensorType);

                Assert.IsTrue(assertContext.MeasureTypes.Any(mt => mt.Id == deletedId && mt.IsDeleted == false));
            }
        }
        public async Task ReturnTrue_WhenMeasureTypeExists()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "ReturnTrue_WhenMeasureTypeExists")
                             .Options;

            var existingId          = Guid.NewGuid().ToString();
            var existingMeasureUnit = "ExistingMeasureUnit";
            var existingSensorType  = "ExistingSensorType";
            var existingMeasureType = new MeasureType
            {
                Id                 = existingId,
                MeasureUnit        = existingMeasureUnit,
                SuitableSensorType = existingSensorType,
                IsDeleted          = false
            };

            using (var arrangeContext = new SmartDormitoryContext(contextOptions))
            {
                await arrangeContext.MeasureTypes.AddAsync(existingMeasureType);

                await arrangeContext.SaveChangesAsync();
            }

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut    = new MeasureTypeService(assertContext);
                var result = await sut.Exists(existingId);

                Assert.IsTrue(result);
            }
        }
        public async Task SoftDelete_WhenMeasureTypeExists()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "SoftDelete_WhenMeasureTypeExists")
                             .Options;

            var existingId                 = Guid.NewGuid().ToString();
            var existingMeasureUnit        = "Some existing measure unit";
            var existingSuitableSensorType = "Some existing sensor type";

            using (var arrangeContext = new SmartDormitoryContext(contextOptions))
            {
                await arrangeContext.MeasureTypes.AddAsync(
                    new MeasureType
                {
                    Id                 = existingId,
                    MeasureUnit        = existingMeasureUnit,
                    SuitableSensorType = existingSuitableSensorType,
                    IsDeleted          = false
                });

                await arrangeContext.SaveChangesAsync();
            }

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut = new MeasureTypeService(assertContext);

                await sut.DeleteType(existingId);

                Assert.IsTrue(assertContext.MeasureTypes.Any(mt => mt.Id == existingId && mt.IsDeleted == true));
            }
        }
        public async Task ThrowArgumentException_WhenPassedInvalidGuid()
        {
            // Arrange
            var contextMock = new Mock <SmartDormitoryContext>();

            var sut = new MeasureTypeService(contextMock.Object);

            // Act & Assert
            await Assert.ThrowsExceptionAsync <ArgumentException>(
                () => sut.GetType("invalidGuid"), "Parameter typeId is not a valid GUID!");
        }
        public async Task ThrowArugmentNullException_WhenPassedNullSensorId()
        {
            // Arrange
            var contextMock = new Mock <SmartDormitoryContext>();

            var sut = new MeasureTypeService(contextMock.Object);

            // Act & Assert
            await Assert.ThrowsExceptionAsync <ArgumentNullException>(
                () => sut.GetType(null), "Parameter typeId cannot be null!");
        }
Exemple #7
0
        public async Task ReturnZero_IfNoMeasureTypes()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "ReturnZero_IfNoMeasureTypes")
                             .Options;

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut    = new MeasureTypeService(assertContext);
                var result = await sut.TotalCount();

                Assert.AreEqual(0, result);
            }
        }
        public async Task ReturnNull_IfNosuchMeasureType()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "ReturnNull_IfNosuchMeasureType")
                             .Options;

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut    = new MeasureTypeService(assertContext);
                var result = await sut.GetMeasureType("Some inexisting measure unit", "Some inexisting senstor type");

                Assert.IsNull(result);
            }
        }
Exemple #9
0
        public async Task ReturnEmptyList_WhenNoValidRecords()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "ReturnEmptyList_WhenNoValidRecords")
                             .Options;

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut    = new MeasureTypeService(assertContext);
                var result = await sut.GetAllDeleted();

                Assert.IsTrue(!result.Any());
            }
        }
        public async Task ReturnFalse_WhenMeasureTypeDoesntExists()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "ReturnFalse_WhenMeasureTypeDoesntExists")
                             .Options;

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut    = new MeasureTypeService(assertContext);
                var result = await sut.Exists(Guid.NewGuid().ToString());

                Assert.IsFalse(result);
            }
        }
        public async Task ThrowsEntityDoesntExistException_WhenDoesntExists()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "ThrowsEntityDoesntExistException_WhenDoesntExists")
                             .Options;

            var inexistingId = Guid.NewGuid().ToString();

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut = new MeasureTypeService(assertContext);

                await Assert.ThrowsExceptionAsync <EntityDoesntExistException>(
                    () => sut.DeleteType(inexistingId), "\nMeasure Type doesn't exists!");
            }
        }
Exemple #12
0
        public async Task Return_CorrectList()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "Return_CorrectListAllDeleted")
                             .Options;

            var deletedId  = Guid.NewGuid().ToString();
            var deletedId2 = Guid.NewGuid().ToString();

            using (var arrangeContext = new SmartDormitoryContext(contextOptions))
            {
                await arrangeContext.MeasureTypes.AddRangeAsync(
                    new MeasureType
                {
                    Id                 = deletedId,
                    MeasureUnit        = "Some measure unit",
                    SuitableSensorType = "Some description",
                    IsDeleted          = false
                },
                    new MeasureType
                {
                    Id                 = deletedId2,
                    MeasureUnit        = "Some soft deleted measure unit",
                    SuitableSensorType = "Some soft deleted description",
                    IsDeleted          = true
                });

                await arrangeContext.SaveChangesAsync();
            }

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut    = new MeasureTypeService(assertContext);
                var result = await sut.GetAllDeleted();

                Assert.IsTrue(result.Count() == 2);
                Assert.IsTrue(result.Any(mt => mt.Id == deletedId));
                Assert.IsTrue(result.Any(mt => mt.Id == deletedId2));
            }
        }
        public async Task AddMeasureType_IfDoesntExists()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "AddMeasureType_IfDoesntExists")
                             .Options;

            var newMeasureUnit        = "New measure unit";
            var newSuitableSensorType = "New sensor type";

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut = new MeasureTypeService(assertContext);

                await sut.Create(newMeasureUnit, newSuitableSensorType);

                Assert.IsTrue(assertContext.MeasureTypes.Count() == 1);
                Assert.IsTrue(assertContext.MeasureTypes.Any(mt => mt.MeasureUnit == newMeasureUnit && mt.SuitableSensorType == newSuitableSensorType));
            }
        }
Exemple #14
0
        public async Task ReturnEmptyList_WhenOnlySoftDeletedItems()
        {
            // Arrange
            contextOptions = new DbContextOptionsBuilder <SmartDormitoryContext>()
                             .UseInMemoryDatabase(databaseName: "ReturnEmptyList_WhenOnlySoftDeletedItems")
                             .Options;

            using (var arrangeContext = new SmartDormitoryContext(contextOptions))
            {
                await arrangeContext.MeasureTypes.AddRangeAsync(
                    new MeasureType
                {
                    Id                 = Guid.NewGuid().ToString(),
                    MeasureUnit        = "Some measure unit",
                    SuitableSensorType = "Some description",
                    IsDeleted          = true
                },
                    new MeasureType
                {
                    Id                 = Guid.NewGuid().ToString(),
                    MeasureUnit        = "Some soft deleted measure unit",
                    SuitableSensorType = "Some soft deleted description",
                    IsDeleted          = true
                });

                await arrangeContext.SaveChangesAsync();
            }

            // Act && Asert
            using (var assertContext = new SmartDormitoryContext(contextOptions))
            {
                var sut    = new MeasureTypeService(assertContext);
                var result = await sut.GetAllNotDeleted();

                Assert.IsTrue(result.Count() == 0);
            }
        }