diff --git a/sideboard/tests/test_lib.py b/sideboard/tests/test_lib.py index e435121..a355e57 100644 --- a/sideboard/tests/test_lib.py +++ b/sideboard/tests/test_lib.py @@ -12,259 +12,7 @@ import cherrypy from mock import Mock -from sideboard.lib import Model, serializer, ajax, is_listy, log, cached_property, request_cached_property, threadlocal, register_authenticator, restricted, all_restricted, RWGuard - - -class TestModel(TestCase): - def assert_model(self, data, unpromoted=None): - model = Model(data, 'test', unpromoted) - self.assertEqual('some_uuid', model.id) - self.assertEqual('some_uuid', model['id']) - self.assertEqual(5, model.foo) - self.assertEqual(5, model['foo']) - self.assertEqual({'baz': 'baf'}, model.bar) - self.assertEqual({'baz': 'baf'}, model['bar']) - - def test_missing_key(self): - model = Model({}, 'test') - self.assertIs(None, model.does_not_exist) - - def test_id_unsettable(self): - model = Model({'id': 'some_uuid'}, 'test') - model.id = 'some_uuid' - model['id'] = 'some_uuid' - self.assertEqual(model.id, 'some_uuid') - with self.assertRaises(Exception): - model.id = 'another_uuid' - with self.assertRaises(Exception): - model['id'] = 'another_uuid' - - def test_extra_data_only(self): - d = { - 'id': 'some_uuid', - 'extra_data': { - 'test_foo': 5, - 'test_bar': {'baz': 'baf'} - } - } - for data in [d, dict(d, test_data={})]: - self.assert_model(data) - - model = Model(d, 'test') - model.fizz = 'buzz' - model['buzz'] = 'fizz' - self.assertEqual('fizz', model._data['extra_data']['test_buzz']) - self.assertEqual('buzz', model._data['extra_data']['test_fizz']) - - def test_project_data(self): - d = { - 'id': 'some_uuid', - 'test_data': { - 'foo': 5, - 'bar': {'baz': 'baf'} - } - } - for data in [d, dict(d, extra_data={})]: - self.assert_model(data) - model = Model(data, 'test') - model.fizz = 'buzz' - model['buzz'] = 'fizz' - self.assertEqual('fizz', model._data['test_data']['buzz']) - self.assertEqual('buzz', model._data['test_data']['fizz']) - - def test_both_data(self): - data = { - 'id': 'some_uuid', - 'extra_data': { - 'test_foo': 5 - }, - 'test_data': { - 'bar': {'baz': 'baf'} - } - } - self.assert_model(data) - model = Model(data, 'test') - model.fizz = 'buzz' - model['buzz'] = 'fizz' - self.assertEqual('fizz', model._data['test_data']['buzz']) - self.assertEqual('buzz', model._data['test_data']['fizz']) - model.foo = 6 - model.bar = {'baf': 'baz'} - self.assertEqual({}, model._data['extra_data']) - self.assertEqual(6, model.foo) - self.assertEqual({'baf': 'baz'}, model['bar']) - self.assertEqual(6, model._data['test_data']['foo']) - self.assertEqual({'baf': 'baz'}, model._data['test_data']['bar']) - - def test_unpromoted_prepromotion(self): - data = { - 'id': 'some_uuid', - 'extra_data': { - 'foo': 5, - 'test_bar': {'baz': 'baf'} - } - } - self.assert_model(data, {'foo'}) - model = Model(data, 'test', unpromoted={'foo'}) - model.foo = 6 - self.assertEqual(6, model.foo) - self.assertNotIn('foo', model._data) - self.assertEqual(6, model._data['extra_data']['foo']) - - def test_unpromoted_postpromotion(self): - data = { - 'id': 'some_uuid', - 'foo': 5, - 'extra_data': { - 'test_bar': {'baz': 'baf'} - } - } - self.assert_model(data, {'foo'}) - model = Model(data, 'test', unpromoted={'foo'}) - model.foo = 6 - self.assertEqual(6, model.foo) - self.assertEqual(6, model._data['foo']) - self.assertNotIn('foo', model._data['extra_data']) - - def test_unpromoted_not_present(self): - data = {'id': 'some_uuid'} - model = Model(data, 'test', unpromoted={'foo'}) - self.assertIs(None, model.foo) - model.foo = 'bar' - self.assertEqual('bar', model.foo) - self.assertNotIn('foo', model._data) - self.assertEqual('bar', model._data['extra_data']['foo']) - - def test_subclass(self): - self.assertRaises(Exception, Model, {}) - - class TestModel(Model): - _prefix = 'test' - _unpromoted = {'foo'} - _defaults = {'baz': 'baf'} - - data = {'id': 'some_uuid'} - model = TestModel(data) - self.assertIs(None, model.foo) - model.foo = 'bar' - self.assertEqual('baf', model.baz) - self.assertEqual('bar', model.foo) - self.assertNotIn('foo', model._data) - self.assertEqual('bar', model._data['extra_data']['foo']) - - def test_defaults(self): - data = { - 'extra_data': { - 'test_foo': -1, - 'bar': -2 - }, - 'test_data': { - 'baz': -3 - }, - 'baf': -4 - } - model = Model(data, 'test', {'bar', 'baf', 'fizz'}, { - 'foo': 1, - 'bar': 2, - 'baz': 3, - 'baf': 4, - 'fizz': 5, - 'buzz': 6 - }) - self.assertEqual(model.foo, -1) - self.assertEqual(model.bar, -2) - self.assertEqual(model.baz, -3) - self.assertEqual(model.baf, -4) - self.assertEqual(model.fizz, 5) - self.assertEqual(model.buzz, 6) - model.foo, model.bar, model.baz, model.baf = range(11, 15) - self.assertEqual(model.foo, 11) - self.assertEqual(model.bar, 12) - self.assertEqual(model.baz, 13) - self.assertEqual(model.baf, 14) - self.assertEqual(model.fizz, 5) - self.assertEqual(model.buzz, 6) - - def test_to_dict(self): - data = { - 'id': 'some_uuid', - 'extra_data': { - 'test_foo': 5, - 'fizz': 'buzz', - 'spam': 'eggs' - }, - 'test_data': {'bar': 'baz'} - } - model = Model(data, 'test', {'fizz'}) - serialized = { - 'id': 'some_uuid', - 'foo': 5, - 'bar': 'baz', - 'fizz': 'buzz', - 'extra_data': {'spam': 'eggs'} - } - self.assertEqual(model.to_dict(), serialized) - serialized.pop('extra_data') - self.assertEqual(dict(model), serialized) - - def test_query(self): - model = Model({'_model': 'Test', 'id': 'some_uuid'}, 'test') - self.assertEqual(model.query, { - '_model': 'Test', - 'field': 'id', - 'value': 'some_uuid' - }) - for data in [{}, {'_model': 'Test'}, {'id': 'some_uuid'}]: - with self.assertRaises(Exception): - Model(data, 'test').query - - def test_dirty(self): - data = { - 'id': 'some_uuid', - 'spam': 'eggs', - 'extra_data': { - 'test_foo': 5 - }, - 'test_data': { - 'bar': {'baz': 'baf'} - } - } - self.assertEqual(Model(data, 'test').dirty, {}) - - model = Model(data, 'test') - model.spam = 'nee' - self.assertEqual(model.dirty, {'spam': 'nee'}) - - model = Model(data, 'test') - model.foo = 6 - self.assertEqual(model.dirty, {'extra_data': {}, 'test_data': {'foo': 6, 'bar': {'baz': 'baf'}}}) - - model = Model(data, 'test') - model.bar = {'fizz': 'buzz'} - self.assertEqual(model.dirty, {'test_data': {'bar': {'fizz': 'buzz'}}}) - - model = Model(data, 'test') - model.bar['baz'] = 'zab' - self.assertEqual(model.dirty, {'test_data': {'bar': {'baz': 'zab'}}}) - - model = Model(data, 'test') - model.foo = 6 - model.bar = 'baz' - model.spam = 'nee' - model.fizz = 'buzz' - self.assertEqual(model.dirty, { - 'spam': 'nee', - 'test_data': { - 'foo': 6, - 'bar': 'baz', - 'fizz': 'buzz' - }, - 'extra_data': {} - }) - - model = Model({}, 'test') - model.foo = 'bar' - self.assertEqual(model.dirty, {'extra_data': {'test_foo': 'bar'}}) +from sideboard.lib import serializer, ajax, is_listy, log, cached_property, request_cached_property, threadlocal, register_authenticator, restricted, all_restricted, RWGuard class TestSerializer(TestCase):