def generate_data_collection(self): """Generate a Data collection. Returns: """ content = {"root": {"element": "value2"}} self.data_no_workspace = Data(template=self.template, title="Data 1", user_id="1").save() self.data_workspace_1 = Data( template=self.template, title="Data 3", user_id="1", workspace=self.workspace_user1.id, dict_content=content, ).save() self.data_public_workspace = Data( template=self.template, title="DataDoubleTitle", user_id="2", workspace=self.public_workspace.id, ).save() self.data_collection = [ self.data_no_workspace, self.data_workspace_1, self.data_public_workspace, ]
def create(self, validated_data): """ Create and return a new `Data` instance, given the validated data. """ # Create data instance = Data( template=validated_data["template"], workspace=validated_data["workspace"] if "workspace" in validated_data else None, title=validated_data["title"], user_id=str(self.context["request"].user.id), ) # Get template template_api.get(instance.template.id, request=self.context["request"]) # Set xml content instance.xml_content = validated_data["xml_content"] # Save the data and retrieve the inserted object inserted_data = data_api.upsert(instance, self.context["request"]) # Encode the response body inserted_data.xml_content = inserted_data.xml_content.encode("utf-8") return inserted_data
def generate_data_collection(self): """ Generate a Data collection. Returns: """ content_1 = { "root": { "element": "value", "list": [ {"element_list_1": 1}, {"element_list_2": 2} ], "complex": { "child1": "test", "child2": 0 } } } content_2 = { "root": { "element": "value2" } } # NOTE: no xml_content to avoid using unsupported GridFS mock self.data_1 = Data(template=self.template, user_id='1', dict_content=content_1, title='title').save() self.data_2 = Data(template=self.template, user_id='2', dict_content=content_2, title='title2').save() self.data_collection = [self.data_1, self.data_2]
def generate_data_collection(self): """ Generate a Data collection. Returns: """ self.data_1 = Data(template=self.template, title='Data 1', user_id='1').save() self.data_2 = Data(template=self.template, title='Data 2', user_id='2').save() self.data_3 = Data(template=self.template, title='Data 3', user_id='1', workspace=self.workspace_1.id).save() self.data_4 = Data(template=self.template, title='DataDoubleTitle', user_id='2', workspace=self.workspace_2.id).save() self.data_5 = Data(template=self.template, title='DataDoubleTitle', user_id='1', workspace=self.workspace_1.id).save() self.data_collection = [self.data_1, self.data_2, self.data_3, self.data_4, self.data_5]
def generate_data_collection(self): """ Generate a Data collection. Returns: """ data_1 = Data(template=self.template, title='Data 1', user_id='1').save() self.data_user_1.append(data_1) data_2 = Data(template=self.template, title='Data 2', user_id='2').save() self.data_user_2.append(data_2) data_3 = Data(template=self.template, title='Data 3', user_id='1', workspace=self.workspace_1.id).save() self.data_user_1.append(data_3) data_4 = Data(template=self.template, title='Data 4', user_id='2', workspace=self.workspace_2.id).save() self.data_user_2.append(data_4) self.data_collection = [data_1, data_2, data_3, data_4]
def test_data_get_all_return_objects_data_in_collection(self): # Act db_content = Data.get_all(DATA_SORTING_FIELDS) excluded_id_list = [str(db_content[0].pk)] result = Data.get_all_except([], excluded_id_list) # Assert self.assertTrue(result.count() == len(self.fixture.data_collection) - len(excluded_id_list))
def test_data_get_all_except_return_collection_of_data(self): # Act db_content = Data.get_all(DATA_SORTING_FIELDS) excluded_id_list = [str(db_content[0].pk)] result = Data.get_all_except([], excluded_id_list) # Assert self.assertTrue(all(isinstance(item, Data) for item in result))
def generate_data_collection(self): """Generate a Data collection. Returns: """ self.data_1 = Data(template=self.template_1, title="Data 1", user_id="1") self.data_1.xml_content = '<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \ <test>test</test> \ </root>' self.data_1.save() self.data_2 = Data(template=self.template_1, title="Data 2", user_id="1") self.data_2.xml_content = '<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \ <test>test</test> \ </root>' self.data_2.save() self.data_3 = Data(template=self.template_2, title="Data 3", user_id="1") self.data_3.xml_content = '<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \ <test>test</test> \ </root>' self.data_3.save() self.data_4 = Data(template=self.template_3, title="Data4", user_id="1") self.data_4.xml_content = '<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \ <other>test</other> \ </root>' self.data_4.save() self.data_5 = Data(template=self.template_3, title="Data5", user_id="1") self.data_5.xml_content = '<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \ <other>test</other> \ </root>' self.data_5.save() self.data_collection = [ self.data_1, self.data_2, self.data_3, self.data_4, self.data_5, ]
def generate_data_collection(self): """ Generate a Data collection. Returns: """ # NOTE: no xml_content to avoid using unsupported GridFS mock self.data_1 = Data(self.template, '1', None, 'title').save() self.data_2 = Data(self.template, '2', None, 'title2').save() self.data_collection = [self.data_1, self.data_2]
def test_get_all_except_data_ordering(self): # Arrange ascending_order_by_field = ["+title"] descending_order_by_field = ["-title"] # Act ascending_result = Data.get_all_except(ascending_order_by_field) descending_result = Data.get_all_except(descending_order_by_field) # Assert for i in range(len(ascending_result)): self.assertTrue(ascending_result.all()[i].title == descending_result.all()[len(ascending_result) - i - 1].title)
def test_get_all_by_workspace_data_ordering(self): # Arrange workspace = self.fixture.workspace_1.id ascending_order_by_field = ["+title"] descending_order_by_field = ["-title"] # Act ascending_result = Data.get_all_by_workspace(workspace, ascending_order_by_field) descending_result = Data.get_all_by_workspace(workspace, descending_order_by_field) # Assert for i in range(len(ascending_result)): self.assertTrue(ascending_result.all()[i].title == descending_result.all()[len(ascending_result) - i - 1].title)
def test_get_all_data_from_user_and_from_workspace_data_ordering(self): # Arrange mock_user = create_mock_user('1', is_superuser=False) ascending_order_by_field = ["+title"] descending_order_by_field = ["-title"] # Act ascending_result = Data.get_all_by_user_and_workspace(mock_user.id, [self.fixture.workspace_1], ascending_order_by_field) descending_result = Data.get_all_by_user_and_workspace(mock_user.id, [self.fixture.workspace_1], descending_order_by_field) # Assert for i in range(len(ascending_result)): self.assertTrue(ascending_result.all()[i].title == descending_result.all()[len(ascending_result) - i - 1].title)
def test_execute_query_data_ordering(self): # Arrange query = {} ascending_order_by_field = ["+title"] descending_order_by_field = ["-title"] # Act ascending_result = Data.execute_query(query, ascending_order_by_field) descending_result = Data.execute_query(query, descending_order_by_field) # Assert for i in range(len(ascending_result)): self.assertTrue(ascending_result.all()[i].title == descending_result.all()[len(ascending_result) - i - 1].title)
def test_get_all_by_list_template_data_ordering(self): # Arrange template = self.fixture.template.id ascending_order_by_field = ["+title"] descending_order_by_field = ["-title"] # Act ascending_result = Data.get_all_by_list_template([template], ascending_order_by_field) descending_result = Data.get_all_by_list_template([template], descending_order_by_field) # Assert for i in range(len(ascending_result)): self.assertTrue(ascending_result.all()[i].title == descending_result.all()[len(ascending_result) - i - 1].title)
def test_data_get_all_ordering(self): # Arrange access_control_data_fixture.insert_data() ascending_order_by_field = ["+title"] descending_order_by_field = ["-title"] # Act ascending_result = Data.get_all(ascending_order_by_field) descending_result = Data.get_all(descending_order_by_field) # Assert for i in range(len(ascending_result)): self.assertTrue(ascending_result.all()[i].title == descending_result.all()[len(ascending_result) - i - 1].title)
def _create_data(title="test"): """Create a data Args: title: Returns: """ data = Data(title=title, template="6137af4b91cb055990297f35", user_id="1") data.template = _get_template() data.xml_content = '<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ><string>x</string></root>' return data
def test_data_get_all_multi_field_sorting(self): # Arrange ascending_order_by_multi_field = ["+title", "+user_id"] descending_order_by_multi_field = ["+title", "-user_id"] # Act ascending_result = Data.get_all(ascending_order_by_multi_field) descending_result = Data.get_all(descending_order_by_multi_field) # Assert self.assertEqual(self.fixture.data_4.user_id, ascending_result.all()[4].user_id) self.assertEqual(self.fixture.data_5.user_id, ascending_result.all()[3].user_id) self.assertEqual(self.fixture.data_4.user_id, descending_result.all()[3].user_id) self.assertEqual(self.fixture.data_5.user_id, descending_result.all()[4].user_id)
def _create_data(template, title="test"): """Create a data Args: template title: Returns: """ data = Data(title=title, template="6137af4b91cb055990297f35", user_id="1") data.id = "6111b84691cb057552b3da20" data.template = template data.xml_content = "<root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><test>value</test></root>" return data
def test_get_all_by_user_id_multi_field_sorting(self): # Arrange ascending_order_by_multi_field = ["+user_id", "+title"] descending_order_by_multi_field = ["+user_id", "-title"] # Act ascending_result = Data.get_all_by_user_id(1, ascending_order_by_multi_field) descending_result = Data.get_all_by_user_id(1, descending_order_by_multi_field) # Assert self.assertEqual(self.fixture.data_1.title, ascending_result.all()[0].title) self.assertEqual(self.fixture.data_3.title, ascending_result.all()[1].title) self.assertEqual(self.fixture.data_3.title, descending_result.all()[1].title) self.assertEqual(self.fixture.data_1.title, descending_result.all()[2].title)
def create(self, validated_data): """ Create and return a new `Data` instance, given the validated data. """ # Create data instance = Data( template=validated_data['template'], title=validated_data['title'], user_id=str(validated_data['user'].id), ) # Set xml content instance.xml_content = validated_data['xml_content'] # Save the data return data_api.upsert(instance, validated_data['user'])
def test_get_all_by_list_workspace_multi_field_sorting(self): # Arrange ascending_order_by_multi_field = ["+workspace", "+title"] descending_order_by_multi_field = ["+workspace", "-title"] workspace = self.fixture.workspace_1.id # Act ascending_result = Data.get_all_by_list_workspace([workspace], ascending_order_by_multi_field) descending_result = Data.get_all_by_list_workspace([workspace], descending_order_by_multi_field) # Assert self.assertEqual(self.fixture.data_3.user_id, ascending_result.all()[0].user_id) self.assertEqual(self.fixture.data_5.user_id, ascending_result.all()[1].user_id) self.assertEqual(self.fixture.data_3.user_id, descending_result.all()[1].user_id) self.assertEqual(self.fixture.data_5.user_id, descending_result.all()[0].user_id)
def generate_data_collection(self): """ Generate a Data collection. Returns: """ # NOTE: no xml_content to avoid using unsupported GridFS mock self.data_1 = Data(template=self.template, user_id='1', dict_content=None, title='title').save() self.data_2 = Data(template=self.template, user_id='2', dict_content=None, title='title2').save() self.data_3 = Data(template=self.template, user_id='1', dict_content=None, title='title3').save() self.data_collection = [self.data_1, self.data_2, self.data_3]
def test_get_all_by_list_template_multi_field_sorting(self): # Arrange ascending_order_by_multi_field = ["+template", "+title"] descending_order_by_multi_field = ["+template", "-title"] template = self.fixture.template.id # Act ascending_result = Data.get_all_by_list_template([template], ascending_order_by_multi_field) descending_result = Data.get_all_by_list_template([template], descending_order_by_multi_field) # Assert self.assertEqual(self.fixture.data_1.title, ascending_result.all()[0].title) self.assertEqual(self.fixture.data_2.user_id, ascending_result.all()[1].user_id) self.assertEqual(self.fixture.data_2.user_id, descending_result.all()[3].user_id) self.assertEqual(self.fixture.data_1.user_id, descending_result.all()[4].user_id)
def test_get_all_data_from_user_and_from_workspace_multi_field_sorting(self): # Arrange ascending_order_by_multi_field = ["+template", "+title"] descending_order_by_multi_field = ["+template", "-title"] mock_user = create_mock_user('1', is_superuser=False) # Act ascending_result = Data.get_all_by_user_and_workspace(mock_user.id, [self.fixture.workspace_1], ascending_order_by_multi_field) descending_result = Data.get_all_by_user_and_workspace(mock_user.id, [self.fixture.workspace_1], descending_order_by_multi_field) # Assert self.assertEqual(self.fixture.data_1.title, ascending_result.all()[0].title) self.assertEqual(self.fixture.data_3.user_id, ascending_result.all()[1].user_id) self.assertEqual(self.fixture.data_3.user_id, descending_result.all()[1].user_id) self.assertEqual(self.fixture.data_1.user_id, descending_result.all()[2].user_id)
def test_data_get_all_except_inexistant_id_return_collection_of_data(self): # Act object_id_list = [str(data.pk) for data in Data.get_all(DATA_SORTING_FIELDS)] inexistant_object_id = str(ObjectId()) # If the generated object id correspond to one in DB we generate another one while inexistant_object_id in object_id_list: inexistant_object_id = str(ObjectId) excluded_id_list = [inexistant_object_id] result = Data.get_all_except([], excluded_id_list) # Assert self.assertTrue(all(isinstance(item, Data) for item in result))
def test_data_get_all_except_inexistant_id_return_objects_data_in_collection(self): # Act object_id_list = [str(data.pk) for data in Data.get_all(DATA_SORTING_FIELDS)] inexistant_object_id = str(ObjectId()) # If the generated object id correspond to one in DB we generate another one while inexistant_object_id in object_id_list: inexistant_object_id = str(ObjectId) excluded_id_list = [inexistant_object_id] result = Data.get_all_except([], excluded_id_list) # Assert self.assertTrue(result.count() == len(self.fixture.data_collection))
def test_execute_query_multi_field_sorting(self): # Arrange ascending_order_by_multi_field = ["+title", "+user_id"] descending_order_by_multi_field = ["+title", "-user_id"] query = {} # Act ascending_result = Data.execute_query(query, ascending_order_by_multi_field) descending_result = Data.execute_query(query, descending_order_by_multi_field) # Assert self.assertEqual(self.fixture.data_4.user_id, ascending_result.all()[4].user_id) self.assertEqual(self.fixture.data_5.user_id, ascending_result.all()[3].user_id) self.assertEqual(self.fixture.data_4.user_id, descending_result.all()[3].user_id) self.assertEqual(self.fixture.data_5.user_id, descending_result.all()[4].user_id)
def create_data(template, user_id, title, content): """Create mock data Args: template: user_id: title: content: Returns: """ data = Data(template=template, user_id=user_id, title=title) data.dict_content = content return data
def mock_data(version=""): with open(join(DUMP_OAI_PMH_TEST_PATH, "data{0}.json".format(version))) as f: data = f.read() data_json = json.loads(data) list_data = [Data(**x) for x in data_json] return list_data
def test_data_get_all_by_user_id_return_collection_of_data_from_user(self): # Arrange user_id = 1 # Act result = Data.get_all_by_user_id(user_id) # Assert self.assertTrue(all(item.user_id == str(user_id) for item in result))