@@ -52,10 +52,39 @@ def setUpClass(cls) -> None:
52
52
def setUp (self ) -> None :
53
53
self .datasource : Datasource = Datasource ()
54
54
self .collection_person = Collection ("Person" , self .datasource )
55
+ self .collection_person .add_fields (
56
+ {
57
+ "id" : {
58
+ "is_primary_key" : True ,
59
+ "type" : FieldType .COLUMN ,
60
+ "column_type" : PrimitiveType .NUMBER ,
61
+ "filter_operators" : set ([Operator .EQUAL , Operator .IN ]),
62
+ }
63
+ }
64
+ )
65
+
66
+ self .no_searchable_fields_collection = Collection ("NotSearchable" , self .datasource )
67
+ self .no_searchable_fields_collection .add_fields (
68
+ {
69
+ "id" : {
70
+ "is_primary_key" : True ,
71
+ "type" : FieldType .COLUMN ,
72
+ "column_type" : PrimitiveType .NUMBER ,
73
+ "filter_operators" : set (),
74
+ }
75
+ }
76
+ )
55
77
self .datasource .add_collection (self .collection_person )
78
+ self .datasource .add_collection (self .no_searchable_fields_collection )
56
79
57
80
self .datasource_decorator = DatasourceDecorator (self .datasource , SearchCollectionDecorator )
58
- self .decorated_collection_person = self .datasource_decorator .get_collection ("Person" )
81
+ self .decorated_collection_person : SearchCollectionDecorator = self .datasource_decorator .get_collection (
82
+ "Person"
83
+ ) # type:ignore
84
+
85
+ self .decorated_not_searchable_collection : SearchCollectionDecorator = self .datasource_decorator .get_collection (
86
+ "NotSearchable"
87
+ ) # type:ignore
59
88
60
89
def test_replace_search_should_work (self ):
61
90
def replacer (search : Any , search_extended : bool , context : CollectionCustomizationContext ):
@@ -64,9 +93,28 @@ def replacer(search: Any, search_extended: bool, context: CollectionCustomizatio
64
93
self .decorated_collection_person .replace_search (replacer )
65
94
assert self .decorated_collection_person ._replacer == replacer
66
95
67
- def test_schema_is_searchable_should_be_true (self ):
96
+ def test_schema_is_searchable_should_be_true_by_default_when_fields_can_be_searched (self ):
68
97
assert self .decorated_collection_person .schema ["searchable" ] is True
69
98
99
+ def test_schema_is_searchable_should_be_false_when_no_fields_can_be_searched (self ):
100
+ assert self .decorated_not_searchable_collection .schema ["searchable" ] is False
101
+
102
+ def test_schema_conflict_on_replace_and_disable_apply_the_latest_one (self ):
103
+ self .decorated_collection_person .mark_schema_as_dirty ()
104
+ assert self .decorated_collection_person .schema ["searchable" ] is True
105
+
106
+ self .decorated_collection_person .disable_search ()
107
+ self .decorated_collection_person .mark_schema_as_dirty ()
108
+ assert self .decorated_collection_person .schema ["searchable" ] is False
109
+
110
+ self .decorated_collection_person .replace_search (None )
111
+ self .decorated_collection_person .mark_schema_as_dirty ()
112
+ assert self .decorated_collection_person .schema ["searchable" ] is True
113
+
114
+ def test_schema_is_searchable_should_be_false_when_disabling_search (self ):
115
+ self .decorated_collection_person .disable_search ()
116
+ assert self .decorated_collection_person .schema ["searchable" ] is False
117
+
70
118
def test_refine_filter_should_return_the_given_filter_for_empty_filter (self ):
71
119
filter_ = Filter ({"search" : None })
72
120
@@ -169,6 +217,7 @@ def test_search_must_be_applied_on_all_fields(self):
169
217
"condition_tree" : ConditionTreeBranch (
170
218
Aggregator .OR ,
171
219
conditions = [
220
+ ConditionTreeLeaf ("id" , Operator .EQUAL , 1584 ),
172
221
ConditionTreeLeaf ("number" , Operator .EQUAL , 1584 ),
173
222
ConditionTreeLeaf ("label" , Operator .CONTAINS , "1584" ),
174
223
],
@@ -206,11 +255,11 @@ def test_for_enum_value(self):
206
255
def test_search_number_in_all_field (self ):
207
256
self .collection_person .add_field (
208
257
"field1" ,
209
- Column (column_type = PrimitiveType .NUMBER , filter_operators = [Operator .EQUAL ], type = FieldType .COLUMN ),
258
+ Column (column_type = PrimitiveType .NUMBER , filter_operators = set ( [Operator .EQUAL ]) , type = FieldType .COLUMN ),
210
259
)
211
260
self .collection_person .add_field (
212
261
"field2" ,
213
- Column (column_type = PrimitiveType .NUMBER , filter_operators = [Operator .EQUAL ], type = FieldType .COLUMN ),
262
+ Column (column_type = PrimitiveType .NUMBER , filter_operators = set ( [Operator .EQUAL ]) , type = FieldType .COLUMN ),
214
263
)
215
264
216
265
self .collection_person .add_field (
@@ -230,6 +279,7 @@ def test_search_number_in_all_field(self):
230
279
"condition_tree" : ConditionTreeBranch (
231
280
Aggregator .OR ,
232
281
conditions = [
282
+ ConditionTreeLeaf ("id" , Operator .EQUAL , 1584 ),
233
283
ConditionTreeLeaf ("field1" , Operator .EQUAL , 1584 ),
234
284
ConditionTreeLeaf ("field2" , Operator .EQUAL , 1584 ),
235
285
],
@@ -572,3 +622,13 @@ async def replacer_fn(value, extended, context):
572
622
filter_ = Filter ({"search" : "something" , "search_extended" : True })
573
623
self .loop .run_until_complete (self .decorated_collection_person ._refine_filter (self .mocked_caller , filter_ ))
574
624
spy_replacer_fn .assert_awaited_with ("something" , True , ANY )
625
+
626
+ def test_disable_search_should_mark_schema_as_dirty (self ):
627
+ with patch .object (self .decorated_collection_person , "mark_schema_as_dirty" ) as mark_schema_as_dirty :
628
+ self .decorated_collection_person .disable_search ()
629
+ mark_schema_as_dirty .assert_called_once ()
630
+
631
+ def test_replace_search_should_mark_schema_as_dirty (self ):
632
+ with patch .object (self .decorated_collection_person , "mark_schema_as_dirty" ) as mark_schema_as_dirty :
633
+ self .decorated_collection_person .replace_search (None )
634
+ mark_schema_as_dirty .assert_called_once ()
0 commit comments