Example #1
0
    def test_null_fields_are_preserved(self):
        input_row = '{"float": "10.5"}'
        expected_row = {'float': 10.5, 'string': None}
        schema = self._make_schema([('float', 'FLOAT', []),
                                    ('string', 'STRING', [])])
        coder = _JsonToDictCoder(schema)

        actual = coder.decode(input_row)
        self.assertEqual(expected_row, actual)
Example #2
0
    def test_values_are_converted(self):
        input_row = '{"float": "10.5", "string": "abc"}'
        expected_row = {'float': 10.5, 'string': 'abc'}
        schema = self._make_schema([('float', 'FLOAT', []),
                                    ('string', 'STRING', [])])
        coder = _JsonToDictCoder(schema)

        actual = coder.decode(input_row)
        self.assertEqual(expected_row, actual)
Example #3
0
 def test_coder_is_pickable(self):
     try:
         schema = self._make_schema([
             ('record', 'RECORD', [
                 ('float', 'FLOAT', []),
             ]),
             ('integer', 'INTEGER', []),
         ])
         coder = _JsonToDictCoder(schema)
         pickler.loads(pickler.dumps(coder))
     except pickle.PicklingError:
         self.fail('{} is not pickable'.format(coder.__class__.__name__))
Example #4
0
    def test_record_field_is_properly_converted(self):
        input_row = '{"record": {"float": "55.5"}, "integer": 10}'
        expected_row = {'record': {'float': 55.5}, 'integer': 10}
        schema = self._make_schema([
            ('record', 'RECORD', [
                ('float', 'FLOAT', []),
            ]),
            ('integer', 'INTEGER', []),
        ])
        coder = _JsonToDictCoder(schema)

        actual = coder.decode(input_row)
        self.assertEqual(expected_row, actual)
  def test_record_and_repeatable_field_is_properly_converted(self):
    input_row = b'{"record": [{"float": "55.5"}, {"float": "65.5"}], ' \
                b'"integer": 10}'
    expected_row = {'record': [{'float': 55.5}, {'float': 65.5}], 'integer': 10}
    schema = self._make_schema([
        (
            'record',
            'RECORD',
            'REPEATED', [
                ('float', 'FLOAT', 'NULLABLE', []),
            ]),
        ('integer', 'INTEGER', 'NULLABLE', []),
    ])
    coder = _JsonToDictCoder(schema)

    actual = coder.decode(input_row)
    self.assertEqual(expected_row, actual)