Example #1
0
        public void UpdateAssociatedEntityWhereNewValueIsNull()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var project = uow.Projects
                    .Include(p => p.LeadCoordinator)
                    .Single(p => p.Id == 2);

                Assert.IsNotNull(project.LeadCoordinator);

                project.LeadCoordinator = null;

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                Assert.IsTrue(uow.Projects
                    .Include(p => p.LeadCoordinator)
                    .Single(p => p.Id == 2)
                    .LeadCoordinator == null);
            }
        }
Example #2
0
        public void OwnedCollectionWithOwnedCollection()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var company1 = uow.Companies
                    .Include(p => p.Contacts.Select(m => m.Infos))
                    .First();

                company1.Contacts.First().Infos.First().Email = "testeremail";
                company1.Contacts.First().Infos.Add(new ContactInfo {Description = "Test", Email = "*****@*****.**"});

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                var value = uow.Companies.Include(p => p.Contacts.Select(m => m.Infos))
                    .First();

                Assert.AreEqual(2, value.Contacts.First().Infos.Count);
                Assert.AreEqual("testeremail", value.Contacts.First().Infos.First().Email);
            }
        }
Example #3
0
        public void UpdateAssociatedEntityToNull()
        {
            Manager coordinator;
            using (var uow = new TestDbContext(_connection))
            {
                var project = uow.Projects
                    .Include(p => p.LeadCoordinator)
                    .Single(p => p.Id == 2);

                coordinator = project.LeadCoordinator;
                project.LeadCoordinator = null;

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {

                Assert.IsTrue(uow.Projects
                    .Include(p => p.LeadCoordinator)
                    .Single(p => p.Id == 2)
                    .LeadCoordinator == null);

                Assert.IsTrue(uow.Managers.Any(x => x.PartKey == coordinator.PartKey));
            }
        }
Example #4
0
        public void OwnedCollectionRemove()
        {
            using (var uow = new TestDbContext(_connection, new GraphdiffAggregateUpdateStrategy()))
            {
                var company1 = uow.Companies
                    .Include(p => p.Contacts.Select(m => m.Infos))
                    .Single(p => p.Id == 2);

                var contact = company1.Contacts.Single();
                company1.Contacts.Remove(contact);

                var repo = new CompanyRepository(uow);
                repo.AlterarAgregacao(company1, cfg => cfg.HasMany(c => c.Contacts));

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                var company = uow.Companies.Include(p => p.Contacts.Select(m => m.Infos)).Single(p => p.Id == 2);

                Assert.IsTrue(company.Contacts.Count == 0);

            }
        }
Example #5
0
        public void OwnedCollectionUpdate()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var company1 = uow.Companies
                    .Include(p => p.Contacts)
                    .Single(p => p.Id == 2);

                company1.Name = "Company #1"; // Change from Company 1 to Company #1
                company1.Contacts.First().FirstName = "Bobby"; // change to bobby

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                var company = uow.Companies.Include(p => p.Contacts).Single(p => p.Id == 2);
                var contact = company.Contacts.First();

                Assert.AreEqual("Company #1", company.Name);
                Assert.AreEqual("Bobby", contact.FirstName);
                Assert.AreEqual("Jones", contact.LastName);
            }
        }
Example #6
0
        public void UpdateOwnedEntityRemoveChildEntity()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var company1 = uow.Companies
                    .Include(c => c.Address)
                    .Single(c => c.Id == 1);

                company1.Address = null;

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {

                Assert.IsTrue(uow.Companies
                    .Include(p => p.Address)
                    .Single(p => p.Id == 1)
                    .Address == null);

                Assert.IsFalse(uow.CompanyAddresses.Any());
            }
        }
Example #7
0
        public void AssociatedCollectionRemove()
        {
            Company company;
            using (var uow = new TestDbContext(_connection))
            {
                var project1 = uow.Projects
                    .Include(p => p.Stakeholders)
                    .Single(p => p.Id == 2);

                company = project1.Stakeholders.First();
                project1.Stakeholders.Remove(company);

                uow.Commit();
            }
            using (var uow = new TestDbContext(_connection))
            {
                Assert.IsTrue(uow.Projects
                    .Include(p => p.Stakeholders)
                    .Single(p => p.Id == 2)
                    .Stakeholders.Count == 0);

                Assert.IsTrue(uow.Companies.Any(p => p.Id == company.Id));
            }
        }
Example #8
0
        public void MarksAssociatedRelationAsChangedEvenIfEntitiesAreUnchanged()
        {
            Manager manager1;

            using (var uow = new TestDbContext(_connection))
            {
                var project1 = uow.Projects.Include(m => m.LeadCoordinator).Single(p => p.Id == 1);
                manager1 = uow.Managers.First();

                project1.LeadCoordinator = manager1;

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection)) {
                Assert.AreEqual(manager1.PartKey, uow.Projects.Include(m => m.LeadCoordinator).Single(p => p.Id == 1).LeadCoordinator.PartKey);
            }
        }
Example #9
0
        public void OwnedCollectionAddMultiple()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var company1 = uow.Companies
                    .Include(p => p.Contacts.Select(m => m.Infos))
                    .Single(p => p.Id == 2);

                company1.Contacts.Add(new CompanyContact
                {
                    FirstName = "Charlie",
                    LastName = "Sheen",
                    Infos = new List<ContactInfo>
                    {
                        new ContactInfo {PhoneNumber = "123456789", Description = "Home"}
                    }
                });
                company1.Contacts.Add(new CompanyContact
                {
                    FirstName = "Tim",
                    LastName = "Sheen"
                });
                company1.Contacts.Add(new CompanyContact
                {
                    FirstName = "Emily",
                    LastName = "Sheen"
                });
                company1.Contacts.Add(new CompanyContact
                {
                    FirstName = "Mr",
                    LastName = "Sheen",
                    Infos = new List<ContactInfo>
                    {
                        new ContactInfo {PhoneNumber = "123456789", Description = "Home"}
                    }
                });
                company1.Contacts.Add(new CompanyContact
                {
                    FirstName = "Mr",
                    LastName = "X"
                });

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                var company = uow.Companies.Include(p => p.Contacts.Select(m => m.Infos)).Single(p => p.Id == 2);

                Assert.IsTrue(company.Contacts.Count == 6);
                Assert.IsTrue(company.Contacts.Any(c => c.FirstName == "Charlie"));
                Assert.IsTrue(company.Contacts.Single(c => c.FirstName == "Charlie").Infos.Any(i => i.Description == "Home"));
                Assert.IsTrue(company.Contacts.Any(c => c.FirstName == "Emily"));
                Assert.IsFalse(company.Contacts.Single(c => c.FirstName == "Emily").Infos.Any());
            }
        }
Example #10
0
        public void EnsureWeCanUseCyclicRelationsOnOwnedCollections()
        {
            Manager manager;
            using (var uow = new TestDbContext(_connection))
            {
                manager = uow.Managers.Include(p => p.Employees).First();

                var newEmployee = new Employee {Key = "assdf", FirstName = "Test Employee", Manager = manager};
                manager.Employees.Add(newEmployee);

                uow.Commit();
            }
            using (var uow = new TestDbContext(_connection))
            {
                Assert.AreEqual(manager.FirstName, uow.Employees.Include(p => p.Manager).Single(p => p.Key == "assdf").Manager.FirstName);
            }
            using (var uow = new TestDbContext(_connection))
            {
                Assert.AreEqual("Test Employee", uow.Managers.Include(p => p.Employees).Single(p => p.PartKey == manager.PartKey).Employees.Single(p => p.Key == "assdf").FirstName);
            }
        }
Example #11
0
        public void EntityUpdate()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var company1 = uow.Companies.Single(p => p.Id == 2);
                company1.Name = "Company #1"; // Change from Company 1 to Company #1

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                Assert.IsTrue(uow.Companies.Single(p => p.Id == 2).Name == "Company #1");
            }
        }
Example #12
0
        public void DoesNotUpdateEntityIfNoChangesHaveBeenMade()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var company1 = uow.Companies.Single(p => p.Id == 2);

                Assert.IsTrue(uow.ChangeTracker.Entries().All(p => p.State == System.Data.Entity.EntityState.Unchanged));
            }
        }
Example #13
0
 public void DiscoverEntitySetInheritanceProxy()
 {
     using (var uow = new TestDbContext(_connection))
     {
         var contact = uow.Companies.Single(p => p.Id == 2).Contacts.FirstOrDefault();
         var set = ((IObjectContextAdapter)uow).ObjectContext.TryGetEntitySet(contact);
         Assert.IsNotNull(set);
     }
 }
Example #14
0
 public void DiscoverEntitySetInheritance()
 {
     using (var uow = new TestDbContext(_connection))
     {
         var c = new CompanyContact();
         var set = ((IObjectContextAdapter)uow).ObjectContext.TryGetEntitySet(c);
         Assert.IsNotNull(set);
     }
 }
Example #15
0
        public void UpdateAssociatedEntityWherePreviousValueWasNull()
        {
            Manager coord;
            using (var uow = new TestDbContext(_connection))
            {
                var project = uow.Projects
                    .Include(p => p.LeadCoordinator)
                    .Single(p => p.Id == 1);

                coord = uow.Managers.Single(p => p.PartKey == "manager1" && p.PartKey2 == 1);

                project.LeadCoordinator = coord;

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                Assert.IsTrue(uow.Projects
                    .Include(p => p.LeadCoordinator)
                    .Single(p => p.Id == 1)
                    .LeadCoordinator.PartKey == coord.PartKey);
            }
        }
Example #16
0
        public void OwnedCollectionAddRemoveUpdate()
        {
            Company company2;

            using (var uow = new TestDbContext(_connection))
            {
                company2 = uow.Companies
                    .Include(p => p.Contacts.Select(m => m.Infos))
                    .Single(p => p.Id == 2);

                company2.Contacts.Add(new CompanyContact
                {
                    FirstName = "Hello",
                    LastName = "Test",
                    Infos = new List<ContactInfo>
                    {
                        new ContactInfo {PhoneNumber = "123456789", Description = "Hello Home"}
                    }
                });

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection, new GraphdiffAggregateUpdateStrategy()))
            {
                company2 = uow.Companies
                    .Include(p => p.Contacts.Select(m => m.Infos))
                    .Single(p => p.Id == 2);

                // Update
                company2.Name = "Company #1"; // Change from Company 1 to Company #1
                company2.Contacts.First().FirstName = "Terrrrrry";

                // Remove
                var contatoARemover = company2.Contacts.Skip(1).First();
                Assert.IsTrue(contatoARemover.Infos.Any());
                company2.Contacts.Remove(contatoARemover);

                // add
                company2.Contacts.Add(new CompanyContact
                {
                    FirstName = "Charlie",
                    LastName = "Sheen",
                    Infos = new List<ContactInfo>
                    {
                        new ContactInfo {PhoneNumber = "123456789", Description = "Home"}
                    }
                });

                new CompanyRepository(uow).AlterarAgregacao(company2, cfg => cfg
                    .HasMany(p => p.Contacts, with => with
                        .HasMany(p => p.Infos)));

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {

                var test = uow.Companies
                    .Include(p => p.Contacts.Select(m => m.Infos))
                    .Single(p => p.Id == 2);

                Assert.AreEqual("Company #1", test.Name);
                Assert.AreEqual(2, test.Contacts.Count);
                Assert.AreEqual("Terrrrrry", test.Contacts.First().FirstName);
                Assert.AreEqual("Charlie", test.Contacts.Skip(1).First().FirstName);
                Assert.AreEqual("Home", test.Contacts.Skip(1).First().Infos.Single().Description);
            }
        }
Example #17
0
        public void UpdateOwnedEntityNewEntity()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var company1 = uow.Companies
                    .Include(c => c.Address)
                    .Single(c => c.Id == 1);

                company1.IdAddress = 2;
                company1.Address = new CompanyAddress() {Id = 2, Street = "Route 66"};

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                Assert.AreEqual("Route 66", uow.Companies.Include(p => p.Address).Single(p => p.Id == 1).Address.Street);

                // PS: Faz esse teste, porque não faz sentido alterar a PK de um 1:1.
                Assert.AreEqual(1, uow.Companies.Include(p => p.Address).Single(p => p.Id == 1).Address.Id);
                Assert.AreNotEqual(2, uow.Companies.Include(p => p.Address).Single(p => p.Id == 1).Address.Id);

                Assert.AreEqual(1, uow.CompanyAddresses.Count());
            }
        }
Example #18
0
        public static void SetupTheDatabase(TestContext testContext)
        {
            _connection = Effort.DbConnectionFactory.CreateTransient();

            using (var uow = new TestDbContext(_connection))
            {
                var company1 = uow.Companies.Add(new Company
                {
                    Name = "Company 1",
                    Address = new CompanyAddress() { Id = 1, Street = "Campinho" },
                    Contacts = new List<CompanyContactBase>
                    {
                        new CompanyContact
                        {
                            FirstName = "Bob",
                            LastName = "Brown",
                            Infos = new List<ContactInfo>
                            {
                                new ContactInfo
                                {
                                    Description = "Home",
                                    Email = "*****@*****.**",
                                    PhoneNumber = "0255525255"
                                }
                            }
                        }
                    },
                });

                var company2 = uow.Companies.Add(new Company
                {
                    Name = "Company 2",
                    Contacts = new List<CompanyContactBase>
                    {
                        new CompanyContact
                        {
                            FirstName = "Tim",
                            LastName = "Jones",
                            Infos = new List<ContactInfo>
                            {
                                new ContactInfo
                                {
                                    Description = "Work",
                                    Email = "*****@*****.**",
                                    PhoneNumber = "456456456456"
                                }
                            }
                        }
                    }
                });

                var project1 = uow.Projects.Add(new Project
                {
                    Name = "Major Project 1",
                    Deadline = DateTime.Now,
                    Stakeholders = new List<Company> { company2 }
                });

                var project2 = uow.Projects.Add(new Project
                {
                    Name = "Major Project 2",
                    Deadline = DateTime.Now,
                    Stakeholders = new List<Company> { company1 }
                });

                var manager1 = uow.Managers.Add(new Manager
                {
                    PartKey = "manager1",
                    PartKey2 = 1,
                    FirstName = "Trent"
                });
                var manager2 = uow.Managers.Add(new Manager
                {
                    PartKey = "manager2",
                    PartKey2 = 2,
                    FirstName = "Timothy"
                });

                var employee = new Employee
                {
                    Manager = manager1,
                    Key = "Asdf",
                    FirstName = "Test employee",
                };

                uow.Employees.Add(employee);

                project2.LeadCoordinator = manager2;

                uow.Commit();
            }
        }
Example #19
0
        public void UpdateOwnedEntityUpdateValues()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var company1 = uow.Companies
                    .Include(c => c.Address)
                    .Single(c => c.Id == 1);

                company1.Address.Street = "Route 66";

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                Assert.AreEqual("Route 66", uow.Companies.Include(p => p.Address).Single(p => p.Id == 1).Address.Street);
            }
        }
Example #20
0
        public void AssociatedCollectionAdd()
        {
            using (var uow = new TestDbContext(_connection))
            {
                var project1 = uow.Projects
                    .Include(p => p.Stakeholders)
                    .Single(p => p.Id == 2);

                var company2 = uow.Companies.Single(p => p.Id == 2);

                project1.Stakeholders.Add(company2);

                uow.Commit();
            }

            using (var uow = new TestDbContext(_connection))
            {
                Assert.IsTrue(uow.Projects
                    .Include(p => p.Stakeholders)
                    .Single(p => p.Id == 2)
                    .Stakeholders.Count == 2);
            }
        }