def test_null_object_transform(self):
     schema =  {"type": "object",
                "properties": {"addrs": {"type": ["null", "object"],
                                         "properties": {"city": {"type": "string"}}}}}
     none_data = {'addrs': None}
     self.assertDictEqual(none_data, transform(none_data, schema))
     empty_data = {'addrs': {}}
     self.assertDictEqual(empty_data, transform(empty_data, schema))
 def test_multi_type_array_transform(self):
     schema =  {"type": ["null", "array", "integer"],
                "items": {"type": "date-time", "format": "date-time"}}
     data = ["2017-01-01"]
     expected = ["2017-01-01T00:00:00.000000Z"]
     self.assertEqual(expected, transform(data, schema))
     data = 23
     expected = 23
     self.assertEqual(expected, transform(data, schema))
 def test_multi_type_object_transform(self):
     schema =  {"type": ["null", "object", "string"],
                "properties": {"whatever": {"type": "date-time",
                                            "format": "date-time"}}}
     data = {"whatever": "2017-01-01"}
     expected = {"whatever": "2017-01-01T00:00:00.000000Z"}
     self.assertDictEqual(expected, transform(data, schema))
     data = "justastring"
     expected = "justastring"
     self.assertEqual(expected, transform(data, schema))
    def test_datetime_transform(self):
        schema = {"type": "string", "format": "date-time"}
        string_datetime = "2017-01-01T00:00:00Z"
        transformed_string_datetime = "2017-01-01T00:00:00.000000Z"
        self.assertEqual(transformed_string_datetime, transform(string_datetime, schema, NO_INTEGER_DATETIME_PARSING))
        self.assertEqual('1970-01-02T00:00:00.000000Z', transform(86400, schema, UNIX_SECONDS_INTEGER_DATETIME_PARSING))
        self.assertEqual(transformed_string_datetime, transform(string_datetime, schema, UNIX_SECONDS_INTEGER_DATETIME_PARSING))
        self.assertEqual('1970-01-01T00:01:26.400000Z', transform(86400, schema, UNIX_MILLISECONDS_INTEGER_DATETIME_PARSING))
        self.assertEqual(transformed_string_datetime, transform(string_datetime, schema, UNIX_MILLISECONDS_INTEGER_DATETIME_PARSING))

        trans = Transformer(NO_INTEGER_DATETIME_PARSING)
        self.assertIsNone(trans._transform_datetime('cat'))
        self.assertIsNone(trans._transform_datetime(0))

        trans.integer_datetime_fmt = UNIX_SECONDS_INTEGER_DATETIME_PARSING
        self.assertIsNone(trans._transform_datetime('cat'))
 def test_pattern_properties_match_multiple(self):
     schema = {"type": "object",
               "patternProperties": { ".+?cost": {"type": "number"},
                                      ".+(?<!cost)$": {"type": "string"}}}
     dict_value = {"name": "chicken", "unit_cost": 1.45, "SKU": '123456'}
     expected = dict(dict_value)
     self.assertEqual(expected, transform(dict_value, schema))
 def test_nested_transform(self):
     schema =  {"type": "object",
                "properties": {"addrs": {"type": "array",
                                         "items": {"type": "object",
                                                   "properties": {"addr1": {"type": "string"},
                                                                  "city": {"type": "string"},
                                                                  "state": {"type": "string"},
                                                                  'amount': {'type': 'integer'}}}}}}
     data = {'addrs': [{'amount': '123'}, {'amount': '456'}]}
     expected = {'addrs': [{'amount': 123}, {'amount': 456}]}
     self.assertDictEqual(expected, transform(data, schema))
 def test_anyof_datetime(self):
     schema = {'anyOf': [{'type': 'null'}, {'format': 'date-time', 'type': 'string'}]}
     string_datetime = '2016-03-10T18:47:20Z'
     transformed_string_datetime = '2016-03-10T18:47:20.000000Z'
     self.assertEqual(transformed_string_datetime, transform(string_datetime, schema))
     self.assertIsNone(transform(None, schema))
 def test_datetime_fractional_seconds_transform(self):
     schema = {"type": "string", "format": "date-time"}
     string_datetime = "2017-01-01T00:00:00.123000Z"
     self.assertEqual(string_datetime, transform(string_datetime, schema, NO_INTEGER_DATETIME_PARSING))
 def test_datetime_string_with_timezone(self):
     schema = {"type": "string", "format": "date-time"}
     string_datetime = "2017-03-18T07:00:05-0700"
     transformed_string_datetime = "2017-03-18T14:00:05.000000Z"
     self.assertEqual(transformed_string_datetime, transform(string_datetime, schema))
 def test_integer_transform(self):
     schema = {'type': 'integer'}
     self.assertEqual(123, transform(123, schema))
     self.assertEqual(123, transform('123', schema))
     self.assertEqual(1234, transform('1,234', schema))
 def test_null_transform(self):
     self.assertEqual('', transform('', {'type': ['null', 'string']}))
     self.assertEqual('', transform('', {'type': [ 'string', 'null']}))
     self.assertEqual(None, transform(None, {'type': [ 'string', 'null']}))
     self.assertEqual(None, transform('', {'type': ['null']}))
     self.assertEqual(None, transform(None, {'type': ['null']}))
 def test_pattern_properties_match(self):
     schema = {"type": "object",
               "patternProperties": { ".+": {"type": "string"}}}
     dict_value = {"name": "chicken", "unit_cost": '1.45', "SKU": '123456'}
     expected = dict(dict_value)
     self.assertEqual(expected, transform(dict_value, schema))
 def test_drops_fields_which_are_unsupported(self):
     schema = {"type": "object",
               "properties": { "name": {"type": "string"}}}
     metadata = {('properties','name'): {"inclusion": "unsupported"}}
     dict_value = {"name": "chicken"}
     self.assertEqual({}, transform(dict_value, schema, NO_INTEGER_DATETIME_PARSING, metadata=metadata))
 def test_keeps_fields_without_metadata(self):
     schema = {"type": "object",
               "properties": { "name": {"type": "string"}}}
     metadata = {('properties','age'): {"inclusion": "automatic"}}
     dict_value = {"name": "chicken"}
     self.assertEqual({"name": "chicken"}, transform(dict_value, schema, NO_INTEGER_DATETIME_PARSING, metadata=metadata))
 def test_keeps_selected_data_from_dicts(self):
     schema = {"type": "object",
               "properties": { "name": {"type": "string"}}}
     metadata = {('properties','name'): {"selected": True}}
     dict_value = {"name": "chicken"}
     self.assertEqual({"name": "chicken"}, transform(dict_value, schema, NO_INTEGER_DATETIME_PARSING, metadata=metadata))
 def test_drops_no_data_when_not_dict(self):
     schema = {"type": "string"}
     metadata = {}
     string_value = "hello"
     self.assertEqual(string_value, transform(string_value, schema, NO_INTEGER_DATETIME_PARSING, metadata=metadata))