@@ -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