Beispiel #1
0
    def test_message_to_dict(self):
        struct0 = struct_pb2.Struct()
        struct0['a'] = 0
        struct1 = struct_pb2.Struct()
        struct1['a'] = 1

        dt0 = datetime.datetime(2018, 2, 20)
        dt1 = datetime.datetime(2018, 2, 21)
        ts0 = timestamp_pb2.Timestamp()
        ts0.FromDatetime(dt0)
        ts1 = timestamp_pb2.Timestamp()
        ts1.FromDatetime(dt1)
        dur = duration_pb2.Duration()
        dur.FromTimedelta(datetime.timedelta(seconds=2, microseconds=3))

        msg = testmessage_pb2.TestMessage(
            str='a',
            strs=['a', 'b'],
            num=1,
            nums=[0, 1, 2],
            nested=testmessage_pb2.NestedMessage(num=1, str='a'),
            nesteds=[
                testmessage_pb2.NestedMessage(num=1, str='a'),
                testmessage_pb2.NestedMessage(num=2, str='b'),
            ],
            empty=empty_pb2.Empty(),
            empties=[empty_pb2.Empty(), empty_pb2.Empty()],
            e=testmessage_pb2.E1,
            es=[testmessage_pb2.E0, testmessage_pb2.E2],
            struct=struct0,
            structs=[struct0, struct1],
            timestamp=ts0,
            timestamps=[ts0, ts1],
            repeated_container=testmessage_pb2.RepeatedContainer(nums=[1, 2]),
            duration=dur,
        )
        row = bqh.message_to_dict(msg)

        expected = {
            'str': u'a',
            'strs': [u'a', u'b'],
            'num': 1L,
            'nums': [0L, 1L, 2L],
            'nested': {
                'num': 1L,
                'str': u'a'
            },
            'nesteds': [
                {
                    'num': 1L,
                    'str': u'a'
                },
                {
                    'num': 2L,
                    'str': u'b'
                },
            ],

            # empty messages are omitted
            'e': 'E1',
            'es': ['E0', 'E2'],

            # structs are compared separately.
            'timestamp': dt0.isoformat(),
            'timestamps': [dt0.isoformat(), dt1.isoformat()],
            'repeated_container': {
                'nums': [1L, 2L]
            },
            'duration': 2.000003,
        }
Beispiel #2
0
 def test_message_to_dict_repeated_container_with_no_elems(self):
     row = bqh.message_to_dict(
         testmessage_pb2.TestMessage(
             repeated_container=testmessage_pb2.RepeatedContainer()))
     self.assertNotIn('repeated_container', row)
Beispiel #3
0
 def test_message_to_dict_omit_null(self):
     with self.assertRaisesRegexp(
             ValueError, '^Invalid value -1 for enum type bigquery.E$'):
         bqh.message_to_dict(testmessage_pb2.TestMessage(e=-1))
Beispiel #4
0
 def test_message_to_dict_empty(self):
     row = bqh.message_to_dict(testmessage_pb2.TestMessage())
     expected = {'e': 'E0', 'str': u'', 'num': 0}
     self.assertEqual(row, expected)