Esempio 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
Esempio n. 2
0
    def test_serialise(self):
        resource = AmountResource(a=None, b=Amount(10), c=Amount(22.02, 'AUD'))

        actual = json_codec.dumps(resource, sort_keys=True)

        self.assertEqual(
            actual,
            '{"$": "odin.tests.AmountResource", "a": null, "b": [10.0, "XXX"], "c": [22.02, "AUD"]}'
        )
Esempio n. 3
0
 def get_linked_agents(self, agents):
     """Transforms and creates ArchivesSpace agents."""
     linked_agents = []
     for agent in agents:
         agent_data = map_agents(SourceCreator(type=agent["type"], name=agent["name"]))
         agent_ref = self.aspace_client.get_or_create(
             agent["type"], "title", agent["name"],
             self.start_time, json.loads(json_codec.dumps(agent_data)))
         linked_agents.append({"uri": agent_ref})
     return linked_agents
Esempio n. 4
0
    def test_serialise(self):
        resource = AmountResource(
            a=None,
            b=Amount(10),
            c=Amount(22.02, 'AUD')
        )

        actual = json_codec.dumps(resource, sort_keys=True)

        self.assertEqual(
            actual,
            '{"$": "odin.tests.AmountResource", "a": null, "b": [10.0, "XXX"], "c": [22.02, "AUD"]}'
        )
Esempio n. 5
0
    def test_dumps_with_valid_data(self):
        book = Book(title="Consider Phlebas",
                    isbn="0-333-45430-8",
                    num_pages=471,
                    rrp=19.50,
                    genre="sci-fi",
                    fiction=True,
                    published=datetime.datetime(1987, 1, 1, tzinfo=utc))
        book.publisher = Publisher(name="Macmillan")
        book.authors.append(Author(name="Iain M. Banks"))

        library = Library(name="Public Library", books=[book])
        actual = json_codec.dumps(library)

        assertJSONEqual(
            """
{
    "$": "Library",
    "name": "Public Library",
    "books": [
        {
            "$": "library.Book",
            "publisher": {
                "$": "Publisher",
                "name": "Macmillan"
            },
            "num_pages": 471,
            "isbn": "0-333-45430-8",
            "title": "Consider Phlebas",
            "authors": [
                {
                    "$": "Author",
                    "name": "Iain M. Banks"
                }
            ],
            "fiction": true,
            "published": "1987-01-01T00:00:00+00:00",
            "genre": "sci-fi",
            "rrp": 19.5
        }
    ],
    "subscribers": [],
    "book_count": 1
}
        """, actual)
Esempio n. 6
0
    def test_serialise(self):
        resource = GeoResource(lat_a=None,
                               lat_b=latitude(23.67),
                               lat_c=latitude(-23.67),
                               lng_a=None,
                               lng_b=longitude(123.56),
                               lng_c=longitude(-123.56),
                               latlng_a=None,
                               latlng_b=latlng(23.67, -123.56),
                               point_a=None,
                               point_b=point(66.66, -33.33))

        actual = json_codec.dumps(resource, sort_keys=True)

        self.assertEqual(
            actual, '{"$": "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]}')
Esempio n. 7
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)
Esempio n. 8
0
    def test_dumps_with_valid_data(self):
        book = Book(title="Consider Phlebas", isbn="0-333-45430-8", num_pages=471, rrp=19.50, genre="sci-fi", fiction=True,
                    published=datetime.datetime(1987, 1, 1, tzinfo=utc))
        book.publisher = Publisher(name="Macmillan")
        book.authors.append(Author(name="Iain M. Banks"))

        library = Library(name="Public Library", books=[book])
        actual = json_codec.dumps(library)

        assertJSONEqual("""
{
    "$": "Library",
    "name": "Public Library",
    "books": [
        {
            "$": "library.Book",
            "publisher": {
                "$": "Publisher",
                "name": "Macmillan"
            },
            "num_pages": 471,
            "isbn": "0-333-45430-8",
            "title": "Consider Phlebas",
            "authors": [
                {
                    "$": "Author",
                    "name": "Iain M. Banks"
                }
            ],
            "fiction": true,
            "published": "1987-01-01T00:00:00+00:00",
            "genre": "sci-fi",
            "rrp": 19.5
        }
    ],
    "subscribers": [],
    "book_count": 1
}
        """, actual)
Esempio 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)
Esempio 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
Esempio n. 11
0
    def test_serialise(self):
        resource = GeoResource(
            lat_a=None,
            lat_b=latitude(23.67),
            lat_c=latitude(-23.67),
            lng_a=None,
            lng_b=longitude(123.56),
            lng_c=longitude(-123.56),
            latlng_a=None,
            latlng_b=latlng(23.67, -123.56),
            point_a=None,
            point_b=point(66.66, -33.33)
        )

        actual = json_codec.dumps(resource, sort_keys=True)

        assert (
            actual ==
            '{"$": "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]}'
        )
Esempio n. 12
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)
Esempio n. 13
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)))