1
1
import unittest
2
2
3
3
from pypika import (
4
+ AliasedQuery ,
4
5
Query ,
5
6
Tables ,
6
7
)
7
8
8
9
9
10
class ImmutabilityTests (unittest .TestCase ):
10
- table_a , table_b = Tables ("a" , "b" )
11
+ table_a , table_b , table_c = Tables ("a" , "b" , "c " )
11
12
12
13
def test_select_returns_new_query_instance (self ):
13
14
query_a = Query .from_ (self .table_a ).select (self .table_a .foo )
@@ -16,6 +17,72 @@ def test_select_returns_new_query_instance(self):
16
17
self .assertIsNot (query_a , query_b )
17
18
self .assertNotEqual (str (query_a ), str (query_b ))
18
19
20
+ def test_groupby_returns_new_query_instance (self ):
21
+ query_a = Query .from_ (self .table_a ).select (self .table_a .foo ).groupby (self .table_a .foo )
22
+ query_b = query_a .groupby (self .table_a .bar )
23
+
24
+ self .assertIsNot (query_a , query_b )
25
+ self .assertNotEqual (str (query_a ), str (query_b ))
26
+
27
+ def test_orderby_return_new_query_instance (self ):
28
+ query_a = Query .from_ (self .table_a ).select (self .table_a .foo ).orderby (self .table_a .foo )
29
+ query_b = query_a .orderby (self .table_a .bar )
30
+
31
+ self .assertIsNot (query_a , query_b )
32
+ self .assertNotEqual (str (query_a ), str (query_b ))
33
+
34
+ def test_join_return_new_query_instance (self ):
35
+ base = Query .from_ (self .table_a ).select (self .table_a .foo )
36
+ query_a = base .join (self .table_b ).on (self .table_a .foo == self .table_b .bar ).select (self .table_b .bar )
37
+ query_b = query_a .join (self .table_c ).on (self .table_a .foo == self .table_c .baz ).select (self .table_c .baz )
38
+
39
+ self .assertIsNot (query_a , query_b )
40
+ self .assertNotEqual (str (query_a ), str (query_b ))
41
+
42
+ def test_use_index_return_new_query_instance (self ):
43
+ query_a = Query .from_ (self .table_a ).select (self .table_a .foo ).use_index ('idx1' )
44
+ query_b = query_a .use_index ('idx2' )
45
+
46
+ self .assertIsNot (query_a , query_b )
47
+ self .assertNotEqual (str (query_a ), str (query_b ))
48
+
49
+ def test_force_index_return_new_query_instance (self ):
50
+ query_a = Query .from_ (self .table_a ).select (self .table_a .foo ).force_index ('idx1' )
51
+ query_b = query_a .force_index ('idx2' )
52
+
53
+ self .assertIsNot (query_a , query_b )
54
+ self .assertNotEqual (str (query_a ), str (query_b ))
55
+
56
+ def test_with_return_new_query_instance (self ):
57
+ alias_1 = Query .from_ (self .table_a ).select (self .table_a .foo )
58
+ alias_2 = Query .from_ (self .table_b ).select (self .table_b .bar )
59
+ query_a = Query .from_ (AliasedQuery ('a1' )).select ('foo' ).with_ (alias_1 , 'a1' )
60
+ query_b = query_a .with_ (alias_2 , 'a2' )
61
+
62
+ self .assertIsNot (query_a , query_b )
63
+ self .assertNotEqual (str (query_a ), str (query_b ))
64
+
65
+ def test_insert_into_return_new_query_instance (self ):
66
+ query_a = Query .into (self .table_a ).insert ('foo1' )
67
+ query_b = query_a .insert ('foo2' )
68
+
69
+ self .assertIsNot (query_a , query_b )
70
+ self .assertNotEqual (str (query_a ), str (query_b ))
71
+
72
+ def test_replace_into_return_new_query_instance (self ):
73
+ query_a = Query .into (self .table_a ).replace ('foo' )
74
+ query_b = query_a .replace ('bar' )
75
+
76
+ self .assertIsNot (query_a , query_b )
77
+ self .assertNotEqual (str (query_a ), str (query_b ))
78
+
79
+ def test_update_set_return_new_query_instance (self ):
80
+ query_a = Query .update (self .table_a ).set (self .table_a .foo , 'foo' )
81
+ query_b = query_a .set (self .table_a .bar , 'bar' )
82
+
83
+ self .assertIsNot (query_a , query_b )
84
+ self .assertNotEqual (str (query_a ), str (query_b ))
85
+
19
86
def test_queries_after_join (self ):
20
87
query1 = Query .from_ (self .table_a ).select (self .table_a .foo )
21
88
query2 = query1 .join (self .table_b ).on (self .table_a .foo == self .table_b .bar ).select (self .table_b .buz )
0 commit comments