public async Task TestUpdateOnEntityAlreadyTrackedOk()
        {
            //SETUP
            var unique  = Guid.NewGuid().ToString();
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();
            }
            using (var context = new EfCoreContext(options))
            {
                var utData  = context.SetupEntitiesDirect();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var author = await service.ReadSingleAsync <Author>(1);

                author.Email = unique;
                await service.UpdateAndSaveAsync(author);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
            }
            using (var context = new EfCoreContext(options))
            {
                context.Authors.Find(1).Email.ShouldEqual(unique);
            }
        }
        public async Task TestDbQueryReadSingleWhereDirectOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
                context.ExecuteScriptFileInTransaction(TestData.GetFilePath("ReplaceTableWithView.sql"));

                context.Add(new Parent
                {
                    Children = new List <Child> {
                        new Child {
                            MyString = "Hello"
                        }, new Child {
                            MyString = "Goodbye"
                        }
                    }
                });
                context.SaveChanges();

                context.ChangeTracker.Clear();

                var utData  = context.SetupEntitiesDirect();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var entity = await service.ReadSingleAsync <ChildReadOnly>(x => x.ChildId == 1);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                entity.ShouldNotBeNull();
            }
        }
        public async Task TestDbQueryReadSingleWhereViaDtoOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                context.Database.EnsureCreated();
                context.Add(new Parent
                {
                    Children = new List <Child> {
                        new Child {
                            MyString = "Hello"
                        }, new Child {
                            MyString = "Goodbye"
                        }
                    }
                });
                context.SaveChanges();
            }

            using (var context = new TestDbContext(options))
            {
                var utData  = context.SetupSingleDtoAndEntities <ChildDbQueryDto>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var entity = await service.ReadSingleAsync <ChildDbQueryDto>(x => x.ChildId == 1);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                entity.ShouldNotBeNull();
            }
        }
        public async Task TestDbQueryReadSingleFindFail()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                var utData  = context.SetupEntitiesDirect();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => service.ReadSingleAsync <ChildReadOnly>(1));

                //VERIFY
                ex.Message.ShouldEqual("The class ChildReadOnly of style DbQuery cannot be used in a Find.");
            }
        }
        public async Task TestNotEntityNoDtoOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupEntitiesDirect();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => service.ReadSingleAsync <string>(1));

                //VERIFY
                ex.Message.ShouldEqual("The class String is not registered as entity class in your DbContext EfCoreContext.");
            }
        }
        public async Task TestGetSingleOnEntityWhereException()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupEntitiesDirect();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => service.ReadSingleAsync <Book>(x => true));

                //VERIFY
                ex.Message.ShouldEqual("Sequence contains more than one element.");
            }
        }
        public async Task TestProjectSingleWhereBad()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupSingleDtoAndEntities <BookTitleAndCount>();
                var service = new CrudServicesAsync(context, utData.Wrapped);

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => service.ReadSingleAsync <BookTitleAndCount>(x => true));

                //VERIFY
                ex.Message.ShouldEqual("Source sequence contains more than one element.");
            }
        }
示例#8
0
        public async Task TestProjectSingleBad()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupSingleDtoAndEntities <BookTitleAndCount>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = await service.ReadSingleAsync <BookTitleAndCount>(999);

                //VERIFY
                service.IsValid.ShouldBeFalse(service.GetAllErrors());
                service.GetAllErrors().ShouldEqual("Sorry, I could not find the Book you were looking for.");
            }
        }
示例#9
0
        public async Task TestReadSingleWhereNullIsNotErrorAsync(int bookId, string message)
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupSingleDtoAndEntities <BookTitle>(_configNoError);
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var book = await service.ReadSingleAsync <Book>(x => x.BookId == bookId);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                service.Message.ShouldEqual(message);
            }
        }
示例#10
0
        public async Task TestProjectSingleWhereOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupSingleDtoAndEntities <BookTitleAndCount>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = await service.ReadSingleAsync <BookTitleAndCount>(x => x.BookId == 1);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                dto.BookId.ShouldEqual(1);
                dto.Title.ShouldEqual("Refactoring");
            }
        }
示例#11
0
        public async Task TestReadSingleCollection()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupSingleDtoAndEntities <BookWithAuthors>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var dto = await service.ReadSingleAsync <BookWithAuthors>(1);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                dto.BookId.ShouldEqual(1);
                dto.Authors.Count.ShouldEqual(1);
            }
        }
        public async Task TestGetSingleOnEntityWhereBad()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupEntitiesDirect();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var book = await service.ReadSingleAsync <Book>(x => x.BookId == 99);

                //VERIFY
                service.IsValid.ShouldBeFalse();
                service.Errors.First().ToString().ShouldEqual("Sorry, I could not find the Book you were looking for.");
                book.ShouldBeNull();
            }
        }
        public async Task TestGetSingleOnEntityWhereOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupEntitiesDirect();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var book = await service.ReadSingleAsync <Book>(x => x.BookId == 1);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                book.BookId.ShouldEqual(1);
                context.Entry(book).State.ShouldEqual(EntityState.Unchanged);
            }
        }
        public async Task EmailSentForNewAlbum()
        {
            //Setup
            DbContext.SeedDatabaseSubscriberToTwoArtists();

            //Attempt and Verify
            var subscriptionDto = await CrudServicesAsync.ReadSingleAsync <SubscriptionDto>(1L);

            CrudServicesAsync.IsValid.ShouldBeTrue(CrudServicesAsync.GetAllErrors());
            subscriptionDto.Id.ShouldEqual(1);

            var artistDto = await CrudServicesAsync.ReadSingleAsync <ArtistDto>(1L);

            var output = await _subscriberAppService.NotifySubscribers(new NotifySubscribersInput
            {
                Subscriptions = new List <SubscriptionDto> {
                    subscriptionDto
                },
                Artist = artistDto,
                Album  = new AlbumDto
                {
                    CreatedDate = DateTime.UtcNow,
                    Name        = "Testing < 123 <br /> with HTML",
                    ReleaseDate = DateTime.UtcNow.AddDays(-1).ToString("yyyy-MM-dd"),
                    SpotifyId   = "1iBbBHhftbF1lJmOPs4D18",
                    Image       = new SpotifyImageDto
                    {
                        Width  = 300,
                        Height = 300,
                        Url    = "https://i.scdn.co/image/ab67616d00001e023587a048cf3e56f0efc56990"
                    }
                }
            });

            output.HasError.ShouldBeFalse(output.ErrorMessage);

            //TODO: verify that the email exists on disk?
        }
示例#15
0
        public async Task TestReadSingleWhereNullIsErrorAsync(int bookId, bool shouldBeValid)
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupSingleDtoAndEntities <BookTitle>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper);

                //ATTEMPT
                var book = await service.ReadSingleAsync <Book>(x => x.BookId == bookId);

                //VERIFY
                service.IsValid.ShouldEqual(shouldBeValid);
                if (!service.IsValid)
                {
                    service.GetAllErrors().ShouldEqual("Sorry, I could not find the Book you were looking for.");
                }
            }
        }
        public async Task TestProjectSingleWhereBad()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var utData  = context.SetupSingleDtoAndEntities <BookTitleAndCount>();
                var service = new CrudServicesAsync(context, utData.ConfigAndMapper, new CreateNewDBContextHelper(() => new EfCoreContext(options)));

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => service.ReadSingleAsync <BookTitleAndCount>(x => true));

                //VERIFY
#if NETCOREAPP2_1
                ex.Message.ShouldEqual("Source sequence contains more than one element.");
#elif NETCOREAPP3_0
                ex.Message.ShouldEqual("Enumerator failed to MoveNextAsync.");
#endif
            }
        }