def get_activated_tenants(self, tenant_id=None): if not tenant_id: tenants_list = m.fetch(match='tenant_id', collection=self.utilization_collection_name) log.info(f"Activated_tenants: {tenants_list}") return tenants_list tenants_list = m.fetch(match={'tenant_id': tenant_id}, collection=self.utilization_collection_name) log.info(f"Activated tenant: {tenants_list}") return tenants_list
def test_fetch_distinct(): doc_list = m.fetch( match = 'name', collection = "TestCollection", as_list = True ) # print(doc_list) assert_that(doc_list).is_instance_of(list).is_not_empty()
def test_fetch_all_with_match(): datagen = m.fetch( match = {'name': 'John Show'}, collection="TestCollection", ) # print("\ntest_fetch_all_with_match:: datagen", datagen, type(datagen)) assert_that(len(list(datagen))).is_greater_than_or_equal_to(1)
def test_fetch_one_with_id(): data_dict = m.fetch( match = id1, collection="TestCollection" ) # print("\n\\ test_fetch_with_id:: data_dict, id1", data_dict, list(data_dict), id1) assert_that(data_dict).contains_entry({"_id": id1}) assert_that(data_dict['age']).is_instance_of(int) assert_that(data_dict['birthdate']).is_instance_of(dt.datetime)
def check_quota(self, tenant_id, number_of_units=0, 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") query = {'tenant_id': tenant_id, 'unit_type': unit_type} results = mongodata.fetch(query, self.collection) if not results: new_user_response, status = self.init_quota(tenant_id, unit_type) if status == 200: results = mongodata.fetch(query, self.collection) if results: quota = results[0] else: return new_user_response, status else: quota = results[0] # Reset quota if needed quota_reset_date = dateparser.parse(quota['quota_reset_date']) current_date = datetime.datetime.utcnow() if quota_reset_date < current_date: quota['quota_reset_date'] = get_quota_reset_date() quota['monthly_quota_consumed'] = 0 mongodata.update(self.schema, results[0], quota, self.collection) # Recall check_quota method with the new reseted date and quota self.check_quota(tenant_id, unit_type, number_of_units) if quota['monthly_quota_consumed'] <= quota[ 'monthly_quota'] + number_of_units: return { 'status': 'success', 'message': 'Utilization within monthly quota' }, 200 else: return {'status': 'fail', 'message': 'Monthly quota exceeded'}, 402
def fetch_data(self, request_obj): self.request_obj = request_obj results = m.fetch(match=self.query, collection=self.collection) log.debug(f"Fetching data with query {self.query} got {results}") if isinstance(results, str): abort(500, reason=results) if not results: abort(404, reason='Requested data not found') return results
def test_update_new_doc_existing_id(): new_data = { "_id": existing_id, "name": "cornelia" } updated_data = m.update( schema = AnotherDummySchema, collection="TestCollection", match = new_data["_id"], new_data = new_data, append = True ) data = m.fetch( match = new_data["_id"], collection="TestCollection", ) assert_that(data).is_equal_to(new_data)
def update_quota(self, tenant_id, number_of_units, 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") current_utilization = mongodata.fetch(match={ 'tenant_id': tenant_id, 'unit_type': unit_type }, collection=self.collection) if current_utilization: new_utilization = current_utilization[0] new_utilization['monthly_quota_consumed'] += number_of_units updated_docs = mongodata.update(schema=self.schema, match=current_utilization[0], new_data=new_utilization, collection=self.collection, append=False) if updated_docs == 1: return {'status': 'success', 'message': 'Quota updated'}, 200 else: return { 'status': 'fail', 'message': 'Quota failed to be updated' }, 500 else: new_user_status, response = self.init_quota(tenant_id, unit_type) if response == 200: retry_update = self.update_quota(tenant_id, unit_type, number_of_units) return retry_update else: return new_user_status, response
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 test_update_id_field_match(): new_data = { "_id": existing_id, "name": "razvan", "test_list": ["data"] } updated_data = m.update( schema = AnotherDummySchema, collection="TestCollection", match = {"_id": existing_id,"name": "cornelia"}, new_data = new_data, append = True ) data = m.fetch( match = new_data["_id"], collection="TestCollection", ) # print(data) assert_that(data['_id']).is_equal_to(new_data["_id"])
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)