示例#1
0
        public async Task DeleteGenre()
        {
            var databaseName = Guid.NewGuid().ToString();
            var context      = BuildContext(databaseName);
            var mapper       = BuildMap();

            context.Genres.Add(new Genre()
            {
                Name = "Genre 1"
            });
            context.SaveChanges();

            var context2 = BuildContext(databaseName);

            var controller = new GenresV2Controller(context2, mapper);

            var response = await controller.Delete(1);

            var result = response as StatusCodeResult;

            Assert.AreEqual(204, result.StatusCode);

            var context3 = BuildContext(databaseName);
            var exists   = await context3.Genres.AnyAsync();

            Assert.IsFalse(exists);
        }
示例#2
0
        public async Task GetGenreByExistingId()
        {
            //Preparation
            var databaseName = Guid.NewGuid().ToString();
            var context      = BuildContext(databaseName);
            var mapper       = BuildMap();

            context.Genres.Add(new Genre()
            {
                Name = "Genre1"
            });
            context.Genres.Add(new Genre()
            {
                Name = "Genre2"
            });
            context.SaveChanges();

            //will use another context in the same db(and data) because this new context it is dont have on the memorh the 2 records
            var context2 = BuildContext(databaseName);

            //Testing
            var controller = new GenresV2Controller(context2, mapper);

            var Id       = 1;
            var response = await controller.Get(Id);

            var result = response.Result as OkObjectResult;
            var genre  = result.Value as GenreDto;

            Assert.AreEqual(Id, genre.Id);
        }
示例#3
0
        public async Task UpdateGenre()
        {
            var databaseName = Guid.NewGuid().ToString();
            var context      = BuildContext(databaseName);
            var mapper       = BuildMap();

            context.Genres.Add(new Genre {
                Name = "New Genre1"
            });
            context.SaveChanges();

            var context2         = BuildContext(databaseName);
            var controller       = new GenresV2Controller(context2, mapper);
            var genreCreationDTO = new GenreCreationDto()
            {
                Name = "New name"
            };

            var id       = 1;
            var response = await controller.Put(id, genreCreationDTO);

            var result = response as StatusCodeResult;

            Assert.AreEqual(204, result.StatusCode);

            var context3 = BuildContext(databaseName);
            var exists   = await context3.Genres.AnyAsync(x => x.Name == "New name");

            Assert.IsTrue(exists);
        }
示例#4
0
        public async Task DeleteGenreNotFound()
        {
            var databaseName = Guid.NewGuid().ToString();
            var context      = BuildContext(databaseName);
            var mapper       = BuildMap();

            var controller = new GenresV2Controller(context, mapper);

            var response = await controller.Delete(1);

            var result = response as StatusCodeResult;

            Assert.AreEqual(404, result.StatusCode);
        }
示例#5
0
        public async Task GetAllGenres()
        {
            //Preparation
            var databaseName = Guid.NewGuid().ToString();
            var context      = BuildContext(databaseName);
            var mapper       = BuildMap();

            context.Genres.Add(new Genre()
            {
                Name = "Genre1"
            });
            context.Genres.Add(new Genre()
            {
                Name = "Genre2"
            });
            context.SaveChanges();

            //will use another context in the same db(and data) because this new context it is dont have on the memorh the 2 records
            var context2 = BuildContext(databaseName);

            //Testing
            var controller = new GenresV2Controller(context2, mapper);

            var response = await controller.Get();

            var result = response.Result as OkObjectResult;

            var genres = result.Value as List <GenreDto>;

            //Verification
            Assert.AreEqual(2, genres.Count);

            //if use as return type Task<ActionResult<List<GenreDTO>>>

            //// Testing
            //var controller = new GenresController(context2, mapper);
            //var response = await controller.Get();

            //// Verification
            //var genres = response.Value;
            //Assert.AreEqual(2, genres.Count);
        }
示例#6
0
        public async Task GetGenreByIdDoesNotExist()
        {
            //Preparation
            var databaseName = Guid.NewGuid().ToString();
            var context      = BuildContext(databaseName);
            var mapper       = BuildMap();

            //Testing
            var controller = new GenresV2Controller(context, mapper);
            ActionResult <GenreDto> response = await controller.Get(1);

            var result     = response.Result as NotFoundResult;
            var statusCode = result.StatusCode;

            Assert.AreEqual(404, statusCode);

            //if use as return type Task<ActionResult<GenreDTO>>
            //var response = await controller.Get(1);
            //var result = response.Result as StatusCodeResult;
            //Assert.AreEqual(404, result.StatusCode);
        }
示例#7
0
        public async Task CreateGenre()
        {
            //Preparation
            var databaseName = Guid.NewGuid().ToString();
            var context      = BuildContext(databaseName);
            var mapper       = BuildMap();

            var newGenre = new GenreCreationDto {
                Name = "New Genre"
            };

            //Testing
            var controller = new GenresV2Controller(context, mapper);
            var response   = await controller.Post(newGenre);

            var result = response as CreatedAtRouteResult;

            Assert.AreEqual(201, result.StatusCode);

            var context2 = BuildContext(databaseName);
            var count    = await context2.Genres.CountAsync();

            Assert.AreEqual(1, count);
        }