def test_default_encoder_is_partial(self): class MockField(interfaces.JSONDeSerializable): def to_partial_json(self): return 'foo' # pragma: no cover @classmethod def from_json(cls, jobj): pass # pragma: no cover mock_field = MockField() from josepy.json_util import Field self.assertIs(Field.default_encoder(mock_field), mock_field) # in particular... self.assertNotEqual('foo', Field.default_encoder(mock_field))
def test_no_omit_boolean(self): from josepy.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 josepy.json_util import Field, JSONObjectWithFieldsMeta self.field = Field('Baz') self.field2 = Field('Baz2') class A(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): 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
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 setUp(self): from josepy.json_util import Field, 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
def test_descriptors(self): mock_value = mock.MagicMock() def decoder(unused_value): return 'd' def encoder(unused_value): return 'e' from josepy.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 josepy.json_util import Field self.assertIs(Field.default_decoder(mock_value), mock_value)
def test_default_decoder_dict_to_frozendict(self): from josepy.json_util import Field obj = Field.default_decoder({'x': 2}) self.assertIsInstance(obj, util.frozendict) self.assertEqual(obj, util.frozendict(x=2))
def test_default_decoder_list_to_tuple(self): from josepy.json_util import Field self.assertEqual((1, 2, 3), Field.default_decoder([1, 2, 3]))