def test_list(self, client): """List projects""" size = 10 ProjectFactory.create_batch(size) response = client.get("/api/projects/") assert response.status_code == status.HTTP_200_OK response_json = json.loads(response.content) assert len(response_json["results"]) == size
def test_list_queries(self, client, expand): """Queries should be constant""" small_size = 1 documents = DocumentFactory.create_batch(small_size) projects = ProjectFactory.create_batch(small_size, documents=documents) for document in documents: NoteFactory.create_batch(small_size, document=document) SectionFactory.create_batch(small_size, document=document) reset_queries() client.get( f"/api/projects/{projects[0].pk}/documents/?expand={expand}") num_queries = len(connection.queries) size = 10 documents = DocumentFactory.create_batch(size) projects = ProjectFactory.create_batch(size, documents=documents) for document in documents: NoteFactory.create_batch(size, document=document) SectionFactory.create_batch(size, document=document) reset_queries() response = client.get( f"/api/projects/{projects[0].pk}/documents/?expand={expand}") assert num_queries == len(connection.queries) assert len(response.json()["results"]) == size
def test_store_statistics(): documents = [] doc_data = [ (Access.public, 10, 10), (Access.organization, 20, 20), (Access.private, 30, 30), (Access.invisible, 1, 40), ] for access, num, page_count in doc_data: documents.extend( DocumentFactory.create_batch(num, access=access, page_count=page_count) ) # to test user / org uniqueness count documents[1].user = documents[0].user documents[1].organization = documents[0].organization documents[1].save() documents[2].organization = documents[0].organization documents[2].save() note_data = [ (Access.public, 35), (Access.organization, 25), (Access.private, 15), (Access.invisible, 0), ] for access, num in note_data: NoteFactory.create_batch(num, access=access, document=documents[0]) num_projects = 42 ProjectFactory.create_batch(num_projects) store_statistics() stats = Statistics.objects.first() assert stats.date == date.today() - timedelta(1) assert stats.total_documents == sum(d[1] for d in doc_data) assert stats.total_documents_public == doc_data[0][1] assert stats.total_documents_organization == doc_data[1][1] assert stats.total_documents_private == doc_data[2][1] assert stats.total_documents_invisible == doc_data[3][1] assert stats.total_pages == sum(d[1] * d[2] for d in doc_data) assert stats.total_pages_public == doc_data[0][1] * doc_data[0][2] assert stats.total_pages_organization == doc_data[1][1] * doc_data[1][2] assert stats.total_pages_private == doc_data[2][1] * doc_data[2][2] assert stats.total_pages_invisible == doc_data[3][1] * doc_data[3][2] assert stats.total_notes == sum(n[1] for n in note_data) assert stats.total_notes_public == note_data[0][1] assert stats.total_notes_organization == note_data[1][1] assert stats.total_notes_private == note_data[2][1] assert stats.total_notes_invisible == note_data[3][1] assert stats.total_projects == num_projects # one repeat user on public documents assert stats.total_users_uploaded == sum(d[1] for d in doc_data) - 1 assert stats.total_users_public_uploaded == doc_data[0][1] - 1 assert stats.total_users_organization_uploaded == doc_data[1][1] assert stats.total_users_private_uploaded == doc_data[2][1] # two repeat organizations on public documents assert stats.total_organizations_uploaded == sum(d[1] for d in doc_data) - 2 assert stats.total_organizations_public_uploaded == doc_data[0][1] - 2 assert stats.total_organizations_organization_uploaded == doc_data[1][1] assert stats.total_organizations_private_uploaded == doc_data[2][1]