public void CreatesAffiliation_WithEstablishmentId()
            {
                Affiliation outEntity       = null;
                const int   personId        = 13;
                const int   establishmentId = 13;
                var         person          = new Person
                {
                    RevisionId = personId,
                };
                var command = new CreateAffiliationCommand
                {
                    PersonId        = personId,
                    EstablishmentId = establishmentId,
                };
                var entities = new Mock <ICommandEntities>(MockBehavior.Strict).Initialize();

                entities.Setup(m => m.Get <Person>()).Returns(new[] { person }.AsQueryable);
                entities.Setup(m => m.Create(It.Is(AffiliationBasedOn(command))))
                .Callback((Entity entity) => outEntity = (Affiliation)entity);
                var handler = new CreateAffiliationHandler(entities.Object);

                handler.Handle(command);

                outEntity.ShouldNotBeNull();
                outEntity.EstablishmentId.ShouldEqual(command.EstablishmentId);
            }
            public void CreatesAffiliation_WithNotIsDefault_WhenPersonAlreadyHasDefaultAffiliation()
            {
                Affiliation outEntity = null;
                const int   personId  = 13;
                var         person    = new Person
                {
                    RevisionId   = personId,
                    Affiliations = new Collection <Affiliation>
                    {
                        new Affiliation {
                            IsDefault = true,
                        }
                    }
                };
                var command = new CreateAffiliationCommand
                {
                    PersonId = personId,
                };
                var entities = new Mock <ICommandEntities>(MockBehavior.Strict).Initialize();

                entities.Setup(m => m.Get <Person>()).Returns(new[] { person }.AsQueryable);
                entities.Setup(m => m.Create(It.Is(AffiliationBasedOn(command))))
                .Callback((Entity entity) => outEntity = (Affiliation)entity);
                var handler = new CreateAffiliationHandler(entities.Object);

                handler.Handle(command);

                outEntity.ShouldNotBeNull();
                outEntity.IsDefault.ShouldBeFalse();
            }
Ejemplo n.º 3
0
            public void IsValidWhen_MatchesEstablishment_WithAnyTypeCategory()
            {
                const int establishmentId = 6;
                var       command         = new CreateAffiliationCommand {
                    EstablishmentId = establishmentId
                };
                var establishment = new Establishment
                {
                    RevisionId = command.EstablishmentId,
                    Type       = new EstablishmentType
                    {
                        Category = new EstablishmentCategory
                        {
                            Code = EstablishmentCategoryCode.Govt,
                        },
                    },
                };
                var entities = new Mock <IQueryEntities>(MockBehavior.Strict).Initialize();

                entities.Setup(m => m.Query <Establishment>()).Returns(new[] { establishment }.AsQueryable);
                entities.Setup(m => m.Query <Person>()).Returns(new Person[] { }.AsQueryable);
                var validator = new CreateAffiliationValidator(entities.Object);

                var results = validator.Validate(command);

                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "EstablishmentId");

                error.ShouldBeNull();
            }
Ejemplo n.º 4
0
            public void IsValidWhen_PersonId_IsNotAlreadyAffiliatedWithEstablishment()
            {
                const int personId        = 3;
                const int establishmentId = 7;
                var       command         = new CreateAffiliationCommand
                {
                    PersonId        = personId,
                    EstablishmentId = establishmentId,
                };
                var person = new Person
                {
                    RevisionId   = personId,
                    Affiliations = new[]
                    {
                        new Affiliation {
                            EstablishmentId = 1
                        }
                    }
                };
                var entities = new Mock <IQueryEntities>(MockBehavior.Strict).Initialize();

                entities.Setup(m => m.Query <Establishment>()).Returns(new Establishment[] { }.AsQueryable);
                entities.Setup(m => m.Query <Person>()).Returns(new[] { person }.AsQueryable);
                var validator = new CreateAffiliationValidator(entities.Object);

                var results = validator.Validate(command);

                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "PersonId");

                error.ShouldBeNull();
            }
Ejemplo n.º 5
0
            public void IsInvalidWhen_PersonId_DoesNotMatchPerson()
            {
                const int personId = 3;
                var       command  = new CreateAffiliationCommand {
                    PersonId = personId
                };
                var entities = new Mock <IQueryEntities>(MockBehavior.Strict).Initialize();

                entities.Setup(m => m.Query <Establishment>()).Returns(new Establishment[] { }.AsQueryable);
                entities.Setup(m => m.Query <Person>()).Returns(new Person[] { }.AsQueryable);
                var validator = new CreateAffiliationValidator(entities.Object);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "PersonId");

                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(string.Format(
                                                   ValidatePerson.FailedBecauseIdMatchedNoEntity,
                                                   personId));
                // ReSharper restore PossibleNullReferenceException
            }
Ejemplo n.º 6
0
            public void ValidWhen_IsTrue_AndEstablishmentIsInstitution()
            {
                const bool isClaimingStudent = true;
                var        command           = new CreateAffiliationCommand
                {
                    IsClaimingStudent = isClaimingStudent
                };
                var establishment = new Establishment
                {
                    Type = new EstablishmentType
                    {
                        Category = new EstablishmentCategory
                        {
                            Code = EstablishmentCategoryCode.Inst,
                        },
                    },
                };
                var entities = new Mock <IQueryEntities>(MockBehavior.Strict).Initialize();

                entities.Setup(m => m.Query <Establishment>()).Returns(new[] { establishment }.AsQueryable);
                entities.Setup(m => m.Query <Person>()).Returns(new Person[] { }.AsQueryable);
                var validator = new CreateAffiliationValidator(entities.Object);

                var results = validator.Validate(command);

                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "IsClaimingStudent");

                error.ShouldBeNull();
            }
            private static Expression <Func <Affiliation, bool> > AffiliationBasedOn(CreateAffiliationCommand command)
            {
                Expression <Func <Affiliation, bool> > affiliationBasedOn = e =>
                                                                            e.EstablishmentId == command.EstablishmentId &&
                                                                            e.IsClaimingStudent == command.IsClaimingStudent &&
                                                                            e.IsClaimingEmployee == command.IsClaimingEmployee
                ;

                return(affiliationBasedOn);
            }
            public void ExecutesCreate_OnAffiliationEntity()
            {
                const int personId = 13;
                var       person   = new Person {
                    RevisionId = personId,
                };
                var command = new CreateAffiliationCommand
                {
                    PersonId = personId,
                };
                var entities = new Mock <ICommandEntities>(MockBehavior.Strict).Initialize();

                entities.Setup(m => m.Get <Person>()).Returns(new[] { person }.AsQueryable);
                entities.Setup(m => m.Create(It.Is(AffiliationBasedOn(command))));
                var handler = new CreateAffiliationHandler(entities.Object);

                handler.Handle(command);

                entities.Verify(m => m.Create(It.Is(AffiliationBasedOn(command))), Times.Once());
            }
            public void ExecutesQuery_ForPerson()
            {
                const int personId = 13;
                var       command  = new CreateAffiliationCommand
                {
                    PersonId = personId,
                };
                var handler = new CreateAffiliationHandler(null);
                NullReferenceException exception = null;

                try
                {
                    handler.Handle(command);
                }
                catch (NullReferenceException ex)
                {
                    exception = ex;
                }

                exception.ShouldNotBeNull();
            }
Ejemplo n.º 10
0
            public void IsInvalidWhen_IsTrue_ButEstablishmentIsNotInstitution()
            {
                const bool isClaimingStudent = true;
                var        establishment     = new Establishment
                {
                    Type = new EstablishmentType
                    {
                        Category = new EstablishmentCategory
                        {
                            Code = "not an institution",
                        },
                    },
                };
                var command = new CreateAffiliationCommand
                {
                    IsClaimingStudent = isClaimingStudent
                };
                var entities = new Mock <IQueryEntities>(MockBehavior.Strict).Initialize();

                entities.Setup(m => m.Query <Establishment>()).Returns(new[] { establishment }.AsQueryable);
                entities.Setup(m => m.Query <Person>()).Returns(new Person[] { }.AsQueryable);
                var validator = new CreateAffiliationValidator(entities.Object);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "IsClaimingStudent");

                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(string.Format(
                                                   ValidateAffiliation.FailedBecauseIsClaimingStudentButEstablishmentIsNotInstitution,
                                                   command.EstablishmentId));
                // ReSharper restore PossibleNullReferenceException
            }