Skip to content

Commit db48c65

Browse files
Adding Conference tests. (Netflix#267)
* `yield_fixture` is deprecated after pytest 3.0. Plain old `fixture` is fine. * [WIP] Adding Conference model and service tests. * Use four spaces per indent in Python files. * Add assertions and stub functions. * Fix naming convention for conference tests. * Remove 'name', as it's not present in the Conference model. * Return a List of Conferences. When asking for all the Conferences of a given resource type, it doesn't make sense to ask for only one, or None (and throw an exception). It seems more sensible to request the whole list, or even a paginated list. Since this method isn't used anywhere in the code yet, we'll just return all(). * Black formatting. * Ensure our stub functions have args. * Test retrieval of a List of Conferences. * Add a test fixture to create multiple conferences. * Class Meta above instance vars * Incorporating suggested change * Lowercase as suggested * Implements test_conference_get_by_conference_id * get_by_incident_id fails * Implements test_conference_get_all * Ensure we're testing the created Conference. * Move ConferenceFactory later. This way an IncidentFactory has been defined and is available. * Give mocked Incident objects a fake ID. * This test works fine now.
1 parent cd962e4 commit db48c65

File tree

5 files changed

+122
-14
lines changed

5 files changed

+122
-14
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ indent_style=space
88
indent_size=2
99
trim_trailing_whitespace=true
1010

11+
[*.py]
12+
indent_size=4
13+
1114
[{*.ng,*.sht,*.html,*.shtm,*.shtml,*.htm}]
1215
indent_style=space
1316
indent_size=2

src/dispatch/conference/service.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, List
22

33
from .models import Conference, ConferenceCreate
44

@@ -11,10 +11,10 @@ def get_by_resource_id(*, db_session, resource_id: str) -> Optional[Conference]:
1111
return db_session.query(Conference).filter(Conference.resource_id == resource_id).one_or_none()
1212

1313

14-
def get_by_resource_type(*, db_session, resource_type: str) -> Optional[Conference]:
15-
return (
16-
db_session.query(Conference).filter(Conference.resource_type == resource_type).one_or_none()
17-
)
14+
def get_by_resource_type(*, db_session, resource_type: str) -> List[Optional[Conference]]:
15+
"""Return a list of all conferences matching a given resource type.
16+
May return an empty list if no conferences are available."""
17+
return db_session.query(Conference).filter(Conference.resource_type == resource_type).all()
1818

1919

2020
def get_by_conference_id(db_session, conference_id: str) -> Optional[Conference]:
@@ -24,11 +24,7 @@ def get_by_conference_id(db_session, conference_id: str) -> Optional[Conference]
2424

2525

2626
def get_by_incident_id(*, db_session, incident_id: str) -> Optional[Conference]:
27-
return (
28-
db_session.query(Conference)
29-
.filter(Conference.incident_id == incident_id)
30-
.one()
31-
)
27+
return db_session.query(Conference).filter(Conference.incident_id == incident_id).one()
3228

3329

3430
def get_all(*, db_session):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import pytest
2+
3+
4+
def test_conference_create(session):
5+
from dispatch.conference.service import create
6+
from dispatch.conference.models import ConferenceCreate
7+
8+
resource_id = "000000"
9+
resource_type = "resourcetype"
10+
weblink = "https://www.example.com"
11+
conference_id = "12345"
12+
conference_challenge = "a0v0a0v9a"
13+
14+
conference_in = ConferenceCreate(
15+
resource_id=resource_id,
16+
resource_type=resource_type,
17+
weblink=weblink,
18+
conference_id=conference_id,
19+
conference_challenge=conference_challenge,
20+
)
21+
conference = create(db_session=session, conference_in=conference_in)
22+
assert conference
23+
assert conference.resource_id == "000000"
24+
assert conference.resource_type == "resourcetype"
25+
assert conference.weblink == "https://www.example.com"
26+
assert conference.conference_id == "12345"
27+
assert conference.conference_challenge == "a0v0a0v9a"
28+
29+
30+
def test_conference_get(session, conference):
31+
from dispatch.conference.service import get
32+
33+
test_conference = get(db_session=session, conference_id=conference.id)
34+
assert test_conference.id == conference.id
35+
assert test_conference.conference_challenge == conference.conference_challenge
36+
37+
38+
def test_conference_get_by_resource_id(session, conference):
39+
from dispatch.conference.service import get_by_resource_id
40+
41+
test_conference = get_by_resource_id(db_session=session, resource_id=conference.resource_id)
42+
43+
assert test_conference.resource_id == conference.resource_id
44+
assert test_conference.conference_challenge == conference.conference_challenge
45+
46+
47+
def test_conference_get_by_resource_type(session, conference):
48+
"""The service method returns a list of conferences that match a given resource type. We'll test
49+
to ensure that the first and last items returned match the desired resource type."""
50+
from dispatch.conference.service import get_by_resource_type
51+
52+
conferences = get_by_resource_type(db_session=session, resource_type=conference.resource_type)
53+
54+
assert conferences[0].resource_type == conference.resource_type
55+
assert conferences[-1].resource_type == conference.resource_type
56+
57+
58+
def test_conference_get_by_conference_id(session, conference):
59+
from dispatch.conference.service import get_by_conference_id
60+
61+
test_conference = get_by_conference_id(session, conference.conference_id)
62+
63+
assert test_conference.conference_id == conference.conference_id
64+
assert test_conference.conference_challenge == conference.conference_challenge
65+
66+
67+
def test_conference_get_by_incident_id(session, conference):
68+
from dispatch.conference.service import get_by_incident_id
69+
70+
test_conference = get_by_incident_id(db_session=session, incident_id=conference.incident.id)
71+
72+
assert test_conference.incident.id == conference.incident.id
73+
assert test_conference.conference_challenge == conference.conference_challenge
74+
75+
76+
def test_conference_get_all(session, conferences):
77+
"""The test should not rely on the conferences created earlier, to pass.
78+
Therefore, we pass "conferences" as an argument, to manually create several in the DB.
79+
"""
80+
81+
from dispatch.conference.service import get_all
82+
83+
test_conferences = get_all(db_session=session).all()
84+
85+
assert len(test_conferences) > 1

tests/conftest.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from dispatch.database import Base, engine, SessionLocal
2929

3030
from .factories import (
31+
ConferenceFactory,
3132
ConversationFactory,
3233
DefinitionFactory,
3334
DocumentFactory,
@@ -70,7 +71,7 @@ def pytest_runtest_makereport(item, call):
7071
parent._previousfailed = item
7172

7273

73-
@pytest.yield_fixture(scope="session")
74+
@pytest.fixture(scope="session")
7475
def testapp():
7576
# we only want to use test plugins so unregister everybody else
7677
from dispatch.plugins.base import unregister, plugins
@@ -82,7 +83,7 @@ def testapp():
8283
yield app
8384

8485

85-
@pytest.yield_fixture(scope="session", autouse=True)
86+
@pytest.fixture(scope="session", autouse=True)
8687
def db():
8788
if database_exists(str(config.SQLALCHEMY_DATABASE_URI)):
8889
drop_database(str(config.SQLALCHEMY_DATABASE_URI))
@@ -94,7 +95,7 @@ def db():
9495
drop_database(str(config.SQLALCHEMY_DATABASE_URI))
9596

9697

97-
@pytest.yield_fixture(scope="function")
98+
@pytest.fixture(scope="function")
9899
def session(db):
99100
"""
100101
Creates a new database session with (with working transaction)
@@ -105,7 +106,7 @@ def session(db):
105106
db.rollback()
106107

107108

108-
@pytest.yield_fixture(scope="function")
109+
@pytest.fixture(scope="function")
109110
def client(testapp, session, client):
110111
yield TestClient(testapp)
111112

@@ -232,6 +233,16 @@ def Tag(session):
232233
return TagFactory()
233234

234235

236+
@pytest.fixture
237+
def conference(session):
238+
return ConferenceFactory()
239+
240+
241+
@pytest.fixture
242+
def conferences(session):
243+
return [ConferenceFactory(), ConferenceFactory(), ConferenceFactory()]
244+
245+
235246
@pytest.fixture
236247
def conversation(session):
237248
return ConversationFactory()

tests/factories.py

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from dispatch.database import SessionLocal
1111

1212
from dispatch.team.models import TeamContact
13+
from dispatch.conference.models import Conference
1314
from dispatch.conversation.models import Conversation
1415
from dispatch.definition.models import Definition
1516
from dispatch.document.models import Document
@@ -621,6 +622,7 @@ class Meta:
621622
class IncidentFactory(BaseFactory):
622623
"""Incident Factory."""
623624

625+
id = Sequence(lambda n: f'1{n}')
624626
title = FuzzyText()
625627
description = FuzzyText()
626628
status = FuzzyChoice(["Active", "Stable", "Closed"])
@@ -669,3 +671,14 @@ def individual_contact(self, create, extracted, **kwargs):
669671

670672
if extracted:
671673
self.individual_contact_id = extracted.id
674+
675+
676+
class ConferenceFactory(ResourceBaseFactory):
677+
"""Conference Factory."""
678+
679+
class Meta:
680+
model = Conference
681+
682+
conference_id = Sequence(lambda n: f"conference{n}")
683+
conference_challenge = FuzzyText()
684+
incident = SubFactory(IncidentFactory)

0 commit comments

Comments
 (0)