def test_multiplePlaceholderComparisons(self): """ Test that using multiple different placeholders in a comparison at once properly gives each a unique name. """ s = Store() p1 = Placeholder(PlaceholderTestItem) p2 = Placeholder(PlaceholderTestItem) query = ItemQuery( s, PlaceholderTestItem, AND(PlaceholderTestItem.attr == p1.attr, PlaceholderTestItem.other == p1.other, PlaceholderTestItem.attr == p2.attr, PlaceholderTestItem.characters == p2.characters)) sql, args = query._sqlAndArgs('SELECT', '*') self.assertEquals( sql, 'SELECT * ' 'FROM %s, %s AS placeholder_0, %s AS placeholder_1 ' 'WHERE ((%s = placeholder_0.[attr]) AND ' '(%s = placeholder_0.[other]) AND ' '(%s = placeholder_1.[attr]) AND ' '(%s = placeholder_1.[characters]))' % (PlaceholderTestItem.getTableName(s), PlaceholderTestItem.getTableName(s), PlaceholderTestItem.getTableName(s), PlaceholderTestItem.attr.getColumnName(s), PlaceholderTestItem.other.getColumnName(s), PlaceholderTestItem.attr.getColumnName(s), PlaceholderTestItem.characters.getColumnName(s))) self.assertEquals(args, [])
def test_placeholderTableName(self): """ Test that the table name of a Placeholder is the same as the table name of the underlying Item class. """ s = Store() p = Placeholder(PlaceholderTestItem) self.assertEquals(p.getTableName(s), PlaceholderTestItem.getTableName(s))
def _placeholderAttributeSimilarity(self, kind, sql, args): s = Store() value = u'text' p = Placeholder(PlaceholderTestItem) # Explicitly call this, since we aren't going through ItemQuery. p.getTableAlias(s, ()) comparison = getattr(p.characters, kind)(value) self.failUnless(IComparison.providedBy(comparison)) self.assertEquals(comparison.getQuery(s), sql % (p.characters.getColumnName(s), )) self.assertEquals(comparison.getArgs(s), [args % (value, )])
def test_placeholderOrdering(self): """ Placeholders should be ordered based on the order in which they were instantiated. """ p1 = Placeholder(PlaceholderTestItem) p2 = Placeholder(PlaceholderTestItem) self.failUnless(p1 < p2) self.failUnless(p2 > p1) self.failIf(p1 >= p2) self.failIf(p2 <= p1) self.failIf(p1 == p2) self.failIf(p2 == p1) self.failUnless(p1 != p2) self.failUnless(p2 != p1)
def test_placeholderAttributeError(self): """ Test that trying to get an attribute from a Placeholder which is not an L{IComparison} on the underlying Item class raises an AttributeError. """ p = Placeholder(PlaceholderTestItem) self.assertRaises(AttributeError, getattr, p, 'nonexistentAttribute')
def test_placeholderStoreID(self): """ Test that the C{storeID} attribute of a Placeholder can be retrieved just like any other attribute. """ value = 0 p = Placeholder(PlaceholderTestItem) self.failUnless(IComparison.providedBy(p.storeID > value))
def test_placeholderType(self): """ Test that the C{type} attribute of a Placeholder column is the Placeholder from which it came. """ p = Placeholder(PlaceholderTestItem) a = p.attr self.assertIdentical(a.type, p)
def test_placeholderAntiContainment(self): """ Test that placeholder attributes can be used with the SQL NOT IN operator. """ s = Store() value = [1, 2, 3] p = Placeholder(PlaceholderTestItem) # Call this since we're not using ItemQuery p.getTableAlias(s, ()) comparison = p.attr.notOneOf(value) self.failUnless(IComparison.providedBy(comparison)) self.assertEquals(comparison.getQuery(s), '%s NOT IN (?, ?, ?)' % (p.attr.getColumnName(s), )) self.assertEquals(comparison.getArgs(s), value)
def test_placeholderLikeTarget(self): """ Test that a placeholder can be used as the right-hand argument to a SQL LIKE expression. """ s = Store() p = Placeholder(PlaceholderTestItem) # Call this since we're not using ItemQuery p.getTableAlias(s, ()) comparison = PlaceholderTestItem.attr.like(p.attr) self.failUnless(IComparison.providedBy(comparison)) self.assertEquals( comparison.getQuery(s), '(%s LIKE (placeholder_0.[attr]))' % (PlaceholderTestItem.attr.getColumnName(s), )) self.assertEquals(comparison.getArgs(s), [])
def test_placeholderColumnInterface(self): """ Test that a column from a placeholder provides L{IColumn}. """ value = 0 p = Placeholder(PlaceholderTestItem) a = p.attr self.failUnless(IColumn.providedBy(a))
def test_placeholderComparisonQuery(self): """ Test that the result of L{IComparison.getQuery} on an attribute retrieved from a Placeholder returns SQL which correctly uses an alias of the wrapped table. """ s = Store() p = Placeholder(PlaceholderTestItem) # Explicitly call this here, since we're not going through ItemQuery or # another more reasonable codepath, which would have called it for us. p.getTableAlias(s, ()) value = 0 comparison = (p.attr > value) self.assertEquals(comparison.getQuery(s), '(placeholder_0.[attr] > ?)') self.assertEquals(comparison.getArgs(s), [value])
def test_placeholderAntiContainmentTarget(self): """ Test that a placeholder attribute can be used as the right-hand argument to the SQL NOT IN operator. """ s = Store() p = Placeholder(PlaceholderTestItem) # Call this since we're not using ItemQuery p.getTableAlias(s, ()) comparison = PlaceholderTestItem.attr.notOneOf(p.attr) self.failUnless(IComparison.providedBy(comparison)) self.assertEquals( comparison.getQuery(s), '%s NOT IN (%s)' % (PlaceholderTestItem.attr.getColumnName(s), p.attr.getColumnName(s))) self.assertEquals(comparison.getArgs(s), [])
def test_placeholderAccessor(self): """ Test that the __get__ of SQLAttribute does the obvious thing, and returns its value when given an instance. """ dummy = FullImplementationDummyClass(dummyAttribute=1234) self.assertEqual( Placeholder(FullImplementationDummyClass ).dummyAttribute.__get__(dummy), 1234) self.assertEqual(dummy.dummyAttribute, 1234)
def test_placeholderSorting(self): """ Placeholders should sort based on the order in which they were instantiated. """ placeholders = [Placeholder(PlaceholderTestItem) for n in xrange(10)] shuffledPlaceholders = list(placeholders) random.shuffle(shuffledPlaceholders) shuffledPlaceholders.sort() self.assertEquals(placeholders, shuffledPlaceholders)
def test_placeholderAttributeValueComparison(self): """ Test that getting an attribute from a Placeholder which exists on the underlying Item class and comparing it to a value returns an L{IComparison} provider. """ value = 0 p = Placeholder(PlaceholderTestItem) for op in COMPARISON_OPS: self.failUnless(IComparison.providedBy(op(p.attr, value))) self.failUnless(IComparison.providedBy(op(value, p.attr)))
def test_fullyQualifiedPlaceholder(self): """ Verify that the L{IColumn.fullyQualifiedName} implementation on placeholder attributes returns a usable string, but one which is recognizable as an invalid Python identifier. """ ph = Placeholder(SQLAttributeDummyClass) self.assertEqual( 'axiom.test.test_attributes.SQLAttributeDummyClass' '.dummyAttribute.<placeholder:%d>' % (ph._placeholderCount,), ph.dummyAttribute.fullyQualifiedName())
def test_placeholderQuery(self): """ Test that a BaseQuery can be created with Placeholder instances and the SQL it emits as a result correctly assigns and uses table aliases. """ s = Store() p = Placeholder(PlaceholderTestItem) sql, args = ItemQuery(s, p)._sqlAndArgs('SELECT', '*') self.assertEquals( sql, 'SELECT * FROM %s AS placeholder_0' % (PlaceholderTestItem.getTableName(s), ))
def test_placeholderComparisonArgs(self): """ Test that the result of L{IComparison.getArgs} on an attribute retrieved from a Placeholder returns the right values for the comparison. """ s = Store() p = Placeholder(PlaceholderTestItem) value = 0 args = (p.attr > value).getArgs(s) self.assertEquals(args, [0])
def test_placeholderComparisonTables(self): """ Test that the result of L{IComparison.getInvolvedTables} on an attribute retrieved from a Placeholder returns a special placeholder item. """ s = Store() p = Placeholder(PlaceholderTestItem) value = 0 involvedTables = (p.attr > value).getInvolvedTables() self.assertEquals(len(involvedTables), 1) theTable = iter(involvedTables).next() self.assertEquals(theTable.getTableName(s), PlaceholderTestItem.getTableName(s)) self.assertEquals(theTable.getTableAlias(s, ()), 'placeholder_0')
def test_placeholderAliasAssignment(self): """ Test that each placeholder selects a unique alias for itself. """ s = Store() p1 = Placeholder(PlaceholderTestItem) p2 = Placeholder(PlaceholderTestItem) aliases = [] self.assertEquals(p1.getTableAlias(s, aliases), 'placeholder_0') self.assertEquals(p1.getTableAlias(s, aliases), 'placeholder_0') aliases.append('placeholder_') self.assertEquals(p1.getTableAlias(s, aliases), 'placeholder_0') self.assertEquals(p2.getTableAlias(s, aliases), 'placeholder_1')
def test_placeholderSorting(self): """ Test that a placeholder attribute can be used as a sort key. """ s = Store() p = Placeholder(PlaceholderTestItem) query = ItemQuery(s, PlaceholderTestItem, sort=p.attr.ascending) sql, args = query._sqlAndArgs('SELECT', '*') expectedSQL = ('SELECT * ' 'FROM %s, %s AS placeholder_0 ' 'ORDER BY placeholder_0.[attr] ASC') expectedSQL %= (PlaceholderTestItem.getTableName(s), ) * 2 self.assertEquals(sql, expectedSQL) self.assertEquals(args, [])
def test_placeholderComparison(self): """ Test that a comparison which contains a Placeholder also results in properly generated SQL. """ s = Store() p = Placeholder(PlaceholderTestItem) query = ItemQuery(s, PlaceholderTestItem, PlaceholderTestItem.attr == p.attr) sql, args = query._sqlAndArgs('SELECT', '*') self.assertEquals( sql, 'SELECT * ' 'FROM %s, %s AS placeholder_0 ' 'WHERE (%s.[attr] = placeholder_0.[attr])' % (PlaceholderTestItem.getTableName(s), PlaceholderTestItem.getTableName(s), PlaceholderTestItem.getTableName(s))) self.assertEquals(args, [])