Skip to content

Commit 54dbd6d

Browse files
Merge pull request #5 from reloc8/bugfix/global-statistics-when-no-results
Fix global statistics query result when no results
2 parents 73d9ad1 + 77a33f0 commit 54dbd6d

3 files changed

Lines changed: 72 additions & 4 deletions

File tree

fetch_properties/core/schema/resolver.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,21 @@ def find_statistics_by_filter(self, filter: PropertyFilter) -> Statistics:
202202
geohash=geohash
203203
))
204204

205-
global_result = list(global_results)[0]
205+
global_results = list(global_results)
206+
207+
min_price = None
208+
max_price = None
209+
avg_price = None
210+
if len(global_results) > 0:
211+
global_result = global_results[0]
212+
min_price = global_result.get('price').get('min')
213+
max_price = global_result.get('price').get('max')
214+
avg_price = global_result.get('price').get('avg')
215+
206216
global_price_statistics = PriceStatisticsMapper.map(
207-
min_price=global_result.get('price').get('min'),
208-
max_price=global_result.get('price').get('max'),
209-
avg_price=global_result.get('price').get('avg')
217+
min_price=min_price,
218+
max_price=max_price,
219+
avg_price=avg_price
210220
)
211221
global_statistics = GlobalStatisticsMapper.map(price_statistics=global_price_statistics)
212222

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"data": {
3+
"statisticsByFilter": {
4+
"localStatistics": [],
5+
"globalStatistics": {
6+
"price": {
7+
"min": null,
8+
"max": null,
9+
"avg": null
10+
}
11+
}
12+
}
13+
}
14+
}

tests/test_lambda_fetch_properties.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,50 @@ def testRunWhenReceiveApiGatewayEventAndQueryStatistics(self):
163163

164164
mock_mongodb_client.drop_database(mongodb_database)
165165

166+
def testRunWhenReceiveApiGatewayEventAndQueryStatisticsAndCollectionIsEmpty(self):
167+
168+
mongodb_uri = MONGODB_CONTAINER.get_connection_url()
169+
os.environ['MONGODB_URI'] = mongodb_uri
170+
os.environ['MONGODB_MAX_PAGE_SIZE'] = '2'
171+
os.environ['MONGODB_DATABASE'] = ''
172+
os.environ['MONGODB_COLLECTION'] = ''
173+
174+
with open('resources/event-api-gateway.json', 'r') as file:
175+
event = json.load(file)
176+
177+
with open('resources/query-3.graphql', 'r') as file:
178+
query = ' '.join(file.readlines())
179+
180+
with open('resources/response-body-4.json', 'r') as file:
181+
expected_body = json.load(file)
182+
183+
with open('resources/event-api-gateway-response.json', 'r') as file:
184+
event_response = json.load(file)
185+
186+
event['queryStringParameters'] = dict(query=query)
187+
expected_response = event_response
188+
expected_response['body'] = json.dumps(expected_body)
189+
190+
from fetch_properties.core.handler import LAMBDA_HANDLER, MONGODB_CONNECTION
191+
192+
mongodb_connection = MONGODB_CONNECTION
193+
mongodb_database = mongodb_connection.database
194+
mongodb_collection = mongodb_connection.collection
195+
mock_mongodb_client = pymongo.MongoClient(mongodb_uri)
196+
197+
actual_response = LAMBDA_HANDLER.run(event=event, context=None)
198+
199+
actual_body = json.loads(actual_response['body'])
200+
expected_body = json.loads(expected_response['body'])
201+
202+
self.assertEqual('timeSeriesDB', mongodb_database)
203+
self.assertEqual('properties', mongodb_collection)
204+
205+
self.assertCountEqual(actual_body['data']['statisticsByFilter']['localStatistics'], expected_body['data']['statisticsByFilter']['localStatistics'])
206+
self.assertDictEqual(actual_body['data']['statisticsByFilter']['globalStatistics'], expected_body['data']['statisticsByFilter']['globalStatistics'])
207+
208+
mock_mongodb_client.drop_database(mongodb_database)
209+
166210
@classmethod
167211
def tearDownClass(cls) -> None:
168212

0 commit comments

Comments
 (0)