# Path: breezeminder/forms/validators.py
# class Numeric(ActiveStateValidator):
#     def __init__(self, length=None, message=None):
#         if not message:
#             if length is not None:
#                 message = 'This value must be a %i digit number' % length
#             else:
#                 message = 'This value must be a number'
#         self.message = message
#         self.length = length
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         value = re.sub(r'[^0-9]', '', field.data.strip())
#         if not value.isdigit() or (self.length is not None and len(value) != self.length):
#             raise ValidationError(self.message)
# 
# class CardNumber(Numeric):
#     """ Check a card number is 16 digits """
# 
#     def __init__(self, message=None):
#         super(CardNumber, self).__init__(16, message)
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         value = re.sub(r'[^0-9]', '', field.data.strip())
#         if len(value) != 16:
#             raise ValidationError(self.message)
# 
# class ExactLength(ActiveStateValidator):
#     """ Checks that a field is an exact length """
# 
#     def __init__(self, length, message=None):
#         self.length = length
#         if message is None:
#             message = 'This field must have a length of %s characters' % length
#         self.message = message
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         if len(field.data.strip()) != self.length:
#             raise ValidationError(self.message)
# 
# class PhoneNumber(Numeric):
#     """ Check a phone number is 10 digits """
#     valid_phone_pattern = re.compile(r'^([2-9][0-9]{2}){2}[0-9]{4}$')
#     invalid_patterns = [
#         re.compile(r'^\d{3}555\d{4}$'),  # Non 555 numbers
#         re.compile(r'^(\d11\d{7}|\d{4}11\d{4})$'),  # Non N11 numbers
#     ]
# 
#     def __init__(self, message=None):
#         super(PhoneNumber, self).__init__(10, message)
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         value = re.sub(r'[^0-9]', '', field.data.strip())
# 
#         # Check empty and valid length
#         if (not value) or len(value) != 10:
#             raise ValidationError(self.message)
# 
#         # Check valid pattern
#         if not self.valid_phone_pattern.match(value):
#             raise ValidationError(self.message)
# 
#         # Check all patterns that must pass
#         for pat in self.invalid_patterns:
#             if pat.match(value):
#                 raise ValidationError(self.message)
# 
# class PositiveNumber(ActiveStateValidator):
#     """ Checks that a value is positive. Expects an integer or float field """
#     def __init__(self, message=None):
#         if message is None:
#             message = 'This value must be greater than 0'
#         self.message = message
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         try:
#             if int(field.data) < 0:
#                 raise ValidationError(self.message)
#         except:
#             raise ValidationError(self.message)
# 
# class Conditional(ActiveStateValidator):
#     """
#     Conditionally apply a validator if the value of another field if
#     that field's value matches the one we're looking for. Specify the
#     form field name as a string and the value that should invoke a passed
#     list of validators. The validators kwarg could be either a list or single
#     validator.
# 
#     There's also an expectation that conditional validators will completely
#     override any optional validator.
#     """
#     active = True
# 
#     def __init__(self, field, value, validators=[], not_equals=False):
#         self.field = field
#         self.value = value
#         self.not_equals = not_equals
#         if not isinstance(validators, list):
#             validators = [validators]
#         self.validators = validators
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         field_data = getattr(form, self.field).data.strip()
# 
#         # Don't invoke validators if 
#         # 1) We want non-equal values and the values are equal
#         # 2) We want equal values and the values are not equal
#         if (self.not_equals and field_data == self.value) or \
#                 ((not self.not_equals) and field_data != self.value):
#             return
# 
#         for validator in self.validators:
#             validator(form, field)


from mock import Mock
from unittest2 import TestCase
from wtforms import ValidationError
from breezeminder.forms.validators import (Numeric,
                                           CardNumber,
                                           ExactLength,
                                           PhoneNumber,
                                           PositiveNumber,
                                           Conditional)
                       '4042001234']
        self.invalids = ['4045551234',
                         '+14042004824',  # This SHOULD be valid, just not now
                         '1887898788',
                         '4041118980',
                         '911',
                         '411',
                         '4049111234',
                         '9113774878']


class PositiveNumberTestCase(ValidatorTestCase):

    def setUp(self):
        super(PositiveNumberTestCase, self).setUp()
        self.validator = PositiveNumber()
        self.valids = ['1', '0', '100', '1000', '+1']
        self.invalids = ['foo', '-1', '-1000']


def fake_raising_validator(*args):
    raise ValidationError('FooBar')


class ConditionalTestCase(TestCase):

    def setUp(self):
        self.form = Mock()
        self.form.field = Mock()
        self.field = Mock()

# Path: breezeminder/forms/validators.py
# class Numeric(ActiveStateValidator):
#     def __init__(self, length=None, message=None):
#         if not message:
#             if length is not None:
#                 message = 'This value must be a %i digit number' % length
#             else:
#                 message = 'This value must be a number'
#         self.message = message
#         self.length = length
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         value = re.sub(r'[^0-9]', '', field.data.strip())
#         if not value.isdigit() or (self.length is not None and len(value) != self.length):
#             raise ValidationError(self.message)
# 
# class CardNumber(Numeric):
#     """ Check a card number is 16 digits """
# 
#     def __init__(self, message=None):
#         super(CardNumber, self).__init__(16, message)
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         value = re.sub(r'[^0-9]', '', field.data.strip())
#         if len(value) != 16:
#             raise ValidationError(self.message)
# 
# class ExactLength(ActiveStateValidator):
#     """ Checks that a field is an exact length """
# 
#     def __init__(self, length, message=None):
#         self.length = length
#         if message is None:
#             message = 'This field must have a length of %s characters' % length
#         self.message = message
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         if len(field.data.strip()) != self.length:
#             raise ValidationError(self.message)
# 
# class PhoneNumber(Numeric):
#     """ Check a phone number is 10 digits """
#     valid_phone_pattern = re.compile(r'^([2-9][0-9]{2}){2}[0-9]{4}$')
#     invalid_patterns = [
#         re.compile(r'^\d{3}555\d{4}$'),  # Non 555 numbers
#         re.compile(r'^(\d11\d{7}|\d{4}11\d{4})$'),  # Non N11 numbers
#     ]
# 
#     def __init__(self, message=None):
#         super(PhoneNumber, self).__init__(10, message)
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         value = re.sub(r'[^0-9]', '', field.data.strip())
# 
#         # Check empty and valid length
#         if (not value) or len(value) != 10:
#             raise ValidationError(self.message)
# 
#         # Check valid pattern
#         if not self.valid_phone_pattern.match(value):
#             raise ValidationError(self.message)
# 
#         # Check all patterns that must pass
#         for pat in self.invalid_patterns:
#             if pat.match(value):
#                 raise ValidationError(self.message)
# 
# class PositiveNumber(ActiveStateValidator):
#     """ Checks that a value is positive. Expects an integer or float field """
#     def __init__(self, message=None):
#         if message is None:
#             message = 'This value must be greater than 0'
#         self.message = message
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         try:
#             if int(field.data) < 0:
#                 raise ValidationError(self.message)
#         except:
#             raise ValidationError(self.message)
# 
# class Conditional(ActiveStateValidator):
#     """
#     Conditionally apply a validator if the value of another field if
#     that field's value matches the one we're looking for. Specify the
#     form field name as a string and the value that should invoke a passed
#     list of validators. The validators kwarg could be either a list or single
#     validator.
# 
#     There's also an expectation that conditional validators will completely
#     override any optional validator.
#     """
#     active = True
# 
#     def __init__(self, field, value, validators=[], not_equals=False):
#         self.field = field
#         self.value = value
#         self.not_equals = not_equals
#         if not isinstance(validators, list):
#             validators = [validators]
#         self.validators = validators
# 
#     def __call__(self, form, field):
#         if not self.active:
#             return
# 
#         field_data = getattr(form, self.field).data.strip()
# 
#         # Don't invoke validators if 
#         # 1) We want non-equal values and the values are equal
#         # 2) We want equal values and the values are not equal
#         if (self.not_equals and field_data == self.value) or \
#                 ((not self.not_equals) and field_data != self.value):
#             return
# 
#         for validator in self.validators:
#             validator(form, field)


# Path: breezeminder/tests/forms/test_validators.py
from mock import Mock
from unittest2 import TestCase
from wtforms import ValidationError
from breezeminder.forms.validators import (Numeric,
                                           CardNumber,
                                           ExactLength,
                                           PhoneNumber,
                                           PositiveNumber,
                                           Conditional)

                       '4042001234']
        self.invalids = ['4045551234',
                         '+14042004824',  # This SHOULD be valid, just not now
                         '1887898788',
                         '4041118980',
                         '911',
                         '411',
                         '4049111234',
                         '9113774878']


class PositiveNumberTestCase(ValidatorTestCase):

    def setUp(self):
        super(PositiveNumberTestCase, self).setUp()
        self.validator = PositiveNumber()
        self.valids = ['1', '0', '100', '1000', '+1']
        self.invalids = ['foo', '-1', '-1000']


def fake_raising_validator(*args):
    raise ValidationError('FooBar')


class ConditionalTestCase(TestCase):

    def setUp(self):
        self.form = Mock()
        self.form.field = Mock()
        self.field = Mock()

        self.validator = Conditional('field',