def test_single_inverse_link(self): """ Can resolve a single link. """ created_bazes = [] baz_ids = [] for i in range(0, 3): baz = {'name': 'Baz#%d' % i, '_id': '%d' % i} created_bazes.append(baz) baz_ids.append(baz['_id']) foo = {'_id': '123', 'bazes': baz_ids} foos = api.interfaces['foos'] bazes = api.interfaces['bazes'] foos.storage = Storage() bazes.storage = Storage() foos.storage.get = Mock(return_value=[foo]) bazes.storage.get_by_id = Mock(return_value=created_bazes[0]) linked_foo = bazes.link(baz_ids[0], 'foo') bazes.storage.get_by_id.assert_called_once_with(Baz, baz_ids[0]) foos.storage.get.assert_called_once_with(Foo, filter={'bazes': baz_ids[0]}, limit=1) self.assertEquals(linked_foo, foo)
def test_single_link_get_embedded(self): """ Embedded links are included when fetching the referencing item. """ foo = {'_id': '123', 'stuff': 'foo'} bar = {'_id': '321', 'embedded_foo': '123'} foos = api.interfaces['foos'] bars = api.interfaces['bars'] foos.storage = Storage() bars.storage = Storage() foos.storage.get_by_id = Mock(return_value=foo) bars.storage.get_by_id = Mock(return_value=bar) bar = bars.get('321') self.assertEquals(bar['embedded_foo'], foo)
def test_single_link(self): """ Can get a link through a link. """ foo = {'_id': '123', 'stuff': 'foo'} bar = {'_id': '321', 'foo': '123'} foos = api.interfaces['foos'] bars = api.interfaces['bars'] foos.storage = Storage() bars.storage = Storage() foos.storage.get_by_id = Mock(return_value=foo) bars.storage.get_by_id = Mock(return_value=bar) bars = api.interfaces['bars'] linked_foo = bars.link('321', 'foo') self.assertEquals(linked_foo, foo) bars.storage.get_by_id.assert_called_once_with(Bar, '321') foos.storage.get_by_id.assert_called_once_with(Foo, '123')
def test_multiple_link_get_embedded(self): """ Embedded link list is included when fetching the referencing item. """ created_bazes = [] baz_ids = [] for i in range(0, 3): baz = {'name': 'Baz#%d' % i, '_id': '%d' % i} created_bazes.append(baz) baz_ids.append(baz['_id']) foo = {'_id': '123', 'embedded_bazes': baz_ids} foos = api.interfaces['foos'] bazes = api.interfaces['bazes'] foos.storage = Storage() bazes.storage = Storage() foos.storage.get_by_id = Mock(return_value=foo) bazes.storage.get_by_ids = Mock(return_value=created_bazes) fetched_foo = foos.get(foo['_id']) self.assertEquals(fetched_foo['embedded_bazes'], created_bazes)
def test_pass(self): """ Should do nothing special when initialized with a well-defined model """ model = Model(storage=Storage()) class Foo(model.Entity): bar = Link('Bar') class Bar(model.Entity): foos = ListOf(Link(Foo)) Foo.get_link('bar') Bar.get_link('foos')
def test_fail_add_to_frozen(self): """ Can't add an entity to a frozen model """ model = Model(storage=Storage()) class Foo(model.Entity): pass model.freeze() with self.assertRaises(Exception): class Bar(model.Entity): pass
import unittest import json import msgpack from mock import Mock import urllib from flask import Flask from cellardoor import errors from cellardoor.api import API from cellardoor.wsgi.flask_integration import create_blueprint from cellardoor.model import Model, Entity, Text, Link, ListOf from cellardoor.storage import Storage from cellardoor.api.interface import ALL, LIST, GET, CREATE model = Model(storage=Storage()) api = API(model) class Foo(model.Entity): name = Text(required=True) bar = Link('Bar') bazes = ListOf(Link('Baz')) class Bar(model.Entity): pass class Baz(model.Entity): pass
def get_interface(self, name, storage=None): if storage is None: storage = Storage() interface = api.interfaces[name] interface.set_storage(storage) return interface
def setUp(self): storage = Storage() model.storage = storage for interface in api.interfaces.values(): interface.set_storage(storage)
from cellardoor.storage import Storage from cellardoor import errors from cellardoor.authorization import ObjectProxy identity = ObjectProxy('identity') item = ObjectProxy('item') class CopyingMock(Mock): def __call__(self, *args, **kwargs): args = deepcopy(args) kwargs = deepcopy(kwargs) return super(CopyingMock, self).__call__(*args, **kwargs) storage = Storage() model = Model(storage=storage) api = API(model) class Foo(model.Entity): stuff = Text(required=True) optional_stuff = Text() bars = InverseLink('Bar', 'foo') bazes = ListOf(Link('Baz')) embedded_bazes = ListOf(Link('Baz', embeddable=True)) embedded_foos = ListOf( Link('Foo', embeddable=True, embed_by_default=False, embedded_fields=('stuff', )))
import unittest from mock import Mock from cellardoor import errors from cellardoor.api import API, StandardOptionsMixin, InterfaceProxy, FilterProxy, LinkProxy from cellardoor.api.methods import ALL from cellardoor.storage import Storage from cellardoor.model import Model, Entity storage = Storage() storage.create = Mock() model = Model(storage=storage) api = API(model) class Foo(model.Entity): pass class Foos(api.Interface): entity = Foo method_authorization = { ALL: None } class TestStandardOptionsMixin(unittest.TestCase): def test_has_options(self): options = StandardOptionsMixin() self.assertTrue(hasattr(options, '_options')) self.assertEquals(options._options, {})
def test_abstract_storage(self): storage = Storage() with self.assertRaises(NotImplementedError): storage.get(None) with self.assertRaises(NotImplementedError): storage.get_by_ids(None, None) with self.assertRaises(NotImplementedError): storage.get_by_id(None, None) with self.assertRaises(NotImplementedError): storage.create(None, None) with self.assertRaises(NotImplementedError): storage.update(None, None, None) with self.assertRaises(NotImplementedError): storage.delete(None, None) with self.assertRaises(NotImplementedError): storage.check_filter(None, None, None)