Example #1
0
        public void LocationService_GetAsync_DoeNotReturnLocationsFromWrongProject()
        {
            //Arrange
            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();
            var mockDbContextScope = new Mock<IDbContextReadOnlyScope>();
            var mockEfDbContext = new Mock<EFDbContext>();
            mockDbContextScopeFac.Setup(x => x.CreateReadOnly(DbContextScopeOption.JoinExisting)).Returns(mockDbContextScope.Object);
            mockDbContextScope.Setup(x => x.DbContexts.Get<EFDbContext>()).Returns(mockEfDbContext.Object);

            var projectPerson1 = new Person { Id = "dummyUserId1", FirstName = "Firs1", LastName = "Last1" };
            var projectPerson2 = new Person { Id = "dummyUserId2", FirstName = "Firs2", LastName = "Last2" };

            var project1 = new Project { Id = "dummyId1", ProjectName = "Project1", ProjectAltName = "ProjectAlt1", IsActive_bl = true, ProjectCode = "CODE1", 
                ProjectPersons = new List<Person> { projectPerson1 } };
            var project2 = new Project { Id = "dummyId2", ProjectName = "Project2", ProjectAltName = "ProjectAlt2", IsActive_bl = false, ProjectCode = "CODE2", 
                ProjectPersons = new List<Person> { projectPerson2 } };

            var dbEntry1 = new Location { Id = "dummyEntryId1", LocName = "Loc1", LocAltName = "LocAlt1", IsActive_bl = false, Comments = "DummyComments1",
                AccessInfo = "DummyInfo1", AssignedToProject = project1 };
            var dbEntry2 = new Location { Id = "dummyEntryId2", LocName = "Loc2", LocAltName = "LocAlt2", IsActive_bl = true, Comments = "DummyComments2",
                AccessInfo = "DummyInfo2", AssignedToProject = project2 };
            var dbEntries = (new List<Location> { dbEntry1, dbEntry2 }).AsQueryable();

            var mockDbSet = new Mock<DbSet<Location>>();
            mockDbSet.As<IDbAsyncEnumerable<Location>>().Setup(m => m.GetAsyncEnumerator()).Returns(new MockDbAsyncEnumerator<Location>(dbEntries.GetEnumerator())); 
            mockDbSet.As<IQueryable<Location>>().Setup(m => m.Provider).Returns(new MockDbAsyncQueryProvider<Location>(dbEntries.Provider));
            mockDbSet.As<IQueryable<Location>>().Setup(m => m.Expression).Returns(dbEntries.Expression);
            mockDbSet.As<IQueryable<Location>>().Setup(m => m.ElementType).Returns(dbEntries.ElementType);
            mockDbSet.As<IQueryable<Location>>().Setup(m => m.GetEnumerator()).Returns(dbEntries.GetEnumerator());
            mockDbSet.Setup(x => x.Include(It.IsAny<string>())).Returns(mockDbSet.Object);
            
            mockEfDbContext.Setup(x => x.Locations).Returns(mockDbSet.Object);

            var locationService = new LocationService(mockDbContextScopeFac.Object, projectPerson1.Id);

            //Act
            var resultLocations = locationService.GetAsync(new[] { dbEntry1.Id, dbEntry2.Id }).Result;
            
            //Assert
            Assert.IsTrue(resultLocations.Count == 0);
        }
Example #2
0
 public async Task<ActionResult> Edit(Location[] records)
 {
     ViewBag.ServiceName = "LocationService.EditAsync";
     var newEntryIds = await locationService.EditAsync(records).ConfigureAwait(false);
     return DbJson(new { Success = "True", newEntryIds = newEntryIds });
 }
Example #3
0
        public void LocationService_LookupAsync_ReturnsMatchingRecords()
        {
            // Arrange
            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();
            var mockDbContextScope = new Mock<IDbContextReadOnlyScope>();
            var mockEfDbContext = new Mock<EFDbContext>();
            mockDbContextScopeFac.Setup(x => x.CreateReadOnly(DbContextScopeOption.JoinExisting)).Returns(mockDbContextScope.Object);
            mockDbContextScope.Setup(x => x.DbContexts.Get<EFDbContext>()).Returns(mockEfDbContext.Object);

            var projectPerson1 = new Person { Id = "dummyUserId1", FirstName = "Firs1", LastName = "Last1" };

            var project1 = new Project { Id = "dummyId1", ProjectName = "Project1", ProjectAltName = "ProjectAlt1", IsActive_bl = true, ProjectCode = "CODE1", 
                ProjectPersons = new List<Person> { projectPerson1 } };

            var dbEntry1 = new Location { Id = "dummyEntryId1", LocName = "Loc1", LocAltName = "LocAlt1", IsActive_bl = true, 
                AccessInfo = "DummyInfo1" , AssignedToProject = project1 };
            var dbEntry2 = new Location { Id = "dummyEntryId2", LocName = "Loc2", LocAltName = "LocAlt2", IsActive_bl = true,
                AccessInfo = "DummyInfo2" , AssignedToProject = project1 };
            var dbEntry3 = new Location { Id = "dummyEntryId3", LocName = "Loc3", LocAltName = "LocAlt3", IsActive_bl = false, 
                AccessInfo = "DummyInfo3" , AssignedToProject = project1 };
            var dbEntries = (new List<Location> { dbEntry1, dbEntry2, dbEntry3 }).AsQueryable();

            var mockDbSet = new Mock<DbSet<Location>>();
            mockDbSet.As<IDbAsyncEnumerable<Location>>().Setup(m => m.GetAsyncEnumerator()).Returns(new MockDbAsyncEnumerator<Location>(dbEntries.GetEnumerator()));
            mockDbSet.As<IQueryable<Location>>().Setup(m => m.Provider).Returns(new MockDbAsyncQueryProvider<Location>(dbEntries.Provider));
            mockDbSet.As<IQueryable<Location>>().Setup(m => m.Expression).Returns(dbEntries.Expression);
            mockDbSet.As<IQueryable<Location>>().Setup(m => m.ElementType).Returns(dbEntries.ElementType);
            mockDbSet.As<IQueryable<Location>>().Setup(m => m.GetEnumerator()).Returns(dbEntries.GetEnumerator());
            mockDbSet.Setup(x => x.Include(It.IsAny<string>())).Returns(mockDbSet.Object);
            mockEfDbContext.Setup(x => x.Locations).Returns(mockDbSet.Object);

            var locationService = new LocationService(mockDbContextScopeFac.Object, projectPerson1.Id);

            //Act
            var returnedLocs = locationService.LookupAsync("Loc1", true).Result;

            //Assert
            Assert.IsTrue(returnedLocs.Count == 1);
            Assert.IsTrue(returnedLocs[0].LocAltName.Contains("LocAlt1"));
        }
        public void BaseDbService_EditAsync_DoesNotUpdateIfProjLocIdsNotMatching()
        {
            // Arrange
            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();
            var mockDbContextScope = new Mock<IDbContextScope>();
            var mockEfDbContext = new Mock<EFDbContext>();
            mockDbContextScopeFac.Setup(x => x.Create(DbContextScopeOption.JoinExisting)).Returns(mockDbContextScope.Object);
            mockDbContextScope.Setup(x => x.DbContexts.Get<EFDbContext>()).Returns(mockEfDbContext.Object);

            var userId = "DummyUserId";

            var record1 = new PersonLogEntry
            {
                Id = "dummyRecordId1",
                LogEntryDateTime = new DateTime(2000, 1, 1),
                IsActive_bl = false,
                AssignedToProject_Id = "DummyProjectId",
                AssignedToProjectEvent_Id = "DummyProjEntryID",
                ModifiedProperties = new[] { "LogEntryDateTime" }
            };
            var record2 = new PersonLogEntry
            {
                Id = "dummyRecordId2",
                LogEntryDateTime = new DateTime(2000, 1, 2),
                IsActive_bl = true,
                AssignedToProject_Id = "DummyProjectId",
                AssignedToProjectEvent_Id = "DummyProjEntryID",
                AssignedToLocation_Id = "DummyLocId",
                ModifiedProperties = new[] { "LogEntryDateTime", "AssignedToProjectEvent_Id", "AssignedToLocation_Id" }
            };
            var records = new PersonLogEntry[] { record1, record2 };

            var projEventDbEntry = new ProjectEvent { Id = "DummyProjEntryID", AssignedToProject_Id = "DummyProjectId" };

            var locDbEntry = new Location { Id = "DummyLocId", AssignedToProject_Id = "DummyProjectId2" };

            var dbEntry1 = new PersonLogEntry { Id = "dummyEntryId1", LogEntryDateTime = new DateTime(2001, 1, 1), IsActive_bl = true };
            var dbEntry2 = new PersonLogEntry { Id = "dummyEntryId2", LogEntryDateTime = new DateTime(2001, 1, 2), IsActive_bl = false };

            mockEfDbContext.Setup(x => x.PersonLogEntrys.FindAsync(record1.Id)).Returns(Task.FromResult(dbEntry1));
            mockEfDbContext.Setup(x => x.PersonLogEntrys.FindAsync(record2.Id)).Returns(Task.FromResult(dbEntry2));
            mockEfDbContext.Setup(x => x.ProjectEvents.FindAsync(projEventDbEntry.Id)).Returns(Task.FromResult<ProjectEvent>(projEventDbEntry));
            mockEfDbContext.Setup(x => x.ProjectEvents.FindAsync(It.IsNotIn<string>(new[] { projEventDbEntry.Id }))).Returns(Task.FromResult<ProjectEvent>(null));
            mockEfDbContext.Setup(x => x.Locations.FindAsync(locDbEntry.Id)).Returns(Task.FromResult<Location>(locDbEntry));
            mockEfDbContext.Setup(x => x.PersonLogEntrys.Add(record1)).Verifiable();
            mockEfDbContext.Setup(x => x.SaveChangesAsync()).Returns(Task.FromResult<int>(1));

            var personLogEntryService = new PersonLogEntryService(mockDbContextScopeFac.Object, userId);

            //Act
            var serviceResult = personLogEntryService.EditAsync(records).Result;

            //Assert
        }