def test_analysis_framework_filter(self): af1, af2, af3 = AnalysisFrameworkFactory.create_batch(3) p1 = ProjectFactory.create(analysis_framework=af1) p2 = ProjectFactory.create(analysis_framework=af2) ProjectFactory.create(analysis_framework=af3) obtained = self.filter_class(data=dict( analysis_frameworks=[af1.id, af2.id])).qs expected = [p2, p1] self.assertQuerySetIdEqual(expected, obtained)
def test_public_analysis_framework(self): query = ''' query MyQuery { publicAnalysisFrameworks (ordering: "id") { page pageSize totalCount results { id title } } } ''' AnalysisFrameworkFactory.create_batch(4, is_private=False) AnalysisFrameworkFactory.create_batch(5, is_private=True) content = self.query_check(query) self.assertEqual( content['data']['publicAnalysisFrameworks']['totalCount'], 4, content)
def test_user_filters(self): query = ''' query UserQuery($membersExcludeFramework: ID, $membersExcludeProject: ID, $search: String) { users( membersExcludeFramework: $membersExcludeFramework, membersExcludeProject: $membersExcludeProject, search: $search ) { results { organization lastName language isActive id firstName displayPictureUrl displayName } page pageSize } } ''' project1, project2 = ProjectFactory.create_batch(2) af1, af2 = AnalysisFrameworkFactory.create_batch(2) user = UserFactory.create(first_name='Normal Guy') user1, user2, user3 = UserFactory.create_batch(3) project1.add_member(user1) project1.add_member(user2) project2.add_member(user2) project2.add_member(user) af1.add_member(user1) af1.add_member(user2) af1.add_member(user) af2.add_member(user2) def _query_check(filters, **kwargs): return self.query_check(query, variables=filters, **kwargs) # Without authentication ----- content = _query_check({}, assert_for_error=True) # With authentication ----- self.force_login(user) # Without any filters for name, filters, count, users in ( ('no-filter', dict(), 4, [user, user1, user2, user3]), ('exclude-project-1', dict(membersExcludeProject=project1.pk), 2, [user, user3]), ('exclude-project-2', dict(membersExcludeProject=project2.pk), 2, [user1, user3]), ('exclude-af-1', dict(membersExcludeFramework=af1.pk), 1, [user3]), ('exclude-af-2', dict(membersExcludeFramework=af2.pk), 3, [user, user1, user3]), ('search', dict(search='Guy'), 1, [user]), ): content = _query_check(filters)['data']['users']['results'] self.assertEqual(len(content), count, (name, content)) self.assertListIds(content, users, (name, content))
def test_leads_entries_query(self): query = ''' query MyQuery ($id: ID!, $leadId: ID!) { project(id: $id) { analysisFramework { id } lead(id: $leadId) { id entriesCount { total controlled } entries { id } } } } ''' af, af_new = AnalysisFrameworkFactory.create_batch(2) user = UserFactory.create() project = ProjectFactory.create(analysis_framework=af) project.add_member(user, role=self.project_role_member) lead = LeadFactory.create(project=project) controlled_entries = EntryFactory.create_batch(2, lead=lead, controlled=True) not_controlled_entries = EntryFactory.create_batch(3, lead=lead, controlled=False) def _query_check(): return self.query_check(query, variables={ 'id': project.id, 'leadId': lead.id }) # -- With login self.force_login(user) response = _query_check() self.assertIdEqual( response['data']['project']['analysisFramework']['id'], af.pk) content = response['data']['project']['lead'] self.assertIdEqual(content['id'], lead.pk, content) self.assertEqual(content['entriesCount']['total'], 5, content) self.assertEqual(content['entriesCount']['controlled'], 2, content) self.assertListIds(content['entries'], [*controlled_entries, *not_controlled_entries], content) # Now change AF project.analysis_framework = af_new project.save(update_fields=('analysis_framework', )) new_controlled_entries = EntryFactory.create_batch(4, lead=lead, controlled=True) new_not_controlled_entries = EntryFactory.create_batch( 2, lead=lead, controlled=False) response = _query_check() self.assertIdEqual( response['data']['project']['analysisFramework']['id'], af_new.pk) content = response['data']['project']['lead'] self.assertIdEqual(content['id'], lead.pk, content) self.assertEqual(content['entriesCount']['total'], 6, content) self.assertEqual(content['entriesCount']['controlled'], 4, content) self.assertListIds( content['entries'], [*new_controlled_entries, *new_not_controlled_entries], content)
def test_leads_fields_query(self): """ Test leads field value """ query = ''' query MyQuery ($id: ID!) { project(id: $id) { analysisFramework { id } leads(ordering: ASC_ID) { page pageSize totalCount results { id status createdAt title publishedOn priority entriesCount { total controlled } authors { id mergedAs { id title logo { id file { name url } } } } source { id logo { id file { name url } } mergedAs { id title } } } } } } ''' af, af_new = AnalysisFrameworkFactory.create_batch(2) project = ProjectFactory.create(analysis_framework=af) org_type = OrganizationTypeFactory.create() org1 = OrganizationFactory.create(organization_type=org_type) org2 = OrganizationFactory.create(organization_type=org_type, parent=org1) org3 = OrganizationFactory.create(organization_type=org_type) # User with role user = UserFactory.create() project.add_member(user) # Create lead lead1 = LeadFactory.create(project=project, source=org1) lead2 = LeadFactory.create(project=project, source=org2, authors=[org1, org3]) lead3 = LeadFactory.create(project=project) # Some entries for entriesCount EntryFactory.create_batch(2, lead=lead1, controlled=True) EntryFactory.create_batch(5, lead=lead1) EntryFactory.create_batch(10, lead=lead2) # -- With login self.force_login(user) # --- member user (only unprotected leads) self.force_login(user) content = self.query_check(query, variables={'id': project.id}) self.assertIdEqual( content['data']['project']['analysisFramework']['id'], af.pk) results = content['data']['project']['leads']['results'] # Count check self.assertEqual(content['data']['project']['leads']['totalCount'], 3, content) self.assertListIds(results, [lead1, lead2, lead3], content) self.assertEqual(len(results[0]['authors']), 0, content) # Source check self.assertIdEqual(results[0]['source']['id'], org1.id, content) self.assertEqual(results[0]['source']['logo']['file']['name'], str(org1.logo.file.name), content) self.assertEqual(results[0]['source']['logo']['file']['url'], self.get_media_url(org1.logo.file.name), content) # Authors check self.assertListIds(results[1]['authors'], [org1, org3], content) self.assertIdEqual(results[1]['source']['mergedAs']['id'], org1.id, content) # Entries Count check for index, (total_count, controlled_count) in enumerate([ [7, 2], [10, 0], [0, 0], ]): self.assertEqual(results[index]['entriesCount']['total'], total_count, content) self.assertEqual(results[index]['entriesCount']['controlled'], controlled_count, content) # Change AF, this will now not show old entries content = self.query_check(query, variables={'id': project.id}) project.analysis_framework = af_new project.save(update_fields=('analysis_framework', )) EntryFactory.create_batch(2, lead=lead1, controlled=True) EntryFactory.create_batch(1, lead=lead2, controlled=False) content = self.query_check(query, variables={'id': project.id}) self.assertIdEqual( content['data']['project']['analysisFramework']['id'], af_new.pk) results = content['data']['project']['leads']['results'] # Entries Count check (After AF change) for index, (total_count, controlled_count) in enumerate([ [2, 2], [1, 0], [0, 0], ]): self.assertEqual(results[index]['entriesCount']['total'], total_count, content) self.assertEqual(results[index]['entriesCount']['controlled'], controlled_count, content)