Esempio n. 1
0
    def test_dump_and_load_(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"),
            published=[datetime.datetime(1987, 1, 1, tzinfo=utc)]
        )

        fp = StringIO()
        json_codec.dump(in_resource, fp)

        fp.seek(0)
        out_resource = json_codec.load(fp)

        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)
        self.assertEqual(out_resource.published[0], in_resource.published[0])
Esempio n. 2
0
    def test_dump_and_load_(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"),
            published=[datetime.datetime(1987, 1, 1, tzinfo=utc)])

        fp = StringIO()
        json_codec.dump(in_resource, fp)

        fp.seek(0)
        out_resource = json_codec.load(fp)

        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
        assert out_resource.published[0] == in_resource.published[0]
Esempio n. 3
0
from __future__ import absolute_import
import unittest
import os
from odin import filtering
from odin.traversal import TraversalPath
from odin.codecs import json_codec

# Open library fixture
with open(os.path.join(os.path.dirname(__file__), 'fixtures',
                       'library.json')) as f:
    library = json_codec.load(f)

first_book = TraversalPath.parse('books[0]').get_value(library)


class FilterTestCase(unittest.TestCase):
    def test_matches_top_level_resource(self):
        flt = filtering.Equal('title', "Consider Phlebas")
        self.assertTrue(flt(first_book))

    def test_matches_multiple_matches_resource(self):
        # Less than fiction $20 with a single author and not fantasy
        flt = filtering.And(
            filtering.Equal('fiction', True),
            filtering.LessThan('rrp', 20),
            filtering.Equal('authors', 1, len),
            filtering.NotEqual('genre', 'fantasy'),
        )
        self.assertTrue(flt(first_book))

    def test_matches_sub_resource(self):
Esempio n. 4
0
 def demand_tariff(self):
     with open('./fixtures/demand_tariff.json') as f:
         tariff = json_codec.load(f, Tariff)
     return tariff
Esempio n. 5
0
 def scheduled_tariff(self):
     with open('./fixtures/scheduled_tariff.json') as f:
         tariff = json_codec.load(f, Tariff)
     return tariff
Esempio n. 6
0
 def supply_payment_tariff(self):
     with open('./fixtures/supply_payment.json') as f:
         tariff = json_codec.load(f, Tariff)
     return tariff
Esempio n. 7
0
 def block_tariff(self):
     with open('./fixtures/block_tariff.json') as f:
         tariff = json_codec.load(f, Tariff)
     return tariff
Esempio n. 8
0
 def seasonal_tariff(self):
     with open('./fixtures/seasonal_tariff.json') as f:
         tariff = json_codec.load(f, Tariff)
     return tariff
Esempio n. 9
0
 def test_load_invalid_data(self):
     with pytest.raises(exceptions.ValidationError):
         json_codec.load(open(os.path.join(FIXTURE_PATH_ROOT, "book-invalid.json")))
Esempio n. 10
0
    def test_load_valid_data(self):
        library = json_codec.load(open(os.path.join(FIXTURE_PATH_ROOT, "book-valid.json")))

        assert "Consider Phlebas" == library.books[0].title
Esempio n. 11
0
from __future__ import absolute_import
import unittest
import os
from odin import filtering
from odin.traversal import TraversalPath
from odin.codecs import json_codec


# Open library fixture
with open(os.path.join(os.path.dirname(__file__), 'fixtures', 'library.json')) as f:
    library = json_codec.load(f)

first_book = TraversalPath.parse('books[0]').get_value(library)


class FilterTestCase(unittest.TestCase):
    def test_matches_top_level_resource(self):
        flt = filtering.Equal('title', "Consider Phlebas")
        self.assertTrue(flt(first_book))

    def test_matches_multiple_matches_resource(self):
        # Less than fiction $20 with a single author and not fantasy
        flt = filtering.And(
            filtering.Equal('fiction', True),
            filtering.LessThan('rrp', 20),
            filtering.Equal('authors', 1, len),
            filtering.NotEqual('genre', 'fantasy'),
        )
        self.assertTrue(flt(first_book))

    def test_matches_sub_resource(self):
Esempio n. 12
0
 def test_load_invalid_data(self):
     with self.assertRaises(exceptions.ValidationError):
         json_codec.load(open(os.path.join(FIXTURE_PATH_ROOT, "book-invalid.json")))
Esempio n. 13
0
    def test_load_valid_data(self):
        library = json_codec.load(open(os.path.join(FIXTURE_PATH_ROOT, "book-valid.json")))

        self.assertEqual("Consider Phlebas", library.books[0].title)