コード例 #1
0
        public void SexIdTest()
        {
            const string propertyName = nameof(StudentValidator.SexId);
            const int    min          = SexIdMin;
            const int    max          = SexIdMax;

            var repository = new FakeStudentRepository();

            // Check less than min
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = min - 1;
                validator.SexId(value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                var error  = ValidationsTestHelper.CheckErrorType <ComparisonValidationError <int> >(errors);
                Assert.AreEqual(ComparisonResultType.Less, error.ComparisonResult);
                Assert.AreEqual(min, error.ComparisonValue);
            }

            // Check is min
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = min;
                validator.SexId(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Check is max
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = max;
                validator.SexId(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Check bigger than max
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = max + 1;
                validator.SexId(value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                var error  = ValidationsTestHelper.CheckErrorType <ComparisonValidationError <int> >(errors);
                Assert.AreEqual(ComparisonResultType.More, error.ComparisonResult);
                Assert.AreEqual(max, error.ComparisonValue);
            }
        }
コード例 #2
0
ファイル: GroupValidatorTests.cs プロジェクト: ch3l/Velvetech
        public void NameTest()
        {
            const string propertyName  = nameof(GroupValidator.Name);
            const int    upperBoundary = NameUpperBoundary;

            // Check for null
            {
                var    validator = new DefaultGroupValidator();
                string value     = null;
                validator.Name(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <NullValidationError>(errors);
            }

            // Check is empty
            {
                var validator = new DefaultGroupValidator();
                var value     = "";
                validator.Name(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <EmptyValidationError <IEnumerable <char> > >(errors);
            }

            // Whitespaces check without boundary crossing
            {
                var validator = new DefaultGroupValidator();
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => ' ').ToArray());
                validator.Name(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Whitespaces check with boundary crossing
            {
                var validator = new DefaultGroupValidator();
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => ' ').ToArray());
                validator.Name(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Upper boundary check without crossing
            {
                var validator = new DefaultGroupValidator();
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => 'x').ToArray());
                validator.Name(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Upper boundary check with crossing
            {
                var validator = new DefaultGroupValidator();
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => 'x').ToArray());
                validator.Name(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <LengthComparisonValidationError>(errors);
                ValidationsTestHelper.CheckUpperBoundaryCross(
                    (LengthComparisonValidationError)validator.Errors[propertyName][0],
                    upperBoundary);
            }
        }
コード例 #3
0
        public async Task CallsignTestAsync()
        {
            const string propertyName  = nameof(StudentValidator.Callsign);
            const int    lowerBoundary = CallsignLowerBoundary;
            const int    upperBoundary = CallsignUpperBoundary;

            var repository = new FakeStudentRepository();

            // Check for null
            {
                var    validator = new DefaultStudentValidator(new StudentValidationService(repository));
                string value     = null;
                validator.Callsign(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Check is empty
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = "";
                validator.Callsign(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Whitespaces check without boundary crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => ' ').ToArray());
                validator.Callsign(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Whitespaces check with boundary crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => ' ').ToArray());
                validator.Callsign(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Lower boundary check without crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, lowerBoundary).Select(x => 'x').ToArray());
                validator.Callsign(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Lower boundary check with crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, lowerBoundary - 1).Select(x => 'x').ToArray());
                validator.Callsign(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <LengthComparisonValidationError>(errors);
                ValidationsTestHelper.CheckLowerBoundaryCross(
                    (LengthComparisonValidationError)validator.Errors[propertyName][0],
                    lowerBoundary);
            }

            // Upper boundary check without crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => 'x').ToArray());
                validator.Callsign(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Upper boundary check with crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => 'x').ToArray());
                validator.Callsign(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <LengthComparisonValidationError>(errors);
                ValidationsTestHelper.CheckUpperBoundaryCross(
                    (LengthComparisonValidationError)validator.Errors[propertyName][0],
                    upperBoundary);
            }

            // Check for uniqueness
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));

                string value = null;
                await validator.CallsignUniqueness(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);

                value = new string(Enumerable.Range(1, lowerBoundary).Select(x => 'x').ToArray());
                await validator.CallsignUniqueness(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);

                var student = await Student.BuildAsync(validator, 1, "firstname", "middlename", "lastname", value);

                await repository.AddAsync(student);

                await validator.CallsignUniqueness(value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <UniquenessValidationError <string> >(errors);

                value = null;
                await validator.CallsignUniqueness(value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <UniquenessValidationError <string> >(errors);
            }
        }
コード例 #4
0
        public void MiddlenameTest()
        {
            const string propertyName  = nameof(StudentValidator.Middlename);
            const int    upperBoundary = MiddlenameUpperBoundary;

            var repository = new FakeStudentRepository();

            // Check for null
            {
                var    validator = new DefaultStudentValidator(new StudentValidationService(repository));
                string value     = null;
                validator.Middlename(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Check is empty
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = "";
                validator.Middlename(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Whitespaces check without boundary crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => ' ').ToArray());
                validator.Middlename(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Whitespaces check with boundary crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => ' ').ToArray());
                validator.Middlename(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <WhitespacesValidationError>(errors);
            }

            // Upper boundary check without crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary).Select(x => 'x').ToArray());
                validator.Middlename(ref value);

                ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 0);
            }

            // Upper boundary check with crossing
            {
                var validator = new DefaultStudentValidator(new StudentValidationService(repository));
                var value     = new string(Enumerable.Range(1, upperBoundary + 1).Select(x => 'x').ToArray());
                validator.Middlename(ref value);

                var errors = ValidationsTestHelper.CheckErrorsCount(validator, propertyName, 1);
                ValidationsTestHelper.CheckErrorType <LengthComparisonValidationError>(errors);
                ValidationsTestHelper.CheckUpperBoundaryCross(
                    (LengthComparisonValidationError)validator.Errors[propertyName][0],
                    upperBoundary);
            }
        }