public void WhenRelationshipWithTheSameTypeAlreadyExistsThrowsPersonalRelationshipAlreadyExistsException() { var sameTypeAsRequest = _typeInRequest; _person.PersonalRelationships = new List <PersonalRelationship>() { PersonalRelationshipsHelper.CreatePersonalRelationship(_person, _otherPerson, sameTypeAsRequest) }; _mockDatabaseGateway.Setup(x => x.GetPersonWithPersonalRelationshipsByPersonId(It.IsAny <long>(), It.IsAny <bool>())) .Returns(_person); Action act = () => _personalRelationshipsUseCase.ExecutePost(_request); act.Should().Throw <PersonalRelationshipAlreadyExistsException>() .WithMessage($"Personal relationship with 'type' of '{_request.Type}' already exists."); }
public void SetUp() { _mockDatabaseGateway = new Mock <IDatabaseGateway>(); _personalRelationshipsUseCase = new PersonalRelationshipsUseCase(_mockDatabaseGateway.Object); _mockDatabaseGateway.Setup(x => x.GetPersonalRelationshipTypeByDescription(_typeInRequest.Description)) .Returns(_typeInRequest); _request = PersonalRelationshipsHelper.CreatePersonalRelationshipRequest(type: _typeInRequest.Description); _person = TestHelpers.CreatePerson(_request.PersonId); _otherPerson = TestHelpers.CreatePerson(_request.OtherPersonId); _typeInExistingRelationship = PersonalRelationshipsHelper.CreatePersonalRelationshipType("partner"); _relationship = PersonalRelationshipsHelper.CreatePersonalRelationship(_person, _otherPerson, _typeInExistingRelationship); _rel_details = PersonalRelationshipsHelper.CreatePersonalRelationshipDetail(_relationship.Id, "some details for the relationship"); }
public void WhenThereAreMultipleRelationshipsReturnsAllRelevantPersonalRelationships() { var(person, _, _, personalRelationshipType, _) = PersonalRelationshipsHelper.SavePersonWithPersonalRelationshipToDatabase(DatabaseContext); var anotherPerson = TestHelpers.CreatePerson(); var anotherRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(person, anotherPerson, personalRelationshipType, id: 1); var andAnotherPerson = TestHelpers.CreatePerson(); var andAnotherRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(person, andAnotherPerson, personalRelationshipType, hasEnded: true, id: 2); DatabaseContext.Persons.Add(anotherPerson); DatabaseContext.Persons.Add(andAnotherPerson); DatabaseContext.PersonalRelationships.Add(anotherRelationship); DatabaseContext.PersonalRelationships.Add(andAnotherRelationship); DatabaseContext.SaveChanges(); var response = _databaseGateway.GetPersonWithPersonalRelationshipsByPersonId(person.Id); response.PersonalRelationships.Should().HaveCount(2); }
public void SetUp() { _mockDatabaseGateway = new Mock <IDatabaseGateway>(); _personalRelationshipsUseCase = new PersonalRelationshipsUseCase(_mockDatabaseGateway.Object); _mockDatabaseGateway.Setup(x => x.GetPersonalRelationshipTypeByDescription(_typeInRequest.Description)) .Returns(_typeInRequest); var typeInExistingRelationship = PersonalRelationshipsHelper.CreatePersonalRelationshipType("partner"); _mockDatabaseGateway.Setup(x => x.GetPersonalRelationshipTypeByDescription(typeInExistingRelationship.Description)) .Returns(typeInExistingRelationship); _request = PersonalRelationshipsHelper.CreatePersonalRelationshipRequest(type: _typeInRequest.Description); _person = TestHelpers.CreatePerson(_request.PersonId); _otherPerson = TestHelpers.CreatePerson(_request.OtherPersonId); var personalRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(_person, _otherPerson, typeInExistingRelationship); _person.PersonalRelationships = new List <PersonalRelationship>() { personalRelationship }; _mockDatabaseGateway.Setup(x => x.GetPersonsByListOfIds(It.IsAny <List <long> >())) .Returns(new List <Person>() { _person, _otherPerson }); _mockDatabaseGateway.Setup(x => x.GetPersonWithPersonalRelationshipsByPersonId(It.IsAny <long>(), It.IsAny <bool>())) .Returns(_person); _mockDatabaseGateway.Setup(x => x.CreatePersonalRelationship(It.IsAny <CreatePersonalRelationshipRequest>())) .Returns(personalRelationship); _mockDatabaseGateway.Setup(x => x.GetWorkerByEmail(It.IsAny <string>())) .Returns(TestHelpers.CreateWorker(email: _request.CreatedBy)); }
public void WhenThereAreRelationshipsDoesNotReturnRelationshipsAgainstPersonsMarkedForDeletion() { var(person, _, _, personalRelationshipType, _) = PersonalRelationshipsHelper.SavePersonWithPersonalRelationshipToDatabase(DatabaseContext); var anotherPerson = TestHelpers.CreatePerson(); var anotherRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(person, anotherPerson, personalRelationshipType, id: 1); var personMarkedForDeletion = TestHelpers.CreatePerson(); personMarkedForDeletion.MarkedForDeletion = true; var andAnotherRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(person, personMarkedForDeletion, personalRelationshipType, id: 2); DatabaseContext.Persons.Add(anotherPerson); DatabaseContext.Persons.Add(personMarkedForDeletion); DatabaseContext.PersonalRelationships.Add(anotherRelationship); DatabaseContext.PersonalRelationships.Add(andAnotherRelationship); DatabaseContext.SaveChanges(); var response = _databaseGateway.GetPersonWithPersonalRelationshipsByPersonId(person.Id); response.PersonalRelationships.Should().HaveCount(2); }
public void SetsTheMarkedForDeletionFlagToFalseForPersonAndRelatedEntitiesAndRemovesTheRecordFromDeletedPersonRecordTable() { //person to be restored, new master record and relationship var(deletedPerson, newMasterPersonRecord, personalRelationship, personalRelationshipType, _) = PersonalRelationshipsHelper.SavePersonWithPersonalRelationshipToDatabase(DatabaseContext); //address var address = DatabaseGatewayHelper.CreateAddressDatabaseEntity(personId: deletedPerson.Id); address.MarkedForDeletion = true; DatabaseContext.Addresses.Add(address); //telephone numbers var telephoneNumber = DatabaseGatewayHelper.CreatePhoneNumberEntity(personId: deletedPerson.Id); address.MarkedForDeletion = true; DatabaseContext.PhoneNumbers.Add(telephoneNumber); //other names var otherName = DatabaseGatewayHelper.CreatePersonOtherNameDatabaseEntity(personId: deletedPerson.Id); otherName.MarkedForDeletion = true; DatabaseContext.PersonOtherNames.Add(otherName); //allocations var allocation = TestHelpers.CreateAllocation(personId: (int)deletedPerson.Id); allocation.MarkedForDeletion = true; DatabaseContext.Allocations.Add(allocation); //warning notes var warningNote = TestHelpers.CreateWarningNote(personId: deletedPerson.Id); warningNote.MarkedForDeletion = true; DatabaseContext.WarningNotes.Add(warningNote); //inverse relationship (type not handled for simplicity) var inversePersonalRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(newMasterPersonRecord, deletedPerson, personalRelationshipType); inversePersonalRelationship.MarkedForDeletion = true; DatabaseContext.PersonalRelationships.Add(inversePersonalRelationship); //record marked as soft deleted DatabaseContext.DeletedPersonRecords.Add(new DeletedPersonRecord() { DeletedId = deletedPerson.Id, MasterId = newMasterPersonRecord.Id }); DatabaseContext.SaveChanges(); //TODO: wrap into transaction when running manually (BEGIN; <SCRIPT> END;) //TODO: this is designed to be run against individual IDs. Replace the {deletedPerson.Id} with appropriate MosaicId (long) DatabaseContext.Database.ExecuteSqlInterpolated($@" UPDATE dbo.dm_persons SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id}; UPDATE dbo.dm_addresses SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id}; UPDATE dbo.dm_telephone_numbers SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id}; UPDATE dbo.sccv_person_other_name SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id}; UPDATE dbo.sccv_allocations_combined SET marked_for_deletion = false WHERE mosaic_id = {deletedPerson.Id}; UPDATE dbo.sccv_warning_note SET marked_for_deletion = false WHERE person_id = {deletedPerson.Id}; UPDATE dbo.sccv_personal_relationship SET marked_for_deletion = false WHERE fk_person_id = {deletedPerson.Id}; UPDATE dbo.sccv_personal_relationship SET marked_for_deletion = false WHERE fk_other_person_id = {deletedPerson.Id}; DELETE from dbo.sccv_deleted_person_record where deleted_person_id = {deletedPerson.Id}; "); DatabaseContext.Persons.First(x => x.Id == deletedPerson.Id).MarkedForDeletion.Should().BeFalse(); DatabaseContext.Addresses.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse(); DatabaseContext.PhoneNumbers.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse(); DatabaseContext.PersonOtherNames.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse(); DatabaseContext.Allocations.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse(); DatabaseContext.WarningNotes.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse(); DatabaseContext.PersonalRelationships.First(x => x.PersonId == deletedPerson.Id).MarkedForDeletion.Should().BeFalse(); DatabaseContext.PersonalRelationships.First(x => x.OtherPersonId == newMasterPersonRecord.Id).MarkedForDeletion.Should().BeFalse(); DatabaseContext.DeletedPersonRecords.Any(x => x.DeletedId == deletedPerson.Id).Should().BeFalse(); }
public void SetsTheMarkedForDeletionFlagForPersonAndRelatedEntitiesAndUpdatesTheDeletedPersonRecordsTableWithCorrectRecords() { //person to be deleted, new master record and relationship var(personToBeDeleted, newMasterPersonRecord, personalRelationship, personalRelationshipType, _) = PersonalRelationshipsHelper.SavePersonWithPersonalRelationshipToDatabase(DatabaseContext); //address var address = DatabaseGatewayHelper.CreateAddressDatabaseEntity(personId: personToBeDeleted.Id); DatabaseContext.Addresses.Add(address); //telephone numbers var telephoneNumber = DatabaseGatewayHelper.CreatePhoneNumberEntity(personId: personToBeDeleted.Id); DatabaseContext.PhoneNumbers.Add(telephoneNumber); //other names var otherName = DatabaseGatewayHelper.CreatePersonOtherNameDatabaseEntity(personId: personToBeDeleted.Id); DatabaseContext.PersonOtherNames.Add(otherName); //allocations var allocation = TestHelpers.CreateAllocation(personId: (int)personToBeDeleted.Id); DatabaseContext.Allocations.Add(allocation); //warning notes var warningNote = TestHelpers.CreateWarningNote(personId: personToBeDeleted.Id); DatabaseContext.WarningNotes.Add(warningNote); //inverse relationship (type not handled for simplicity) var inversePersonalRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(newMasterPersonRecord, personToBeDeleted, personalRelationshipType); DatabaseContext.PersonalRelationships.Add(inversePersonalRelationship); //record marked for deletion DatabaseContext.PersonRecordsToBeDeleted.Add(new PersonRecordToBeDeleted() { IdToDelete = personToBeDeleted.Id, MasterId = newMasterPersonRecord.Id }); DatabaseContext.SaveChanges(); DatabaseContext.Database.ExecuteSqlInterpolated(_queryUnderTest); //flags DatabaseContext.Persons.First(x => x.Id == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue(); DatabaseContext.Addresses.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue(); DatabaseContext.PhoneNumbers.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue(); DatabaseContext.PersonOtherNames.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue(); DatabaseContext.Allocations.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue(); DatabaseContext.WarningNotes.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue(); DatabaseContext.PersonalRelationships.First(x => x.PersonId == personToBeDeleted.Id).MarkedForDeletion.Should().BeTrue(); DatabaseContext.PersonalRelationships.First(x => x.OtherPersonId == newMasterPersonRecord.Id).MarkedForDeletion.Should().BeTrue(); //record in deleted person record table var expectedDeletedPersonRecordEntry = new DeletedPersonRecord() { DeletedId = personToBeDeleted.Id, MasterId = newMasterPersonRecord.Id, Timestamp = DateTime.Now.ToUniversalTime() }; DatabaseContext.DeletedPersonRecords.First(x => x.DeletedId == personToBeDeleted.Id).MasterId.Should().Be(expectedDeletedPersonRecordEntry.MasterId); DatabaseContext.DeletedPersonRecords.First(x => x.DeletedId == personToBeDeleted.Id).DeletedId.Should().Be(expectedDeletedPersonRecordEntry.DeletedId); DatabaseContext.DeletedPersonRecords.First(x => x.DeletedId == personToBeDeleted.Id).Timestamp.Should().BeCloseTo(expectedDeletedPersonRecordEntry.Timestamp, precision: 5000); }