def test_init(self): self.assertRaises(TypeError, sql.Tree, '', sql.Node, sql.Node) self.assertRaises(TypeError, sql.Tree, sql.Node, '', sql.Node) self.assertRaises(TypeError, sql.Tree, sql.Node, sql.Node, '') self.assertEqual(sql.Tree(sql.Str('a'), sql.Str('b'), sql.Str('c')).tree, 'a') self.assertEqual(sql.Tree(sql.Str('a'), sql.Str('b'), sql.Str('c')).left, 'b') self.assertEqual(sql.Tree(sql.Str('a'), sql.Str('b'), sql.Str('c')).right, 'c')
def test_Or(self): b = sql.Or(sql.Str('a'), sql.Str('b')) self.assertTrue(b) self.assertTrue(sql.Or(sql.Str('a'), sql.Str(''))) self.assertTrue(sql.Or(sql.Str(''), sql.Str('b'))) self.assertFalse(sql.Or(sql.Str(''), sql.Str(''))) self.assertEqual(b, 'a or b')
def test_And(self): a = sql.And(sql.Str('a'), sql.Str('b')) self.assertTrue(a) self.assertTrue(sql.And(sql.Str('a'), sql.Str(''))) self.assertTrue(sql.And(sql.Str(''), sql.Str('b'))) self.assertFalse(sql.And(sql.Str(''), sql.Str(''))) self.assertEqual(a, 'a and b')
def test_where(self): self.assertRaises(TypeError, sql.Where, '=', 1, 'a') w = sql.Where('=', 'id', 3) self.assertTrue(w) self.assertTrue(sql.Where('=', 'id', sql.Value(None))) self.assertEqual(w.operation, sql.WHERE.EQUAL) self.assertEqual(w, 'id = 3') self.assertEqual(sql.Where('=', 'id', '3'), 'id = "3"') self.assertEqual(sql.WhereTrue(), '1') self.assertEqual(sql.WhereStr('abc'), 'abc') self.assertEqual(sql.WhereEqual('id', None), 'id is NULL') self.assertEqual(sql.WhereNotEqual('id', None), 'id is not NULL') self.assertEqual(sql.WhereLess('id', None), 'id < NULL') self.assertEqual(sql.WhereLessEqual('id', sql.Str('')), 'id <=') self.assertEqual(sql.WhereGreater('id', sql.Value()), 'id > NULL') self.assertEqual(sql.WhereGreaterEqual('id', sql.Key('id')), 'id >= `id`') self.assertEqual(sql.WhereIn('id', (1, None, False, '')), 'id in ( 1, NULL, False, "" )') self.assertEqual(sql.WhereNotIn('id', ()), 'id not in ( )') self.assertEqual(sql.WhereBetween('id', '', None), 'id between ""') self.assertEqual(sql.WhereNotBetween('id', '', False), 'id not between "" and False') self.assertEqual(sql.WhereLike('id', ''), 'id like ""') self.assertRaises(TypeError, sql.WhereNotLike, 'id', 1) self.assertEqual(sql.WhereNotLike('id', '%s'), 'id not like "%s"')
def test_to_sql(self): self.assertEqual(sql.Str('a'), 'a')
def test_is_equal(self): s_1 = sql.Str('') s_2 = sql.Str('1') self.assertEqual(s_1, '') self.assertNotEqual(s_1, s_2)
def test_is_true(self): s_1 = sql.Str('') s_2 = sql.Str('1') self.assertTrue(s_2) self.assertFalse(s_1)
def test_hash(self): s = sql.Str('a') self.assertEqual(hash(s), hash('a'))
def test_init(self): s = sql.Str('') self.assertEqual(s.str, '') self.assertEqual(s._str, s.str) self.assertEqual(type(s.str), str)
def test_Bracket(self): a = sql.Bracket(sql.Str('a')) self.assertTrue(a) self.assertFalse(sql.Bracket(sql.Str(''))) self.assertEqual(a, '( a )') self.assertEqual(a.bracket(), '( ( a ) )')
def test_and_or_bracket(self): a = sql.Tree(sql.TREE.AND, sql.Str('a'), sql.Str('b')) self.assertEqual(a, 'a and b') self.assertEqual(a.and_(sql.Str('c')), 'a and b and c') self.assertEqual(a.or_(sql.Str('c')), 'a and b or c') self.assertEqual(a.bracket(), '( a and b )')
def test_sql(self): self.assertEqual(sql.Tree(sql.Str('a'), sql.Str('b'), sql.Str('c')), 'b a c') self.assertEqual(sql.Tree(sql.Str(''), sql.Str('b'), sql.Str('c')), 'b') self.assertEqual(sql.Tree(sql.Str('a'), sql.Str(''), sql.Str('c')), 'c') self.assertEqual(sql.Tree(sql.Str('a'), sql.Str('b'), sql.Str('')), 'b')
def test_bool(self): self.assertTrue(sql.Tree(sql.Str('a'), sql.Str('b'), sql.Str('c'))) self.assertFalse(sql.Tree(sql.Str(''), sql.Str('b'), sql.Str('c'))) self.assertTrue(sql.Tree(sql.Str('a'), sql.Str(''), sql.Str('c'))) self.assertTrue(sql.Tree(sql.Str('a'), sql.Str('b'), sql.Str('')))
def test_iter(self): t, l, r = sql.Tree(sql.Str('a'), sql.Str('b'), sql.Str('c')) self.assertEqual(t, 'a') self.assertEqual(l, 'b') self.assertEqual(r, 'c')
def test_init(self): self.assertRaises(TypeError, sql.List, ('',)) self.assertEqual(sql.List((sql.Str('a'),)), 'a') self.assertEqual(sql.List((sql.Value(),)), 'NULL')