def test_get_similar_objects(self): """Check similar objects manually and via Query API.""" similar_objects = Assessment.get_similar_objects_query( id_=self.assessment.id, types=["Assessment"], ).all() expected_ids = {id_ for id_, weight in self.id_weight_map.items() if weight >= Assessment.similarity_options["threshold"]} self.assertSetEqual( {obj.id for obj in similar_objects}, expected_ids, ) query = [{ "object_name": "Assessment", "type": "ids", "filters": { "expression": { "op": {"name": "similar"}, "object_name": "Assessment", "id": self.assessment.id, }, }, }] response = self.client.post( "/query", data=json.dumps(query), headers={"Content-Type": "application/json"}, ) self.assertSetEqual( set(json.loads(response.data)[0]["Assessment"]["ids"]), expected_ids, )
def test_get_similar_objects_weights(self): # pylint: disable=invalid-name """Check weights counted for similar objects.""" similar_objects = Assessment.get_similar_objects_query( id_=self.assessment.id, types=["Assessment"], threshold=0, # to include low weights too ).all() # casting to int from Decimal to prettify the assertion method output id_weight_map = {obj.id: int(obj.weight) for obj in similar_objects} self.assertDictEqual(id_weight_map, self.id_weight_map)
def test_similarity_for_request(self): """Check special case for similarity for Request by Audit.""" request1 = factories.RequestFactory(audit_id=self.audit.id) request2 = factories.RequestFactory(audit_id=self.audit.id) self.make_relationships(request1, [self.control, self.regulation]) requests_by_request = Request.get_similar_objects_query( id_=request1.id, types=["Request"], threshold=0, ).all() self.assertSetEqual( {(obj.type, obj.id, obj.weight) for obj in requests_by_request}, {("Request", request2.id, 5)}, ) requests_by_assessment = Assessment.get_similar_objects_query( id_=self.assessment.id, types=["Request"], threshold=0, ).all() self.assertSetEqual( {(obj.type, obj.id, obj.weight) for obj in requests_by_assessment}, {("Request", request1.id, 18), ("Request", request2.id, 5)}, ) assessments_by_request = Request.get_similar_objects_query( id_=request1.id, types=["Assessment"], threshold=0, ).all() other_assessments = {("Assessment", assessment.id, self.id_weight_map[assessment.id]) for assessment in self.other_assessments} self.assertSetEqual( {(obj.type, obj.id, obj.weight) for obj in assessments_by_request}, {("Assessment", self.assessment.id, 18)}.union(other_assessments), )