Skip to content

Commit

Permalink
MongoDB Adapter Batch_Create and Pagination in find() (#122)
Browse files Browse the repository at this point in the history
* mongodb batch_create

* try to fix tests

* fixed tests and pushed pipfile

* fixed tests

* handle pagination in find

* resolve comments
  • Loading branch information
abdullah-syngenta authored Aug 31, 2022
1 parent 6c500cc commit 75fb9fa
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 14 deletions.
27 changes: 21 additions & 6 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion syngenta_digital_dta/mongo/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def create(self, **kwargs):
super().publish('create', data, **kwargs)
return data

def batch_create(self, **kwargs):
items = []
for item in kwargs['data']:
item = schema_mapper.map_to_schema(item, self.model_schema_file, self.model_schema)
item['_id'] = item[self.model_identifier]
items.append(item)
self.connection.insert_many(items)
super().publish('batch_create', items, **kwargs)
return items

def read(self, **kwargs):
if kwargs.get('operation') == 'query':
return self.find(**kwargs)
Expand All @@ -61,7 +71,7 @@ def find_one(self, **kwargs):
return self.connection.find_one(kwargs['query'])

def find(self, **kwargs):
results = self.connection.find(kwargs['query'])
results = self.connection.find(kwargs['query'], **kwargs.get('params', {}))
return list(results)

def update(self, **kwargs):
Expand Down
37 changes: 37 additions & 0 deletions tests/syngenta_digital_dta/mongo/mock_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import uuid


def get_standard():
return {
'test_id': str(uuid.uuid4()),
Expand All @@ -17,3 +18,39 @@ def get_standard():
'created': '2020-10-05',
'modified': '2020-10-05'
}


def get_items():
return [{
'test_id': str(uuid.uuid4()),
'test_query_id': str(uuid.uuid4()),
'object_key': {
'string_key': 'nothing'
},
'array_number': [1, 2, 3],
'array_objects': [
{
'array_string_key': 'a',
'array_number_key': 1
}
],
'created': '2020-10-05',
'modified': '2020-10-05'
},
{
'test_id': str(uuid.uuid4()),
'test_query_id': str(uuid.uuid4()),
'object_key': {
'string_key': 'nothing'
},
'array_number': [1, 2, 3],
'array_objects': [
{
'array_string_key': 'a',
'array_number_key': 1
}
],
'created': '2020-10-05',
'modified': '2020-10-05'
}
]
35 changes: 28 additions & 7 deletions tests/syngenta_digital_dta/mongo/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def test_create_succeed(self):
self.assertDictEqual(result, data)
self.adapter.delete(query={'test_id': result['test_id']})

def test_batch_create_succeed(self):
data = mock_data.get_items()
result = self.adapter.batch_create(data=data)
for item in result:
item.pop('_id')
self.assertListEqual(result, data)
for item in result:
self.adapter.delete(query={'test_id': item['test_id']})

def test_create_fail_non_unique(self):
data = mock_data.get_standard()
data['test_id'] = 'fail-non-unique'
Expand All @@ -44,22 +53,22 @@ def test_create_fail_non_unique(self):
self.assertTrue(False)
except:
self.assertTrue(True)
self.adapter.delete(query={'test_id': data['test_id']}) # clean up
self.adapter.delete(query={'test_id': data['test_id']}) # clean up

def test_read(self):
data = mock_data.get_standard()
self.adapter.create(data=data)
result = self.adapter.read(query={'test_id': data['test_id']})
result.pop('_id')
self.assertDictEqual(result, data)
self.adapter.delete(query={'test_id': data['test_id']}) # clean up
self.adapter.delete(query={'test_id': data['test_id']}) # clean up

def test_query_allowed(self):
data = mock_data.get_standard()
self.adapter.create(data=data)
result = self.adapter.query(operation='find', query={'test_id': data['test_id']})
self.assertTrue(len(list(result)) == 1)
self.adapter.delete(query={'test_id': data['test_id']}) # clean up
self.adapter.delete(query={'test_id': data['test_id']}) # clean up

def test_not_query_allowed(self):
data = mock_data.get_standard()
Expand All @@ -79,16 +88,28 @@ def test_read_many(self):
count += 1
results = self.adapter.read(query={'test_query_id': 'some-query'}, operation='query')
for result in results:
self.adapter.delete(query={'test_id': result['test_id']}) # clean up
self.adapter.delete(query={'test_id': result['test_id']}) # clean up
self.assertTrue(len(results) >= 3)

def test_read_many_pagination(self):
count = 0
while count < 10:
data = mock_data.get_standard()
data['test_query_id'] = 'some-query'
self.adapter.create(data=data)
count += 1
results = self.adapter.read(query={'test_query_id': 'some-query'}, operation='query', params={'skip': 5, 'limit': 5})
for result in results:
self.adapter.delete(query={'test_id': result['test_id']}) # clean up
self.assertEqual(len(results), 5)

def test_update_success(self):
data = mock_data.get_standard()
self.adapter.create(data=data)
data['array_number'] = [4,5,6]
data['array_number'] = [4, 5, 6]
result = self.adapter.update(query={'test_id': data['test_id']}, data=data, update_list_operation='replace')
self.assertDictEqual(result, data)
self.adapter.delete(query={'test_id': data['test_id']}) # clean up
self.adapter.delete(query={'test_id': data['test_id']}) # clean up

def test_update_fail(self):
data = mock_data.get_standard()
Expand All @@ -103,7 +124,7 @@ def test_upsert(self):
result = self.adapter.upsert(query={'test_id': data['test_id']}, data=data)
result.pop('_id')
self.assertDictEqual(result, data)
self.adapter.delete(query={'test_id': data['test_id']}) # clean up
self.adapter.delete(query={'test_id': data['test_id']}) # clean up

def test_delete(self):
data = mock_data.get_standard()
Expand Down

0 comments on commit 75fb9fa

Please sign in to comment.