def test_10b(self): "test that loading can continue if foreign model instance is missing" tfile = tempfile.NamedTemporaryFile() f = MyModel(int_type=1).save() m = f.mydepmodel_set.create(pk1=1) with open(tfile.name, 'w') as f: f.write(json.dumps([m.dict])) MyModel.objects.clear() man = Manager(MyDepModel) self.assertIsNone(man.load(JSONStorage(tfile.name))) self.assertEqual(0, man.count)
def test_1(self): "verify class/instance implementation" man = Manager(MyModel) self.assertEqual("MyModelManager", man._name) self.assertTrue(repr(man)) self.assertTrue(str(man))
def test_10(self): "test that duplicate pk throws an error" tfile = tempfile.NamedTemporaryFile() m = MyModel(int_type=1) with open(tfile.name, 'w') as f: f.write(json.dumps([m.dict, m.dict])) man = Manager(MyModel) self.assertRaises(KeyError, man.load, JSONStorage(tfile.name))
def test_10a(self): "test that empty pk throws an error" tfile = tempfile.NamedTemporaryFile() m = MyModel(str_type='string data') with open(tfile.name, 'w') as f: f.write(json.dumps([m.dict, m.dict])) man = Manager(MyModel) self.assertRaises(MyModel.EmptyPrimaryKey, man.load, JSONStorage(tfile.name))
def test_5(self): "test saving actual model objects" tfile = tempfile.NamedTemporaryFile() now = tznow() m = MyModel(int_type=3, str_type='string', dt_type=now) d = { u'int_type': 3, u'str_type': u'string', u'dt_type': now.isoformat() } man = Manager(MyModel) man.save(m) storage = JSONStorage(tfile.name) man.store(storage) man.store(storage, force=True) # just make sure force works man.store(storage) # now verify this is a no op from_disk = [e for e in storage.read(MyModel)] self.assertDictEqual(d, from_disk[0])
def test_3(self): "test deleting" man = Manager(MyModel) m1 = MyModel(int_type=1) man.save(m1, dirty=False) self.assertEqual(1, len(man)) man.delete(m1) self.assertEqual(0, len(man)) # make sure deleting a non-existant instance is a no-op man.delete(m1) self.assertEqual(0, len(man)) self.assertTrue(man.dirty)
def test_2(self): "test saving" man = Manager(MyModel) # no primary key in m1 m1 = MyModel() self.assertRaises(AssertionError, man.save, m1) # successful save m1.int_type = 1 self.assertEqual(1, m1.int_type) self.assertEqual(1, m1.pk) man.save(m1) self.assertEqual(1, len(man)) self.assertEqual(1, man.count) # same primary key, same len man.save(m1) self.assertEqual(1, len(man))
def test_6(self): "test loading actual model objects" tfile = tempfile.NamedTemporaryFile() now = tznow() m1 = MyModel(int_type=3, str_type='string', dt_type=now) man = Manager(MyModel) man.save(m1) storage = JSONStorage(tfile.name) man.store(storage) man = Manager(MyModel) self.assertRaises(KeyError, man.get, m1.pk) man.load(storage) m2 = man.get(m1.pk) self.assertDictEqual(m1.dict, m2.dict)