public void CreatesCorrectIdentifiers()
        {
            var testValue         = Random.String();
            var nhsNumber         = Random.String();
            var newPersonIdentity = CreateRepository(createContext).CreatePerson(
                new[]
            {
                Identifiers.NhsNumber(nhsNumber),
                Identifiers.PersonIdentifier(testValue, testIdentifierDefinition)
            },
                defaultAuthority.ToAuthority()
                );

            createContext.SaveChanges();

            var newPerson = readContext.People.Find(newPersonIdentity.Id);

            newPerson.Should().NotBeNull();

            var createdIdentifiers = readContext.PersonIdentifiers.Where(_ => _.Person == newPerson).ToArray();

            createdIdentifiers.Should().HaveCount(2);

            createdIdentifiers.Should().NotContain(_ => _.Deleted != null);
            createdIdentifiers.Should()
            .ContainSingleIdentifierValue(testIdentifierDefinition, testValue)
            .And
            .ContainSingleIdentifierValue(Identifiers.Definitions.NhsNumber, nhsNumber);
        }
        public void UpdatesIdentifiersCorrectly()
        {
            var localRepository = CreateRepository(updateContext);
            var newTestValue    = Random.String();

            localRepository.UpdatePerson(
                new PersonIdentity(personOne.Id),
                new[]
            {
                Identifiers.PersonIdentifier(newTestValue, testIdentifierDefinition),
                Identifiers.NhsNumber(personOneNhsNumber)
            },
                defaultAuthority.ToAuthority());
            updateContext.SaveChanges();


            var identifierEntities = readContext.PersonIdentifiers.Where(_ => _.Person.Id == personOne.Id).ToArray();

            identifierEntities.Should()
            .ContainSingleIdentifierValue(testIdentifierDefinition, personTwoNhsNumber, deleted: true)
            .And
            .ContainSingleIdentifierValue(testIdentifierDefinition, newTestValue)
            .And
            .ContainSingleIdentifierValue(Identifiers.Definitions.NhsNumber, personOneNhsNumber);
        }
        public void FindsAConsentedPersonWhenConsentIsRequired()
        {
            var personOneAgain = createContext.Find <PersonEntity>(personOne.Id);
            var study          = new StudyEntity {
                Name = Random.String()
            };
            var subject = new StudySubjectEntity
            {
                Person = personOneAgain, Study = study, SubjectIdentifier = Random.String()
            };
            var consent = new ConsentEntity
            {
                DateProvided = DateTime.Today, GivenBy = personOneAgain, StudySubject = subject
            };

            createContext.AddRange(study, subject, consent);
            createContext.SaveChanges();

            repository.FindPersonBy(
                new PersonIdentifierSpecification(Identifiers.NhsNumber(personOneNhsNumber))
                )?.Id.Should().Be(personOne.Id);

            repository.FindPersonBy(
                new CompositePersonSpecification(
                    new PersonIdentifierSpecification(Identifiers.NhsNumber(personOneNhsNumber)),
                    new ConsentedPersonSpecification(new StudyIdentity(study.Id))
                    )
                ).Should().Be((PersonIdentity)personOne.Id);
        }
Example #4
0
        public void StoresConsentHeaderWhenAddingConsent()
        {
            var subjectIdentifier = Random.String();

            var(personId, studySubjectEntity, studySubject) = CreateStudySubject(subjectIdentifier);

            var dateGiven = Random.Date().Date;


            var consent = CreateConsentRepository(updateContext)
                          .AddConsent(
                new Common.Consent.Consent(studySubject, dateGiven, personId, null)
                );

            updateContext.SaveChanges();


            var consentEntity = readContext.Set <ConsentEntity>()
                                .Include(_ => _.GivenBy)
                                .Include(_ => _.StudySubject)
                                .ThenInclude(_ => _.Study)
                                .SingleOrDefault(_ => _.Id == consent.Id);

            Assert.Equal(studySubjectEntity.Id, consentEntity.StudySubject.Id);
            Assert.Equal(personId.Id, consentEntity.GivenBy.Id);
            Assert.Equal(dateGiven, consentEntity.DateProvided);
            Assert.Null(consentEntity.DateWithdrawn);
        }
        public void CorrectlyLoadsCurrentIdentifiers()
        {
            var localRepository = CreateRepository(updateContext);
            var newTestValue    = Random.String();

            localRepository.UpdatePerson(
                new PersonIdentity(personOne.Id),
                new[]
            {
                Identifiers.PersonIdentifier(newTestValue, testIdentifierDefinition)
            },
                defaultAuthority.ToAuthority());
            updateContext.SaveChanges();

            var personIdentifiers = CreateRepository(readContext).GetPersonIdentifiers(personOne.Id).ToArray();

            personIdentifiers.Should().HaveCount(2, "because there are two active identifiers");


            personIdentifiers.Should().ContainSingle(
                _ => Equals(_.Value.Value, personOneNhsNumber) && _.Definition == Identifiers.Definitions.NhsNumber);


            personIdentifiers.Should().ContainSingle(
                _ => Equals(_.Value.Value, newTestValue) && _.Definition == testIdentifierDefinition);
        }
        /// <inheritdoc />
        public IdentityRepositoryTests(ITestOutputHelper outputHelper, DatabaseFixture fixture) : base(
                outputHelper,
                fixture)
        {
            Formatter.AddFormatter(PersonIdentifierFormatter.Instance);
            Formatter.AddFormatter(DictionaryIdentifierFormatter.Instance);

            personOne        = new PersonEntity();
            personTwo        = new PersonEntity();
            defaultAuthority = new AuthorityEntity("Default " + Random.String(), 1000, Random.String());

            Context.AddRange(personOne, personTwo);
            Context.Add(defaultAuthority);

            testIdentifierDefinition = Identifiers.Definitions.String("Test");
            Context.AddRange(
                NhsNumberEntity(personOne, personOneNhsNumber),
                NhsNumberEntity(personTwo, personTwoNhsNumber),
                IdentifierEntity(personOne, testIdentifierDefinition, personTwoNhsNumber, defaultAuthority)
                );
            Context.SaveChanges();

            Assert.NotNull(readContext.People.Find(personOne.Id));
            Assert.NotNull(readContext.People.Find(personTwo.Id));
            Assert.Equal(2, readContext.Set <PersonIdentifierEntity>().Count(_ => _.Person == personOne));
            Assert.Single(readContext.Set <PersonIdentifierEntity>().Where(_ => _.Person == personTwo));

            repository = CreateRepository(CreateNewContextInSameTransaction());
        }
Example #7
0
        public void CanAddAStudySubject()
        {
            var person = createContext.Add(new PersonEntity()).Entity;

            createContext.SaveChanges();
            var personId = new PersonIdentity(person.Id);

            var subjectIdentifier = Random.String();
            var addedSubject      = CreateStudySubjectRepository(updateContext).AddStudySubject(
                new StudySubject(studyId, subjectIdentifier, personId));

            updateContext.SaveChanges();

            using (new AssertionScope())
            {
                addedSubject.Should().NotBeNull();
                addedSubject.StudyId.Should().Be(studyId);
                addedSubject.PersonId.Should().Be(personId);
                addedSubject.SubjectIdentifier.Should().Be(subjectIdentifier);
            }

            var studySubjectEntity = readContext.Set <StudySubjectEntity>()
                                     .Include(_ => _.Study)
                                     .Include(_ => _.Person)
                                     .SingleOrDefault(
                _ => _.Study.Id == addedSubject.StudyId && _.Person.Id == addedSubject.PersonId);

            using (new AssertionScope())
            {
                studySubjectEntity.Should().NotBeNull();
                studySubjectEntity.SubjectIdentifier.Should().Be(subjectIdentifier);
                studySubjectEntity.Study.Id.Should().Be(studyId.Id);
                studySubjectEntity.Person.Id.Should().Be(personId.Id);
            }
        }
Example #8
0
            /// <inheritdoc />
            public WhenGettingConsentedSubjectIdentifiers_ForAKnownStudy_OnlyReturnsSubjectIdentifiers()
            {
                studySubjects = Enumerable.Range(0, 100)
                                .Select((_, index) =>
                                        new StudySubject(StudyId, Random.String(10), (PersonIdentity)(index + 25)))
                                .ToArray();


                A.CallTo(() => StudySubjects.GetConsentedSubjects(StudyId))
                .Returns(studySubjects);

                result = CreateConsentController(Consents).GetConsentedSubjectsForStudy(StudyId);
            }
Example #9
0
            public WhenTryingToRecordConsent_ForAnExistingStudySubject_WithActiveConsentForDifferentIdentifiers()
            {
                var givenEvidence = new [] { A.Dummy <Evidence>() };
                var dateGiven     = 3.November(1472);

                existingConsent = new Consent(StudySubject, dateGiven, Random.Long(), givenEvidence);
                A.CallTo(
                    () =>
                    Consents.FindActiveConsent(
                        StudySubject))
                .Returns(null);

                RecordConsent(Array.Empty <IIdentifierValueDto>(), dateGiven);
            }
        public void SavesConsent()
        {
            var consentContext = Server.Host.Services.GetService <ConsentContext>();

            var study = consentContext.Add(new StudyEntity {
                Name = Random.String()
            }).Entity;
            var person = consentContext.Add(new PersonEntity()).Entity;

            consentContext.SaveChanges();

            var result = ApiClient.PutConsent(
                new ConsentSpecification
            {
                StudyId           = study.Id,
                DateGiven         = Random.Date().Date,
                GivenBy           = person.Id,
                PersonId          = person.Id,
                SubjectIdentifier = Random.String(15),
                Evidence          = new []
                {
                    Evidences.ClientMedwayDto(status: "Competent", givenBy: "Self", takenBy: "Jackson Pollock"),
                }
            });

            Assert.NotNull(result);
            var newConsentId  = Assert.IsType <long>(result);
            var consentEntity = consentContext.Set <ConsentEntity>()
                                .Include(_ => _.StudySubject)
                                .ThenInclude(_ => _.Study)
                                .Include(_ => _.StudySubject)
                                .ThenInclude(_ => _.Person)
                                .Include(_ => _.GivenBy)
                                .Single(_ => _.Id == newConsentId);

            Assert.NotNull(consentEntity);

            var evidence = consentContext.Set <GivenEvidenceEntity>().SingleOrDefault(_ => _.Consent.Id == newConsentId);

            using (new AssertionScope())
            {
                evidence.Should().NotBeNull();
                evidence.Value.Should().Be(
                    "<medway><competent-status>Competent</competent-status><consent-given-by>Self</consent-given-by><consent-taken-by>Jackson Pollock</consent-taken-by></medway>");
                evidence.Type.Should().Be("medway");
            }
        }
        public void CanPersistCompositeIdentifiers()
        {
            var name      = Identifiers.Name("Francis", "Drake");
            var nhsNumber = Identifiers.NhsNumber(Random.String());
            var person    = CreateRepository(createContext).CreatePerson(
                new[] { name, nhsNumber },
                defaultAuthority.ToAuthority());

            createContext.SaveChanges();

            var personIdentifiers = CreateRepository(readContext).GetPersonIdentifiers(person);

            personIdentifiers.Should().Contain(name)
            .And.Contain(nhsNumber)
            .And.HaveCount(2)
            .And.OnlyHaveUniqueItems();
        }
        public void CanFindAPersonByAgencyId()
        {
            var agency = createContext.Add(new AgencyEntity("test", "test")).Entity;

            createContext.SaveChanges();
            var agencyId = createContext.Add(
                new PersonAgencyId
            {
                AgencyId = agency.Id, PersonId = personTwo.Id, SpecificId = Random.String()
            }).Entity;

            createContext.SaveChanges();

            var found = repository.FindPersonBy(new AgencyIdentifierPersonSpecification("test", agencyId.SpecificId));

            Assert.Equal(personTwo, found);
        }
        public void GetAgencyFieldAndSubfields()
        {
            var field1 = createContext.Add(
                new IdentifierDefinitionEntity("first field", "field-1:composite(one:string,two:string)")).Entity;
            var agencyName = Random.String(15);

            createContext.Add(
                new AgencyEntity(Random.String(), agencyName)
            {
                Fields = { new AgencyFieldEntity {
                               Identifier = field1, Order = 1, Subfields = "one"
                           } }
            });
            createContext.SaveChanges();

            var agency = repository.GetAgency(agencyName);

            agency.Fields.Should().BeEquivalentTo("field-1::one");
        }
Example #14
0
        public void StoresConsentGivenEvidenceWhenAddingConsent()
        {
            var subjectIdentifier = Random.String();

            var(personId, _, studySubject) = CreateStudySubject(subjectIdentifier);

            var dateGiven = Random.Date().Date;


            var evidence           = Evidences.MedwayEvidence(competencyStatus: "Competent", takenBy: "Nurse Randall");
            var marshalledEvidence =
                new CompositeIdentifierXmlMarshaller <Evidence, EvidenceDefinition>(KnownEvidence.Medway)
                .MarshallToXml(evidence)
                .ToString(SaveOptions.DisableFormatting);

            var consent = CreateConsentRepository(updateContext)
                          .AddConsent(
                new Common.Consent.Consent(studySubject, dateGiven, personId, new [] { evidence })
                );

            updateContext.SaveChanges();


            var savedConsent = readContext.Set <ConsentEntity>().AsNoTracking()
                               .Where(_ => _.Id == consent.Id)
                               .Include(_ => _.GivenEvidence)
                               .SingleOrDefault();

            savedConsent.Should().NotBeNull();
            savedConsent.GivenEvidence.Should().ContainSingle();
            var storedEvidence = savedConsent.GivenEvidence.SingleOrDefault();

            storedEvidence.Should().NotBeNull().And.BeOfType <GivenEvidenceEntity>();

            Assert.Equal(KnownEvidence.Medway.SystemName, storedEvidence.Type);

            Assert.Equal(marshalledEvidence,
                         storedEvidence.Value);
            Assert.NotNull(storedEvidence.Consent);
        }
        public void CanSearchForSimpleIdentifiers()
        {
            var nhsNumber           = Random.String();
            var nhsNumberIdentifier = Identifiers.NhsNumber(nhsNumber);

            var createRepository = CreateRepository(createContext);
            var person           = createRepository.CreatePerson(new[] { nhsNumberIdentifier }, defaultAuthority.ToAuthority());

            createContext.SaveChanges();


            var found = People(readContext)
                        .Search(
                readContext,
                new HasIdentifiersCriteria(
                    new IdentifierSearch
            {
                IdentifierName = nhsNumberIdentifier.Definition.SystemName, Value = nhsNumber
            }))
                        .ToArray()
            ;

            found.Should().OnlyContain(_ => _.Id == person.Id);
        }
Example #16
0
        /// <inheritdoc />
        public ConsentRepositoryTests(ITestOutputHelper outputHelper, DatabaseFixture fixture) : base(outputHelper, fixture)
        {
            readContext   = CreateNewContextInSameTransaction();
            updateContext = CreateNewContextInSameTransaction();
            createContext = CreateNewContextInSameTransaction();


            study = createContext.Studies.Add(new StudyEntity {
                Name = Random.String()
            }).Entity;

            var consentedPerson = createContext.People.Add(new PersonEntity()).Entity;

            consentedStudySubject = createContext.Add(new StudySubjectEntity {
                Study = study, Person = consentedPerson, SubjectIdentifier = "Consented"
            }).Entity;
            createContext.Add(
                new ConsentEntity
            {
                StudySubject  = consentedStudySubject,
                DateProvided  = 1.December(1965),
                DateWithdrawn = 1.January(1966),
                GivenBy       = consentedPerson
            });

            activeConsent = createContext.Add(
                new ConsentEntity
            {
                StudySubject = consentedStudySubject,
                DateProvided = 1.February(1966),
                GivenBy      = consentedPerson
            }).Entity;

            var unconsentedPerson = createContext.People.Add(new PersonEntity()).Entity;

            createContext.Add(
                new StudySubjectEntity {
                Study = study, Person = unconsentedPerson, SubjectIdentifier = "Unconsented"
            });

            var withdrawnConsent = createContext.Add(new PersonEntity()).Entity;
            var withdrawnSubject = createContext.Add(
                new StudySubjectEntity {
                Study = study, Person = withdrawnConsent, SubjectIdentifier = "Withdrawn"
            }).Entity;

            withdrawnSubjectWithdrawnDate = 5.January(2018);
            createContext.Add(new ConsentEntity
            {
                StudySubject  = withdrawnSubject,
                DateProvided  = 1.December(2017),
                DateWithdrawn = withdrawnSubjectWithdrawnDate,
                GivenBy       = withdrawnConsent
            });


            createContext.SaveChanges();

            studyId             = new StudyIdentity(study.Id);
            consentedPersonId   = consentedPerson;
            unconsentedPersonId = unconsentedPerson;
            withdrawnPersonId   = withdrawnConsent;

            consents = CreateConsentRepository(readContext);

            studySubjects = CreateStudySubjectRepository(readContext);
            studies       = CreateStudyRepository(readContext);
        }