def test_expr_startswith(self): self.connection.execute("INSERT INTO test VALUES (30, '!!_%blah')") self.connection.execute("INSERT INTO test VALUES (40, '!!blah')") id = Column("id", SQLToken("test")) title = Column("title", SQLToken("test")) expr = Select(id, title.startswith(u"!!_%")) result = list(self.connection.execute(expr)) self.assertEquals(result, [(30, )])
def test_expr_contains_string(self): self.connection.execute("INSERT INTO test VALUES (30, 'blah_%!!x')") self.connection.execute("INSERT INTO test VALUES (40, 'blah!!x')") id = Column("id", SQLToken("test")) title = Column("title", SQLToken("test")) expr = Select(id, title.contains_string(u"_%!!")) result = list(self.connection.execute(expr)) self.assertEquals(result, [(30, )])
def test_expr_endswith(self): self.connection.execute("INSERT INTO test VALUES (30, 'blah_%!!')") self.connection.execute("INSERT INTO test VALUES (40, 'blah!!')") id = Column("id", SQLToken("test")) title = Column("title", SQLToken("test")) expr = Select(id, title.endswith(u"_%!!")) result = list(self.connection.execute(expr)) assert result == [(30,)]
def test_get_insert_identity(self): result = self.connection.execute("INSERT INTO test (title) " "VALUES ('Title 30')") primary_key = (Column("id", SQLToken("test")), ) primary_variables = (Variable(), ) expr = result.get_insert_identity(primary_key, primary_variables) select = Select(Column("title", SQLToken("test")), expr) result = self.connection.execute(select) self.assertEquals(result.get_one(), ("Title 30", ))
def test_set_none_with_allow_none_and_column_with_table(self): column = Column("column_name", SQLToken("table_name")) variable = CustomVariable(allow_none=False, column=column) try: variable.set(None) except NoneError, e: pass
def test_quoting(self): # FIXME "with'quote" should be in the list below, but it doesn't # work because it breaks the parameter mark translation. for reserved_name in ["with space", 'with`"escape', "SELECT"]: reserved_name = SQLToken(reserved_name) expr = Select(reserved_name, tables=Alias(Select(Alias(1, reserved_name)))) result = self.connection.execute(expr) self.assertEquals(result.get_one(), (1, ))
def test_set_none_with_allow_none_and_column_with_table(self): column = Column("column_name", SQLToken("table_name")) variable = CustomVariable(allow_none=False, column=column) try: variable.set(None) except NoneError as e: self.assertTrue("table_name.column_name" in str(e)) else: self.fail("NoneError not raised")
def test_set_none_with_allow_none_and_column_with_table(): column = Column("column_name", SQLToken("table_name")) variable = CustomVariable(allow_none=False, column=column) with pytest.raises(NoneError): variable.set(None)
def __init__(self, cls): self.table = getattr(cls, "__storm_table__", None) if self.table is None: raise ClassInfoError("%s.__storm_table__ missing" % repr(cls)) self.cls = cls if not isinstance(self.table, Expr): self.table = SQLToken(self.table) pairs = [] for attr in dir(cls): column = getattr(cls, attr, None) if isinstance(column, Column): pairs.append((attr, column)) pairs.sort() self.columns = tuple(pair[1] for pair in pairs) self.attributes = dict(pairs) storm_primary = getattr(cls, "__storm_primary__", None) if storm_primary is not None: if type(storm_primary) is not tuple: storm_primary = (storm_primary, ) self.primary_key = tuple(self.attributes[attr] for attr in storm_primary) else: primary = [] primary_attrs = {} for attr, column in pairs: if column.primary != 0: if column.primary in primary_attrs: raise ClassInfoError( "%s has two columns with the same primary id: " "%s and %s" % (repr(cls), attr, primary_attrs[column.primary])) primary.append((column.primary, column)) primary_attrs[column.primary] = attr primary.sort() self.primary_key = tuple(column for i, column in primary) if not self.primary_key: raise ClassInfoError("%s has no primary key information" % repr(cls)) # columns have __eq__ implementations that do things we don't want - we # want to look these up in a dict and use identity semantics id_positions = dict( (id(column), i) for i, column in enumerate(self.columns)) self.primary_key_idx = dict( (id(column), i) for i, column in enumerate(self.primary_key)) self.primary_key_pos = tuple(id_positions[id(column)] for column in self.primary_key) __order__ = getattr(cls, "__storm_order__", None) if __order__ is None: self.default_order = Undef else: if type(__order__) is not tuple: __order__ = (__order__, ) self.default_order = [] for item in __order__: if isinstance(item, basestring): if item.startswith("-"): prop = Desc(getattr(cls, item[1:])) else: prop = getattr(cls, item) else: prop = item self.default_order.append(prop)