コード例 #1
0
ファイル: GroupedCatResultsTests.cs プロジェクト: akki-s/cats
        public async Task Get_Grouped_Results_If_Pet_Type_Null_Should_Not_Include_It()
        {
            var mockDataForGrouping = new List <Owner>
            {
                new Owner {
                    Name   = "Selina",
                    Gender = "Female",
                    Pets   = new List <Pet> {
                        new Pet {
                            Name = "Abe"
                        },
                        new Pet {
                            Type = "Cat", Name = "Garfield"
                        },
                    }
                }
            };

            var mockRepository = new Mock <IPetsRepository>();

            mockRepository.Setup(x => x.GetAllOwnersAndPets())
            .ReturnsAsync(mockDataForGrouping);

            var catService = new PetsService(mockRepository.Object);

            var actualResult = await catService.GetCatsGroupedByOwnerGender().ConfigureAwait(false);

            actualResult.Should().NotBeNull();
            actualResult[0].Cats.Count.Should().Be(1);
            actualResult[0].Cats[0].Name.Should().Be("Garfield");
        }
コード例 #2
0
        public async Task MethodShoudReturnAllPets()
        {
            var list     = new List <Pet>();
            var mockRepo = new Mock <IDeletableEntityRepository <Pet> >();

            mockRepo.Setup(x => x.AllAsNoTracking()).Returns(list.AsQueryable());

            var service   = new PetsService(mockRepo.Object);
            var viewModel = new PetsInListViewModel
            {
                AddedByUserId = "PeshoId",
                BreedId       = 1,
                CityId        = 1,
                DateOfBirth   = new DateTime(2008, 3, 1, 7, 0, 0),
                Id            = 1,
                Name          = "PeshoCat",
            };
            var pet = new Pet
            {
                AddedByUserId = "PeshoId",
                BreedId       = 1,
                CityId        = 1,
                DateOfBirth   = new DateTime(2008, 3, 1, 7, 0, 0),
                Id            = 1,
                Name          = "PeshoCat",
            };

            list.Add(pet);
            service.GetAll <PetsInListViewModel>(1);
        }
コード例 #3
0
        public object GetAll()
        {
            var petsService = new PetsService();
            var result      = petsService.GetAll(_petsDbContext);

            return(result);
        }
コード例 #4
0
ファイル: GroupedCatResultsTests.cs プロジェクト: akki-s/cats
        public async Task Get_Grouped_Results_If_Pet_Name_BlankOrNull_Should_Return_Blank()
        {
            var mockDataForGrouping = new List <Owner>
            {
                new Owner {
                    Name   = "Selina",
                    Gender = "Female",
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat"
                        },
                        new Pet {
                            Type = "Cat", Name = string.Empty
                        },
                    }
                }
            };

            var mockRepository = new Mock <IPetsRepository>();

            mockRepository.Setup(x => x.GetAllOwnersAndPets())
            .ReturnsAsync(mockDataForGrouping);

            var catService = new PetsService(mockRepository.Object);

            var actualResult = await catService.GetCatsGroupedByOwnerGender().ConfigureAwait(false);

            actualResult.Should().NotBeNull();
            actualResult[0].Cats[0].Name.Should().Be(string.Empty);
            actualResult[0].Cats[1].Name.Should().Be(string.Empty);
        }
コード例 #5
0
ファイル: UnitTest1.cs プロジェクト: tiramisuzie/LoveThemBack
        public void UpdatePet()
        {
            DbContextOptions <LoveThemBackAPIDbContext> options =
                new DbContextOptionsBuilder <LoveThemBackAPIDbContext>().UseInMemoryDatabase("UpdatePet")
                .Options;


            using (LoveThemBackAPIDbContext context = new LoveThemBackAPIDbContext(options))
            {
                Pet TestPet = new Pet();
                TestPet.PetID = 1;
                TestPet.Name  = "Snooky";

                var ServicesCreate = new PetsService(context);
                ServicesCreate.AddPet(TestPet);

                Pet UpdatePet = new Pet();
                UpdatePet.PetID = 1;
                UpdatePet.Name  = "Coolio";

                ServicesCreate.UpdatePet(1, UpdatePet);
                var getPet = ServicesCreate.GetById(1);

                Assert.Equal("Coolio", getPet.Value.Name);
            }
        }
コード例 #6
0
        public async Task DeleteAsyncPet()
        {
            var list     = new List <Pet>();
            var mockRepo = new Mock <IDeletableEntityRepository <Pet> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Pet>()));
            mockRepo.Setup(x => x.Delete(It.IsAny <Pet>()))
            .Callback(
                (Pet pet) => list.Remove(pet));
            var service = new PetsService(mockRepo.Object);

            var pet = new Pet
            {
                BreedId       = 1,
                CityId        = 1,
                Id            = 1,
                Name          = "GoshoCat",
                DateOfBirth   = new DateTime(2008, 3, 1, 7, 0, 0),
                SpecieId      = 1,
                AddedByUserId = "PeshoId",
            };

            list.Add(pet);

            await service.DeleteAsync(1, "PeshoId");

            Assert.Equal(0, service.GetCount());
        }
コード例 #7
0
        public async Task AddingAsyncAPetTestAndGetCount()
        {
            var list     = new List <Pet>();
            var mockRepo = new Mock <IDeletableEntityRepository <Pet> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Pet>()))
            .Callback(
                (Pet pet) => list.Add(pet));
            var service = new PetsService(mockRepo.Object);

            var inputModel = new AddPetInputModel
            {
                BreedId     = 1,
                CityId      = 1,
                Id          = 1,
                Name        = "Gosho",
                DateOfBirth = new DateTime(2008, 3, 1, 7, 0, 0),
                Gender      = "Male",
            };
            var imagePaths = new List <string>();

            imagePaths.Add("vasko.com");
            await service.AddAsync(inputModel, 1, "Pesho", imagePaths);

            Assert.Equal(1, service.GetCount());
        }
コード例 #8
0
        public PetsModel Get()
        {
            var response = _ownerClient.GetOwnerDetails(url);
            var owners   = _jsonTransformer.ConvertFromJson(_httpResponseTransformer.TransformToJson(response, _logger));

            return(PetsService.CreatePetsModel(owners));
        }
コード例 #9
0
ファイル: GroupedCatResultsTests.cs プロジェクト: akki-s/cats
        public async Task Get_Grouped_Results_Returns_Data_GroupedByOwnerGender_Sorted_By_Name()
        {
            var mockDataForGrouping = new List <Owner>
            {
                new Owner {
                    Name   = "Selina",
                    Gender = "Female",
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Garfield"
                        },
                        new Pet {
                            Type = "Cat", Name = "Abe"
                        }
                    }
                },
                new Owner {
                    Name   = "Bruce",
                    Gender = "Male",
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Jasper"
                        },
                        new Pet {
                            Type = "Dog", Name = "Scooby"
                        },
                        new Pet {
                            Type = "Cat", Name = "Garry"
                        }
                    }
                }
            };

            var mockRepository = new Mock <IPetsRepository>();

            mockRepository.Setup(x => x.GetAllOwnersAndPets())
            .ReturnsAsync(mockDataForGrouping);

            var catService = new PetsService(mockRepository.Object);

            var actualResult = await catService.GetCatsGroupedByOwnerGender().ConfigureAwait(false);

            actualResult.Should().NotBeNull();
            actualResult.Count.Should().Be(2);
            actualResult[0].OwnerGender.Should().Be("Female");
            actualResult[1].OwnerGender.Should().Be("Male");

            //cats are sorted by their name
            actualResult[0].Cats[0].Name.Should().Be("Abe");
            actualResult[0].Cats[1].Name.Should().Be("Garfield");

            actualResult[1].Cats[0].Name.Should().Be("Garry");
            actualResult[1].Cats[1].Name.Should().Be("Jasper");
        }
コード例 #10
0
ファイル: GroupedCatResultsTests.cs プロジェクト: akki-s/cats
        public async Task GetListOfCats_When_Pets_Data_Is_Null_Should_Return_Null()
        {
            var mockRepository = new Mock <IPetsRepository>();

            mockRepository.Setup(x => x.GetAllOwnersAndPets())
            .ReturnsAsync(() => null);

            var catService   = new PetsService(mockRepository.Object);
            var actualResult = await catService.GetCatsGroupedByOwnerGender().ConfigureAwait(false);

            actualResult.Should().BeNull();
        }
コード例 #11
0
ファイル: GroupedCatResultsTests.cs プロジェクト: akki-s/cats
        public async Task Get_Grouped_Results_Should_Return_Only_Cats()
        {
            var mockDataForGrouping = new List <Owner>
            {
                new Owner {
                    Name   = "Selina",
                    Gender = "Female",
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Garfield"
                        },
                    }
                },
                new Owner {
                    Name   = "Bruce",
                    Gender = "Male",
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Jasper"
                        },
                        new Pet {
                            Type = "Dog", Name = "Scooby"
                        },
                        new Pet {
                            Type = "Cat", Name = "Garfield"
                        },
                    }
                }
            };

            var mockRepository = new Mock <IPetsRepository>();

            mockRepository.Setup(x => x.GetAllOwnersAndPets())
            .ReturnsAsync(mockDataForGrouping);

            var catService = new PetsService(mockRepository.Object);

            var actualResult = await catService.GetCatsGroupedByOwnerGender().ConfigureAwait(false);

            actualResult.Should().NotBeNull();
            actualResult.Count.Should().Be(2);
            actualResult[0].OwnerGender.Should().Be("Female");
            actualResult[1].OwnerGender.Should().Be("Male");

            //only cats should be there
            actualResult[0].Cats.Any(c => c.Name.Equals("scooby", StringComparison.OrdinalIgnoreCase)).Should().BeFalse();
            actualResult[0].Cats.Any(c => c.Name.Equals("scooby", StringComparison.OrdinalIgnoreCase)).Should().BeFalse();
        }
コード例 #12
0
        public void GetPetsClassified_NullDataTest()
        {
            Mock fileServiceMock = new Mock <IFileService>(MockBehavior.Default);

            fileServiceMock.As <IFileService>().Setup(x => x.GetFilePath()).Returns("ABC");

            Mock repositoryMock = new Mock <IPetsRepository>();

            repositoryMock.As <IPetsRepository>().Setup(x => x.GetData(It.IsAny <string>())).Returns <PetsClassified>(null);

            PetsService petservice = new PetsService(fileServiceMock.Object as IFileService, repositoryMock.Object as IPetsRepository);

            var result = petservice.GetPetsClassified();

            Assert.IsNull(result);
        }
コード例 #13
0
ファイル: GroupedCatResultsTests.cs プロジェクト: akki-s/cats
        public async Task Get_Grouped_Results_Two_Owners_Of_Same_Gender_Have_Pet_With_Same_Name_Should_Sort_By_Owner()
        {
            var mockDataForGrouping = new List <Owner>
            {
                new Owner {
                    Name   = "Damian",
                    Gender = "Male",
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Garfield"
                        },
                    }
                },
                new Owner {
                    Name   = "Bruce",
                    Gender = "Male",
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Garfield"
                        },
                    }
                }
            };

            var mockRepository = new Mock <IPetsRepository>();

            mockRepository.Setup(x => x.GetAllOwnersAndPets())
            .ReturnsAsync(mockDataForGrouping);

            var catService = new PetsService(mockRepository.Object);

            var actualResult = await catService.GetCatsGroupedByOwnerGender().ConfigureAwait(false);

            actualResult.Should().NotBeNull();
            actualResult.Count.Should().Be(1);
            actualResult[0].OwnerGender.Should().Be("Male");

            // cats are sorted by their name
            actualResult[0].Cats[0].OwnerName.Should().Be("Bruce");
            actualResult[0].Cats[1].OwnerName.Should().Be("Damian");
        }
コード例 #14
0
        public async Task PostUser_ShouldCreateUser()
        {
            // arrange
            var postBody = Build.PetDto
                           .WithCategory(c => c.WithId(Generator.RandomInt()).WithName("category name"))
                           .WithPhotoUrls(new[] { "photo url" })
                           .WithTags(t => t.WithId(Generator.RandomInt()).WithName("tag name"))
                           .WithStatus(Status.AVAILABLE)
                           .Build();

            // act
            var response = await PetsService.Create(postBody);

            // assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var result = JsonConvert.DeserializeObject <PetDto>(
                await response.Content.ReadAsStringAsync());

            Assert.Equal(-9223372036854775808, result.Id);
        }
コード例 #15
0
ファイル: UnitTest1.cs プロジェクト: tiramisuzie/LoveThemBack
        public void DeletePet()
        {
            DbContextOptions <LoveThemBackAPIDbContext> options =
                new DbContextOptionsBuilder <LoveThemBackAPIDbContext>().UseInMemoryDatabase("DeletePet")
                .Options;


            using (LoveThemBackAPIDbContext context = new LoveThemBackAPIDbContext(options))
            {
                Pet TestPet = new Pet();
                TestPet.PetID = 1;
                TestPet.Name  = "Snooky";

                var ServicesCreate = new PetsService(context);
                ServicesCreate.AddPet(TestPet);
                ServicesCreate.DeletePet(1, 8675309);
                var getPet = context.Pets.FirstOrDefault(x => x.PetID == 1);

                Assert.Null(getPet);
            }
        }
コード例 #16
0
        public async Task DeleteAsyncPetWithWrongUserId()
        {
            var list     = new List <Pet>();
            var mockRepo = new Mock <IDeletableEntityRepository <Pet> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.Delete(It.IsAny <Pet>()));
            var service = new PetsService(mockRepo.Object);

            var pet = new Pet
            {
                BreedId       = 1,
                CityId        = 1,
                Id            = 1,
                Name          = "GoshoCat",
                DateOfBirth   = new DateTime(2008, 3, 1, 7, 0, 0),
                SpecieId      = 1,
                AddedByUserId = "PeshoId",
            };

            list.Add(pet);
            Assert.False(await service.DeleteAsync(1, "WrongId"));
        }
コード例 #17
0
        public async void CheckWhetherHelperReturnsExceptionWhenUrlIsNotSet()
        {
            var mockFactory = new Mock <IHttpClientFactory>();
            var mockConfig  = new Mock <IConfiguration>();
            List <OwnerModel> serviceResponse = new List <OwnerModel>();
            var configuration = new HttpConfiguration();
            Mock <ILogger <PetsService> > mockLoggerService = new Mock <ILogger <PetsService> >();
            var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) => {
                request.SetConfiguration(configuration);
                var response = request.CreateResponse(HttpStatusCode.OK, serviceResponse);
                return(Task.FromResult(response));
            });
            var client = new Mock <HttpClient>();

            mockFactory.Setup(mock => mock.CreateClient(It.IsAny <string>())).Throws(new Exception());
            //  client.Setup(_ => _.SendAsync(It.IsAny<HttpRequestMessage>())).ReturnsAsync(new HttpResponseMessage());

            IHttpClientFactory factory = mockFactory.Object;

            PetsService service = new PetsService(factory, mockLoggerService.Object, mockConfig.Object);

            Task act() => service.GetOwnerPetsDetails(); await Assert.ThrowsAsync <Exception>(act);
        }
コード例 #18
0
        public async void CheckWhetherHelperReturningCorrectValue()
        {
            var mockFactory = new Mock <IHttpClientFactory>();
            var mockConfig  = new Mock <IConfiguration>();
            List <OwnerModel> serviceResponse = new List <OwnerModel>();
            var configuration = new HttpConfiguration();
            Mock <ILogger <PetsService> > mockLoggerService = new Mock <ILogger <PetsService> >();
            var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) => {
                request.SetConfiguration(configuration);
                var response = request.CreateResponse(HttpStatusCode.OK, serviceResponse);
                return(Task.FromResult(response));
            });
            var client = new HttpClient();

            mockFactory.Setup(mock => mock.CreateClient(It.IsAny <string>())).Returns(client);

            IHttpClientFactory factory = mockFactory.Object;

            PetsService service = new PetsService(factory, mockLoggerService.Object, mockConfig.Object);

            var result = await service.GetOwnerPetsDetails();

            Assert.NotNull(result);
        }
コード例 #19
0
ファイル: GroupedCatResultsTests.cs プロジェクト: akki-s/cats
        public async Task Get_Grouped_Results_If_Cats_With_Owner_Gender_Empty_Or_Null_Should_be_Grouped_Under_Empty_Owner()
        {
            var mockDataForGrouping = new List <Owner>
            {
                new Owner {
                    Gender = "Female",
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Abe"
                        },
                    }
                },
                new Owner {
                    Name = "Robin",
                    Pets = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Garfield"
                        },
                    }
                },
                new Owner {
                    Name   = "Bruce",
                    Gender = "Male",
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Jasper"
                        },
                        new Pet {
                            Type = "Cat", Name = "Garfield"
                        },
                    }
                },

                new Owner {
                    Name   = "Alfred",
                    Gender = string.Empty,
                    Pets   = new List <Pet> {
                        new Pet {
                            Type = "Cat", Name = "Garry"
                        },
                    }
                }
            };

            var mockRepository = new Mock <IPetsRepository>();

            mockRepository.Setup(x => x.GetAllOwnersAndPets())
            .ReturnsAsync(mockDataForGrouping);

            var catService = new PetsService(mockRepository.Object);

            var actualResult = await catService.GetCatsGroupedByOwnerGender().ConfigureAwait(false);

            actualResult.Should().NotBeNull();
            actualResult.Count.Should().Be(3);
            actualResult[0].OwnerGender.Should().Be(string.Empty);
            actualResult[0].Cats[0].OwnerName.Should().Be("Robin");
            actualResult[0].Cats[1].OwnerName.Should().Be("Alfred");
            actualResult[0].Cats[0].Name.Should().Be("Garfield");
            actualResult[0].Cats[1].Name.Should().Be("Garry");
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: starsaminf/jalasoft
        public static async Task GetPets()
        {
            PetsService service = new PetsService();

            pets = await service.GetAllPets();
        }
コード例 #21
0
        public void Given_ListOfOwnersWithOnlyDogs_CreatePetsModel_Returns_ModelWithGenders()
        {
            var result = PetsService.CreatePetsModel(CreateDogOwners());

            result.Should().BeEquivalentTo(CreateEmptyPetsModelWithGenders());
        }
コード例 #22
0
        public void Given_EmptyListOfOwners_CreatePetsModel_Returns_EmptyModel()
        {
            var result = PetsService.CreatePetsModel(new List <Owner>());

            result.Should().BeEquivalentTo(CreateEmptyPetsModel());
        }
コード例 #23
0
        public void Given_ListOfOwners_CreatePetsModel_Returns_PetsModel()
        {
            var result = PetsService.CreatePetsModel(CreateOwners());

            result.Should().BeEquivalentTo(CreatePetsModel());
        }
コード例 #24
0
        public void GetPetsClassified_DataRecievedTest()
        {
            List <Person> personDataObj = new List <Person>
            {
                new Person
                {
                    age  = 27, gender = GenderType.Male, name = "A1",
                    pets = new List <Pet>
                    {
                        new Pet
                        {
                            name = "AP1", type = PetType.Cat
                        }
                    }
                },
                new Person
                {
                    age  = 27, gender = GenderType.Male, name = "B1",
                    pets = new List <Pet>
                    {
                        new Pet
                        {
                            name = "BP2", type = PetType.Cat
                        },
                        new Pet
                        {
                            name = "BP1", type = PetType.Dog
                        }
                    }
                },
                new Person
                {
                    age  = 27, gender = GenderType.Male, name = "C1",
                    pets = null
                },
                new Person
                {
                    age  = 27, gender = GenderType.Female, name = "FA1",
                    pets = new List <Pet>
                    {
                        new Pet
                        {
                            name = "FAP4", type = PetType.Cat
                        },
                        new Pet
                        {
                            name = "FAP2", type = PetType.Dog
                        },
                        new Pet
                        {
                            name = "FAP1", type = PetType.Cat
                        }
                        ,
                        new Pet
                        {
                            name = "FAP3", type = PetType.Fish
                        }
                    }
                },
                new Person
                {
                    age  = 27, gender = GenderType.Female, name = "FB1",
                    pets = new List <Pet>
                    {
                        new Pet
                        {
                            name = "FBP1", type = PetType.Fish
                        },
                        new Pet
                        {
                            name = "FBP2", type = PetType.Dog
                        }
                        ,
                        new Pet
                        {
                            name = "FBP3", type = PetType.Cat
                        }
                    }
                }
            };

            Mock fileServiceMock = new Mock <IFileService>(MockBehavior.Default);

            fileServiceMock.As <IFileService>().Setup(x => x.GetFilePath()).Returns("ABC");

            Mock repositoryMock = new Mock <IPetsRepository>();

            repositoryMock.As <IPetsRepository>().Setup(x => x.GetData(It.IsAny <string>())).Returns(personDataObj);

            PetsService petservice = new PetsService(fileServiceMock.Object as IFileService, repositoryMock.Object as IPetsRepository);

            var result = petservice.GetPetsClassified();

            Assert.IsNotNull(result);
            Assert.IsTrue(result.MalePets.Count == 2);
            Assert.IsTrue(result.FemalePets.Count == 3);
        }