Exemplo n.º 1
0
        class U(object):
            _tagmap = {
                'a': bv.Int64(),
                'b': bv.Void(),
                'c': bv.Struct(S),
                'd': bv.List(bv.Int64()),
                'e': bv.Nullable(bv.Int64()),
                'f': bv.Nullable(bv.Struct(S)),
                'g': bv.Void()
            }
            _catch_all = 'g'
            _tag = None

            def __init__(self, tag, value=None):
                self._tag = tag
                self._value = value

            def get_a(self):
                return self._value

            def get_c(self):
                return self._value

            def get_d(self):
                return self._value
Exemplo n.º 2
0
    def test_json_decoder(self):
        self.assertEqual(json_decode(bv.String(), json.dumps('abc')), 'abc')
        self.assertRaises(bv.ValidationError,
                          lambda: json_decode(bv.String(), json.dumps(32)))

        self.assertEqual(json_decode(bv.UInt32(), json.dumps(123)), 123)
        self.assertRaises(
            bv.ValidationError,
            lambda: json_decode(bv.UInt32(), json.dumps('hello')))

        self.assertEqual(json_decode(bv.Boolean(), json.dumps(True)), True)
        self.assertRaises(bv.ValidationError,
                          lambda: json_decode(bv.Boolean(), json.dumps(1)))

        f = '%a, %d %b %Y %H:%M:%S +0000'
        now = datetime.datetime.utcnow().replace(microsecond=0)
        self.assertEqual(
            json_decode(bv.Timestamp('%a, %d %b %Y %H:%M:%S +0000'),
                        json.dumps(now.strftime(f))), now)
        # Try decoding timestamp with bad type
        self.assertRaises(
            bv.ValidationError, lambda: json_decode(
                bv.Timestamp('%a, %d %b %Y %H:%M:%S +0000'), '1'))
        b = b'\xff' * 5
        self.assertEqual(
            json_decode(bv.Bytes(),
                        json.dumps(base64.b64encode(b).decode('ascii'))), b)
        self.assertRaises(bv.ValidationError,
                          lambda: json_decode(bv.Bytes(), json.dumps(1)))
        self.assertEqual(
            json_decode(bv.Nullable(bv.String()), json.dumps(None)), None)
        self.assertEqual(
            json_decode(bv.Nullable(bv.String()), json.dumps('abc')), 'abc')

        self.assertEqual(json_decode(bv.Void(), json.dumps(None)), None)
        # Check that void can take any input if strict is False.
        self.assertEqual(
            json_decode(bv.Void(), json.dumps(12345), strict=False), None)
        # Check that an error is raised if strict is True and there's a non-null value
        self.assertRaises(
            bv.ValidationError,
            lambda: json_decode(bv.Void(), json.dumps(12345), strict=True))
Exemplo n.º 3
0
 def test_nullable_validator(self):
     n = bv.Nullable(bv.String())
     # Absent case
     n.validate(None)
     # Fails string validation
     self.assertRaises(bv.ValidationError, lambda: n.validate(123))
     # Passes
     n.validate('abc')
     # Stacking nullables isn't supported by our JSON wire format
     self.assertRaises(AssertionError,
                       lambda: bv.Nullable(bv.Nullable(bv.String())))
     self.assertRaises(AssertionError, lambda: bv.Nullable(bv.Void()))
Exemplo n.º 4
0
 def test_json_encoder(self):
     self.assertEqual(json_encode(bv.Void(), None), json.dumps(None))
     self.assertEqual(json_encode(bv.String(), 'abc'), json.dumps('abc'))
     self.assertEqual(json_encode(bv.String(), u'\u2650'),
                      json.dumps(u'\u2650'))
     self.assertEqual(json_encode(bv.UInt32(), 123), json.dumps(123))
     # Because a bool is a subclass of an int, ensure they aren't mistakenly
     # encoded as a true/false in JSON when an integer is the data type.
     self.assertEqual(json_encode(bv.UInt32(), True), json.dumps(1))
     self.assertEqual(json_encode(bv.Boolean(), True), json.dumps(True))
     f = '%a, %d %b %Y %H:%M:%S +0000'
     now = datetime.datetime.utcnow()
     self.assertEqual(
         json_encode(bv.Timestamp('%a, %d %b %Y %H:%M:%S +0000'), now),
         json.dumps(now.strftime(f)))
     b = b'\xff' * 5
     self.assertEqual(json_encode(bv.Bytes(), b),
                      json.dumps(base64.b64encode(b).decode('ascii')))
     self.assertEqual(json_encode(bv.Nullable(bv.String()), None),
                      json.dumps(None))
     self.assertEqual(json_encode(bv.Nullable(bv.String()), u'abc'),
                      json.dumps('abc'))
Exemplo n.º 5
0
 def test_void_validator(self):
     v = bv.Void()
     # Passes: Only case that validates
     v.validate(None)
     # Fails validation
     self.assertRaises(bv.ValidationError, lambda: v.validate(123))