Skip to content

Commit

Permalink
Remove more complex / indirect fixtures used in tests (#975)
Browse files Browse the repository at this point in the history
* Move fixtures and constants from conftest to modules

* Remove use of `sample_contacts` fixture

* Remove test get for maximal contact test

This is largely covered by the "example" test

* Switch default email format to HTML in email factory

* Remove minimal / maximal contacts

* Remove now-unused fixtures
  • Loading branch information
grahamalama authored Oct 1, 2024
1 parent 19b748d commit 72b8224
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 330 deletions.
2 changes: 1 addition & 1 deletion tests/factories/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Meta:
first_name = factory.Faker("first_name")
last_name = factory.Faker("last_name")
mailing_country = factory.Faker("country_code")
email_format = "T"
email_format = "H"
email_lang = factory.Faker("language_code")
double_opt_in = False
has_opted_out_of_email = False
Expand Down
57 changes: 0 additions & 57 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,6 @@
},
}

# Common test cases to use in parameterized tests
# List of tuples comprised of name fixture name (defined below) and the list
# of fields that are given a default value in that fixture, and therefore
# should not be overwritten on update.
SAMPLE_CONTACT_PARAMS = [
("minimal_contact_data", set()),
("maximal_contact_data", set()),
("example_contact_data", set()),
("to_add_contact_data", set()),
("simple_default_contact_data", {"amo"}),
("default_newsletter_contact_data", {"newsletters"}),
("default_waitlist_contact_data", {"waitlists"}),
]


def _gather_examples(schema_class) -> dict[str, str]:
"""Gather the examples from a schema definition"""
Expand Down Expand Up @@ -256,12 +242,6 @@ def minimal_contact_data() -> ContactSchema:
)


@pytest.fixture
def minimal_contact(minimal_contact_data: ContactSchema, dbsession) -> ContactSchema:
create_full_contact(dbsession, minimal_contact_data)
return minimal_contact_data


@pytest.fixture
def maximal_contact_data() -> ContactSchema:
email_id = UUID("67e52c77-950f-4f28-accb-bb3ea1a2c51a")
Expand Down Expand Up @@ -405,12 +385,6 @@ def maximal_contact_data() -> ContactSchema:
)


@pytest.fixture
def maximal_contact(dbsession, maximal_contact_data: ContactSchema) -> ContactSchema:
create_full_contact(dbsession, maximal_contact_data)
return maximal_contact_data


@pytest.fixture
def example_contact_data() -> ContactSchema:
return ContactSchema(
Expand Down Expand Up @@ -450,12 +424,6 @@ def to_add_contact_data() -> ContactSchema:
)


@pytest.fixture
def to_add_contact(dbsession, to_add_contact_data: ContactSchema) -> ContactSchema:
create_full_contact(dbsession, to_add_contact_data)
return to_add_contact_data


@pytest.fixture
def simple_default_contact_data():
return ContactSchema(
Expand All @@ -470,14 +438,6 @@ def simple_default_contact_data():
)


@pytest.fixture
def simple_default_contact(
dbsession, simple_default_contact_data: ContactSchema
) -> ContactSchema:
create_full_contact(dbsession, simple_default_contact_data)
return simple_default_contact_data


@pytest.fixture
def default_newsletter_contact_data():
contact = ContactSchema(
Expand All @@ -493,14 +453,6 @@ def default_newsletter_contact_data():
return contact


@pytest.fixture
def default_newsletter_contact(
dbsession, default_newsletter_contact_data: ContactSchema
) -> ContactSchema:
create_full_contact(dbsession, default_newsletter_contact_data)
return default_newsletter_contact_data


@pytest.fixture
def default_waitlist_contact_data():
contact = ContactSchema(
Expand All @@ -516,15 +468,6 @@ def default_waitlist_contact_data():
return contact


@pytest.fixture
def sample_contacts(minimal_contact, maximal_contact, example_contact):
return {
"minimal": (minimal_contact.email.email_id, minimal_contact),
"maximal": (maximal_contact.email.email_id, maximal_contact),
"example": (example_contact.email.email_id, example_contact),
}


@pytest.fixture
def anon_client(dbsession):
"""A test client with no authorization."""
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/routers/contacts/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from ctms.schemas import ContactInSchema, ContactSchema, NewsletterInSchema
from ctms.schemas.waitlist import WaitlistInSchema
from tests.unit.conftest import SAMPLE_CONTACT_PARAMS

API_TEST_CASES = (
("GET", "/ctms", {"primary_email": "[email protected]"}),
Expand Down Expand Up @@ -176,6 +175,17 @@ def find_default_fields(contact: ContactSchema) -> Set[str]:
return default_fields


SAMPLE_CONTACT_PARAMS = [
("minimal_contact_data", set()),
("maximal_contact_data", set()),
("example_contact_data", set()),
("to_add_contact_data", set()),
("simple_default_contact_data", {"amo"}),
("default_newsletter_contact_data", {"newsletters"}),
("default_waitlist_contact_data", {"waitlists"}),
]


@pytest.mark.parametrize("post_contact", SAMPLE_CONTACT_PARAMS, indirect=True)
def test_post_get_put(client, post_contact, put_contact, update_fetched):
"""This encompasses the entire expected flow for basket"""
Expand Down
167 changes: 1 addition & 166 deletions tests/unit/routers/contacts/test_api_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest


def test_get_ctms_for_minimal_contact(client, email_factory):
def test_get_ctms_for_mostly_empty(client, email_factory):
"""GET /ctms/{email_id} returns a contact with most fields unset."""
contact = email_factory(newsletters=1)
newsletter = contact.newsletters[0]
Expand Down Expand Up @@ -74,171 +74,6 @@ def test_get_ctms_for_minimal_contact(client, email_factory):
}


def test_get_ctms_for_maximal_contact(client, maximal_contact):
"""GET /ctms/{email_id} returns a contact with almost all fields set."""
email_id = maximal_contact.email.email_id
resp = client.get(f"/ctms/{email_id}")
assert resp.status_code == 200
assert resp.json() == {
"amo": {
"add_on_ids": "fanfox,foxfan",
"create_timestamp": "2017-05-12T15:16:00+00:00",
"display_name": "#1 Mozilla Fan",
"email_opt_in": True,
"language": "fr,en",
"last_login": "2020-01-27",
"location": "The Inter",
"profile_url": "firefox/user/14508209",
"update_timestamp": "2020-01-27T14:25:43+00:00",
"user": True,
"user_id": "123",
"username": "Mozilla1Fan",
},
"email": {
"basket_token": "d9ba6182-f5dd-4728-a477-2cc11bf62b69",
"create_timestamp": "2010-01-01T08:04:00+00:00",
"double_opt_in": True,
"email_format": "H",
"email_id": "67e52c77-950f-4f28-accb-bb3ea1a2c51a",
"email_lang": "fr",
"first_name": "Fan",
"has_opted_out_of_email": False,
"last_name": "of Mozilla",
"mailing_country": "ca",
"primary_email": "[email protected]",
"sfdc_id": "001A000001aMozFan",
"unsubscribe_reason": "done with this mailing list",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
"fxa": {
"created_date": "2019-05-22T08:29:31.906094+00:00",
"account_deleted": False,
"first_service": "monitor",
"fxa_id": "611b6788-2bba-42a6-98c9-9ce6eb9cbd34",
"lang": "fr,fr-CA",
"primary_email": "[email protected]",
},
"mofo": {
"mofo_contact_id": "5e499cc0-eeb5-4f0e-aae6-a101721874b8",
"mofo_email_id": "195207d2-63f2-4c9f-b149-80e9c408477a",
"mofo_relevant": True,
},
"newsletters": [
{
"format": "H",
"lang": "en",
"name": "ambassadors",
"source": "https://www.mozilla.org/en-US/contribute/studentambassadors/",
"subscribed": False,
"unsub_reason": "Graduated, don't have time for FSA",
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
{
"format": "T",
"lang": "fr",
"name": "common-voice",
"source": "https://commonvoice.mozilla.org/fr",
"subscribed": True,
"unsub_reason": None,
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
{
"format": "H",
"lang": "fr",
"name": "firefox-accounts-journey",
"source": "https://www.mozilla.org/fr/firefox/accounts/",
"subscribed": False,
"unsub_reason": "done with this mailing list",
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
{
"format": "H",
"lang": "en",
"name": "firefox-os",
"source": None,
"subscribed": True,
"unsub_reason": None,
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
{
"format": "H",
"lang": "fr",
"name": "hubs",
"source": None,
"subscribed": True,
"unsub_reason": None,
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
{
"format": "H",
"lang": "en",
"name": "mozilla-festival",
"source": None,
"subscribed": True,
"unsub_reason": None,
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
{
"format": "H",
"lang": "fr",
"name": "mozilla-foundation",
"source": None,
"subscribed": True,
"unsub_reason": None,
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
],
"status": "ok",
"vpn_waitlist": {"geo": "ca", "platform": "windows,android"},
"relay_waitlist": {"geo": "cn"},
"waitlists": [
{
"fields": {"geo": "fr"},
"name": "a-software",
"source": "https://a-software.mozilla.org/",
"subscribed": True,
"unsub_reason": None,
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
{
"fields": {"geo": "cn"},
"name": "relay",
"source": None,
"subscribed": True,
"unsub_reason": None,
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
{
"fields": {"geo": "fr", "platform": "win64"},
"name": "super-product",
"source": "https://super-product.mozilla.org/",
"subscribed": True,
"unsub_reason": None,
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
{
"fields": {"geo": "ca", "platform": "windows,android"},
"name": "vpn",
"source": None,
"subscribed": True,
"unsub_reason": None,
"create_timestamp": "2010-01-01T08:04:00+00:00",
"update_timestamp": "2020-01-28T14:50:00+00:00",
},
],
}


def test_get_ctms_for_api_example(client, example_contact):
"""The API examples represent a valid contact with many fields set.
Test that the API examples are valid."""
Expand Down
Loading

0 comments on commit 72b8224

Please sign in to comment.