Skip to content

Commit

Permalink
tests: address randomness-related failures (pypi#15469)
Browse files Browse the repository at this point in the history
* test: update test to sort the same way as code

The `_simple_detail()` function has sorted the results by version, then
filename, so update test conditions to sort similarly as well.

Exposed with:
--randomly-seed=105155306
--randomly-seed=1843708888
--randomly-seed=19699634

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* test: actually assert things

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* test: use a paginated page in test

Other tests paginating expect a page-like object instead of the list of
items, so supply one.

Exposed via:
--randomly-seed=3227833760
--randomly-seed=235656747

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* test: remove db-controlled sequence from factory

When using `factory.Sequence()` for a database-controlled sequence
column, we can get into a weird state where we're reusing the sequence
since we've flushed already.

Since we already rely on the database to generate the ID for us, we
don't have to supply it ourselves as part of the factory. If we ever
*do* want to set a specific id for a JournalEntry, we would use a
keyword argument like `JournalEntryFactory.create(id=...)`

Exposed via:
--randomly-seed=2329742959

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

---------

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
  • Loading branch information
miketheman authored Feb 23, 2024
1 parent 675794b commit 874c3b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
1 change: 0 additions & 1 deletion tests/common/db/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ class JournalEntryFactory(WarehouseFactory):
class Meta:
model = JournalEntry

id = factory.Sequence(lambda n: n)
name = factory.Faker("word")
version = factory.Sequence(lambda n: str(n) + ".0")
submitted_date = factory.Faker(
Expand Down
21 changes: 19 additions & 2 deletions tests/unit/admin/views/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
import pretend
import pytest

from paginate_sqlalchemy import SqlalchemyOrmPage
from pyramid.httpexceptions import HTTPBadRequest, HTTPMovedPermanently, HTTPSeeOther
from sqlalchemy.orm import joinedload

from tests.common.db.oidc import GitHubPublisherFactory
from warehouse.admin.views import projects as views
from warehouse.observations.models import ObservationKind
from warehouse.packaging.models import Project, Role
from warehouse.packaging.tasks import update_release_description
from warehouse.search.tasks import reindex_project
from warehouse.utils.paginate import paginate_url_factory

from ....common.db.accounts import UserFactory
from ....common.db.observations import ObserverFactory
Expand Down Expand Up @@ -433,18 +436,32 @@ def test_with_page(self, db_request):
observer = ObserverFactory.create()
UserFactory.create(observer=observer)
project = ProjectFactory.create()
observations = ProjectObservationFactory.create_batch(
ProjectObservationFactory.create_batch(
size=30, related=project, observer=observer
)

observations_query = (
db_request.db.query(project.Observation)
.options(joinedload(project.Observation.observer))
.filter(project.Observation.related == project)
.order_by(project.Observation.created.desc())
)
observations_page = SqlalchemyOrmPage(
observations_query,
page=2,
items_per_page=25,
url_maker=paginate_url_factory(db_request),
)

db_request.matchdict["project_name"] = project.normalized_name
db_request.GET["page"] = "2"
result = views.project_observations_list(project, db_request)

assert result == {
"observations": observations[25:],
"observations": observations_page,
"project": project,
}
assert len(observations_page.items) == 5

def test_with_invalid_page(self, db_request):
project = ProjectFactory.create()
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/api/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_no_results_no_serial(self, db_request, content_type, renderer_override)
_assert_has_cors_headers(db_request.response.headers)

if renderer_override is not None:
db_request.override_renderer == renderer_override
assert db_request.override_renderer == renderer_override

@pytest.mark.parametrize(
"content_type,renderer_override",
Expand All @@ -121,7 +121,7 @@ def test_no_results_with_serial(self, db_request, content_type, renderer_overrid
_assert_has_cors_headers(db_request.response.headers)

if renderer_override is not None:
db_request.override_renderer == renderer_override
assert db_request.override_renderer == renderer_override

@pytest.mark.parametrize(
"content_type,renderer_override",
Expand All @@ -142,7 +142,7 @@ def test_with_results_no_serial(self, db_request, content_type, renderer_overrid
_assert_has_cors_headers(db_request.response.headers)

if renderer_override is not None:
db_request.override_renderer == renderer_override
assert db_request.override_renderer == renderer_override

@pytest.mark.parametrize(
"content_type,renderer_override",
Expand All @@ -168,7 +168,7 @@ def test_with_results_with_serial(
_assert_has_cors_headers(db_request.response.headers)

if renderer_override is not None:
db_request.override_renderer == renderer_override
assert db_request.override_renderer == renderer_override


class TestSimpleDetail:
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_no_files_no_serial(self, db_request, content_type, renderer_override):
_assert_has_cors_headers(db_request.response.headers)

if renderer_override is not None:
db_request.override_renderer == renderer_override
assert db_request.override_renderer == renderer_override

@pytest.mark.parametrize(
"content_type,renderer_override",
Expand All @@ -235,7 +235,7 @@ def test_no_files_with_serial(self, db_request, content_type, renderer_override)
_assert_has_cors_headers(db_request.response.headers)

if renderer_override is not None:
db_request.override_renderer == renderer_override
assert db_request.override_renderer == renderer_override

@pytest.mark.parametrize(
"content_type,renderer_override",
Expand All @@ -250,8 +250,8 @@ def test_with_files_no_serial(self, db_request, content_type, renderer_override)
FileFactory.create(release=r, filename=f"{project.name}-{r.version}.tar.gz")
for r in releases
]
# let's assert the result is ordered by string comparison of filename
files = sorted(files, key=lambda key: key.filename)
# let's assert the result is ordered by string comparison of version, filename
files = sorted(files, key=lambda f: (parse(f.release.version), f.filename))
urls_iter = (f"/file/{f.filename}" for f in files)
db_request.matchdict["name"] = project.normalized_name
db_request.route_url = lambda *a, **kw: next(urls_iter)
Expand Down Expand Up @@ -283,7 +283,7 @@ def test_with_files_no_serial(self, db_request, content_type, renderer_override)
_assert_has_cors_headers(db_request.response.headers)

if renderer_override is not None:
db_request.override_renderer == renderer_override
assert db_request.override_renderer == renderer_override

@pytest.mark.parametrize(
"content_type,renderer_override",
Expand Down Expand Up @@ -331,7 +331,7 @@ def test_with_files_with_serial(self, db_request, content_type, renderer_overrid
_assert_has_cors_headers(db_request.response.headers)

if renderer_override is not None:
db_request.override_renderer == renderer_override
assert db_request.override_renderer == renderer_override

@pytest.mark.parametrize(
"content_type,renderer_override",
Expand Down Expand Up @@ -424,4 +424,4 @@ def test_with_files_with_version_multi_digit(
_assert_has_cors_headers(db_request.response.headers)

if renderer_override is not None:
db_request.override_renderer == renderer_override
assert db_request.override_renderer == renderer_override

0 comments on commit 874c3b2

Please sign in to comment.