def test_default_encoder_is_partial(self): class MockField(interfaces.JSONDeSerializable): # pylint: disable=missing-docstring def to_partial_json(self): return 'foo' # pragma: no cover @classmethod def from_json(cls, jobj): pass # pragma: no cover mock_field = MockField() from acme.jose.json_util import Field self.assertTrue(Field.default_encoder(mock_field) is mock_field) # in particular... self.assertNotEqual('foo', Field.default_encoder(mock_field))
def test_no_omit_boolean(self): from acme.jose.json_util import Field for default, omitempty, value in itertools.product([True, False], [True, False], [True, False]): self.assertFalse( Field("foo", default=default, omitempty=omitempty).omit(value))
def setUp(self): from acme.jose.json_util import Field from acme.jose.json_util import JSONObjectWithFieldsMeta self.field = Field('Baz') self.field2 = Field('Baz2') # pylint: disable=invalid-name,missing-docstring,too-few-public-methods # pylint: disable=blacklisted-name class A(object): __metaclass__ = JSONObjectWithFieldsMeta __slots__ = ('bar',) baz = self.field class B(A): pass class C(A): baz = self.field2 self.a_cls = A self.b_cls = B self.c_cls = C
class MockJSONObjectWithFields(JSONObjectWithFields): # pylint: disable=invalid-name,missing-docstring,no-self-argument # pylint: disable=too-few-public-methods x = Field('x', omitempty=True, encoder=(lambda x: x * 2), decoder=(lambda x: x / 2)) y = Field('y') z = Field('Z') # on purpose uppercase @y.encoder def y(value): if value == 500: raise errors.SerializationError() return value @y.decoder def y(value): if value == 500: raise errors.DeserializationError() return value
def test_descriptors(self): mock_value = mock.MagicMock() # pylint: disable=missing-docstring def decoder(unused_value): return 'd' def encoder(unused_value): return 'e' from acme.jose.json_util import Field field = Field('foo') field = field.encoder(encoder) self.assertEqual('e', field.encode(mock_value)) field = field.decoder(decoder) self.assertEqual('e', field.encode(mock_value)) self.assertEqual('d', field.decode(mock_value))
def test_default_decoder_passthrough(self): mock_value = mock.MagicMock() from acme.jose.json_util import Field self.assertTrue(Field.default_decoder(mock_value) is mock_value)
def test_default_decoder_dict_to_frozendict(self): from acme.jose.json_util import Field obj = Field.default_decoder({'x': 2}) self.assertTrue(isinstance(obj, util.frozendict)) self.assertEqual(obj, util.frozendict(x=2))
def test_default_decoder_list_to_tuple(self): from acme.jose.json_util import Field self.assertEqual((1, 2, 3), Field.default_decoder([1, 2, 3]))