def test_insert_one(): id_list = m.insert( schema=DummySchema, collection="TestCollection", data={ "_id": id1, "name": "John Show", "files": ["f1", "f2"], "age": "20", "birthdate": dt.datetime(2021, 9, 29).strftime( '%Y-%m-%d %H:%M:%S' ) } ) assert_that(id_list).is_instance_of(list).contains_only(id1)
def test_insert_many(): id_list = m.insert( schema=DummySchema, collection="TestCollection", data=[ dummy_data, dummy_data, dict(dummy_data, **{"_id": str(uuid.uuid4())}) ] ) #print("\ntest_insert_many:: id_list", id_list) assert_that(id_list).is_not_none().is_length(3)
def insert_data(self, request_obj): self.request_obj = request_obj data = dict( self.query, **{ "_id": str(uuid.uuid4()), "updated_at": datetime.datetime.utcnow().isoformat() }) inserted_docs = m.insert(schema=self.schema, collection=self.collection, data=data) log.debug( f"Inserting data with query {self.query} got {inserted_docs}") if len(inserted_docs) == 0: abort(404, reason='Could not insert data') if isinstance(inserted_docs, str): abort(500, reason=inserted_docs) return "SUCCESS"
def test_update_with_pullall(): _id = str(uuid.uuid4()) data = { "_id": _id, "name": "licenseware", "test_list": [1,2,3], "test_dict": {"nbr":2}, "test_list_of_dict": [ {"file_id": "should be unique", "other_data": "some data"}, {"file_id": "thefileid", "other_data": "some data"} ] } inserted = m.insert(AnotherDummySchema, "TestCollection", data) assert_that(inserted).contains_only(_id) new_data = { "_id": _id, "name": "licenseware_new", "test_list_of_dict": [ {"file_id": "should be unique", "other_data": "changed a little"}, ] } m.update( AnotherDummySchema, match=_id, new_data=new_data, collection="TestCollection", append = True ) saved_data = m.fetch(_id, "TestCollection")
def init_quota(self, tenant_id, unit_type=None): unit_type = unit_type or self._unit_type if not unit_type: raise ValueError( "Please specify a value for 'unit_type' parameter") results = mongodata.fetch(match={ 'tenant_id': tenant_id, 'unit_type': unit_type }, collection=self.collection) if results: return { 'status': 'success', 'message': 'Quota already initialized' }, 200 init_data = { "_id": str(uuid.uuid4()), "tenant_id": tenant_id, "unit_type": unit_type, "monthly_quota": QUOTA[unit_type], "monthly_quota_consumed": 0, "quota_reset_date": get_quota_reset_date() } inserted_ids = mongodata.insert(schema=self.schema, data=init_data, collection=self.collection) if isinstance(inserted_ids, list) and len(inserted_ids) == 1: return {'status': 'success', 'message': 'Quota initialized'}, 201 return {'status': 'fail', 'message': 'Quota failed to initialize'}, 500
def test_update_list_with_append(): catalogdict = { 'product_catalog_id': 'internet_application_server_standard', 'product_name': 'Internet Application Server Standard', 'result': 'Used', 'tenant_id': '2ac111c7-fd19-463e-96c2-1493aea18bed', 'version': '', 'install_location': '', 'product_category': 'Application Server Products', '_id': '57077fca-daae-582d-8335-8f413876c140', } data_list = [ # { # "name": "Dan lupin", # "some_field": "this should remain unchanged" # }, { # "_id": "append_test", "name": 'Alin', "some_field": "this should remain unchanged", "test_list": ["initial_list_value"], "test_list2": [ "initial_list_value2"], "test_dict": {"initial_dict_key":"initial_dict_value"}, "test_list_of_dict": [ {"initial_dict_key":"initial_dict_value", "some_id":"1"}, catalogdict ], } ] id_list = m.insert(AnotherDummySchema, "TestCollection", data_list) assert_that(len(id_list)).is_equal_to(len(data_list)) # print(id_list) #Trying to see if duplicates from new_date are removed new_data = { "_id": id_list[0], 'name': 'Alin', "test_list": ["initial_list_value", "appended_value"], "test_list2": ['initial_list_value2', "appended_value2"], "test_dict": {"initial_dict_key":"initial_dict_value", "new_dict_key":"new_dict_value"}, "test_list_of_dict": [ {"initial_dict_key":"initial_dict_value", "some_id":"1"}, {"new_dict_key":"new_dict_value"}, catalogdict ] } # Updating the same data twice updated_data = m.update( schema = AnotherDummySchema, collection ="TestCollection", match = {"_id": id_list[0]}, new_data = new_data, append = True ) new_data = { "name": 'John', "test_list_of_dict": [ {"initial_dict_key":"initial_dict_value", "some_id":"1"}, {"new_dict_key":"new_dict_value"}, sort_dict(catalogdict) ] } updated_data = m.update( schema = AnotherDummySchema, collection ="TestCollection", match = {"_id": id_list[0]}, new_data = new_data, append = True ) # print(updated_data) data = m.fetch( match = {"_id": id_list[0]}, collection="TestCollection", ) # print(data) dict_ = data[0] assert_that(dict_['test_list']).is_length(2) assert_that(dict_['test_list2']).is_length(2) assert_that(dict_['test_dict'].keys()).is_length(2)