11from unittest .mock import patch
2- from utils .query_builder import CosmosDBQueryBuilder
2+ from utils .query_builder import CosmosDBQueryBuilder , Order
33from utils .repository import remove_white_spaces
44import pytest
55
@@ -41,7 +41,9 @@ def test_add_select_conditions_should_update_select_list(
4141 ],
4242)
4343def test_add_sql_in_condition_should_update_where_list (
44- attribute , ids_list , expected_where_condition_list ,
44+ attribute ,
45+ ids_list ,
46+ expected_where_condition_list ,
4547):
4648 query_builder = CosmosDBQueryBuilder ().add_sql_in_condition (
4749 attribute , ids_list
@@ -66,7 +68,9 @@ def test_add_sql_in_condition_should_update_where_list(
6668 ],
6769)
6870def test_add_sql_where_equal_condition_should_update_where_params_list (
69- data , expected_where_list , expected_params ,
71+ data ,
72+ expected_where_list ,
73+ expected_params ,
7074):
7175 query_builder = CosmosDBQueryBuilder ().add_sql_where_equal_condition (data )
7276
@@ -91,7 +95,8 @@ def test_add_sql_where_equal_condition_with_None_should_not_update_lists():
9195 [(True , ['NOT IS_DEFINED(c.deleted)' ]), (False , [])],
9296)
9397def test_add_sql_visibility_condition (
94- visibility_bool , expected_where_list ,
98+ visibility_bool ,
99+ expected_where_list ,
95100):
96101 query_builder = CosmosDBQueryBuilder ().add_sql_visibility_condition (
97102 visibility_bool
@@ -102,7 +107,12 @@ def test_add_sql_visibility_condition(
102107
103108
104109@pytest .mark .parametrize (
105- "limit_value,expected_limit" , [(1 , 1 ), (10 , 10 ), (None , None ),],
110+ "limit_value,expected_limit" ,
111+ [
112+ (1 , 1 ),
113+ (10 , 10 ),
114+ (None , None ),
115+ ],
106116)
107117def test_add_sql_limit_condition (limit_value , expected_limit ):
108118 query_builder = CosmosDBQueryBuilder ().add_sql_limit_condition (limit_value )
@@ -111,10 +121,16 @@ def test_add_sql_limit_condition(limit_value, expected_limit):
111121
112122
113123@pytest .mark .parametrize (
114- "offset_value,expected_offset" , [(1 , 1 ), (10 , 10 ), (None , None ),],
124+ "offset_value,expected_offset" ,
125+ [
126+ (1 , 1 ),
127+ (10 , 10 ),
128+ (None , None ),
129+ ],
115130)
116131def test_add_sql_offset_condition (
117- offset_value , expected_offset ,
132+ offset_value ,
133+ expected_offset ,
118134):
119135 query_builder = CosmosDBQueryBuilder ().add_sql_offset_condition (
120136 offset_value
@@ -125,10 +141,15 @@ def test_add_sql_offset_condition(
125141
126142@pytest .mark .parametrize (
127143 "select_conditions,expected_condition" ,
128- [([], "*" ), (["c.id" ], "c.id" ), (["c.id" , "c.name" ], "c.id,c.name" ),],
144+ [
145+ ([], "*" ),
146+ (["c.id" ], "c.id" ),
147+ (["c.id" , "c.name" ], "c.id,c.name" ),
148+ ],
129149)
130150def test__build_select_return_fields_in_select_list (
131- select_conditions , expected_condition ,
151+ select_conditions ,
152+ expected_condition ,
132153):
133154 query_builder = CosmosDBQueryBuilder ().add_select_conditions (
134155 select_conditions
@@ -148,7 +169,8 @@ def test__build_select_return_fields_in_select_list(
148169 ],
149170)
150171def test__build_where_should_return_concatenated_conditions (
151- fields , expected_condition ,
172+ fields ,
173+ expected_condition ,
152174):
153175 query_builder = CosmosDBQueryBuilder ().add_sql_where_equal_condition (
154176 fields
@@ -164,7 +186,9 @@ def test__build_where_should_return_concatenated_conditions(
164186 [(1 , "OFFSET @offset" , [{'name' : '@offset' , 'value' : 1 }]), (None , "" , [])],
165187)
166188def test__build_offset (
167- offset , expected_condition , expected_params ,
189+ offset ,
190+ expected_condition ,
191+ expected_params ,
168192):
169193 query_builder = CosmosDBQueryBuilder ().add_sql_offset_condition (offset )
170194
@@ -179,7 +203,9 @@ def test__build_offset(
179203 [(1 , "LIMIT @limit" , [{'name' : '@limit' , 'value' : 1 }]), (None , "" , [])],
180204)
181205def test__build_limit (
182- limit , expected_condition , expected_params ,
206+ limit ,
207+ expected_condition ,
208+ expected_params ,
183209):
184210 query_builder = CosmosDBQueryBuilder ().add_sql_limit_condition (limit )
185211
@@ -235,3 +261,23 @@ def test_build_with_empty_and_None_attributes_return_query_select_all():
235261 assert query == expected_query
236262 assert len (query_builder .get_parameters ()) == 0
237263 assert len (query_builder .where_conditions ) == 0
264+
265+
266+ def test_order_by_condition ():
267+ query_builder = CosmosDBQueryBuilder ().add_sql_order_by_condition (
268+ 'start_date' , Order .DESC
269+ )
270+
271+ assert len (query_builder .order_by ) == 2
272+ assert query_builder .order_by == ('start_date' , 'DESC' )
273+
274+
275+ def test__build_orderBy ():
276+ query_builder = CosmosDBQueryBuilder ().add_sql_order_by_condition (
277+ 'start_date' , Order .DESC
278+ )
279+
280+ orderBy_condition = query_builder ._CosmosDBQueryBuilder__build_order_By ()
281+ expected_order_string = "ORDER BY c.start_date DESC"
282+
283+ assert expected_order_string == orderBy_condition
0 commit comments