def test__normalize_date_SQL(self): """this tests the common date kwargs you can use (in both SQLight and Postgres) if we ever add other backends this might need to be moved out of the general generator test""" i = self.get_interface() s = Schema( self.get_table_name(), foo=Field(datetime.datetime, True), _id=Field(int, True, pk=True), index_foo=Index('foo'), ) i.set_table(s) pk20 = i.insert(s, {'foo': datetime.datetime(2014, 4, 20)}) pk21 = i.insert(s, {'foo': datetime.datetime(2014, 4, 21)}) q = query.Query() q.is_foo(day=20) d = i.get_one(s, q) self.assertEqual(d['_id'], pk20) q = query.Query() q.is_foo(day=21, month=4) d = i.get_one(s, q) self.assertEqual(d['_id'], pk21) q = query.Query() q.is_foo(day=21, month=3) d = i.get_one(s, q) self.assertFalse(d)
def get_orm_class(self, table_name=None, **properties): tn = self.get_table_name(table_name) properties["table_name"] = tn if "interface" not in properties: properties["interface"] = self.get_interface() has_field = False for v in properties.values(): if isinstance(v, Field): has_field = True break elif isinstance(v, type) and issubclass(v, Field): has_field = True break if not has_field: properties.update({ "foo": Field(int, True), "bar": Field(str, True), "ifoobar": Index("foo", "bar"), }) orm_class = type( ByteString(tn) if is_py2 else String(tn), (Orm, ), properties, ) return orm_class
def test_set_index(self): s = Schema("foo") s.set_field("bar", Field(int, True)) s.set_field("che", Field(str)) with self.assertRaises(ValueError): s.set_index("foo", Index()) with self.assertRaises(ValueError): s.set_index("", Index("bar", "che")) s.set_index("bar_che", Index("che", "bar")) with self.assertRaises(ValueError): s.set_index("bar_che", Index("che", "bar")) s.set_index("testing", Index("che", unique=True)) self.assertTrue(s.indexes["testing"].unique)
def get_schema(self, table_name=None, **fields_or_indexes): if not fields_or_indexes: fields_or_indexes.setdefault("foo", Field(int, True)) fields_or_indexes.setdefault("bar", Field(str, True)) fields_or_indexes.setdefault("ifoobar", Index("foo", "bar")) fields_or_indexes.setdefault("_id", Field(long, True, pk=True)) s = Schema(self.get_table_name(table_name), **fields_or_indexes) return s
def test_index_ignore_case(self): i = self.get_interface() s = Schema( self.get_table_name(), _id=Field(int, pk=True), foo=Field(str, True, ignore_case=True), bar=Field(str, True), index_foo=Index('foo', 'bar'), ) i.set_table(s) v = '*****@*****.**' d = i.insert(s, {'foo': v, 'bar': 'bar'}) q = query.Query() q.is_foo(v) r = i.get_one(s, q) self.assertGreater(len(r), 0) lv = list(v) for x in range(len(v)): lv[x] = lv[x].upper() qv = "".join(lv) q = query.Query() q.is_foo(qv) r = i.get_one(s, q) self.assertGreater(len(r), 0) lv[x] = lv[x].lower() d = i.insert(s, {'foo': 'FoO', 'bar': 'bar'}) q = query.Query() q.is_foo('foo') r = i.get_one(s, q) self.assertGreater(len(r), 0) self.assertEqual(r['foo'], 'FoO') q = query.Query() q.is_foo('Foo').is_bar('BAR') r = i.get_one(s, q) self.assertEqual(len(r), 0) q = query.Query() q.is_foo('FoO').is_bar('bar') r = i.get_one(s, q) self.assertGreater(len(r), 0) self.assertEqual(r['foo'], 'FoO') d = i.insert(s, {'foo': 'foo2', 'bar': 'bar'}) q = query.Query() q.is_foo('foo2') r = i.get_one(s, q) self.assertGreater(len(r), 0) self.assertEqual(r['foo'], 'foo2')
def get_schema(self, table_name=None, **fields_or_indexes): if not fields_or_indexes: fields_or_indexes.setdefault("foo", Field(int, True)) fields_or_indexes.setdefault("bar", Field(str, True)) fields_or_indexes.setdefault("ifoobar", Index("foo", "bar")) fields_or_indexes.setdefault("_id", Field(long, True, pk=True)) # remove any None values for k in list(fields_or_indexes.keys()): if not fields_or_indexes[k]: fields_or_indexes.pop(k) s = Schema(self.get_table_name(table_name), **fields_or_indexes) return s
def test___init__(self): """ I had set the class .fields and .indexes attributes to {} instead of None, so you could only ever create one instance of Schema, this test makes sure that's been fixed """ s = Schema("foo") self.assertTrue(isinstance(s.fields, dict)) self.assertTrue(isinstance(s.indexes, dict)) s2 = Schema("bar") self.assertTrue(isinstance(s.fields, dict)) self.assertTrue(isinstance(s.indexes, dict)) s = Schema("foo", bar=Field(int), che=Field(str, True), barche=Index("bar", "che")) self.assertTrue("bar" in s.fields) self.assertTrue("che" in s.fields) self.assertTrue("barche" in s.indexes)
class Torm(Orm): table_name = tn interface = self.get_interface() foo = Field(int, True) bar = Field(str, True) ifoobar = Index("foo", "bar")
class PickleOrm(Orm): """This is only needed to test the test_pickling() method""" foo = Field(int, True) bar = Field(str, True) ifoobar = Index("foo", "bar")