public async Task when_associated_reference_set_to_entity__entity_is_related_to_existing()
        {
            using (TestDbContext context = new TestDbContext())
            {
                IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context);

                // GIVEN an enity root with references:
                AssociatedReference[] references = new[]
                {
                        new AssociatedReference { Id = 1, Name = "Associated Reference 1" },
                        new AssociatedReference { Id = 2, Name = "Associated Reference 2" }
                    };
                context.AddRange(references);
                context.Add(new Entity
                {
                    AssociatedReference = references[0],
                });
                await context.SaveChangesAsync();

                // WHEN the owned and the associated references are set to null:
                Entity detachedEntity = new Entity
                {
                    Id = 1,
                    AssociatedReference = new AssociatedReference { Id = 1, Name = "Modified Associated Reference 1" },
                };

                await detachedContext.Set<Entity>().UpdateAsync(detachedEntity);
                await detachedContext.SaveChangesAsync();

                // THEN the associated reference still exsits:
                Assert.True(context.AssociatedReferences.Any(a => a.Name == "Associated Reference 1"));
            }
        }
 public async Task when_load_sorted__result_is_ordered()
 {
     using (TestDbContext dbContext = new TestDbContext())
     {
         IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(dbContext);
         dbContext.AddRange(new[]
         {
             new Entity { Name = "Order By Entity 2" },
             new Entity { Name = "Order By Entity 1" },
             new Entity { Name = "Order By Entity 3" }
         });
         await dbContext.SaveChangesAsync();
     }
 }
        public async Task when_entity_deleted__owned_properties_are_deleted()
        {
            using (TestDbContext context = new TestDbContext())
            {
                IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context);

                // GIVEN an enity root with an owned list:
                var associatedItems = new List<AssociatedListItem>(new[]
                {
                    new AssociatedListItem { Name = "Associated Item 1" },
                    new AssociatedListItem { Name = "Associated Item 2" }
                });
                context.AddRange(associatedItems);
                await context.SaveChangesAsync();

                context.Add(new Entity
                {
                    OwnedList = new List<OwnedListItem>(new[]  {
                        new OwnedListItem { Name = "Owned Item A" },
                        new OwnedListItem { Name = "Owned Item B" },
                        new OwnedListItem { Name = "Owned Item C" }
                    }),
                    AssociatedList = associatedItems
                });
                context.SaveChanges();

                // WHEN the entity is deleted:
                await detachedContext.Set<Entity>().DeleteAsync(1);
                await detachedContext.SaveChangesAsync();

                // THEN owned items are removed:
                Assert.False(context.OwnedListItems.Any(e => e.Name == "Owned Item A"));
                Assert.False(context.OwnedListItems.Any(e => e.Name == "Owned Item B"));
                Assert.False(context.OwnedListItems.Any(e => e.Name == "Owned Item C"));

                // and the associated items are not removed:
                Assert.True(context.AssociatedListItems.Any(e => e.Name == "Associated Item 1"));
                Assert.True(context.AssociatedListItems.Any(e => e.Name == "Associated Item 2"));
            }
        }
Esempio n. 4
0
        public async Task map_owned_list()
        {
            Mapper        mapper = new Mapper();
            TestDbContext db     = await TestDbContext.CreateInMemorySqliteAsync(mapper);

            db.Roles.Add(new Role {
                Name = "admin"
            });
            db.Roles.Add(new Role {
                Name = "user"
            });
            db.UserTypes.Add(new UserType {
                Name = "system"
            });
            await db.SaveChangesAsync();

            User user = new User
            {
                Name  = "test user",
                Roles = new List <Role>
                {
                    db.Find <Role>(1),
                    db.Find <Role>(2)
                },
                Addresses = new List <Address>
                {
                    new Address {
                        Street = "original street", Number = "123"
                    }
                },
                Profile = new UserProfile
                {
                    FirstName = "test",
                    LastName  = "user"
                },
                UserType = db.Find <UserType>(1)
            };

            db.Users.Add(user);
            await db.SaveChangesAsync();

            User savedUser = await db.Users.Where(u => u.Id == 1)
                             .Include(u => u.Roles)
                             .Include(u => u.Addresses)
                             .Include(u => u.Profile)
                             .Include(u => u.UserType)
                             .FirstOrDefaultAsync();

            await db.MapAsync <User>(new EditUserInput
            {
                Id        = 1,
                Addresses = new List <Address>
                {
                    new Address {
                        Street = "new street", Number = "234"
                    }
                }
            });

            Assert.Equal(1, user.Id);
            Assert.Equal("test user", user.Name);
            Assert.NotNull(user.Profile);
            Assert.Equal("test", user.Profile.FirstName);
            Assert.Equal("user", user.Profile.LastName);
            Assert.NotNull(user.Addresses);
            Assert.Equal(1, user.Addresses.Count);
            Assert.Equal("new street", user.Addresses[0].Street);
            Assert.Equal("234", user.Addresses[0].Number);
            Assert.NotNull(user.Roles);
            Assert.Equal(2, user.Roles.Count);
            Assert.Contains(user.Roles, r => r.Id == 1);
            Assert.Contains(user.Roles, r => r.Id == 2);
            Assert.NotNull(user.UserType);
            Assert.Equal(1, user.UserType.Id);
        }
Esempio n. 5
0
        public async Task map_user()
        {
            Mapper        mapper = new Mapper();
            TestDbContext db     = await TestDbContext.CreateInMemorySqliteAsync(mapper);

            db.Roles.Add(new Role {
                Id = 1, Name = "admin"
            });
            db.Roles.Add(new Role {
                Id = 2, Name = "user"
            });
            db.UserTypes.Add(new UserType {
                Id = 1, Name = "system"
            });
            await db.SaveChangesAsync();

            await db.MapAsync <User>(new UserDTO
            {
                Id      = 1,
                Name    = "cr",
                Profile = new UserProfileDTO
                {
                    FirstName = "chris",
                    LastName  = "redfield"
                },
                Addresses = new List <AddressDTO>
                {
                    new AddressDTO {
                        Street = "rc", Number = "123"
                    }
                },
                Roles = new List <RoleDTO>
                {
                    new RoleDTO {
                        Id = 1
                    },
                    new RoleDTO {
                        Id = 2
                    }
                },
                UserType = new UserTypeDTO {
                    Id = 1
                }
            });

            await db.SaveChangesAsync();

            User user = await db.Users.Where(u => u.Id == 1)
                        .Include(u => u.Roles)
                        .Include(u => u.Addresses)
                        .Include(u => u.Profile)
                        .Include(u => u.UserType)
                        .FirstOrDefaultAsync();

            Assert.Equal(1, user.Id);
            Assert.Equal("cr", user.Name);
            Assert.NotNull(user.Profile);
            Assert.Equal("chris", user.Profile.FirstName);
            Assert.Equal("redfield", user.Profile.LastName);
            Assert.NotNull(user.Addresses);
            Assert.Equal("rc", user.Addresses[0].Street);
            Assert.Equal("123", user.Addresses[0].Number);
            Assert.NotNull(user.Roles);
            Assert.Equal(2, user.Roles.Count);
            Assert.Contains(user.Roles, r => r.Id == 1);
            Assert.Contains(user.Roles, r => r.Id == 2);
            Assert.NotNull(user.UserType);
            Assert.Equal(1, user.UserType.Id);
        }