public async Task Return_TotalUsersCount_WhenParametersAreValid(string id, string username) { //Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "Return_TotalUsersCount_WhenParametersAreValid") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.Users.AddRangeAsync(new User { Id = id, UserName = username }, new User { Id = "anotherValidId", UserName = "******" }); await assertContext.SaveChangesAsync(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userService = new UserService(assertContext); //Act var usersCount = await userService.GetTotalUserAsync(username); //Assert Assert.IsTrue(usersCount == 1); } }
public void Return_Successfully_AllApiSensors(int id, string description, string icbSensorId, double maxVal, double minVal, string tag, string url, double value, int pollingInterval) { //Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "Return_Successfully_AllApiSensors") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { assertContext.MeasureTypes.Add(new MeasureType { Id = 1, Type = "C" }); assertContext.SaveChanges(); assertContext.Sensors.Add(new Sensor { Id = id, Description = description, IcbSensorId = icbSensorId, MaxValue = maxVal, MinValue = minVal, Tag = tag, Url = url, Value = value, TimeStamp = DateTime.Now, PollingInterval = pollingInterval, ModifiedOn = DateTime.Now, MeasureTypeId = 1 }); assertContext.SaveChanges(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var mesureTypesServiceMock = new Mock <IMeasureTypesService>(); var ICBApiSensorsServiceMock = new ICBApiSensorsService(assertContext, mesureTypesServiceMock.Object); //Act var allSensors = ICBApiSensorsServiceMock.ListAllApiSensors().ToList(); //Assert Assert.IsTrue(allSensors[0].Id == id); Assert.IsTrue(allSensors[0].Description == description); Assert.IsTrue(allSensors[0].Tag == tag); Assert.IsTrue(allSensors[0].MinValue == minVal); Assert.IsTrue(allSensors[0].MaxValue == maxVal); Assert.IsTrue(allSensors[0].Value == value); Assert.IsTrue(allSensors[0].PollingInterval == pollingInterval); Assert.IsTrue(allSensors[0].Url == url); Assert.IsTrue(allSensors[0].IcbSensorId == icbSensorId); Assert.IsTrue(allSensors[0].MeasureTypeId == 1); } }
public async Task GetSensorsTypeMinMaxValues_WhenArgumentIsCorrect(string tag) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "GetSensorsTypeMinMaxValues_WhenArgumentIsCorrect") .Options; const double minValue = 5.00; const double maxValue = 15.00; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.Sensors.AddRangeAsync( new Sensor { Id = 1, PollingInterval = 10, Description = "Some description", Tag = tag, MinValue = minValue, MaxValue = maxValue, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }, new Sensor { Id = 2, PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }); await assertContext.SaveChangesAsync(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act var minMax = new List <double>(await userSensorServiceMock.GetSensorsTypeMinMaxValues(tag)); var min = minMax[0]; var max = minMax[1]; // Assert Assert.IsTrue(min == minValue); Assert.IsTrue(max == maxValue); } }
public async Task ThrowArgumentNullException_WhenICBSensorDoesNotExist(int userSensorId, string icbSensorId, string name, string description, double minValue, double maxValue, int pollingInterval, double latitude, double longitude, bool isPublic, bool alarm) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ThrowArgumentNullException_WhenICBSensorDoesNotExist") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.Sensors.AddRangeAsync( new Sensor { Id = 1, IcbSensorId = "Another icbSensorId", PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }, new Sensor { Id = 2, IcbSensorId = "Another icbSensorId 1", PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }); await assertContext.SaveChangesAsync(); } // Act and Assert using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await userSensorServiceMock.EditSensor(userSensorId, icbSensorId, name, description, minValue, maxValue, pollingInterval, latitude, longitude, isPublic, alarm)); } }
public async Task ThrowArgumentException_WhenMaxValueIsOutOfSensorValueRange(string userId, int sensorId, string name, string description, double minValue, double maxValue, int pollingInterval, double latitude, double longitude, bool isPublic, bool alarm, string imageUrl) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ThrowArgumentException_WhenMaxValueIsOutOfSensorValueRange") .Options; int existID = sensorId; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.Sensors.AddRangeAsync( new Sensor { Id = existID, PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }, new Sensor { Id = 2, PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }); await assertContext.SaveChangesAsync(); } // Act and Assert using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await userSensorServiceMock.AddSensorAsync(userId, sensorId, name, description, minValue, maxValue, pollingInterval, latitude, longitude, isPublic, alarm, imageUrl)); } }
public async Task DeleteUserSensor_WhenArgumentIsCorrect(int id) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "DeleteUserSensor_WhenArgumentIsCorrect") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userId = Guid.NewGuid().ToString(); await assertContext.UserSensors.AddAsync( new UserSensors { Id = 1, UserId = userId, SensorId = 1, Name = "Some name", Description = "Some description", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url", }); await assertContext.SaveChangesAsync(); } // Act and Assert using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act await userSensorServiceMock.DeleteSensor(id); var deletedSensor = await assertContext.UserSensors.FirstOrDefaultAsync(s => s.Id == id); Assert.IsTrue(deletedSensor.IsDeleted == true); } }
public async Task ReturnSensor_WhenArgumentIsCorrect(string id) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ReturnSensor_WhenArgumentIsCorrect") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userId = Guid.NewGuid().ToString(); await assertContext.Sensors.AddAsync( new Sensor { Id = 1, IcbSensorId = "Valid icbSensorId", PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }); await assertContext.SaveChangesAsync(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act var updatedSensor = await userSensorServiceMock.UpdateSensorValue(id); var contextSensor = await assertContext.Sensors.FirstOrDefaultAsync(s => s.IcbSensorId == id); // Assert Assert.AreSame(updatedSensor, contextSensor); } }
public async Task AddMeasureType_WhenParameterTypeIsCorrect() { //Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ThrowArgumentNullException_WhenParameterTypeIsCorrect") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.MeasureTypes.AddRangeAsync(new MeasureType() { Type = "Type" }, new MeasureType() { Type = "TestType" }, new MeasureType() { Type = "C" }); await assertContext.SaveChangesAsync(); } //Act && Assert using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var measureTypeServiceMock = new MeasureTypesService(assertContext); var newType = "F"; measureTypeServiceMock.AddMeasureType(newType); var measureType = await assertContext.MeasureTypes .FirstOrDefaultAsync(m => m.Type == newType); Assert.AreEqual(newType, measureType.Type); } }
public async Task ThrowArgumentNullEcxeption_WhenUserSensorIsNotFound(int id) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ThrowArgumentNullEcxeption_WhenUserSensorIsNotFound") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userId = Guid.NewGuid().ToString(); await assertContext.UserSensors.AddAsync( new UserSensors { Id = 1, UserId = userId, SensorId = 1, Name = "Some name", Description = "Some description", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url", }); await assertContext.SaveChangesAsync(); } // Act and Assert using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await userSensorServiceMock.DeleteSensor(id)); } }
public void ThrowArgumentException_WhenSensorExists() { //Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ThrowArgumentNullException_WhenSensorExists") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { assertContext.MeasureTypes.Add(new MeasureType() { Type = "Type" }); assertContext.SaveChanges(); } //Act && Assert using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var measureTypeServiceMock = new MeasureTypesService(assertContext); Assert.ThrowsException <ArgumentException>(() => measureTypeServiceMock.AddMeasureType("Type")); } }
public ICBApiSensorsService(smartDormitoryDbContext context, IMeasureTypesService measureTypesService) { this.context = context ?? throw new ArgumentNullException(nameof(context)); this.measureTypesService = measureTypesService ?? throw new ArgumentNullException(nameof(measureTypesService)); }
public MeasureTypesService(smartDormitoryDbContext context) { this.context = context ?? throw new ArgumentNullException(nameof(context)); }
public async Task ReturnAllPublicUserSensors() { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ReturnAllPublicUserSensors") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.Sensors.AddAsync( new Sensor { Id = 1, PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }); var userId = Guid.NewGuid().ToString(); var userSensor1 = new UserSensors { UserId = userId, SensorId = 1, Name = "Some name", Description = "Some description", MinValue = 11, MaxValue = 18, PollingInterval = 13, Latitude = 1.15, Longitude = 5.15, IsPublic = true, Alarm = false, IsDeleted = false, ImageUrl = "Some Url", }; var userSensor2 = new UserSensors { UserId = userId, SensorId = 1, Name = "Some name1", Description = "Some description1", MinValue = 12, MaxValue = 19, PollingInterval = 23, Latitude = 2.15, Longitude = 6.15, IsPublic = true, Alarm = false, IsDeleted = true, ImageUrl = "Some Url 1", }; var userSensor3 = new UserSensors { UserId = userId, SensorId = 1, Name = "Some name2", Description = "Some description2", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 2", }; await assertContext.UserSensors.AddRangeAsync(userSensor1, userSensor2, userSensor3); await assertContext.Users.AddAsync(new User { Id = userId, UserName = "******", UserSensors = new List <UserSensors>() { userSensor1, userSensor2, userSensor3 } }); await assertContext.SaveChangesAsync(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act var allSensors = await userSensorServiceMock.GetAllPublicUsersSensorsAsync(); var sensors = allSensors.ToList(); // Assert Assert.IsTrue(sensors.Count == 1); } }
public async Task AddUserSensor_WhenArgumentsAreCorrect(string userId, int sensorId, string name, string description, double minValue, double maxValue, int pollingInterval, double latitude, double longitude, bool isPublic, bool alarm, string imageUrl) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "AddUserSensor_WhenArgumentsAreCorrect") .Options; int existID = sensorId; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.Sensors.AddRangeAsync( new Sensor { Id = existID, PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }, new Sensor { Id = 2, PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }); await assertContext.SaveChangesAsync(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act await userSensorServiceMock.AddSensorAsync(userId, sensorId, name, description, minValue, maxValue, pollingInterval, latitude, longitude, isPublic, alarm, imageUrl); var addedSensor = await assertContext.UserSensors.FirstOrDefaultAsync(s => s.SensorId == sensorId); // Assert Assert.IsTrue(addedSensor.UserId == userId); Assert.IsTrue(addedSensor.SensorId == sensorId); Assert.IsTrue(addedSensor.Name == name); Assert.IsTrue(addedSensor.Description == description); Assert.IsTrue(addedSensor.MinValue == minValue); Assert.IsTrue(addedSensor.MaxValue == maxValue); Assert.IsTrue(addedSensor.PollingInterval == pollingInterval); Assert.IsTrue(addedSensor.Latitude == latitude); Assert.IsTrue(addedSensor.Longitude == longitude); Assert.IsTrue(addedSensor.IsPublic == isPublic); Assert.IsTrue(addedSensor.Alarm == alarm); Assert.IsTrue(addedSensor.ImageUrl == imageUrl); } }
public void Return_SensorTotalCount(int id, string description, string icbSensorId, double maxVal, double minVal, string tag, string url, double value, int pollingInterval) { //Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "Return_SensorTotalCount") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { assertContext.MeasureTypes.Add(new MeasureType { Id = 1, Type = "C" }); assertContext.Sensors.AddRange(new Sensor { Id = id, Description = description, IcbSensorId = icbSensorId, MaxValue = maxVal, MinValue = minVal, Tag = tag, Url = url, Value = value, TimeStamp = DateTime.Now, PollingInterval = pollingInterval, ModifiedOn = DateTime.Now, MeasureTypeId = 1 }, new Sensor { Id = 2, Description = description, IcbSensorId = icbSensorId, MaxValue = maxVal, MinValue = minVal, Tag = tag, Url = url, Value = value, TimeStamp = DateTime.Now, PollingInterval = pollingInterval, ModifiedOn = DateTime.Now, MeasureTypeId = 1 }, new Sensor { Id = 3, Description = description, IcbSensorId = icbSensorId, MaxValue = maxVal, MinValue = minVal, Tag = "AnotherTag", Url = url, Value = value, TimeStamp = DateTime.Now, PollingInterval = pollingInterval, ModifiedOn = DateTime.Now, MeasureTypeId = 1 }); assertContext.SaveChanges(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var mesureTypesServiceMock = new Mock <IMeasureTypesService>(); var ICBApiSensorsServiceMock = new ICBApiSensorsService(assertContext, mesureTypesServiceMock.Object); //Act var count = ICBApiSensorsServiceMock.Total(); //Assert Assert.IsTrue(count == 3); } }
public async Task ReturnAllUserSensorsAsync_WhenArgumentIsCorrect() { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ReturnAllUserSensorsAsync_WhenArgumentIsCorrect") .Options; string userId = Guid.NewGuid().ToString(); using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.UserSensors.AddRangeAsync( new UserSensors { UserId = userId, SensorId = 1, Name = "Some name", Description = "Some description", MinValue = 11, MaxValue = 18, PollingInterval = 13, Latitude = 1.15, Longitude = 5.15, IsPublic = true, Alarm = false, IsDeleted = false, ImageUrl = "Some Url", }, new UserSensors { UserId = userId, SensorId = 1, Name = "Some name1", Description = "Some description1", MinValue = 12, MaxValue = 19, PollingInterval = 23, Latitude = 2.15, Longitude = 6.15, IsPublic = true, Alarm = false, IsDeleted = true, ImageUrl = "Some Url 1", }, new UserSensors { UserId = userId + "123", SensorId = 1, Name = "Some name2", Description = "Some description2", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 2", }, new UserSensors { UserId = userId, SensorId = 2, Name = "Some name2", Description = "Some description3", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 3", } ); var sensor1 = new Sensor { Id = 1, PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }; await assertContext.Sensors.AddAsync(sensor1); await assertContext.MeasureTypes.AddAsync(new MeasureType { Type = "TestType", Sensors = new List <Sensor>() { sensor1 } }); await assertContext.SaveChangesAsync(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act var userSensorsMock = await userSensorServiceMock.GetAllUserSensorsAsync(userId); // Assert Assert.IsTrue(userSensorsMock.Count() == 1); Assert.IsTrue(userSensorsMock.First().UserId == userId); } }
public async Task ReturnAllUsersUserSensors_WhenArgumentsAreEmpty(string searchByName, string searchByTag, int page, int pageSize) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ReturnAllUsersUserSensors_WhenArgumentsAreEmpty") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { string userId = Guid.NewGuid().ToString(); var gosho = new User { Id = userId, UserName = "******", UserSensors = new List <UserSensors>(), }; var pesho = new User { Id = userId + "123", UserName = "******", UserSensors = new List <UserSensors>(), }; await assertContext.Users.AddRangeAsync(gosho, pesho); await assertContext.UserSensors.AddRangeAsync( new UserSensors { UserId = userId, User = gosho, SensorId = 1, Name = "Some name", Description = "Some description", MinValue = 11, MaxValue = 18, PollingInterval = 13, Latitude = 1.15, Longitude = 5.15, IsPublic = true, Alarm = false, IsDeleted = false, ImageUrl = "Some Url", }, new UserSensors { UserId = userId, User = gosho, SensorId = 1, Name = "Some name1", Description = "Some description1", MinValue = 12, MaxValue = 19, PollingInterval = 23, Latitude = 2.15, Longitude = 6.15, IsPublic = true, Alarm = false, IsDeleted = true, ImageUrl = "Some Url 1", }, new UserSensors { UserId = userId + "123", User = pesho, SensorId = 1, Name = "Some name2", Description = "Some description2", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 2", }, new UserSensors { UserId = userId, User = pesho, SensorId = 2, Name = "Some name3", Description = "Some description3", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 3", } ); var sensor1 = new Sensor { Id = 1, PollingInterval = 10, Description = "Some description", Tag = "Humidity", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, MeasureTypeId = 1, }; var sensor2 = new Sensor { Id = 2, PollingInterval = 10, Description = "Some description1", Tag = "Temperature", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL1", ModifiedOn = DateTime.Now, MeasureTypeId = 1, }; await assertContext.Sensors.AddAsync(sensor1); await assertContext.MeasureTypes.AddAsync(new MeasureType { Id = 1, Type = "TestType", Sensors = new List <Sensor>() { sensor1, sensor2 } }); await assertContext.SaveChangesAsync(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act var userSensorsMock = await userSensorServiceMock.GetAllUsersSensorsAsync(searchByName, searchByTag, page, pageSize); // Assert Assert.IsTrue(userSensorsMock.Count() == 3); } }
public UserSensorService(smartDormitoryDbContext context) { this.context = context ?? throw new ArgumentNullException(nameof(context)); }
public void ReturnCountOfAllNotDeletedUserSensorsBySearchName(string searchName) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ReturnCountOfAllNotDeletedUserSensorsBySearchName") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userId = Guid.NewGuid().ToString(); var userSensor1 = new UserSensors { UserId = userId, SensorId = 1, Name = "Some name", Description = "Some description", MinValue = 11, MaxValue = 18, PollingInterval = 13, Latitude = 1.15, Longitude = 5.15, IsPublic = true, Alarm = false, IsDeleted = false, ImageUrl = "Some Url", }; var userSensor2 = new UserSensors { UserId = userId, SensorId = 1, Name = "Some name1", Description = "Some description1", MinValue = 12, MaxValue = 19, PollingInterval = 23, Latitude = 2.15, Longitude = 6.15, IsPublic = true, Alarm = false, IsDeleted = true, ImageUrl = "Some Url 1", }; var userSensor3 = new UserSensors { UserId = userId + "123", SensorId = 1, Name = "Some name2", Description = "Some description2", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 2", }; assertContext.UserSensors.AddRange(userSensor1, userSensor2, userSensor3); assertContext.Users.AddRange(new User { Id = userId, UserName = "******", UserSensors = new List <UserSensors>() { userSensor1, userSensor2 } }, new User { Id = userId + "123", UserName = "******", UserSensors = new List <UserSensors>() { userSensor3 } }); assertContext.SaveChanges(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act var totalSensorsCountMock = userSensorServiceMock.TotalByName(searchName); // Assert Assert.IsTrue(totalSensorsCountMock == 1); } }
public void ReturnCountOfUserSensorsBySearchText_WhenArgumentIsCorrect(string searcheText) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ReturnCountOfUserSensorsBySearchText_WhenArgumentIsCorrect") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var sensor1 = new Sensor { Id = 1, PollingInterval = 10, Description = "Some description", Tag = "Humidity", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }; var sensor2 = new Sensor { Id = 2, PollingInterval = 10, Description = "Some description1", Tag = "Temperature", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }; assertContext.Sensors.AddRange(sensor1, sensor2); assertContext.UserSensors.AddRange( new UserSensors { UserId = "123", SensorId = 1, Sensor = sensor1, Name = "Some name", Description = "Some description", MinValue = 11, MaxValue = 18, PollingInterval = 13, Latitude = 1.15, Longitude = 5.15, IsPublic = true, Alarm = false, IsDeleted = false, ImageUrl = "Some Url", }, new UserSensors { UserId = "123", SensorId = 1, Sensor = sensor1, Name = "Some name1", Description = "Some description1", MinValue = 12, MaxValue = 19, PollingInterval = 23, Latitude = 2.15, Longitude = 6.15, IsPublic = true, Alarm = false, IsDeleted = true, ImageUrl = "Some Url 1", }, new UserSensors { UserId = "123" + "123", SensorId = 2, Sensor = sensor2, Name = "Some name2", Description = "Some description2", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 2", }, new UserSensors { UserId = "123", SensorId = 2, Sensor = sensor2, Name = "Some name2", Description = "Some description3", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 3", } ); assertContext.SaveChangesAsync(); } using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act var userSensorsMock = userSensorServiceMock.TotalContainingText(searcheText); // Assert Assert.IsTrue(userSensorsMock == 1); } }
public async Task ThrowArgumentNullException_WhenUserSensorNotFound(int userSensorId, string icbSensorId, string name, string description, double minValue, double maxValue, int pollingInterval, double latitude, double longitude, bool isPublic, bool alarm) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "ThrowArgumentNullException_WhenUserSensorNotFound") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.Sensors.AddRangeAsync( new Sensor { Id = 1, IcbSensorId = "Valid icbSensorId", PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }, new Sensor { Id = 2, IcbSensorId = "Another icbSensorId 1", PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }); var userId = Guid.NewGuid().ToString(); await assertContext.UserSensors.AddRangeAsync( new UserSensors { Id = 1, UserId = userId, SensorId = 1, Name = "Some name", Description = "Some description", MinValue = 13, MaxValue = 20, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url", }, new UserSensors { Id = 2, UserId = userId, SensorId = 2, Name = "Some name 2", Description = "Some description 2", MinValue = 11, MaxValue = 20, PollingInterval = 40, Latitude = 4.15, Longitude = 5.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 2", }); await assertContext.SaveChangesAsync(); } // Act and Assert using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await userSensorServiceMock.EditSensor(userSensorId, icbSensorId, name, description, minValue, maxValue, pollingInterval, latitude, longitude, isPublic, alarm)); } }
public async Task EditUserSensor_WhenArgumentsAreCorrect(int userSensorId, string icbSensorId, string name, string description, double minValue, double maxValue, int pollingInterval, double latitude, double longitude, bool isPublic, bool alarm) { // Arrange contextOptions = new DbContextOptionsBuilder <smartDormitoryDbContext>() .UseInMemoryDatabase(databaseName: "EditUserSensor_WhenArgumentsAreCorrect") .Options; using (var assertContext = new smartDormitoryDbContext(contextOptions)) { await assertContext.Sensors.AddRangeAsync( new Sensor { Id = 1, IcbSensorId = "Valid icbSensorId", PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }, new Sensor { Id = 2, IcbSensorId = "Another icbSensorId 1", PollingInterval = 10, Description = "Some description", Tag = "Some tag", MinValue = 10.00, MaxValue = 20.00, TimeStamp = DateTime.Now, Value = 15.00, Url = "Some URL", ModifiedOn = DateTime.Now, }); var userId = Guid.NewGuid().ToString(); await assertContext.UserSensors.AddRangeAsync( new UserSensors { Id = 1, UserId = userId, SensorId = 1, Name = "Some name", Description = "Some description", MinValue = 13, MaxValue = 18, PollingInterval = 33, Latitude = 3.15, Longitude = 7.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url", }, new UserSensors { Id = 2, UserId = userId, SensorId = 2, Name = "Some name 2", Description = "Some description 2", MinValue = 11, MaxValue = 20, PollingInterval = 40, Latitude = 4.15, Longitude = 5.15, IsPublic = false, Alarm = false, IsDeleted = false, ImageUrl = "Some Url 2", }); await assertContext.SaveChangesAsync(); } // Act and Assert using (var assertContext = new smartDormitoryDbContext(contextOptions)) { var userSensorServiceMock = new UserSensorService(assertContext); // Act await userSensorServiceMock.EditSensor(userSensorId, icbSensorId, name, description, minValue, maxValue, pollingInterval, latitude, longitude, isPublic, alarm); var editedSensorMock = await assertContext.UserSensors.FirstOrDefaultAsync(s => s.Id == userSensorId); Assert.IsTrue(editedSensorMock.Name == name); Assert.IsTrue(editedSensorMock.Description == description); Assert.IsTrue(editedSensorMock.MinValue == minValue); Assert.IsTrue(editedSensorMock.MaxValue == maxValue); Assert.IsTrue(editedSensorMock.PollingInterval == pollingInterval); Assert.IsTrue(editedSensorMock.Latitude == latitude); Assert.IsTrue(editedSensorMock.Longitude == longitude); Assert.IsTrue(editedSensorMock.IsPublic == isPublic); Assert.IsTrue(editedSensorMock.Alarm == alarm); } }
public void ThrowArgumentNullException_When_IncorrectArgumentPassed() { smartDormitoryDbContext context = null; Assert.ThrowsException <ArgumentNullException>(() => new UserSensorService(context)); }