public async Task GetHivesAsync_CollectionWithTwoElements_ReturnsCollectionWithTwoElements() { var mockUserContext = new Mock <IUserContext>(); var mockHiveContext = new Mock <IProductStoreHiveContext>(); List <StoreHive> hiveList = new List <StoreHive>() { new StoreHive() { Id = 1 }, new StoreHive() { Id = 2 } }; List <StoreHiveSection> sectionList = new List <StoreHiveSection>() { new StoreHiveSection() { StoreHiveId = 1 }, new StoreHiveSection() { StoreHiveId = 2 } }; mockHiveContext.Setup(c => c.Hives).ReturnsEntitySet(hiveList); mockHiveContext.Setup(c => c.Sections).ReturnsEntitySet(sectionList); var service = new HiveService(mockHiveContext.Object, mockUserContext.Object); var list = await service.GetHivesAsync(); Assert.Equal(2, list.Count); }
public async Task SetStatusAsync_IdPresent_RequestedHiveStatusChanged() { var mockUserContext = new Mock <IUserContext>(); var mockHiveContext = new Mock <IProductStoreHiveContext>(); List <StoreHive> hiveList = new List <StoreHive>() { new StoreHive() { Id = 1, IsDeleted = true }, new StoreHive() { Id = 2 } }; mockHiveContext.Setup(c => c.Hives).ReturnsEntitySet(hiveList); var service = new HiveService(mockHiveContext.Object, mockUserContext.Object); await service.SetStatusAsync(1, false); var hive = await service.GetHiveAsync(1); Assert.False(hive.IsDeleted); }
public async Task UpdateHiveAsync_ValidRequest_HiveUpdated() { var newHive = new UpdateHiveRequest() { Code = "cc" }; var mockUserContext = new Mock <IUserContext>(); var mockHiveContext = new Mock <IProductStoreHiveContext>(); List <StoreHive> hiveList = new List <StoreHive>() { new StoreHive() { Id = 1, Code = "aa" }, new StoreHive() { Id = 2, Code = "bb" } }; mockHiveContext.Setup(c => c.Hives).ReturnsEntitySet(hiveList); var service = new HiveService(mockHiveContext.Object, mockUserContext.Object); var updatedHive = await service.UpdateHiveAsync(1, newHive); Assert.Equal(newHive.Code, updatedHive.Code); }
public async Task UpdateHiveAsync_IdNotPresent_RequestedResourceNotFoundExceptionThrown() { var newHive = new UpdateHiveRequest() { Code = "bb" }; var mockUserContext = new Mock <IUserContext>(); var mockHiveContext = new Mock <IProductStoreHiveContext>(); List <StoreHive> hiveList = new List <StoreHive>() { new StoreHive() { Id = 1, Code = "aa" }, new StoreHive() { Id = 2, Code = "bb" } }; mockHiveContext.Setup(c => c.Hives).ReturnsEntitySet(hiveList); var service = new HiveService(mockHiveContext.Object, mockUserContext.Object); Assert.ThrowsAsync <RequestedResourceNotFoundException>(() => service.UpdateHiveAsync(3, newHive)); }
public async Task GetHivesTestAsync_UsingDataDouble() { var data = new List <StoreHive> { new StoreHive { Name = "Hive1" }, new StoreHive { Name = "Hive2" }, new StoreHive { Name = "Hive3" }, }.AsQueryable(); var mockSet = new Mock <IEntitySet <StoreHive> >(); mockSet.As <IQueryable <StoreHive> >().Setup(m => m.Provider).Returns(data.Provider); mockSet.As <IQueryable <StoreHive> >().Setup(m => m.Expression).Returns(data.Expression); mockSet.As <IQueryable <StoreHive> >().Setup(m => m.ElementType).Returns(data.ElementType); mockSet.As <IQueryable <StoreHive> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); var mockContext = new Mock <IProductStoreHiveContext>(); mockContext.Setup(c => c.Hives).Returns(mockSet.Object); Mock <IUserContext> userContext = new Mock <IUserContext>(); var service = new HiveService(mockContext.Object, userContext.Object); var hives = await service.GetHivesAsync(); Assert.Equal(3, hives.Count()); Assert.Equal("Hive1", hives[0].Name); Assert.Equal("Hive2", hives[1].Name); Assert.Equal("Hive3", hives[2].Name); }
public async Task GetHivesAsync_CollectionWithThreeElements_ThreeElementsListReturned() { var hives = new List <StoreHive>() { new StoreHive() { Id = 0 }, new StoreHive() { Id = 1 }, new StoreHive() { Id = 2 } }; var context = new Mock <IProductStoreHiveContext>(); context.Setup(c => c.Hives).ReturnsEntitySet(hives); context.Setup(c => c.Sections).ReturnsEntitySet(new List <StoreHiveSection>()); var service = new HiveService(context.Object, new UserContext()); var list = await service.GetHivesAsync(); Assert.Equal(3, list.Count); Assert.Contains(list, h => h.Id == 0); Assert.Contains(list, h => h.Id == 1); Assert.Contains(list, h => h.Id == 2); }
public async void CreateHiveSuccesFull() { var productContext = new Mock <IProductStoreHiveContext>(); productContext.Setup(p => p.Hives).ReturnsEntitySet(new List <StoreHive>()); var userContext = new Mock <IUserContext>(); userContext.Setup(u => u.UserId).Returns(1); var dbHive = new StoreHive { Id = 1, Code = "12314321" }; var myProductMock = new ProductStoreContextMock(new FakeEntitySet <StoreHive>(new List <StoreHive> { dbHive }), null, null); var service = new HiveService(productContext.Object, userContext.Object); var createRequest = new UpdateHiveRequest { Name = "newHive", Address = "address", Code = "111341" }; await service.CreateHiveAsync(createRequest); //Assert.NotNull(service.GetHivesAsync()); }
public async Task GetHives_HiveAndChainedSection_HiveListItemWithProperlyCountedSections( [Frozen] Mock <IProductStoreHiveContext> context, HiveService service) { var hive = new StoreHive { Id = 1 }; var hiveSection = new StoreHiveSection { Id = 1, StoreHive = hive, StoreHiveId = 1 }; hive.Sections = new[] { hiveSection }; context.Setup(c => c.Hives).ReturnsAsyncEntitySet(new[] { hive }); context.Setup(c => c.Sections).ReturnsAsyncEntitySet(new[] { hiveSection }); var result = await service.GetHivesAsync(); Assert.True(result[0].HiveSectionCount == 1); }
public async Task UpdateHiveAsync_SuccessfulTest(int id, string name, string code, string address) { var context = new Mock <IProductStoreHiveContext>(); var userContext = new Mock <IUserContext>(); context.Setup(c => c.Hives).ReturnsEntitySet(new List <StoreHive>() { new StoreHive() { Id = 1, Code = "11111" } }); var service = new HiveService(context.Object, userContext.Object); var request = new UpdateHiveRequest { Name = name, Code = code, Address = address }; await service.UpdateHiveAsync(id, request); var actualHive = await service.GetHiveAsync(id); Assert.Equal(code, actualHive.Code); }
private void QueryHiveData(List <SensorReading> readings, List <Alert> alerts) { try { if (settings.hive != null) { HiveService service = new HiveService(); if (service.SignIn(settings.hive.username, settings.hive.password)) { var hiveReadings = QueryHiveData(service); readings.AddRange(hiveReadings); } else { alerts.Add(new Alert { deviceName = "Hive", alertText = "Sign-in failed." }); } } } catch (Exception ex) { Utils.Log("Exception querying Hive data. {0}", ex); } }
public async Task UpdateHive_UpdateHiveToExistedCode_ExceptionThrown( [Frozen] Mock <IProductStoreHiveContext> context, HiveService service) { var existedhive = new StoreHive { Id = 1, Code = "Code" }; var updatedExistedHive = new StoreHive { Id = 2, Code = "Code1" }; var newHive = new UpdateHiveRequest { Code = "Code" }; context.Setup(c => c.Hives).ReturnsAsyncEntitySet(new[] { existedhive, updatedExistedHive }); await Assert.ThrowsAsync <RequestedResourceHasConflictException>(async() => await service.UpdateHiveAsync(2, newHive)); }
public async Task UpdateHiveAsync_ChangeCodeToExistingHiveCode_ExceptionThrown() { var hives = new List <StoreHive>() { new StoreHive() { Id = 0, Code = "CODE1" }, new StoreHive() { Id = 1, Code = "CODE2" } }; var context = new Mock <IProductStoreHiveContext>(); context.Setup(c => c.Hives).ReturnsEntitySet(hives); var service = new HiveService(context.Object, new UserContext()); var request = new UpdateHiveRequest { Code = "CODE1" }; await Assert.ThrowsAsync <RequestedResourceHasConflictException>(async() => { await service.UpdateHiveAsync(1, request); }); }
public async Task CreateHiveAsync_UniqueCode_HiveWithSpecifiedCodeCreated() { const string newCode = "cc"; var newHive = new UpdateHiveRequest() { Code = newCode }; var mockUserContext = new Mock <IUserContext>(); var mockHiveContext = new Mock <IProductStoreHiveContext>(); List <StoreHive> hiveList = new List <StoreHive>() { new StoreHive() { Id = 1, Code = "aa" }, new StoreHive() { Id = 2, Code = "bb" } }; mockHiveContext.Setup(c => c.Hives).ReturnsEntitySet(hiveList); var service = new HiveService(mockHiveContext.Object, mockUserContext.Object); var hive = await service.CreateHiveAsync(newHive); Assert.Equal(newCode, hive.Code); }
public async Task UpdateHiveAsync_ChangeHiveProperties_UpdatedHiveReturned() { var hives = new List <StoreHive>() { new StoreHive() { Id = 0, Code = "CODE1" }, new StoreHive() { Id = 1, Code = "CODE2" } }; var context = new Mock <IProductStoreHiveContext>(); context.Setup(c => c.Hives).ReturnsEntitySet(hives); var service = new HiveService(context.Object, new UserContext()); var request = new UpdateHiveRequest { Code = "CODE3", Name = "New Name", Address = "New Address" }; var hive = await service.UpdateHiveAsync(1, request); Assert.Equal(request.Code, hive.Code); Assert.Equal(request.Name, hive.Name); Assert.Equal(request.Address, hive.Address); }
public async Task GetHivesAsync_WithoutParameters_HiveListItems( [Frozen] Mock <IProductStoreHiveContext> context, HiveService service, IFixture fixture) { // arrange var dbHives = fixture.CreateMany <StoreHive>(2).ToList(); var dbSections = fixture.CreateMany <StoreHiveSection>(5).ToList(); dbSections[0].StoreHiveId = dbHives[0].Id; dbSections[1].StoreHiveId = dbHives[0].Id; dbSections[2].StoreHiveId = dbHives[1].Id; dbSections[3].StoreHiveId = dbHives[1].Id; dbSections[4].StoreHiveId = dbHives[1].Id; context.Setup(s => s.Hives).ReturnsEntitySet(dbHives); context.Setup(s => s.Sections).ReturnsEntitySet(dbSections); // act var hiveListItems = await service.GetHivesAsync(); // assert Assert.Equal(2, hiveListItems.Count); Assert.Collection( hiveListItems, item => Assert.Equal(2, item.HiveSectionCount), item => Assert.Equal(3, item.HiveSectionCount)); }
// GET: Hive/Delete/5 public ActionResult Delete(int id) { var service = new HiveService(); var model = service.GetHiveById(id); return(View(model)); }
public ActionResult Index() { var service = new HiveService(); var model = service.GetHives(); return(View(model)); }
/// <summary> /// Queries the data in the sensor tag. /// </summary> public ICollection <SensorReading> QueryHiveData(HiveService service, List <SensorDevice> allDevices) { List <SensorReading> readings = new List <SensorReading>(); var hiveBattChannel = service.GetHiveChannel(HiveService.ChannelType.battery); var hiveTempChannel = service.GetHiveChannel(HiveService.ChannelType.temperature); var fromDate = getDeviceHighWaterMark(hiveTempChannel.UUID); var toDate = DateTime.UtcNow; SensorDevice device = new SensorDevice { uuid = hiveTempChannel.UUID, name = "Hive", type = "HiveHome", location = "" }; allDevices.Add(device); Utils.Log("Querying hive {0} data between {1} and {2}...", hiveTempChannel.id, fromDate, toDate); var values = service.QueryHiveValues(hiveTempChannel, device, fromDate, toDate, settings.refreshPeriodMins); var battValues = service.QueryHiveValues(hiveBattChannel, device, fromDate, toDate, settings.refreshPeriodMins); if (values.Any()) { SensorReading lastReading = null; foreach (var pair in values) { lastReading = new SensorReading { timestamp = Utils.getFromEpoch(pair.Key), temperature = pair.Value, lux = null, humidity = null, device = device }; if (battValues.ContainsKey(pair.Key)) { lastReading.battery = battValues[pair.Key]; } readings.Add(lastReading); } if (readings.Any()) { Utils.Log("Found {0} readings for device '{1}' (latest: {2:dd-MMM-yy HH:mm:ss}).", readings.Count(), device.name, readings.Max(x => x.timestamp)); } else { Utils.Log("No readings found for device {1}.", device.name); } // Now query the current battery level, and set it in the most recent reading. lastReading.batteryPercentage = service.QueryHiveBattery(); } return(readings); }
public async Task DeleteHive_DeletingHiveWithUnexistedId_ExceptionThrown( [Frozen] Mock <IProductStoreHiveContext> context, HiveService service) { context.Setup(c => c.Hives).ReturnsAsyncEntitySet(new StoreHive[0]); await Assert.ThrowsAsync <RequestedResourceNotFoundException>(async() => await service.DeleteHiveAsync(1)); }
public ActionResult DeleteHive(int id) { var service = new HiveService(); service.DeleteHive(id); TempData["SaveResult"] = "Your Hive has been deleted. Rest in Pollen."; return(RedirectToAction("Index")); }
public async void DeleteHiveSuccessfull(IFixture fixture, HiveService service, [Frozen] Mock <IProductStoreHiveContext> productContext, [Frozen] Mock <IUserContext> userContext) { var hives = fixture.CreateMany <StoreHive>(5).ToList(); productContext.Setup(p => p.Hives).ReturnsEntitySet(hives); await service.DeleteHiveAsync(1); }
public async Task GetHive_RequestedResourceNotFoundException( [Frozen] Mock <IProductStoreHiveContext> context, HiveService service) { context.Setup(x => x.Hives).ReturnsEntitySet(new List <StoreHive>()); Func <Task> act = async() => await service.GetHiveAsync(0); act.Should().Throw <RequestedResourceNotFoundException>(); }
public void GetHiveAsync_NoSuchHiveIdTest(int id) { var context = new Mock <IProductStoreHiveContext>(); var userContext = new Mock <IUserContext>(); context.Setup(c => c.Hives).ReturnsEntitySet(new List <StoreHive>()); var service = new HiveService(context.Object, userContext.Object); Assert.ThrowsAsync <RequestedResourceNotFoundException>(async() => await service.GetHiveAsync(id)); }
public async Task GetHives_EmptySet_EmptyListReturned( [Frozen] Mock <IProductStoreHiveContext> context, HiveService service) { context.Setup(c => c.Hives).ReturnsAsyncEntitySet(new StoreHive[0]); var result = await service.GetHivesAsync(); Assert.Empty(result); }
public async Task SetStatusAsync_Hive_RequestedResourceNotFoundException(IFixture fixture) { var storeHives = fixture.CreateMany <StoreHive>(0).ToArray(); _context.Setup(s => s.Hives).ReturnsEntitySet(storeHives); var service = new HiveService(_context.Object, _userContext.Object); await Assert.ThrowsAsync <RequestedResourceNotFoundException>(() => service.SetStatusAsync(1, true)); }
public async Task SetStatusAsync_PassesHiveIdAndDeleteStatus_ExpectsRequestedResourceNotFoundException( [Frozen] Mock <IProductStoreHiveContext> contextMock, HiveService hiveService, IFixture fixture) { var storeHives = fixture.CreateMany <StoreHive>(3).ToArray(); contextMock.Setup(c => c.Hives).ReturnsEntitySet(storeHives); await Assert.ThrowsAsync <RequestedResourceNotFoundException>(() => hiveService.SetStatusAsync(123, false)); }
public async Task GetHivesAsync_EmptyCollection_EmptyListReturned() { var context = new Mock <IProductStoreHiveContext>(); context.Setup(c => c.Hives).ReturnsEntitySet(new List <StoreHive>()); context.Setup(c => c.Sections).ReturnsEntitySet(new List <StoreHiveSection>()); var service = new HiveService(context.Object, new UserContext()); var list = await service.GetHivesAsync(); Assert.Empty(list); }
public async Task GetHiveAsync_EmptyCollection_ExceptionThrown() { var context = new Mock <IProductStoreHiveContext>(); context.Setup(c => c.Hives).ReturnsEntitySet(new List <StoreHive>()); var service = new HiveService(context.Object, new UserContext()); await Assert.ThrowsAsync <RequestedResourceNotFoundException>(async() => { await service.GetHiveAsync(0); }); }
public async Task GetHiveAsync_PassesHiveId_ThrowsRequestedResourceNotFoundException( [Frozen] Mock <IProductStoreHiveContext> contextMock, HiveService hiveService, IFixture fixture, int hiveId) { var storeHives = fixture.CreateMany <StoreHive>(0).ToArray(); contextMock.Setup(c => c.Hives).ReturnsEntitySet(storeHives); await Assert.ThrowsAsync <RequestedResourceNotFoundException>(() => hiveService.GetHiveAsync(hiveId)); }
public async Task GetHivesAsync_EmptyCollectionTest() { var context = new Mock <IProductStoreHiveContext>(); var userContext = new Mock <IUserContext>(); context.Setup(c => c.Hives).ReturnsEntitySet(new List <StoreHive>()); var service = new HiveService(context.Object, userContext.Object); var hives = await service.GetHivesAsync(); Assert.Empty(hives); }