Esempio n. 1
0
 def test_from(self):
     self.assertEqual(sql.Where.from_obj('a=1'), 'a=1')
     self.assertEqual(sql.Where.from_obj(dict(a=3)), 'a = 3')
     self.assertRaises(ValueError, sql.Where.from_obj, dict(a=3, v=4))
     self.assertRaises(ValueError, sql.Where.from_obj, dict(k='3', op=4))
     self.assertEqual(sql.Where.from_obj(dict(k='3', value=4)), '3 = 4')
     self.assertEqual(sql.Where.from_obj(dict(k=sql.Key('a'), op='=', v='3')), '`a` = "3"')
     self.assertEqual(sql.Where.from_obj(('a', 1)), 'a = 1')
     self.assertEqual(sql.Where.from_obj(('a', (1, 2))), 'a in ( 1, 2 )')
     self.assertEqual(sql.Where.from_obj(('!=', 'a', None)), 'a is not NULL')
     self.assertEqual(sql.Where.from_obj(('!=', 'a')), '!= = "a"')
     self.assertEqual(sql.Where.from_obj(('a', '>', None)), 'a > NULL')
     self.assertEqual(sql.Where.from_obj(('a', 1, 2)), 'a in ( 1, 2 )')
     self.assertEqual(sql.Where.from_obj((sql.Key('a'), 1, 2, 3, 4)), '`a` in ( 1, 2, 3, 4 )')
Esempio n. 2
0
    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"')
Esempio n. 3
0
 def test_from(self):
     self.assertEqual(sql.Order.from_obj('id'), 'id asc')
     self.assertEqual(sql.Order.from_obj('id asc'), 'id asc')
     self.assertEqual(sql.Order.from_obj(('id',)), 'id asc')
     self.assertEqual(sql.Order.from_obj(('`id`', True)), '`id` desc')
     self.assertRaises(ValueError, sql.Order.from_obj, dict(key='id'))
     self.assertRaises(ValueError, sql.Order.from_obj, dict(o='id', b='f'))
     self.assertEqual(sql.Order.from_obj(dict(key='asc')), 'key asc')
     self.assertEqual(sql.Order.from_obj(dict(key=sql.Key('asc'), o='desc')), '`asc` desc')
Esempio n. 4
0
 def test_order(self):
     # init
     order = sql.Order('id', 'asc')
     self.assertEqual(order.key, sql.Key('id'))
     self.assertEqual(order.order, sql.ORDER.ASC)
     # property
     k, o = order
     self.assertEqual(k, sql.Key('id'))
     self.assertEqual(o, sql.ORDER.ASC)
     # bool
     self.assertTrue(order)
     order._order = None
     self.assertFalse(order)
     # eq
     self.assertEqual(sql.Order('id', True), 'id desc')
     self.assertEqual(sql.Order('id', None), 'id asc')
     self.assertEqual(sql.Order(sql.Key('id'), 'asc'), '`id` asc')
     # asc desc
     self.assertEqual(sql.OrderDesc('id'), sql.Order('id', sql.ORDER.DESC))
     self.assertEqual(sql.OrderAsc('id'), sql.Order('id', sql.ORDER.ASC))
Esempio n. 5
0
 def test_sql(self):
     self.assertEqual(sql.Key('a').to_sql(), '`a`')
Esempio n. 6
0
 def test_sql(self):
     self.assertEqual(sql.SelectList(), '*')
     self.assertEqual(sql.SelectList(('a', sql.Key('a'))), 'a, `a`')
Esempio n. 7
0
 def test_sql(self):
     a = sql.Set((sql.Key('id'), sql.Value('name')))
     self.assertIn(a, ('`id`, "name"', '"name", `id`'))
Esempio n. 8
0
 def test_iter(self):
     k, v = sql.KeyValue('id', 1)
     self.assertEqual(k, sql.Key('id'))
     self.assertEqual(v, sql.Value(1))
Esempio n. 9
0
 def test_hash(self):
     kv = sql.KeyValue('id')
     self.assertEqual(kv.__hash__(), sql.Key('id').__hash__())
     self.assertEqual(kv.__hash__(), 'id'.__hash__())
Esempio n. 10
0
 def test_init(self):
     kv = sql.KeyValue('id')
     self.assertEqual(kv.key, sql.Key('id'))
     self.assertEqual(kv.value, sql.Value())
     kv.value = 4
     self.assertEqual(kv.value, sql.Value(4))