Beispiel #1
0
        public void JobOfferFacadeTest()
        {
            var unit = new UnitOfWork(GetInMemoryOptions());

            Seeder.Seed(unit);

            var jobOfferService = new JobOfferService(unit, new JobOfferQueryObject(unit));
            var jobOfferFacade  = new JobOfferFacade(unit, jobOfferService, mapper);

            var o = new JobOfferDto()
            {
                Name = "Lol"
            };

            jobOfferFacade.CreateAsync(o).Wait();
            var s = unit.JobOfferQuery.FilterByName("Lol").ExecuteAsync().Result.First();

            Assert.NotNull(s.Id);
            Assert.NotNull(jobOfferService.GetByIdAsync(s.Id ?? -1).Result);
            s.Id   = null;
            s.Name = "new lol";
            // Null ID get IMPOSSIBLE due to GetById accepting only int

            // Null ID edit/update
            var excp2 = Assert.Throws <AggregateException>(() =>
                                                           jobOfferFacade.UpdateAsync(mapper.Map <JobOfferDto>(s)).Wait());

            Assert.Contains("The property 'Id' on entity type 'JobOffer' is part of a key " +
                            "and so cannot be modified or marked as modified.",
                            excp2.Message);

            // Null ID delete
            excp2 = Assert.Throws <AggregateException>(() =>
                                                       jobOfferFacade.DeleteAsync(mapper.Map <JobOfferDto>(s)).Wait());
            Assert.Contains("JobOfferDto.Id can't be null!",
                            excp2.Message);

            // Addition with conflicting ID
            // This invalidates the database
            var id1 = unit.JobOfferRepository.GetById(1);

            Assert.NotNull(id1); // makes sure company with id 1 is already in database
            var excp = Assert.Throws <AggregateException>(() =>
                                                          jobOfferFacade.CreateAsync(new JobOfferDto()
            {
                Id = 1
            }).Wait());

            Assert.Contains("The instance of entity type 'JobOffer' cannot be tracked " +
                            "because another instance with the same key value for {'Id'} is already being tracked.",
                            excp.Message);

            unit.Dispose();
        }
Beispiel #2
0
        public void JobOfferServiceTests()
        {
            // Addition
            var unit            = new UnitOfWork(GetInMemoryOptions());
            var jobOfferService = new JobOfferService(unit, new JobOfferQueryObject(unit));
            var c1 = new JobOffer()
            {
                Name = "Lol"
            };

            Assert.Null(c1.Id);
            jobOfferService.CreateAsync(c1).Wait();
            unit.SaveChanges();
            Assert.NotEqual(-1, c1.Id);
            Assert.Equal("Lol", unit.JobOfferRepository.GetById(c1.Id ?? -1).Name);

            Seeder.Seed(unit);

            var c2 = new JobOffer()
            {
                Name = "New addition"
            };

            Assert.Null(c2.Id);
            jobOfferService.CreateAsync(c2).Wait();
            unit.SaveChanges();
            Assert.NotEqual(-1, c2.Id);
            Assert.Equal("New addition", unit.JobOfferRepository.GetById(c2.Id ?? -1).Name);

            // Getter
            Assert.Equal(unit.JobOfferRepository.GetById(1).Name, jobOfferService.GetByIdAsync(1).Result.Name);
            Assert.Equal(unit.JobOfferRepository.GetById(3).Name, jobOfferService.GetByIdAsync(3).Result.Name);
            Assert.Equal(unit.JobOfferRepository.GetById(2).Name, jobOfferService.GetByIdAsync(2).Result.Name);
            Assert.Equal(unit.JobOfferRepository.GetById(2).Description, jobOfferService.GetByIdAsync(2).Result.Description);
            Assert.Equal(unit.JobOfferRepository.GetById(2).CompanyId, jobOfferService.GetByIdAsync(2).Result.CompanyId);

            // Update
            c1.Name = "new Lol";
            jobOfferService.UpdateAsync(c1).Wait();
            unit.SaveChanges();
            Assert.Equal("new Lol", unit.JobOfferRepository.GetById(c1.Id ?? -1).Name);
            Assert.Equal("new Lol", unit.JobOfferQuery
                         .FilterByNameContains("Lol")
                         .ExecuteAsync()
                         .Result
                         .First()
                         .Name);


            var appleOffer = unit.JobOfferQuery.FilterByNameContains("Apple")
                             .OrderBy(o => o.Name)
                             .ExecuteAsync()
                             .Result
                             .First();

            Assert.NotNull(appleOffer);
            Assert.Equal("Software development at Apple", appleOffer.Name);
            appleOffer.Name = "Not Apple";
            jobOfferService.UpdateAsync(appleOffer).Wait();
            unit.SaveChanges();
            Assert.Equal("Not Apple", unit.JobOfferRepository.GetById(appleOffer.Id ?? -1).Name);
            Assert.Equal(new List <string>()
            {
                "Not Apple", "Well-paid position at Apple"
            }, unit.JobOfferQuery
                         .FilterByNameContains("Apple")
                         .OrderBy(o => o.Name)
                         .ExecuteAsync()
                         .Result
                         .Select(o => o.Name));

            // Delete
            int size = unit.JobOfferRepository.GetAll().Count();

            jobOfferService.DeleteAsync(c1.Id ?? -1).Wait();
            Assert.Equal(size - 1, unit.JobOfferRepository.GetAll().Count());
        }
Beispiel #3
0
 public async Task <JobOfferDto> GetByIdAsync(int id)
 {
     return(mapper.Map <JobOfferDto>(await jobOfferService.GetByIdAsync(id)));
 }