Skip to content

Commit

Permalink
fix: old institution-user metrics tests
Browse files Browse the repository at this point in the history
- set INSTITUTIONAL_DASHBOARD_2024 flag inactive for existing
  institution-user metrics tests
- remove `time.sleep` calls; use index refreshes instead (saves ~70 sec)
- unskip tests -- "non-deterministic fails" may be fixed by refreshes?
  • Loading branch information
aaxelb committed Sep 6, 2024
1 parent 190259d commit 65975f9
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions api_tests/institutions/views/test_institution_user_metric_list.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import pytest
import datetime
import csv
from io import StringIO
from random import random
import time

import pytest
from waffle.testutils import override_flag

from api.base.settings.defaults import API_BASE, DEFAULT_ES_NULL_VALUE
import osf.features
from osf_tests.factories import (
InstitutionFactory,
AuthUserFactory,
)

from osf.metrics import UserInstitutionProjectCounts
from api.base import settings

@pytest.mark.es
@pytest.mark.django_db
class TestInstitutionUserMetricList:

@pytest.fixture(autouse=True)
def _waffled(self):
with override_flag(osf.features.INSTITUTIONAL_DASHBOARD_2024, active=False):
yield # these tests apply only before institution dashboard improvements

@pytest.fixture()
def institution(self):
return InstitutionFactory()
Expand Down Expand Up @@ -52,33 +58,31 @@ def admin(self, institution):
@pytest.fixture()
def populate_counts(self, institution, user, user2):
# Old data that shouldn't appear in responses
UserInstitutionProjectCounts.record(
UserInstitutionProjectCounts(
user_id=user._id,
institution_id=institution._id,
department='Biology dept',
public_project_count=4,
private_project_count=4,
timestamp=datetime.date(2019, 6, 4)
).save()
).save(refresh=True)

# New data
UserInstitutionProjectCounts.record(
UserInstitutionProjectCounts(
user_id=user._id,
institution_id=institution._id,
department='Biology dept',
public_project_count=6,
private_project_count=5,
).save()

UserInstitutionProjectCounts.record(
UserInstitutionProjectCounts(
user_id=user2._id,
institution_id=institution._id,
department='Psychology dept',
public_project_count=3,
private_project_count=2,
).save()

time.sleep(10)
).save(refresh=True)

@pytest.fixture()
def populate_more_counts(self, institution, user, user2, user3, populate_counts):
Expand All @@ -89,34 +93,30 @@ def populate_more_counts(self, institution, user, user2, user3, populate_counts)
users.append(AuthUserFactory())

for test_user in users:
UserInstitutionProjectCounts.record(
UserInstitutionProjectCounts(
user_id=test_user._id,
institution_id=institution._id,
department='Psychology dept',
public_project_count=int(10 * random()),
private_project_count=int(10 * random()),
).save()

UserInstitutionProjectCounts.record(
UserInstitutionProjectCounts(
user_id=user3._id,
institution_id=institution._id,
department='Psychology dept',
public_project_count=int(10 * random()),
private_project_count=int(10 * random()),
).save()

time.sleep(10)
).save(refresh=True)

@pytest.fixture()
def populate_na_department(self, institution, user4):
UserInstitutionProjectCounts.record(
UserInstitutionProjectCounts(
user_id=user4._id,
institution_id=institution._id,
public_project_count=1,
private_project_count=1,
).save()

time.sleep(10)
).save(refresh=True)

@pytest.fixture()
def url(self, institution):
Expand Down Expand Up @@ -218,7 +218,6 @@ def test_filter(self, app, url, admin, populate_counts):
resp = app.get(f'{url}?filter[department]=Psychology dept', auth=admin.auth)
assert resp.json['data'][0]['attributes']['department'] == 'Psychology dept'

@pytest.mark.skipif(settings.TRAVIS_ENV, reason='Non-deterministic fails on travis')
def test_sort_and_pagination(self, app, url, user, user2, user3, admin, populate_counts, populate_more_counts, institution):
resp = app.get(f'{url}?sort=user_name&page[size]=1&page=2', auth=admin.auth)
assert resp.status_code == 200
Expand All @@ -229,7 +228,6 @@ def test_sort_and_pagination(self, app, url, user, user2, user3, admin, populate
assert resp.json['links']['meta']['total'] == 11
assert resp.json['data'][-1]['attributes']['user_name'] == 'Zedd'

@pytest.mark.skipif(settings.TRAVIS_ENV, reason='Non-deterministic fails on travis')
def test_filter_and_pagination(self, app, user, user2, user3, url, admin, populate_counts, populate_more_counts, institution):
resp = app.get(f'{url}?page=2', auth=admin.auth)
assert resp.json['links']['meta']['total'] == 11
Expand All @@ -238,7 +236,6 @@ def test_filter_and_pagination(self, app, user, user2, user3, url, admin, popula
assert resp.json['links']['meta']['total'] == 1
assert resp.json['data'][0]['attributes']['user_name'] == 'Zedd'

@pytest.mark.skipif(settings.TRAVIS_ENV, reason='Non-deterministic fails on travis')
def test_filter_and_sort(self, app, url, user, user2, user3, admin, user4, populate_counts, populate_na_department, institution):
"""
Testing for bug where sorting and filtering would throw 502.
Expand Down

0 comments on commit 65975f9

Please sign in to comment.