def test_save_on_exit(self): "make sure we can actually save a database" tfile = tempfile.NamedTemporaryFile() class MyModel( Model ): class Meta: ordering = ['int_type','str_type','dt_type'] filename = tfile.name int_type = fields.IntField(primary_key=True) str_type = fields.StringField() dt_type = fields.DateTimeField() # make sure we're starting with an empty file self.assertFalse( open( tfile.name, 'r').read() ) db = Database( models=[MyModel], save_on_exit=True ) now = tznow() m = MyModel(int_type=1, str_type='string', dt_type=now ) m.save() self.assertEqual( 1, len(MyModel.objects) ) del db # save_on_exit is true self.assertTrue( open( tfile.name, 'r').read() )
def test_4(self): "set fields in init" now = tznow() m = MyModel(int_type=3, str_type='string', dt_type=now) self.assertEqual(3, m.int_type) self.assertEqual('string', m.str_type) self.assertEqual(now, m.dt_type)
def test_40(self): "test limit, equivalent to slicing" # for i in range(3): # MyModel(int_type=i, str_type='string %d' % i, dt_type=now).save() now = tznow() instances = [ MyModel(int_type=i, str_type='string %d' % i, dt_type=now) for i in range(3) ] for inst in instances: inst.save() # make sure all ids are unique ids = [ id(instances[0]), id(MyModel.objects._instances[0]), id(MyModel.objects.get(0)) ] self.assertEqual(len(ids), len(set(ids))) self.assertNotEqual(id(MyModel.objects._instances[0]), id(MyModel.objects.limit(0)[0])) self.assertEqual(list, type(MyModel.objects.all().limit(0))) self.assertEqual(3, len(MyModel.objects.all().limit(0))) self.assertEqual(2, len(MyModel.objects.all().limit(2))) self.assertEqual(2, len(MyModel.objects.all().limit(-2))) self.assertEqual(instances[:2], MyModel.objects.all().limit(2)) self.assertEqual(instances[-2:], MyModel.objects.all().limit(-2))
def test_15(self): "test CSVStorage" tfile = tempfile.NamedTemporaryFile() self.assertFalse(CSVStorage(tfile.name).write(MyModel, None)) now = tznow() m1 = MyModel(int_type=1, str_type='a string, with comma', dt_type=now).save() m2 = MyModel(int_type=2, str_type='a string, with comma', dt_type=now).save() storage = CSVStorage(tfile.name) storage.write(MyModel, [m1, m2]) del storage # with open(tfile.name, 'r') as f: # print f.read() storage = CSVStorage(tfile.name) loaded = [e for e in storage.read(MyModel)] self.assertEqual(2, len(loaded)) m = loaded[0] self.assertEqual(1, m.int_type) self.assertEqual('a string, with comma', m.str_type) self.assertEqual(now, m.dt_type)
def test_9(self): "test dict" now = tznow() m = MyModel(int_type=3, str_type='string', dt_type=now) d = {'int_type': 3, 'str_type': 'string', 'dt_type': now.isoformat()} self.assertDictEqual(d, m.dict)
def test_4(self): "verify that Model.objects exists/works (a ModelManager)" now = tznow() m1 = MyModel(int_type=3, str_type='string', dt_type=now) MyModel.objects.save(m1) m2 = m1.objects.get(m1.pk) self.assertDictEqual(m1.dict, m2.dict)
def test_21(self): "test different filter functions" now = tznow() instances = [ MyModel(int_type=i, str_type='string %d' % i, dt_type=now) for i in range(3) ] for inst in instances: inst.save() results = MyModel.objects.filter(int_type__eq=0) self.assertEqual(1, len(results)) results = MyModel.objects.filter(int_type__gt=0) self.assertEqual(2, len(results)) results = MyModel.objects.filter(int_type=0, str_type='string 0') self.assertEqual(1, len(results)) results = MyModel.objects.filter(int_type=0, str_type='string XXX') self.assertEqual(0, len(results)) results = MyModel.objects.filter(iter_type__in=[1]) self.assertEqual(1, len(results)) results = MyModel.objects.filter(iter_type__rin=1) self.assertEqual(1, len(results)) results = MyModel.objects.filter(iter_type__rin=[1]) self.assertEqual(1, len(results)) results = MyModel.objects.filter(str_type__in=['string 1']) self.assertEqual(1, len(results)) results = MyModel.objects.filter(str_type__rin='1') self.assertEqual(1, len(results)) results = MyModel.objects.filter(str_type__re='1$') self.assertEqual(1, len(results)) results = MyModel.objects.filter(str_type__rei='^STR') self.assertEqual(3, len(results)) results = MyModel.objects.filter(dt_type__gt=fromts(0)) self.assertEqual(3, len(results)) # test pk queries results = MyModel.objects.filter(pk=1) self.assertEqual(1, len(results)) results = MyModel.objects.get(pk=1) self.assertEqual(MyModel, type(results))
def test_10(self): "test dumps/loads" now = tznow() m1 = MyModel(int_type=3, str_type='string', dt_type=now) d = m1.dict self.assertTrue(d) m2 = MyModel(**d) self.assertDictEqual(m1.dict, m2.dict)
def test_4a(self): "set fields after init" m = MyModel() now = tznow() m.int_type = 3 m.str_type = 'string' m.dt_type = now self.assertEqual(3, m.int_type) self.assertEqual('string', m.str_type) self.assertEqual(now, m.dt_type)
def test_5(self): "make sure query doesn't change after manager does" man = MyModel.objects q = Query(man) self.assertEqual(0, len(q)) now = tznow() m = MyModel(int_type=3, str_type='string', dt_type=now).save() self.assertEqual(0, len(q)) self.assertEqual(0, q.count) self.assertEqual(1, len(man))
def test_10(self): "make sure query gets instances" now = tznow() m = MyModel(int_type=3, str_type='string', dt_type=now) m.save() man = MyModel.objects q = Query(man) self.assertEqual(1, len(man)) self.assertEqual(1, len(q)) self.assertEqual(m, q[0])
def test_25(self): "test chaining filters together" now = tznow() instances = [ MyModel(int_type=i, str_type='string', dt_type=now) for i in range(3) ] for inst in instances: inst.save() q = MyModel.objects.filter(int_type__gt=0).filter(int_type=1) self.assertEqual(1, len(q))
def test_11(self): "test save to manager" self.assertEqual(0, len(MyModel.objects)) now = tznow() m1 = MyModel(int_type=3, str_type='string', dt_type=now) m1.save() self.assertEqual(1, len(MyModel.objects)) m1.str_type = 'new string' m1.save() self.assertEqual(1, len(MyModel.objects))
def test_5(self): "test plain FileStorage class" # FIXME this is a crap test, it just read/writes a string tfile = tempfile.NamedTemporaryFile() storage = FileStorage(tfile.name) m1 = MyModel(int_type=1, str_type="str", dt_type=tznow()) self.assertTrue(storage.write(MyModel, [m1])) self.assertTrue(open(tfile.name, 'r').read()) m2 = storage.read(MyModel) self.assertTrue(m2) self.assertEqual(str(m1), m2)
def test_15(self): "make sure query objects are not 'updated' when manager objects changes" now = tznow() m = MyModel(int_type=3, str_type='string', dt_type=now).save() man = MyModel.objects q = Query(man) mq = q[0] self.assertEqual(m.str_type, mq.str_type) m.str_type = 'new string' self.assertNotEqual(m.str_type, mq.str_type)
def test_16(self): "test CSVStorage with wrong header" def remap_fieldnames(model_class, row): fields = model_class.Meta.fields.keys() for k in list(row.keys()): results_key = k.lower().replace(' ', '_') if results_key not in fields: if k == 'Bad Name': results_key = 'dt_type' row[results_key] = row.pop(k) return row tfile = tempfile.NamedTemporaryFile() now = tznow() m = MyModel(int_type=1, str_type='a string, with comma', dt_type=now).save() with open(tfile.name, 'w') as f: d = { 'Int Type': 1, 'Str Type': 'a string, with comma', 'Bad Name': now } writer = csv.DictWriter(f, fieldnames=d.keys()) writer.writeheader() writer.writerow(d) # with open(tfile.name, 'r') as f: # print f.read() m = MyModel(int_type=1, str_type='a string, with comma', dt_type=now).save() storage = CSVStorage(tfile.name) storage.remap_fieldnames = remap_fieldnames loaded = [e for e in storage.read(MyModel)] self.assertEqual(1, len(loaded)) m = loaded[0] self.assertEqual(1, m.int_type) self.assertEqual('a string, with comma', m.str_type) self.assertEqual(now, m.dt_type)
def test_20(self): "test filter function" now = tznow() instances = [ MyModel(int_type=i, str_type='string', dt_type=now) for i in range(3) ] for inst in instances: inst.save() for i in range(3): q = MyModel.objects.filter(int_type=i) self.assertEqual(1, len(q)) self.assertEqual(i, q[0].int_type)
def test_values(self): """ test .values(), return dict instead of objects """ now = tznow() m = MyModel(int_type=1, str_type=u'string', dt_type=now).save() d = {'int_type': 1, 'str_type': u'string', 'dt_type': now} q = MyModel.objects.all() self.assertDictEqual(d, q.values()[0]) self.assertDictEqual({ 'int_type': 1, 'str_type': u'string' }, q.values('int_type', 'str_type')[0]) self.assertDictEqual({'str_type': u'string'}, q.values('str_type')[0])
def test_30(self): "test query iteration" now = tznow() instances = [ MyModel(int_type=i, str_type='string', dt_type=now) for i in range(3) ] for inst in instances: inst.save() q = MyModel.objects.filter() c = 0 for i in q: c += 1 self.assertEqual(3, c)
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)
def test_35(self): "test order_by" # make some instances now = tznow() instances = [ MyModel(int_type=i, str_type='string', dt_type=now).save() for i in range(3) ] self.assertEqual(instances, list(MyModel.objects.all())) self.assertEqual(Query, type(MyModel.objects.all().order_by('int_type'))) self.assertEqual(instances, list(MyModel.objects.all())) self.assertEqual(instances[::-1], list(MyModel.objects.all().order_by('-int_type'))) self.assertEqual( instances, list(MyModel.objects.all().order_by('str_type', 'dt_type'))) self.assertEqual(instances, list(MyModel.objects.all().order_by('pk')))
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_6(self): "test saving/loading" tfile = tempfile.NamedTemporaryFile() class MyModel( Model ): class Meta: ordering = ['int_type','str_type','dt_type'] filename = tfile.name int_type = fields.IntField(primary_key=True) str_type = fields.StringField() dt_type = fields.DateTimeField() db = Database( models=[MyModel] ) man = MyModel.objects now = tznow() instances = [ MyModel(int_type=i, str_type='string', dt_type=now ) for i in range(3)] for inst in instances: man.save(inst) db.store() self.assertTrue( os.path.getsize(tfile.name) ) del db # with open(tfile.name,'r') as f: # print f.read() db = Database( models=[MyModel] ) db.load() # not sure if this is a valid test, MyModel.objects is still around model = db.get_model('MyModel') self.assertEqual( 3, len(model.objects) )