def test_select_clone(self): """Test cloning a SELECT""" select1 = DBSelect('a', {'c': 'col'} ).left_join('b', 'a.c = b.d', {'d': 'col'} ).where('a.col = ?', 1 ).order('b.d', 'DESC' ).limit(1, 2 ).distinct(True) select2 = select1.clone() self.assertEqual(str(select1), str(select2)) select2.or_where('b.col = ?', 1) self.assertNotEqual(str(select1), str(select2)) select1.unset(select1.WHERE) select2.unset(select2.WHERE) self.assertEqual(str(select1), str(select2))
def test_select_unset(self): """Test unsetting parts of SELECT""" select = DBSelect('a', {'c': 'col'} ).left_join('b', 'a.c = b.d', {'d': 'col'} ).where('a.col = ?', 1 ).order('b.d', 'DESC' ).limit(1, 2 ).distinct(True) self.assertEqual( re.sub(r'\s+', ' ', str(select)), 'SELECT DISTINCT "a"."col" AS "c", "b"."col" AS "d" ' + 'FROM "a" LEFT JOIN "b" ON a.c = b.d ' + 'WHERE (a.col = ?) ' + 'ORDER BY "b"."d" DESC ' + 'LIMIT 2, 1' ) select.unset(select.FROM | select.COLUMNS).set_from('x') self.assertEqual( re.sub(r'\s+', ' ', str(select)), 'SELECT DISTINCT "x".* ' + 'FROM "x" WHERE (a.col = ?) ORDER BY "b"."d" DESC LIMIT 2, 1' ) select.unset(select.WHERE) self.assertEqual( re.sub(r'\s+', ' ', str(select)), 'SELECT DISTINCT "x".* ' + 'FROM "x" ORDER BY "b"."d" DESC LIMIT 2, 1' ) select.unset(select.DISTINCT | select.ORDER | select.LIMIT) self.assertEqual( re.sub(r'\s+', ' ', str(select)), 'SELECT "x".* FROM "x"' )