public void AssemblyLogEntryService_GetAsync_DoeNotReturnFromWrongProject()
        {
            //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 AssemblyLogEntry
            {
                Id = "dummyEntryId1",
                AssemblyDb_Id = "dummyAssyId1",
                AssignedToProject_Id = "dummyProjId1",
                IsActive_bl = false,
                Comments = "DummyComments1",
                AssignedToProject = project1
            };
            var dbEntry2 = new AssemblyLogEntry
            {
                Id = "dummyEntryId2",
                AssemblyDb_Id = "dummyAssyId2",
                AssignedToProject_Id = "dummyProjId1",
                IsActive_bl = true,
                Comments = "DummyComments2",
                AssignedToProject = project2
            };
            var dbEntries = (new List<AssemblyLogEntry> { dbEntry1, dbEntry2 }).AsQueryable();

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

            mockEfDbContext.Setup(x => x.AssemblyLogEntrys).Returns(mockDbSet.Object);

            var assyLogEntryService = new AssemblyLogEntryService(mockDbContextScopeFac.Object, projectPerson1.Id);

            //Act
            var resultAssemblyLogEntrys = assyLogEntryService.GetAsync(new[] { "dummyEntryId1", "dummyEntryId2" }).Result;

            //Assert
            Assert.IsTrue(resultAssemblyLogEntrys.Count == 0);
        }
Esempio n. 2
0
        public void AssemblyDbService_EditStatusAsync_EditsStatusIfDifferent()
        {
            // Arrange
            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();
            var mockDbContextScope = new Mock<IDbContextScope>();
            var mockDbContextScopeReadOnly = new Mock<IDbContextReadOnlyScope>();
            var mockEfDbContext = new Mock<EFDbContext>();
            mockDbContextScopeFac.Setup(x => x.Create(DbContextScopeOption.JoinExisting)).Returns(mockDbContextScope.Object);
            mockDbContextScopeFac.Setup(x => x.CreateReadOnly(DbContextScopeOption.JoinExisting)).Returns(mockDbContextScopeReadOnly.Object);
            mockDbContextScope.Setup(x => x.DbContexts.Get<EFDbContext>()).Returns(mockEfDbContext.Object);
            mockDbContextScopeReadOnly.Setup(x => x.DbContexts.Get<EFDbContext>()).Returns(mockEfDbContext.Object);

            var ids = new[] { "dummyEntryId1", "dummyEntryId2"};
            var statusId = "newStatusId";

            var userId = "dummyUserId1";
            var projectPerson1 = new Person { Id = userId, 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 AssemblyDb { Id = "dummyEntryId1", AssyName = "Assy1", AssyAltName = "AssyAlt1", IsActive_bl = true, 
                TechnicalDetails = "DummyInfo1" , AssignedToProject = project1 };
            var dbEntry2 = new AssemblyDb { Id = "dummyEntryId2", AssyName = "Assy2", AssyAltName = "AssyAlt2", IsActive_bl = true,
                TechnicalDetails = "DummyInfo2" , AssignedToProject = project1, AssemblyStatus_Id = statusId };
            var dbEntry3 = new AssemblyDb { Id = "dummyEntryId3", AssyName = "Assy3", AssyAltName = "AssyAlt3", IsActive_bl = true, 
                TechnicalDetails = "DummyInfo3" , AssignedToProject = project1 };
            var dbEntries = (new List<AssemblyDb> { dbEntry1, dbEntry2, dbEntry3 }).AsQueryable();
            
            var mockDbSet = new Mock<DbSet<AssemblyDb>>();
            mockDbSet.As<IDbAsyncEnumerable<AssemblyDb>>().Setup(m => m.GetAsyncEnumerator()).Returns(new MockDbAsyncEnumerator<AssemblyDb>(() => dbEntries.GetEnumerator()));
            mockDbSet.As<IQueryable<AssemblyDb>>().Setup(m => m.Provider).Returns(new MockDbAsyncQueryProvider<AssemblyDb>(dbEntries.Provider));
            mockDbSet.As<IQueryable<AssemblyDb>>().Setup(m => m.Expression).Returns(dbEntries.Expression);
            mockDbSet.As<IQueryable<AssemblyDb>>().Setup(m => m.ElementType).Returns(dbEntries.ElementType);
            mockDbSet.As<IQueryable<AssemblyDb>>().Setup(m => m.GetEnumerator()).Returns(() => dbEntries.GetEnumerator());
            mockDbSet.Setup(x => x.Include(It.IsAny<string>())).Returns(mockDbSet.Object);
            mockDbSet.Setup(x => x.FindAsync(dbEntry1.Id)).Returns(Task.FromResult(dbEntry1));
            mockDbSet.Setup(x => x.FindAsync(dbEntry2.Id)).Returns(Task.FromResult(dbEntry2));

            mockEfDbContext.Setup(x => x.AssemblyDbs).Returns(mockDbSet.Object);

            var logEntry = new AssemblyLogEntry();

            mockEfDbContext.Setup(x => x.Set<AssemblyDb>().FindAsync(dbEntry1.Id)).Returns(Task.FromResult(dbEntry1));
            mockEfDbContext.Setup(x => x.Set<AssemblyDb>().FindAsync(dbEntry2.Id)).Returns(Task.FromResult(dbEntry1));

            mockEfDbContext.Setup(x => x.AssemblyLogEntrys.Add(It.IsAny<AssemblyLogEntry>())).Callback<AssemblyLogEntry>( x => logEntry = x);
            mockEfDbContext.Setup(x => x.SaveChangesAsync()).Returns(Task.FromResult<int>(1));

            var assemblyService = new AssemblyDbService(mockDbContextScopeFac.Object, userId);

            //Act
            assemblyService.EditStatusAsync(ids,statusId).Wait();

            //Assert
            Assert.IsTrue(dbEntry1.AssemblyStatus_Id == statusId);
            Assert.IsTrue(dbEntry2.AssemblyStatus_Id == statusId);
            Assert.IsTrue(logEntry.AssignedToLocation_Id == dbEntry2.AssignedToLocation_Id);
            mockDbContextScopeFac.Verify(x => x.Create(DbContextScopeOption.JoinExisting), Times.Once);
            mockDbContextScope.Verify(x => x.DbContexts.Get<EFDbContext>(), Times.Once);
            mockEfDbContext.Verify(x => x.AssemblyLogEntrys.Add(It.IsAny<AssemblyLogEntry>()), Times.Once);
            mockEfDbContext.Verify(x => x.SaveChangesAsync(), Times.Once);
        }
 public async Task<ActionResult> Edit(AssemblyLogEntry[] records)
 {
     ViewBag.ServiceName = "AssemblyLogEntryService.EditAsync";
     var newEntryIds = await assyLogEntryService.EditAsync(records).ConfigureAwait(false);
     return DbJson(new { Success = "True", newEntryIds = newEntryIds });
 }
Esempio n. 4
0
        public void AssemblyDbService_EditAsync_AddsLogEntryWhenCreate()
        {
            // 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 assembly1 = new AssemblyDb { Id = "dummyAssemblyDbId1", AssyName = "Assy1", AssyAltName = "AssyAlt1", IsActive_bl = false, TechnicalDetails = "DummyInfo1",
                                        AssignedToLocation_Id = "dummyLocId1", ModifiedProperties = new string[] { "AssyName", "AssyAltName", "AssignedToLocation_Id" }};
            var assembly2 = new AssemblyDb { Id = "dummyAssemblyDbId2", AssyName = "Assy2", AssyAltName = "AssyAlt2", IsActive_bl = true, TechnicalDetails = "DummyInfo2",
                                        AssignedToLocation_Id = "dummyLocId2"};
            var assemblys = new AssemblyDb[] { assembly1, assembly2 };

            var dbEntry1 = new AssemblyDb { Id = "dummyEntryId1", AssyName = "DbEntry1", AssyAltName = "DbEntryAlt1", IsActive_bl = true, TechnicalDetails = "DbEntryInfo1",
                                        AssignedToLocation_Id = "dummyLocId3" };
            var dbEntry2 = new AssemblyDb { Id = "dummyEntryId2", AssyName = "DbEntry2", AssyAltName = "DbEntryAlt2", IsActive_bl = false, TechnicalDetails = "DbEntryInfo2",
                                        AssignedToLocation_Id = "dummyLocId4" };

            mockEfDbContext.Setup(x => x.Set<AssemblyDb>().FindAsync(assembly1.Id)).Returns(Task.FromResult(dbEntry1));
            mockEfDbContext.Setup(x => x.Set<AssemblyDb>().FindAsync(assembly2.Id)).Returns(Task.FromResult<AssemblyDb>(null));
            mockEfDbContext.Setup(x => x.Set<AssemblyDb>().Add(It.IsAny<AssemblyDb>())).Verifiable();
            var logEntry = new AssemblyLogEntry();
            mockEfDbContext.Setup(x => x.AssemblyLogEntrys.Add(It.IsAny<AssemblyLogEntry>())).Callback<AssemblyLogEntry>( x => logEntry = x);
            mockEfDbContext.Setup(x => x.SaveChangesAsync()).Returns(Task.FromResult<int>(1));

            var assemblyService = new AssemblyDbService(mockDbContextScopeFac.Object, "DummyUserId");

            //Act
            var newEntriesList = assemblyService.EditAsync(assemblys).Result;

            //Assert
            Assert.IsTrue(assembly1.AssyName == dbEntry1.AssyName);
            Assert.IsTrue(assembly1.AssyAltName == dbEntry1.AssyAltName);
            Assert.IsTrue(assembly1.TechnicalDetails != dbEntry1.TechnicalDetails);
            Assert.IsTrue(assembly1.IsActive_bl != dbEntry1.IsActive_bl);
            Assert.IsTrue(assembly1.AssignedToLocation_Id == dbEntry1.AssignedToLocation_Id);
            Assert.IsTrue(assembly1.AssyName == dbEntry1.AssyName);
            Assert.IsTrue(assembly1.AssyAltName == dbEntry1.AssyAltName);
            Assert.IsTrue(assembly1.TechnicalDetails != dbEntry1.TechnicalDetails);
            Assert.IsTrue(assembly1.IsActive_bl != dbEntry1.IsActive_bl);
            Assert.IsTrue(assembly2.AssignedToLocation_Id != dbEntry2.AssignedToLocation_Id);
            Assert.IsTrue(logEntry.AssignedToLocation_Id == assembly2.AssignedToLocation_Id);
            mockDbContextScopeFac.Verify(x => x.Create(DbContextScopeOption.JoinExisting), Times.Once);
            mockDbContextScope.Verify(x => x.DbContexts.Get<EFDbContext>(), Times.Once);
            mockEfDbContext.Verify(x => x.Set<AssemblyDb>().FindAsync(It.IsAny<string>()), Times.Exactly(2));
            mockEfDbContext.Verify(x => x.Set<AssemblyDb>().Add(assembly2), Times.Once);
            mockEfDbContext.Verify(x => x.AssemblyLogEntrys.Add(It.IsAny<AssemblyLogEntry>()), Times.Exactly(2));
            mockEfDbContext.Verify(x => x.SaveChangesAsync(), Times.Once);
        }