Exemplo n.º 1
0
        public async Task EFRepository_Update_NotExisting_InBDD_AsExpected()
        {
            try
            {
                using (var repo = new TestBlogEFRepository())
                {
                    var b = new WebSite
                    {
                        Url = "http://www.microsoft.com"
                    };
                    b.FakePersistenceId(Guid.NewGuid());
                    repo.MarkForUpdate(b);
                    await repo.SaveAsync().ConfigureAwait(false);
                }

                using (var repo = new TestBlogEFRepository())
                {
                    var testB = await repo.GetAsync().ToListAsync().ConfigureAwait(false);

                    testB.Should().HaveCount(1);
                    testB[0].Url.Should().Be("http://www.microsoft.com");
                }
            }
            finally
            {
                DeleteAll();
            }
        }
Exemplo n.º 2
0
        public async Task EFRepository_SimpleGet_NoFilter_NoOrder_NoIncludes_Should_Returns_All()
        {
            try
            {
                using (var ctx = new TestDbContext())
                {
                    ctx.Add(new WebSite
                    {
                        Url = "https://blogs.msdn.net"
                    });
                    ctx.Add(new WebSite
                    {
                        Url = "https://www.microsoft.com"
                    });
                    await ctx.SaveChangesAsync().ConfigureAwait(false);
                }

                using (var repo = new TestBlogEFRepository())
                {
                    var sites = await repo.GetAsync().ToListAsync().ConfigureAwait(false);

                    sites.Should().HaveCount(2);
                    sites.Any(s => s.Url.Contains("msdn")).Should().BeTrue();
                    sites.Any(s => s.Url.Contains("microsoft")).Should().BeTrue();
                }
            }
            finally
            {
                DeleteAll();
            }
        }
Exemplo n.º 3
0
        public async Task EFRepository_Update_AsExpected()
        {
            try
            {
                using (var ctx = new TestDbContext())
                {
                    var b = new WebSite
                    {
                        Url = "http://www.microsoft.com"
                    };
                    ctx.Add(b);
                    await ctx.SaveChangesAsync().ConfigureAwait(false);
                }
                using (var repo = new TestBlogEFRepository())
                {
                    var w = await repo.GetAsync().FirstOrDefaultAsync().ConfigureAwait(false);

                    w.Url = "https://www.microsoft.com";
                    repo.MarkForUpdate(w);
                    await repo.SaveAsync().ConfigureAwait(false);
                }

                using (var repo = new TestBlogEFRepository())
                {
                    var testB = await repo.GetAsync().ToListAsync().ConfigureAwait(false);

                    testB.Should().HaveCount(1);
                    testB[0].Url.Should().Be("https://www.microsoft.com");
                }
            }
            finally
            {
                DeleteAll();
            }
        }
Exemplo n.º 4
0
        public async Task EFRepository_Insert_Id_AlreadySet_AsExpected()
        {
            try
            {
                using (var repo = new TestBlogEFRepository())
                {
                    var b = new WebSite
                    {
                        Url = "http://www.microsoft.com"
                    };
                    typeof(WebSite).GetProperty("Id").SetValue(b, Guid.NewGuid());
                    repo.MarkForInsert(b);
                    await repo.SaveAsync().ConfigureAwait(false);
                }

                using (var repo = new TestBlogEFRepository())
                {
                    var testB = await repo.GetAsync().ToListAsync().ConfigureAwait(false);

                    testB.Should().HaveCount(1);
                    testB[0].Url.Should().Be("http://www.microsoft.com");
                }
            }
            finally
            {
                DeleteAll();
            }
        }
Exemplo n.º 5
0
        public async Task EFRepository_Insert_AsExpected()
        {
            try
            {
                using (var repo = new TestBlogEFRepository())
                {
                    var b = new WebSite
                    {
                        Url = "http://www.microsoft.com"
                    };
                    repo.MarkForInsert(b);
                    await repo.SaveAsync().ConfigureAwait(false);
                }

                using (var repo = new TestBlogEFRepository())
                {
                    var testB = await repo.GetAsync().ToList().ConfigureAwait(false);

                    testB.Should().HaveCount(1);
                    testB[0].Url.Should().Be("http://www.microsoft.com");
                }
            }
            finally
            {
                DeleteAll();
            }
        }
Exemplo n.º 6
0
        public async Task EFRepository_SimpleGet_NoFilter_NoOrder_WithIncludes_Should_GetLinkedData()
        {
            try
            {
                using (var ctx = new TestDbContext())
                {
                    ctx.Add(new WebSite
                    {
                        Url        = "https://blogs.msdn.net",
                        HyperLinks = new List <Hyperlink>
                        {
                            new Hyperlink
                            {
                                Value = "https://blogs.msdn.net"
                            },
                            new Hyperlink
                            {
                                Value = "https://blogs2.msdn.net"
                            }
                        }
                    });
                    ctx.Add(new WebSite
                    {
                        Url        = "https://www.microsoft.com",
                        HyperLinks = new List <Hyperlink>
                        {
                            new Hyperlink
                            {
                                Value = "https://www.microsoft.com"
                            },
                            new Hyperlink
                            {
                                Value = "https://www.visualstudio.com"
                            }
                        }
                    });
                    await ctx.SaveChangesAsync().ConfigureAwait(false);
                }

                using (var repo = new TestBlogEFRepository())
                {
                    var sites = await repo.GetAsync(includes : w => w.HyperLinks).ToListAsync().ConfigureAwait(false);

                    sites.Should().HaveCount(2);
                    sites.Any(s => s.Url.Contains("msdn")).Should().BeTrue();
                    sites.Any(s => s.Url.Contains("microsoft")).Should().BeTrue();

                    var site = sites.Find(s => s.Url.Contains("msdn"));
                    site.HyperLinks.Should().HaveCount(2);
                    site.HyperLinks.Any(u => u.Value.Contains("blogs.")).Should().BeTrue();
                    site.HyperLinks.Any(u => u.Value.Contains("blogs2.")).Should().BeTrue();
                }
            }
            finally
            {
                DeleteAll();
            }
        }
Exemplo n.º 7
0
        public async Task EFRepository_MarkForDelete_DeleteBlog_Should_Delete_Posts_Logical_Cascade()
        {
            try
            {
                using (var ctx = new TestDbContext())
                {
                    var entity = new WebSite
                    {
                        Url = "http://dotnet.microsoft.com/"
                    };
                    var post = new Post
                    {
                        WebSite         = entity,
                        Published       = true,
                        PublicationDate = DateTime.Today,
                        QuickUrl        = "http://dotnet.microsoft.com/abgfazJSQg2",
                        Version         = 1,
                        Content         = "test data"
                    };
                    entity.Posts = new List <Post> {
                        post
                    };
                    ctx.Add(entity);
                    ctx.SaveChanges();
                }

                using (var ctx = new TestDbContext())
                {
                    ctx.Set <WebSite>().Should().HaveCount(1);
                    ctx.Set <Post>().Should().HaveCount(1);
                }

                using (var rep = new TestBlogEFRepository())
                {
                    var b = await rep.GetAsync().FirstAsync().ConfigureAwait(false);

                    rep.MarkForDelete(b);
                    await rep.SaveAsync().ConfigureAwait(false);
                }

                using (var ctx = new TestDbContext())
                {
                    ctx.Set <WebSite>().Should().HaveCount(1);
                    ctx.Set <Post>().Should().HaveCount(1);

                    ctx.Set <WebSite>().ToList().Where(e => !e.Deleted).Should().HaveCount(0);
                    ctx.Set <Post>().ToList().Where(e => !e.Deleted).Should().HaveCount(1); // Le soft delete est à gérer niveau repository
                }
            }
            finally
            {
                DeleteAll();
            }
        }
Exemplo n.º 8
0
        public async Task EFRepository_MarkForDelete_Logical_Deletion_Globally_Deactivated()
        {
            try
            {
                EFCoreInternalExecutionContext.DisableLogicalDeletion = true;
                using (var ctx = new TestDbContext())
                {
                    var entity = new WebSite
                    {
                        Url = "http://dotnet.microsoft.com/"
                    };
                    var post = new Post
                    {
                        WebSite         = entity,
                        Published       = true,
                        PublicationDate = DateTime.Today,
                        QuickUrl        = "http://dotnet.microsoft.com/abgfazJSQg2",
                        Version         = 1,
                        Content         = "test data"
                    };
                    entity.Posts = new List <Post> {
                        post
                    };
                    ctx.Add(entity);
                    ctx.SaveChanges();
                }

                using (var ctx = new TestDbContext())
                {
                    ctx.Set <WebSite>().Should().HaveCount(1);
                    ctx.Set <Post>().Should().HaveCount(1);
                }

                using (var rep = new TestBlogEFRepository())
                {
                    var b = await rep.GetAsync().FirstAsync().ConfigureAwait(false);

                    rep.MarkForDelete(b);
                    await rep.SaveAsync().ConfigureAwait(false);
                }

                using (var ctx = new TestDbContext())
                {
                    ctx.Set <WebSite>().Should().HaveCount(0);
                    ctx.Set <Post>().Should().HaveCount(0);
                }
            }
            finally
            {
                EFCoreInternalExecutionContext.DisableLogicalDeletion = false;
                DeleteAll();
            }
        }