Skip to content

Commit 9a84ddc

Browse files
committed
added tests
1 parent 602d20b commit 9a84ddc

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

packages/google-cloud-firestore/tests/system/pipeline_e2e/search.yaml

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,133 @@ tests:
2222
args:
2323
- stringValue: "technology"
2424
name: document_matches
25+
- description: search_stage_full_options
26+
pipeline:
27+
- Collection: books
28+
- Search:
29+
- SearchOptions:
30+
query:
31+
DocumentMatches:
32+
- Constant: "science"
33+
limit: 5
34+
retrieval_depth: 10
35+
offset: 2
36+
query_enhancement: disabled
37+
language_code: en
38+
assert_proto:
39+
pipeline:
40+
stages:
41+
- args:
42+
- referenceValue: /books
43+
name: collection
44+
- args: []
45+
name: search
46+
options:
47+
limit:
48+
integerValue: '5'
49+
retrieval_depth:
50+
integerValue: '10'
51+
offset:
52+
integerValue: '2'
53+
query_enhancement:
54+
stringValue: "disabled"
55+
language_code:
56+
stringValue: "en"
57+
query:
58+
functionValue:
59+
args:
60+
- stringValue: "science"
61+
name: document_matches
62+
- description: search_stage_with_sort_and_select
63+
pipeline:
64+
- Collection: books
65+
- Search:
66+
- SearchOptions:
67+
query: "science"
68+
sort:
69+
Ordering:
70+
- Field: rating
71+
- DESCENDING
72+
select:
73+
- title
74+
add_fields:
75+
- AliasedExpression:
76+
- FunctionExpression.string_concat:
77+
- Field: title
78+
- Constant: " - Selected"
79+
- "title_selected"
80+
assert_proto:
81+
pipeline:
82+
stages:
83+
- args:
84+
- referenceValue: /books
85+
name: collection
86+
- args: []
87+
name: search
88+
options:
89+
query:
90+
functionValue:
91+
args:
92+
- stringValue: "science"
93+
name: document_matches
94+
sort:
95+
arrayValue:
96+
values:
97+
- mapValue:
98+
fields:
99+
direction:
100+
stringValue: descending
101+
expression:
102+
fieldReferenceValue: rating
103+
select:
104+
mapValue:
105+
fields:
106+
title:
107+
fieldReferenceValue: title
108+
add_fields:
109+
mapValue:
110+
fields:
111+
title_selected:
112+
functionValue:
113+
args:
114+
- fieldReferenceValue: title
115+
- stringValue: " - Selected"
116+
name: string_concat
117+
- description: expression_between
118+
pipeline:
119+
- Collection: books
120+
- Where:
121+
- FunctionExpression.between:
122+
- Field: published
123+
- Constant: 1950
124+
- Constant: 1970
125+
- Select:
126+
- title
127+
- Sort:
128+
- Ordering:
129+
- Field: title
130+
- ASCENDING
131+
assert_results:
132+
- title: Dune
133+
- title: One Hundred Years of Solitude
134+
- title: The Lord of the Rings
135+
- title: To Kill a Mockingbird
136+
- description: expression_geo_distance
137+
pipeline:
138+
- Collection: geopoints
139+
- Search:
140+
- SearchOptions:
141+
query:
142+
FunctionExpression.less_than:
143+
- FunctionExpression.geo_distance:
144+
- Field: location
145+
- Constant: GEOPOINT(37.0,-122.0)
146+
- Constant: 150000.0
147+
- Select:
148+
- name
149+
- Sort:
150+
- Ordering:
151+
- Field: name
152+
- ASCENDING
153+
assert_results:
154+
- name: SF

packages/google-cloud-firestore/tests/unit/v1/test_pipeline_stages.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Constant,
2525
Field,
2626
Ordering,
27+
DocumentMatches,
2728
)
2829
from google.cloud.firestore_v1.types.document import Value
2930
from google.cloud.firestore_v1.vector import Vector
@@ -778,6 +779,68 @@ def test_to_pb_percent_mode(self):
778779
assert len(result_percent.options) == 0
779780

780781

782+
class TestSearch:
783+
def test_search_defaults(self):
784+
options = stages.SearchOptions(query="technology")
785+
assert options.query.name == "document_matches"
786+
assert options.limit is None
787+
assert options.retrieval_depth is None
788+
assert options.sort is None
789+
assert options.add_fields is None
790+
assert options.select is None
791+
assert options.offset is None
792+
assert options.query_enhancement is None
793+
assert options.language_code is None
794+
795+
stage = stages.Search(options)
796+
pb_opts = stage._pb_options()
797+
assert "query" in pb_opts
798+
assert "limit" not in pb_opts
799+
assert "retrieval_depth" not in pb_opts
800+
801+
def test_search_full_options(self):
802+
options = stages.SearchOptions(
803+
query=DocumentMatches("tech"),
804+
limit=10,
805+
retrieval_depth=2,
806+
sort=Ordering("score", Ordering.Direction.DESCENDING),
807+
add_fields=[Field("extra")],
808+
select=[Field("name")],
809+
offset=5,
810+
query_enhancement="disabled",
811+
language_code="en",
812+
)
813+
assert options.limit == 10
814+
assert options.retrieval_depth == 2
815+
assert len(options.sort) == 1
816+
assert options.offset == 5
817+
assert options.query_enhancement == stages.QueryEnhancement.DISABLED
818+
assert options.language_code == "en"
819+
820+
stage = stages.Search(options)
821+
pb_opts = stage._pb_options()
822+
823+
assert pb_opts["limit"].integer_value == 10
824+
assert pb_opts["retrieval_depth"].integer_value == 2
825+
assert len(pb_opts["sort"].array_value.values) == 1
826+
assert pb_opts["offset"].integer_value == 5
827+
assert pb_opts["query_enhancement"].string_value == "disabled"
828+
assert pb_opts["language_code"].string_value == "en"
829+
assert "query" in pb_opts
830+
831+
def test_search_string_query_wrapping(self):
832+
options = stages.SearchOptions(query="science")
833+
assert options.query.name == "document_matches"
834+
835+
def test_search_query_enhancement_enum(self):
836+
options = stages.SearchOptions(query="q", query_enhancement=stages.QueryEnhancement.REQUIRED)
837+
assert options.query_enhancement == stages.QueryEnhancement.REQUIRED
838+
839+
stage = stages.Search(options)
840+
pb_opts = stage._pb_options()
841+
assert pb_opts["query_enhancement"].string_value == "required"
842+
843+
781844
class TestSelect:
782845
def _make_one(self, *args, **kwargs):
783846
return stages.Select(*args, **kwargs)

0 commit comments

Comments
 (0)