Example #1
0
 def test_datetime_any(self):
     """
     test datetime field with 'any' format
     :return:
     """
     descriptor = clone(BASE_FIELD)
     descriptor['type'] = 'datetime'
     # format='any'.
     # The main problem is to be sure that a dd/mm/yyyy is not interpreted as mm/dd/yyyy
     descriptor['format'] = 'any'
     f = SchemaField(descriptor)
     valid_values = [
         '2016-07-10 13:55:00',
         '10/07/2016 13:55',
         '10/07/16 1:55 pm',
         '2016-07-10 13:55:00',
         '10-July-2016 13:55:00',
         '10-JUlY-16 13:55:00',
         '10-07-2016 13:55:00',
         '10-07-16 13:55:00'
     ]
     expected_dt = datetime.datetime(2016, 7, 10, 13, 55, 00)
     for v in valid_values:
         dt = f.cast(v)
         self.assertIsInstance(dt, datetime.datetime)
         self.assertEqual(expected_dt, dt)
     invalid_value = ['djskdj']
     for v in invalid_value:
         with self.assertRaises(Exception):
             f.cast(v)
Example #2
0
    def test_enums(self):
        """
        Test that if a field has an enum constraint and if the data doesn't fit the error message should give the list
        of the possible values.
        :return:
        """
        descriptor = {
            "name": "Enum",
            "title": "Test Enum message",
            "type": "string",
            "format": "default",
            "constraints": {
                "required": False,
                "enum": ["val1", "val2", "val3"]
            }
        }
        f = SchemaField(descriptor)
        valid_values = ['val1', 'val2', 'val3',
                        '']  # non required should accept blank
        for v in valid_values:
            self.assertIsNone(f.validation_error(v))

        wrong_values = ['xxx']
        for v in wrong_values:
            msg = f.validation_error(v)
            self.assertTrue(msg)
            # test that the error message contains each of the enum values.
            for vv in f.constraints['enum']:
                self.assertTrue(msg.find(vv) >= 0)
Example #3
0
    def test_enums(self):
        """
        Test that if a field has an enum constraint and if the data doesn't fit the error message should give the list
        of the possible values.
        :return:
        """
        descriptor = {
            "name": "Enum",
            "title": "Test Enum message",
            "type": "string",
            "format": "default",
            "constraints": {
                "required": False,
                "enum": ["val1", "val2", "val3"]
            }
        }
        f = SchemaField(descriptor)
        valid_values = ['val1', 'val2', 'val3', '']  # non required should accept blank
        for v in valid_values:
            self.assertIsNone(f.validation_error(v))

        wrong_values = ['xxx']
        for v in wrong_values:
            msg = f.validation_error(v)
            self.assertTrue(msg)
            # test that the error message contains each of the enum values.
            for vv in f.constraints['enum']:
                self.assertTrue(msg.find(vv) >= 0)
Example #4
0
 def test_datetime_any(self):
     """
     test datetime field with 'any' format
     :return:
     """
     descriptor = clone(BASE_FIELD)
     descriptor['type'] = 'datetime'
     # format='any'.
     # The main problem is to be sure that a dd/mm/yyyy is not interpreted as mm/dd/yyyy
     descriptor['format'] = 'any'
     f = SchemaField(descriptor)
     valid_values = [
         '2016-07-10 13:55:00', '10/07/2016 13:55', '10/07/16 1:55 pm',
         '2016-07-10 13:55:00', '10-July-2016 13:55:00',
         '10-JUlY-16 13:55:00', '10-07-2016 13:55:00', '10-07-16 13:55:00'
     ]
     expected_dt = datetime.datetime(2016, 7, 10, 13, 55, 00)
     for v in valid_values:
         dt = f.cast(v)
         self.assertIsInstance(dt, datetime.datetime)
         self.assertEqual(expected_dt, dt)
     invalid_value = ['djskdj']
     for v in invalid_value:
         with self.assertRaises(Exception):
             f.cast(v)
Example #5
0
    def test_date(self):
        descriptor = clone(BASE_FIELD)
        descriptor['type'] = 'date'
        # 'default' format = ISO
        descriptor['format'] = 'default'
        f = SchemaField(descriptor)
        valid_values = ['2016-07-29']
        for v in valid_values:
            date = f.cast(v)
            self.assertIsInstance(date, datetime.date)
            self.assertEqual(datetime.date(2016, 7, 29), date)
        invalid_value = ['29/07/2016', '07/29/2016', '2016-07-29 15:28:37']
        for v in invalid_value:
            with self.assertRaises(Exception):
                f.cast(v)

        # format='any'.
        # The main problem is to be sure that a dd/mm/yyyy is not interpreted as mm/dd/yyyy
        descriptor['format'] = 'any'
        f = SchemaField(descriptor)
        valid_values = [
            '2016-07-10', '10/07/2016', '10/07/16', '2016-07-10 15:28:37',
            '10-July-2016', '10-JUlY-16', '10-07-2016', '10-07-16'
        ]
        expected_date = datetime.date(2016, 7, 10)
        for v in valid_values:
            date = f.cast(v)
            self.assertIsInstance(date, datetime.date)
            self.assertEqual(expected_date, date)
        invalid_value = ['djskdj']
        for v in invalid_value:
            with self.assertRaises(Exception):
                f.cast(v)
Example #6
0
 def test_get_method(self):
     """
     test that the SchemaField has the dict-like get('key', default)
     """
     field = self.base_field
     sch = SchemaField(field)
     self.assertTrue(hasattr(sch, 'get'))
     self.assertEquals(field.get('Name'), sch.get('Name'))
     self.assertEquals(field.get('constraints'), sch.get('constraints'))
     self.assertEquals(None, sch.get('bad_keys'))
     self.assertEquals('default', sch.get('bad_keys', 'default'))
Example #7
0
 def test_name_mandatory(self):
     """
     A schema field without name should throw an exception
     """
     field = self.base_field
     del field['name']
     with self.assertRaises(FieldSchemaError):
         SchemaField(field)
     # no blank
     field = self.base_field
     field['name'] = ''
     with self.assertRaises(FieldSchemaError):
         SchemaField(field)
Example #8
0
    def test_string(self):
        # test that a blank string '' is not accepted when the field is required
        null_values = ['null', 'none', 'nil', 'nan', '-', '']
        desc = clone(BASE_FIELD)
        desc['type'] = 'string'
        desc['constraints'] = clone(REQUIRED_CONSTRAINTS)
        f = SchemaField(desc)
        for v in null_values:
            with self.assertRaises(Exception):
                f.cast(v)

        # test non unicode (python 2)
        value = 'not unicode'
        self.assertIsInstance(f.cast(value), six.text_type)
        self.assertEqual(f.cast(value), value)
Example #9
0
 def test_boolean(self):
     true_values = [True, 'True', 'true', 'YES', 'yes', 'y', 't', '1', 1]
     false_values = [False, 'FALSE', 'false', 'NO', 'no', 'n', 'f', '0', 0]
     wrong_values = [2, 3, 'FLSE', 'flse', 'NON', 'oui', 'maybe', 'not sure']
     descriptor = self.base_field_descriptor
     descriptor['type'] = 'boolean'
     # only 'default' format
     descriptor['format'] = 'default'
     f = SchemaField(descriptor)
     for v in true_values:
         self.assertTrue(f.cast(v))
     for v in false_values:
         self.assertFalse(f.cast(v))
     for v in wrong_values:
         with self.assertRaises(Exception):
             f.cast(v)
Example #10
0
 def test_column_name(self):
     """
     'column_name' is a property that is equal to name
     """
     field = self.base_field
     sch = SchemaField(field)
     self.assertEquals(sch.name, sch.column_name)
     self.assertNotEqual(sch.column_name, sch.title)
Example #11
0
    def test_date_custom_format(self):
        format_ = 'fmt:%d %b %y'  # ex 30 Nov 14
        descriptor = {
            'name': 'Date with fmt',
            'type': 'date',
            'format': format_
        }
        field = SchemaField(descriptor)
        value = '30 Nov 14'
        self.assertEqual(field.cast(value), datetime.date(2014, 11, 30))

        format_ = 'fmt:%d/%m/%Y'
        descriptor = {
            'name': 'Date with fmt',
            'type': 'date',
            'format': format_
        }
        field = SchemaField(descriptor)
        value = '12/07/2016'
        value = field.cast(value)
        self.assertEqual(type(value), datetime.date)
        self.assertEqual(value, datetime.date(2016, 7, 12))
Example #12
0
 def test_get_method(self):
     """
     test that the SchemaField has the dict-like get('key', default)
     """
     field = self.base_field
     sch = SchemaField(field)
     self.assertTrue(hasattr(sch, 'get'))
     self.assertEquals(field.get('Name'), sch.get('Name'))
     self.assertEquals(field.get('constraints'), sch.get('constraints'))
     self.assertEquals(None, sch.get('bad_keys'))
     self.assertEquals('default', sch.get('bad_keys', 'default'))
Example #13
0
    def test_date(self):
        descriptor = clone(BASE_FIELD)
        descriptor['type'] = 'date'
        # 'default' format = ISO
        descriptor['format'] = 'default'
        f = SchemaField(descriptor)
        valid_values = ['2016-07-29']
        for v in valid_values:
            date = f.cast(v)
            self.assertIsInstance(date, datetime.date)
            self.assertEqual(datetime.date(2016, 7, 29), date)
        invalid_value = ['29/07/2016', '07/29/2016', '2016-07-29 15:28:37']
        for v in invalid_value:
            with self.assertRaises(Exception):
                f.cast(v)

        # format='any'. 
        # The main problem is to be sure that a dd/mm/yyyy is not interpreted as mm/dd/yyyy
        descriptor['format'] = 'any'
        f = SchemaField(descriptor)
        valid_values = [
            '2016-07-10',
            '10/07/2016',
            '10/07/16',
            '2016-07-10 15:28:37',
            '10-July-2016',
            '10-JUlY-16',
            '10-07-2016',
            '10-07-16'
        ]
        expected_date = datetime.date(2016, 7, 10)
        for v in valid_values:
            date = f.cast(v)
            self.assertIsInstance(date, datetime.date)
            self.assertEqual(expected_date, date)
        invalid_value = ['djskdj']
        for v in invalid_value:
            with self.assertRaises(Exception):
                f.cast(v)
Example #14
0
    def test_string(self):
        # test that a blank string '' is not accepted when the field is required
        null_values = ['null', 'none', 'nil', 'nan', '-', '']
        desc = clone(BASE_FIELD)
        desc['type'] = 'string'
        desc['constraints'] = clone(REQUIRED_CONSTRAINTS)
        f = SchemaField(desc)
        for v in null_values:
            with self.assertRaises(Exception):
                f.cast(v)

        # test non unicode (python 2)
        value = 'not unicode'
        self.assertIsInstance(f.cast(value), six.text_type)
        self.assertEqual(f.cast(value), value)
Example #15
0
 def test_boolean(self):
     true_values = [True, 'True', 'true', 'YES', 'yes', 'y', 't', '1', 1]
     false_values = [False, 'FALSE', 'false', 'NO', 'no', 'n', 'f', '0', 0]
     wrong_values = [
         2, 3, 'FLSE', 'flse', 'NON', 'oui', 'maybe', 'not sure'
     ]
     descriptor = self.base_field_descriptor
     descriptor['type'] = 'boolean'
     # only 'default' format
     descriptor['format'] = 'default'
     f = SchemaField(descriptor)
     for v in true_values:
         self.assertTrue(f.cast(v))
     for v in false_values:
         self.assertFalse(f.cast(v))
     for v in wrong_values:
         with self.assertRaises(Exception):
             f.cast(v)
Example #16
0
    def test_date_custom_format(self):
        format_ = 'fmt:%d %b %y'  # ex 30 Nov 14
        descriptor = {
            'name': 'Date with fmt',
            'type': 'date',
            'format': format_
        }
        field = SchemaField(descriptor)
        value = '30 Nov 14'
        self.assertEqual(field.cast(value), datetime.date(2014, 11, 30))

        format_ = 'fmt:%d/%m/%Y'
        descriptor = {
            'name': 'Date with fmt',
            'type': 'date',
            'format': format_
        }
        field = SchemaField(descriptor)
        value = '12/07/2016'
        value = field.cast(value)
        self.assertEqual(type(value), datetime.date)
        self.assertEqual(value, datetime.date(2016, 7, 12))
Example #17
0
 def test_constraints(self):
     """
     test that the constraints property returned a SchemaConstraints
     """
     self.assertIsInstance(
         SchemaField(BASE_FIELD).constraints, SchemaConstraints)