示例#1
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);
            }
        }
示例#2
0
        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 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);
            }
        }