def setUp(self): super(TestModel, self).setUp() self.modeldir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self.modeldir) self.models = ModelManager(self.modeldir, MyModel)
InitScript, ] class SharedStorage(Model): FIELDS = [ Field('type', data_type=str), Field('source', data_type=str) ] class ContainerRequest(Container): pass hosts = ModelManager(MODELS_DIR, Host) users = ModelManager(MODELS_DIR, User) shared_storage = ModelManager(MODELS_DIR, SharedStorage) container_requests = ModelManager(MODELS_DIR, ContainerRequest) def _get_user_by_openid(openid): for x in users.list(): x = users.get(x) if x.openid == openid: return x return None users.get_user_by_openid = _get_user_by_openid def _generate_api_key():
class TestModel(unittest.TestCase): def setUp(self): super(TestModel, self).setUp() self.modeldir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self.modeldir) self.models = ModelManager(self.modeldir, MyModel) def test_create_validate(self): with self.assertRaises(ModelError): self.models.create('m1', {'strfield': 'x'}) with self.assertRaises(ModelError): self.models.create('m1', {'strfield': 'x', 'intfield': 'x'}) def test_create(self): self.models.create('m1', {'strfield': 'x', 'intfield': 42}) m = self.models.get('m1') self.assertIn('intfield', dir(m)) self.assertIn('strfield', dir(m)) self.assertEqual('x', m.strfield) self.assertEqual(42, m.intfield) def test_update(self): self.models.create('m1', {'strfield': 'x', 'intfield': 42}) m = self.models.get('m1') m.update({'strfield': 'y'}) m = self.models.get('m1') self.assertEqual('y', m.strfield) self.assertEqual(42, m.intfield) with self.assertRaises(ModelError): m.update({'intfield': '12'}) def test_delete(self): self.models.create('m1', {'strfield': 'x', 'intfield': 42}) m = self.models.get('m1') m.delete() with self.assertRaises(ModelError): m = self.models.get('m1')