Beispiel #1
0
        public async Task GetRandomGenresShouldNotReturnDeletedGenres()
        {
            var options = new DbContextOptionsBuilder <AlexandriaDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new AlexandriaDbContext(options);
            await db.Genres.AddRangeAsync(
                new Genre
            {
                Name        = "test1",
                Description = "description1",
                IsDeleted   = true,
            },
                new Genre
            {
                Name        = "test2",
                Description = "description2",
                IsDeleted   = true,
            },
                new Genre
            {
                Name        = "test3",
                Description = "description3",
            });

            await db.SaveChangesAsync();

            var genresService = new GenresService(db);

            var result = await genresService.GetRandomGenresAsync <GenreTestModel>(2);

            Assert.Single(result);
        }
Beispiel #2
0
        public void GetAllReturnsGenreModels()
        {
            var genres        = new List <Genre>();
            var mockGenreRepo = new Mock <IGenresRepository>();

            mockGenreRepo.Setup(x => x.All()).Returns(genres.AsQueryable());
            genres.Add(new Genre()
            {
                Id   = "test",
                Name = "123"
            });
            genres.Add(new Genre()
            {
                Id   = "123",
                Name = "test"
            });

            var service = new GenresService(mockGenreRepo.Object);

            var result      = service.AllGenres();
            var secondGenre = result.ToArray()[1];

            Assert.Equal(2, result.Count());
            Assert.Equal("test", result.First().Id);
            Assert.Equal("123", result.First().Name);
            Assert.Equal("123", secondGenre.Id);
            Assert.Equal("test", secondGenre.Name);
        }
Beispiel #3
0
        public async Task GetGenreShouldReturnCorrectGenre()
        {
            var options = new DbContextOptionsBuilder <AlexandriaDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new AlexandriaDbContext(options);
            await db.Genres.AddRangeAsync(
                new Genre
            {
                Name        = "test1",
                Description = "description1",
            },
                new Genre
            {
                Name        = "test2",
                Description = "description2",
            },
                new Genre
            {
                Name        = "test3",
                Description = "description3",
                IsDeleted   = true,
            });

            await db.SaveChangesAsync();

            var genresService = new GenresService(db);

            var result = await genresService.GetGenreByIdAsync <GenreTestModel>(1);

            Assert.Equal("test1", result.Name);
            Assert.Equal("description1", result.Description);
        }
Beispiel #4
0
        public async Task GetGenresCountShouldReturnCorrectCount()
        {
            var options = new DbContextOptionsBuilder <AlexandriaDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new AlexandriaDbContext(options);
            await db.Genres.AddRangeAsync(
                new Genre
            {
                Name        = "test1",
                Description = "description1",
            },
                new Genre
            {
                Name        = "test2",
                Description = "description2",
            },
                new Genre
            {
                Name        = "test3",
                Description = "description3",
            });

            await db.SaveChangesAsync();

            var genresService = new GenresService(db);

            var result = await genresService.GetGenresCountAsync();

            Assert.Equal(3, result);
        }
Beispiel #5
0
        public async Task EditGenreShouldWorkCorrectly(string name, string description)
        {
            var options = new DbContextOptionsBuilder <AlexandriaDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new AlexandriaDbContext(options);
            await db.Genres.AddAsync(
                new Genre
            {
                Name        = "test",
                Description = "description",
            });

            await db.SaveChangesAsync();

            var genresService = new GenresService(db);

            await genresService.EditGenreAsync(1, name, description);

            var result = await db.Genres.FirstOrDefaultAsync();

            Assert.Equal(name, result.Name);
            Assert.Equal(description, result.Description);
            Assert.NotNull(result.ModifiedOn);
        }
Beispiel #6
0
        public async Task DeleteGenreShouldSetIsDeletedAndDeletedOn()
        {
            var options = new DbContextOptionsBuilder <AlexandriaDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new AlexandriaDbContext(options);
            await db.Genres.AddAsync(
                new Genre
            {
                Name        = "test",
                Description = "description",
            });

            await db.SaveChangesAsync();

            var genresService = new GenresService(db);

            await genresService.DeleteGenreByIdAsync(1);

            var result = await db.Genres.FirstOrDefaultAsync();

            Assert.True(result.IsDeleted);
            Assert.NotNull(result.DeletedOn);
        }
Beispiel #7
0
 public GenresServiceTest()
 {
     AddGenreCommandMock    = new Mock <IAddGenreCommand>();
     UpdateGenreCommandMock = new Mock <IUpdateGenreCommand>();
     ListAllGenresQueryMock = new Mock <IListAllGenresQuery>();
     ServiceUnderTest       = new GenresService(AddGenreCommandMock.Object,
                                                UpdateGenreCommandMock.Object,
                                                ListAllGenresQueryMock.Object);
 }
Beispiel #8
0
        public void Create_ExistsGenre_ShouldThrownGenreDublicateException()
        {
            var dto = new GenreDto()
            {
                Name = DefaultData.Genres.CSharp.Name
            };

            Assert.Throws <GenreDublicateException>(async() => await GenresService.Create(dto));
        }
Beispiel #9
0
        public async Task GetTree_ShouldReturnRootNodeAndChildren()
        {
            var genres = await GenresService.GetTree();

            foreach (var genre in genres)
            {
                Assert.That(genre.Parent, Is.Null);
            }
        }
Beispiel #10
0
        public void DeleteGenreAsyncThrowsArgumentExceptionWhenGivenInvalidId()
        {
            var genres        = new List <Genre>();
            var mockGenreRepo = new Mock <IGenresRepository>();

            mockGenreRepo.Setup(x => x.All()).Returns(genres.AsQueryable());

            var service = new GenresService(mockGenreRepo.Object);

            Assert.ThrowsAsync <ArgumentException>(() => service.DeleteGenre("ivan"));
        }
Beispiel #11
0
        public async Task Delete_NodeWithNestedNodesRecurcivelly_ShouldDeleteNodeWithNestedNodes()
        {
            var genre = Genres.Single(x => x.Id == DefaultData.Genres.Programming.Id);

            await GenresService.Delete(genre.Id, true);

            var genres = Genres.Where(x => x.Parent?.Id == genre.Id);

            Assert.That(genres.Where(x => x.Id == genre.Id), Is.Empty);
            Assert.That(genres, Is.Empty);
        }
Beispiel #12
0
        public async Task Delete_RootNodes_ShouldDeleteAllGenres()
        {
            var rootGenres = Genres.Where(x => x.Parent == null).ToList();

            for (int i = 0, len = rootGenres.Count(); i < len; i++)
            {
                await GenresService.Delete(rootGenres[i].Id, true);
            }

            Assert.That(Genres, Is.Empty);
        }
Beispiel #13
0
        public async Task Update_ShouldUpdated()
        {
            var genre = Genres.First();
            var dto   = new GenreDto()
            {
                Id   = genre.Id,
                Name = "very updated genre name"
            };
            await GenresService.Update(dto.Id, dto);

            Assert.That(Genres.SingleOrDefault(x => string.Equals(x.Name, dto.Name, StringComparison.CurrentCultureIgnoreCase)), Is.Not.Null);
        }
Beispiel #14
0
        public void Update_NameIncorrect_ShouldThrownGenreIncorrectException()
        {
            var dto = new GenreDto()
            {
                Id = DefaultData.Genres.DotNet.Id
            };

            Assert.Throws <GenreIncorrectException>(async() =>
            {
                await GenresService.Update(dto.Id, dto);
            });
        }
        public GenresServiceTest()
        {
            this.genresRepo = new Mock <IDeletableEntityRepository <Genre> >();

            this.genresList = new List <Genre>();
            this.service    = new GenresService(this.genresRepo.Object);

            this.genresRepo.Setup(x => x.All()).Returns(this.genresList.AsQueryable());
            this.genresRepo.Setup(x => x.AllAsNoTracking()).Returns(this.genresList.AsQueryable());
            this.genresRepo.Setup(x => x.AddAsync(It.IsAny <Genre>())).Callback(
                (Genre item) => this.genresList.Add(item));
        }
Beispiel #16
0
        public void Update_ExistingName_ShouldThrownGenresDublicateException()
        {
            var dto = new GenreDto()
            {
                Id   = DefaultData.Genres.CSharp.Id,
                Name = DefaultData.Genres.LanguageAndTools.Name,
            };

            Assert.Throws <GenreDublicateException>(async() =>
            {
                await GenresService.Update(dto.Id, dto);
            });
        }
Beispiel #17
0
        public async Task RemoveGenreAsyncWithInCorrectDataShoulThrowNullReferenceException(int groupId, int genreId)
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext             = new ApplicationDbContext(options);
            var genresRepository      = new EfDeletableEntityRepository <Genre>(dbContext);
            var groupGenresRepository = new EfRepository <GroupGenre>(dbContext);
            var genresService         = new GenresService(groupGenresRepository, genresRepository);

            await Assert.ThrowsAsync <NullReferenceException>(async() =>
            {
                await genresService.RemoveGenreAsync(groupId, genreId);
            });
        }
Beispiel #18
0
        public async Task CreateGenreAsyncThrowsArgumentExceptionWhenGivenAExistingGenreName()
        {
            var genres        = new List <Genre>();
            var mockGenreRepo = new Mock <IGenresRepository>();

            mockGenreRepo.Setup(x => x.GenreExist("gosho")).Returns(true);

            var service = new GenresService(mockGenreRepo.Object);

            await Assert.ThrowsAsync <ArgumentException>(() => service.CreateGenreAsync(new Web.ViewModels.Genres.CreateGenreInputModel()
            {
                Name = "gosho"
            }));
        }
Beispiel #19
0
        public async Task CreateGenreShouldAddToDatabase()
        {
            var options = new DbContextOptionsBuilder <AlexandriaDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new AlexandriaDbContext(options);

            var genresService = new GenresService(db);

            await genresService.CreateGenreAsync("test", "test description");

            Assert.Equal(1, await db.Genres.CountAsync());
        }
Beispiel #20
0
        public void EditGenreAsyncThrowsArgumentExceptionWhenGivenInvalidId()
        {
            var genres        = new List <Genre>();
            var mockGenreRepo = new Mock <IGenresRepository>();

            mockGenreRepo.Setup(x => x.All()).Returns(genres.AsQueryable());

            var service = new GenresService(mockGenreRepo.Object);

            Assert.ThrowsAsync <ArgumentException>(() => service.EditGenre(new Web.ViewModels.Genres.EditGenreInputModel()
            {
                Id = "Ivan"
            }));
        }
Beispiel #21
0
        public async Task GetGenreShouldReturnNullWhenNotFound()
        {
            var options = new DbContextOptionsBuilder <AlexandriaDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new AlexandriaDbContext(options);

            var genresService = new GenresService(db);

            var result = await genresService.GetGenreByIdAsync <GenreTestModel>(1);

            Assert.Null(result);
        }
Beispiel #22
0
        public async Task AddGenreAsyncWithCorrectDataShouldReturnCorrectResult()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext             = new ApplicationDbContext(options);
            var genresRepository      = new EfDeletableEntityRepository <Genre>(dbContext);
            var groupGenresRepository = new EfRepository <GroupGenre>(dbContext);
            var genresService         = new GenresService(groupGenresRepository, genresRepository);

            var actual = await genresService.AddGenreAsync(5, 1);

            var expected = 5;

            Assert.Equal(expected, actual);
        }
Beispiel #23
0
        public async Task EditGenreMustReturnFalseWithIncorrectNames(string name)
        {
            AutoMapperConfig.RegisterMappings(typeof(GenreTestModel).Assembly);
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var repository = new EfDeletableEntityRepository <Genre>(new ApplicationDbContext(options.Options));

            await repository.AddAsync(new Genre { Id = 1, Name = "Action" });

            await repository.SaveChangesAsync();

            var service = new GenresService(repository, null);

            await Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => service.EditGenreAsync(1, name));
        }
Beispiel #24
0
        public async Task Create_RootGenre_ShouldValid()
        {
            var oldCount = Genres.Count;
            var dto      = new GenreDto()
            {
                Name = nameof(Create_RootGenre_ShouldValid)
            };
            await GenresService.Create(dto);

            Assert.That(Genres.Count, Is.EqualTo(oldCount + 1));
            var createdGenre = Genres.SingleOrDefault(x => string.Equals(x.Name, nameof(Create_RootGenre_ShouldValid), StringComparison.CurrentCultureIgnoreCase));

            Assert.That(createdGenre, Is.Not.Null);
            Assert.That(createdGenre.Parent, Is.Null);
            Assert.That(createdGenre.Children, Is.Empty);
        }
Beispiel #25
0
        public async Task CreateGenreShouldReturnCorrectId()
        {
            var options = new DbContextOptionsBuilder <AlexandriaDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new AlexandriaDbContext(options);

            var genresService = new GenresService(db);

            var id = await genresService.CreateGenreAsync("test", "test description");

            var result = await db.Genres.FirstOrDefaultAsync();

            Assert.Equal(id, result.Id);
        }
Beispiel #26
0
        public void GetGenreNameWorksCorrectly()
        {
            var genres        = new List <Genre>();
            var mockGenreRepo = new Mock <IGenresRepository>();

            mockGenreRepo.Setup(x => x.All()).Returns(genres.AsQueryable());
            genres.Add(new Genre()
            {
                Id   = "test123",
                Name = "ivan"
            });

            var service = new GenresService(mockGenreRepo.Object);
            var result  = service.GetGenreName("test123");

            Assert.Equal("ivan", result);
        }
Beispiel #27
0
        public async Task AddGenreAsyncWithDublicateNameShouldThrowArgumentException()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext             = new ApplicationDbContext(options);
            var genresRepository      = new EfDeletableEntityRepository <Genre>(dbContext);
            var groupGenresRepository = new EfRepository <GroupGenre>(dbContext);
            var genresService         = new GenresService(groupGenresRepository, genresRepository);

            await groupGenresRepository.AddAsync(new GroupGenre { GroupId = 2, GenreId = 1 });

            await groupGenresRepository.SaveChangesAsync();

            await Assert.ThrowsAsync <ArgumentException>(async() =>
            {
                await genresService.AddGenreAsync(2, 1);
            });
        }
        public MoviesListViewModel()
        {
            genreService = new GenresService();
            movieService = new MovieService();

            Genres = new ObservableCollection <Genre>(genreService.GetAllGenres());
            Movies = new ObservableCollection <Movie>();

            ShowSearchItemsCommand = new Command(ShowSearchItems);
            SearchCommand          = new Command(RePopulateCollection);
            ResetCommand           = new Command(
                () => {
                selectedGenre = null;
                RePopulateCollection();
            });

            RePopulateCollection();
        }
Beispiel #29
0
        public async Task CreateGenreAsyncCreatesNewGenreWhenNameIsNotTaken()
        {
            var genres        = new List <Genre>();
            var mockGenreRepo = new Mock <IGenresRepository>();

            mockGenreRepo.Setup(x => x.GenreExist("gosho")).Returns(false);
            mockGenreRepo.Setup(x => x.AddAsync(It.IsAny <Genre>())).Callback((Genre genre) => genres.Add(genre));

            var service = new GenresService(mockGenreRepo.Object);

            await service.CreateGenreAsync(new Web.ViewModels.Genres.CreateGenreInputModel()
            {
                Name = "gosho"
            });

            Assert.Single(genres);
            Assert.Equal("gosho", genres.First().Name);
        }
Beispiel #30
0
        public async Task DeleteGenreAsyncWorksCorrectly()
        {
            var genres        = new List <Genre>();
            var mockGenreRepo = new Mock <IGenresRepository>();

            mockGenreRepo.Setup(x => x.All()).Returns(genres.AsQueryable());
            mockGenreRepo.Setup(x => x.Delete(It.IsAny <Genre>())).Callback((Genre genre) => genres.Remove(genre));
            genres.Add(new Genre()
            {
                Id   = "test123",
                Name = "ivan"
            });

            var service = new GenresService(mockGenreRepo.Object);

            await service.DeleteGenre("test123");

            Assert.Empty(genres);
        }