Exemplo n.º 1
0
    def test_dumps_and_loads(self):
        in_resource = IdentifiableBook(
            id=uuid.uuid4(),
            purchased_from=From.Ebay,
            title='Consider Phlebas',
            isbn='0-333-45430-8',
            num_pages=471,
            rrp=19.50,
            fiction=True,
            genre="sci-fi",
            authors=[Author(name="Iain M. Banks")],
            publisher=Publisher(name="Macmillan"),
        )

        data = json_codec.dumps(in_resource)
        out_resource = json_codec.loads(data)

        assert out_resource.id == in_resource.id
        assert out_resource.purchased_from == in_resource.purchased_from
        assert out_resource.title == in_resource.title
        assert out_resource.isbn == in_resource.isbn
        assert out_resource.num_pages == in_resource.num_pages
        assert out_resource.rrp == in_resource.rrp
        assert out_resource.fiction == in_resource.fiction
        assert out_resource.genre == in_resource.genre
        assert out_resource.authors[0].name == in_resource.authors[0].name
        assert out_resource.publisher.name == in_resource.publisher.name
Exemplo n.º 2
0
    def test_deserialise(self):
        resource = json_codec.loads(
            '{"$": "odin.tests.AmountResource", "a": null, "b": 10, "c": [23.66, "AUD"]}'
        )

        self.assertEqual(None, resource.a)
        self.assertEqual(Amount(10), resource.b)
        self.assertEqual(Amount(23.66, "AUD"), resource.c)
Exemplo n.º 3
0
    def test_deserialise(self):
        resource = json_codec.loads(
            '{"$": "odin.tests.AmountResource", "a": null, "b": 10, "c": [23.66, "AUD"]}'
        )

        self.assertEqual(None, resource.a)
        self.assertEqual(Amount(10), resource.b)
        self.assertEqual(Amount(23.66, "AUD"), resource.c)
Exemplo n.º 4
0
    def test_deserialise(self):
        resource = json_codec.loads(
            '{"$": "odin.tests.AmountResource", "a": null, "b": 10, "c": [23.66, "AUD"]}'
        )

        assert None == resource.a
        assert Amount(10) == resource.b
        assert Amount(23.66, "AUD") == resource.c
Exemplo n.º 5
0
    def test_deserialise(self):
        resource = json_codec.loads(
            '{"$": "odin.tests.GeoResource", '
            '"lat_a": null, "lat_b": 23.67, "lat_c": -23.67, '
            '"latlng_a": null, "latlng_b": [23.67, -123.56], '
            '"lng_a": null, "lng_b": 123.56, "lng_c": -123.56, '
            '"point_a": null, "point_b": [66.66, -33.33]}')

        self.assertEqual(None, resource.lat_a)
        self.assertEqual(latitude(23.67), resource.lat_b)
        self.assertEqual(latitude(-23.67), resource.lat_c)
        self.assertEqual(None, resource.lng_a)
        self.assertEqual(longitude(123.56), resource.lng_b)
        self.assertEqual(longitude(-123.56), resource.lng_c)
        self.assertEqual(None, resource.latlng_a)
        self.assertEqual(latlng(23.67, -123.56), resource.latlng_b)
        self.assertEqual(None, resource.point_a)
        self.assertEqual(point(66.66, -33.33), resource.point_b)
Exemplo n.º 6
0
    def test_deserialise(self):
        resource = json_codec.loads(
            '{"$": "odin.tests.GeoResource", '
            '"lat_a": null, "lat_b": 23.67, "lat_c": -23.67, '
            '"latlng_a": null, "latlng_b": [23.67, -123.56], '
            '"lng_a": null, "lng_b": 123.56, "lng_c": -123.56, '
            '"point_a": null, "point_b": [66.66, -33.33]}'
        )

        assert None == resource.lat_a
        assert latitude(23.67) == resource.lat_b
        assert latitude(-23.67) == resource.lat_c
        assert None == resource.lng_a
        assert longitude(123.56) == resource.lng_b
        assert longitude(-123.56) == resource.lng_c
        assert None == resource.latlng_a
        assert latlng(23.67, -123.56) == resource.latlng_b
        assert None == resource.point_a
        assert point(66.66, -33.33) == resource.point_b
Exemplo n.º 7
0
    def test_deserialise(self):
        resource = json_codec.loads(
            '{"$": "odin.tests.GeoResource", '
            '"lat_a": null, "lat_b": 23.67, "lat_c": -23.67, '
            '"latlng_a": null, "latlng_b": [23.67, -123.56], '
            '"lng_a": null, "lng_b": 123.56, "lng_c": -123.56, '
            '"point_a": null, "point_b": [66.66, -33.33]}'
        )

        self.assertEqual(None, resource.lat_a)
        self.assertEqual(latitude(23.67), resource.lat_b)
        self.assertEqual(latitude(-23.67), resource.lat_c)
        self.assertEqual(None, resource.lng_a)
        self.assertEqual(longitude(123.56), resource.lng_b)
        self.assertEqual(longitude(-123.56), resource.lng_c)
        self.assertEqual(None, resource.latlng_a)
        self.assertEqual(latlng(23.67, -123.56), resource.latlng_b)
        self.assertEqual(None, resource.point_a)
        self.assertEqual(point(66.66, -33.33), resource.point_b)
Exemplo n.º 8
0
    def test_dumps_and_loads(self):
        in_resource = Book(
            title='Consider Phlebas',
            num_pages=471,
            rrp=19.50,
            fiction=True,
            genre="sci-fi",
            authors=[Author(name="Iain M. Banks")],
            publisher=Publisher(name="Macmillan"),
        )

        data = json_codec.dumps(in_resource)
        out_resource = json_codec.loads(data)

        self.assertEqual(out_resource.title, in_resource.title)
        self.assertEqual(out_resource.num_pages, in_resource.num_pages)
        self.assertEqual(out_resource.rrp, in_resource.rrp)
        self.assertEqual(out_resource.fiction, in_resource.fiction)
        self.assertEqual(out_resource.genre, in_resource.genre)
        self.assertEqual(out_resource.authors[0].name, in_resource.authors[0].name)
        self.assertEqual(out_resource.publisher.name, in_resource.publisher.name)
Exemplo n.º 9
0
    def test_dumps_and_loads(self):
        in_resource = Book(
            title='Consider Phlebas',
            isbn='0-333-45430-8',
            num_pages=471,
            rrp=19.50,
            fiction=True,
            genre="sci-fi",
            authors=[Author(name="Iain M. Banks")],
            publisher=Publisher(name="Macmillan"),
        )

        data = json_codec.dumps(in_resource)
        out_resource = json_codec.loads(data)

        self.assertEqual(out_resource.title, in_resource.title)
        self.assertEqual(out_resource.isbn, in_resource.isbn)
        self.assertEqual(out_resource.num_pages, in_resource.num_pages)
        self.assertEqual(out_resource.rrp, in_resource.rrp)
        self.assertEqual(out_resource.fiction, in_resource.fiction)
        self.assertEqual(out_resource.genre, in_resource.genre)
        self.assertEqual(out_resource.authors[0].name, in_resource.authors[0].name)
        self.assertEqual(out_resource.publisher.name, in_resource.publisher.name)
Exemplo n.º 10
0
    def test_dumps_and_loads(self):
        in_resource = Book(
            title='Consider Phlebas',
            isbn='0-333-45430-8',
            num_pages=471,
            rrp=19.50,
            fiction=True,
            genre="sci-fi",
            authors=[Author(name="Iain M. Banks")],
            publisher=Publisher(name="Macmillan"),
        )

        data = json_codec.dumps(in_resource)
        out_resource = json_codec.loads(data)

        assert out_resource.title == in_resource.title
        assert out_resource.isbn == in_resource.isbn
        assert out_resource.num_pages == in_resource.num_pages
        assert out_resource.rrp == in_resource.rrp
        assert out_resource.fiction == in_resource.fiction
        assert out_resource.genre == in_resource.genre
        assert out_resource.authors[0].name == in_resource.authors[0].name
        assert out_resource.publisher.name == in_resource.publisher.name
Exemplo n.º 11
0
 def get_transformed_object(self, data, from_resource, mapping):
     from_obj = json_codec.loads(json.dumps(data), resource=from_resource)
     transformed = json.loads(json_codec.dumps(mapping.apply(from_obj)))
     return self.remove_keys_from_dict(transformed)
Exemplo n.º 12
0
 def get_transformed_object(self, data, from_resource, mapping):
     """Transforms data into the target object."""
     from_obj = json_codec.loads(json.dumps(data), resource=from_resource)
     return json.loads(json_codec.dumps(mapping.apply(from_obj)))