Beispiel #1
0
        public void RemoveRange_ShouldSucceed_WithMatchingIds(string petId1, string name1, int fkAnimalTypeId1, string petId2, string name2, int fkAnimalTypeId2)
        {
            //Arrange
            Guid petGuid1 = Guid.Parse(petId1);
            Guid petGuid2 = Guid.Parse(petId2);

            tblPet pet1 = new tblPet()
            {
                Id = petGuid1, Name = name1, FKAnimalTypeId = fkAnimalTypeId1
            };
            tblPet pet2 = new tblPet()
            {
                Id = petGuid2, Name = name2, FKAnimalTypeId = fkAnimalTypeId2
            };
            var entities = new tblPet[] { pet1, pet2 };

            _mockPetRepository.Setup(x => x.FindAsync(It.IsAny <Expression <Func <tblPet, bool> > >()))
            .ReturnsAsync(entities);

            //Act
            var result = _service.RemoveRange(entities.Select(x => x.Id));

            //Assert
            _mockPetRepository.Verify(x => x.FindAsync(It.IsAny <Expression <Func <tblPet, bool> > >()), Times.Once());
            _mockPetRepository.Verify(x => x.RemoveRange(entities), Times.Once());
            Assert.True(result.IsCompletedSuccessfully);
        }
        public IHttpActionResult PosttblPet(tblPet tblPet)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.tblPets.Add(tblPet);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (tblPetExists(tblPet.PetId))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = tblPet.PetId }, tblPet));
        }
Beispiel #3
0
        public void AddRange_ShouldSucceed_WithUnusedIds(string petId1, string name1, int fkAnimalTypeId1, string petId2, string name2, int fkAnimalTypeId2)
        {
            //Arrange
            Guid wrongGuid  = Guid.Parse("c3b922ac-a2bf-4b75-a9dc-5b3c44798264");
            Guid ownerGuid1 = Guid.Parse(petId1);
            Guid ownerGuid2 = Guid.Parse(petId2);

            _mockPetRepository.Setup(x => x.FindAsync(It.IsAny <Expression <Func <tblPet, bool> > >()))
            .ReturnsAsync(Enumerable.Empty <tblPet>());

            tblPet owner1 = new tblPet()
            {
                Id = ownerGuid1, Name = name1, FKAnimalTypeId = fkAnimalTypeId1
            };
            tblPet owner2 = new tblPet()
            {
                Id = ownerGuid2, Name = name2, FKAnimalTypeId = fkAnimalTypeId2
            };
            var entities = new tblPet[] { owner1, owner2 };

            //Act
            var result = _service.AddRange(entities);

            //Assert
            _mockPetRepository.Verify(x => x.FindAsync(It.IsAny <Expression <Func <tblPet, bool> > >()), Times.Once());
            _mockPetRepository.Verify(x => x.AddRange(entities), Times.Once());
            Assert.True(result.IsCompletedSuccessfully);
        }
        public IHttpActionResult PuttblPet(long id, tblPet tblPet)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != tblPet.PetId)
            {
                return(BadRequest());
            }

            db.Entry(tblPet).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!tblPetExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #5
0
        public void RemoveRange_ShouldFail_WhenRemovingAllNonExistingEntities(string petId1, string name1, int fkAnimalTypeId1, string petId2, string name2, int fkAnimalTypeId2)
        {
            //Arrange
            Guid   wrongGuid = Guid.Parse("c3b922ac-a2bf-4b75-a9dc-5b3c44798264");
            Guid   petGuid1  = Guid.Parse(petId1);
            Guid   petGuid2  = Guid.Parse(petId2);
            tblPet pet1      = new tblPet()
            {
                Id = petGuid1, Name = name1, FKAnimalTypeId = fkAnimalTypeId1
            };
            tblPet pet2 = new tblPet()
            {
                Id = petGuid2, Name = name2, FKAnimalTypeId = fkAnimalTypeId2
            };
            var entities = new tblPet[] { pet1, pet2 };

            _mockPetRepository.Setup(x => x.FindAsync(It.IsAny <Expression <Func <tblPet, bool> > >()))
            .ReturnsAsync(new tblPet[] { pet1 });

            //Act
            var task = _service.RemoveRange(entities.Select(x => x.Id));

            //Assert
            _mockPetRepository.Verify(x => x.FindAsync(It.IsAny <Expression <Func <tblPet, bool> > >()), Times.Once());
            Assert.ThrowsAsync <InvalidOperationException>(() => task);
            _mockPetRepository.Verify(x => x.RemoveRange(entities), Times.Never());
            Assert.False(task.IsCompletedSuccessfully);
        }
Beispiel #6
0
        public void Add_ShouldSucceed_WithUnusedId(string petId, string ownerId, string name, int fkAnimalTypeId)
        {
            //Arrange
            Guid   wrongGuid = Guid.Parse("c3b922ac-a2bf-4b75-a9dc-5b3c44798264");
            Guid   petGuid   = Guid.Parse(petId);
            Guid   ownerGuid = Guid.Parse(ownerId);
            tblPet pet       = new tblPet()
            {
                Id = petGuid, Name = name, FKAnimalTypeId = fkAnimalTypeId, FKOwnerId = ownerGuid
            };

            _mockPetRepository.SetupSequence(x => x.GetByIdAsync(petGuid))
            .ReturnsAsync((tblPet)null)
            .ReturnsAsync(pet);
            _mockOwnerRepository.Setup(x => x.GetByIdAsync(ownerGuid))
            .ReturnsAsync(new tblOwner {
                Id = ownerGuid, FirstName = "Joe", LastName = "Bob"
            });
            _mockPetRepository.Setup(x => x.Add(pet))
            .ReturnsAsync(pet);

            //Act
            var result = _service.Add(pet);

            //Assert
            //_mockPetRepository.Verify(x => x.GetByIdAsync(petGuid), Times.Once());
            _mockOwnerRepository.Verify(x => x.GetByIdAsync(ownerGuid), Times.Once());
            _mockPetRepository.Verify(x => x.Add(pet), Times.Once());
            _mockPetRepository.Verify(x => x.GetByIdAsync(petGuid), Times.Exactly(2));
            Assert.True(result.IsCompletedSuccessfully);
        }
        public IHttpActionResult GettblPet(long id)
        {
            tblPet tblPet = db.tblPets.Find(id);

            if (tblPet == null)
            {
                return(NotFound());
            }

            return(Ok(tblPet));
        }
        // HL - This function will create pet
        public bool CreatePet(tblPet pet)
        {
            pet.tblPetType = TblPetTypeDAO.Instance.GetType(pet.typeID);
            tblPet result = DBProvider.Instance.Db.tblPet.Add(pet);

            if (result != null)
            {
                DBProvider.Instance.Db.SaveChanges();
            }
            return(result != null);
        }
        //this function will set pet's status to false
        public bool DeletePet(int pID)
        {
            tblPet pet = DBProvider.Instance.Db.tblPet.Find(pID);

            if (pet != null)
            {
                pet.status = false;
                DBProvider.Instance.Db.SaveChanges();
                return(true);
            }
            return(false);
        }
        public IHttpActionResult DeletetblPet(long id)
        {
            tblPet tblPet = db.tblPets.Find(id);

            if (tblPet == null)
            {
                return(NotFound());
            }

            db.tblPets.Remove(tblPet);
            db.SaveChanges();

            return(Ok(tblPet));
        }
Beispiel #11
0
        //TODO: "Get food for pet" method

        public async Task <tblPet> Add(tblPet entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("Cannot add blank pet");
            }
            if (await _petRepository.GetByIdAsync(entity.Id).ConfigureAwait(false) != null)
            {
                throw new ArgumentException("Pet already exists.");
            }
            if (entity.FKOwnerId.HasValue && await _ownerRepository.GetByIdAsync(entity.FKOwnerId.Value) == null)
            {
                throw new InvalidOperationException("Unable to add pet for owner that doesn't exist.");
            }

            var createdEntity = await _petRepository.Add(entity);

            return(await _petRepository.GetByIdAsync(createdEntity.Id));
        }
Beispiel #12
0
        public void Remove_ShouldFail_IfPetDoesntExists(string petId, string name, int fkAnimalTypeId)
        {
            //Arrange
            Guid   wrongGuid = Guid.Parse("c3b922ac-a2bf-4b75-a9dc-5b3c44798264");
            Guid   guid      = Guid.Parse(petId);
            tblPet fakePet   = new tblPet {
                Id = guid, Name = name, FKAnimalTypeId = fkAnimalTypeId
            };

            _mockPetRepository.Setup(x => x.GetByIdAsync(guid))
            .ReturnsAsync((tblPet)null);

            //Act
            var task = _service.Remove(guid);

            //Assert
            Assert.ThrowsAnyAsync <InvalidOperationException>(() => task);
            _mockPetRepository.Verify(x => x.Remove(It.IsAny <tblPet>()), Times.Never());
        }
Beispiel #13
0
 private void dgvPets_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
 {
     if (e.RowIndex != -1)
     {
         int pID = (int)dgvPets.CurrentRow.Cells[0].Value;
         try {
             tblPet pet = TblPetDAO.Instance.GetPetByID(pID);
             this.Hide();
             PetDetail frm = new PetDetail(false, pet);
             frm.ShowDialog();
             this.Show();
             if (frm.IsAction)
             {
                 RefreshDgv();
             }
         } catch (Exception) {
         }
     }
 }
Beispiel #14
0
        public void Add_ShouldFail_IfPetAlreadyExists(string petId, string name, int fkAnimalTypeId)
        {
            //Arrange
            Guid   guid    = Guid.Parse(petId);
            tblPet fakePet = new tblPet {
                Id = guid, Name = name, FKAnimalTypeId = fkAnimalTypeId
            };

            _mockPetRepository.Setup(x => x.GetByIdAsync(guid))
            .ReturnsAsync(fakePet);

            //Act
            var task = _service.Add(fakePet);

            //Assert
            Assert.ThrowsAnyAsync <InvalidOperationException>(() => task);
            _mockPetRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>()), Times.Once());
            _mockPetRepository.Verify(x => x.Add(It.IsAny <tblPet>()), Times.Never());
            Assert.False(task.IsCompletedSuccessfully);
        }
Beispiel #15
0
        public void Remove_ShouldSucceed_WithMatchingId(string petId)
        {
            //Arrange
            Guid   petGuid     = Guid.Parse(petId);
            tblPet existingPet = new tblPet()
            {
                Id             = petGuid,
                Name           = "bill",
                FKAnimalTypeId = 0
            };

            _mockPetRepository.Setup(x => x.GetByIdAsync(petGuid))
            .ReturnsAsync(existingPet);

            //Act
            var result = _service.Remove(petGuid);

            //Assert
            _mockPetRepository.Verify(x => x.Remove(existingPet), Times.Once());
            Assert.True(result.IsCompletedSuccessfully);
        }
        // HL - This function will update pet
        public bool UpdatePet(tblPet pet)
        {
            var update = (from tblPet in DBProvider.Instance.Db.tblPet where tblPet.id == pet.id select tblPet)
                         .SingleOrDefault();

            if (update != null)
            {
                update.name         = pet.name;
                update.age          = pet.age;
                update.image        = pet.image;
                update.gender       = pet.gender;
                update.description  = pet.description;
                update.isSterilized = pet.isSterilized;
                update.adopter      = pet.adopter;
                update.furColor     = pet.furColor;
                update.typeID       = pet.typeID;
                update.dateAdopted  = pet.dateAdopted;
                DBProvider.Instance.Db.SaveChangesAsync();
                return(true);
            }
            return(false);
        }
Beispiel #17
0
        public void Add_ShouldFail_WithMismatchingOwnerId(string petId, string ownerId, string name, int fkAnimalTypeId)
        {
            //Arrange
            Guid wrongGuid = Guid.Parse("c3b922ac-a2bf-4b75-a9dc-5b3c44798264");
            Guid petGuid   = Guid.Parse(petId);
            Guid ownerGuid = Guid.Parse(ownerId);

            _mockPetRepository.Setup(x => x.GetByIdAsync(petGuid))
            .ReturnsAsync((tblPet)null);

            tblPet pet = new tblPet()
            {
                Id = petGuid, Name = name, FKAnimalTypeId = fkAnimalTypeId, FKOwnerId = ownerGuid
            };
            //Act
            var result = _service.Add(pet);

            //Assert
            _mockPetRepository.Verify(x => x.GetByIdAsync(petGuid), Times.Once());
            _mockOwnerRepository.Verify(x => x.GetByIdAsync(ownerGuid), Times.Once());
            Assert.ThrowsAsync <InvalidOperationException>(() => result);
            _mockPetRepository.Verify(x => x.Add(pet), Times.Never());
            Assert.False(result.IsCompletedSuccessfully);
        }
Beispiel #18
0
 public async Task Update(tblPet pet)
 {
     _petRepository.Update(pet);
 }