2018-11-27 10:01:36 +01:00
|
|
|
from srht.database import db
|
2019-01-01 15:15:19 +01:00
|
|
|
from tests.factories import TrackerFactory, TicketFactory, UserFactory
|
2019-08-21 08:35:44 +02:00
|
|
|
from tests.factories import ParticipantFactory
|
2018-11-27 10:01:36 +01:00
|
|
|
from tests.utils import logged_in_as
|
2019-01-01 15:15:19 +01:00
|
|
|
from todosrht.tickets import get_or_create_subscription
|
2021-12-18 19:00:24 +01:00
|
|
|
from todosrht.types import TicketSubscription
|
2018-11-27 10:01:36 +01:00
|
|
|
from todosrht.urls import ticket_url
|
|
|
|
|
2019-01-01 15:15:19 +01:00
|
|
|
def test_get_or_create_subscription():
|
2019-08-21 08:35:44 +02:00
|
|
|
participant1 = ParticipantFactory()
|
|
|
|
participant2 = ParticipantFactory()
|
|
|
|
participant3 = ParticipantFactory()
|
2019-01-01 15:15:19 +01:00
|
|
|
tracker = TrackerFactory()
|
|
|
|
ticket = TicketFactory(tracker=tracker)
|
|
|
|
|
|
|
|
# Some existing subscriptions
|
2019-08-21 08:35:44 +02:00
|
|
|
ts1 = TicketSubscription(participant=participant1, ticket=ticket)
|
|
|
|
ts2 = TicketSubscription(participant=participant2, tracker=tracker)
|
2019-01-01 15:15:19 +01:00
|
|
|
db.session.add(ts1)
|
|
|
|
db.session.add(ts2)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
assert set(ticket.subscriptions) == set([ts1])
|
|
|
|
assert set(tracker.subscriptions) == set([ts2])
|
|
|
|
|
|
|
|
# Return existing subs if they exist
|
2019-08-21 08:35:44 +02:00
|
|
|
assert get_or_create_subscription(ticket, participant1) == ts1
|
|
|
|
assert get_or_create_subscription(ticket, participant2) == ts2
|
2019-01-01 15:15:19 +01:00
|
|
|
|
|
|
|
# Create new ticket sub if none exists
|
2019-08-21 08:35:44 +02:00
|
|
|
ts3 = get_or_create_subscription(ticket, participant3)
|
2019-01-01 15:15:19 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
assert set(ticket.subscriptions) == set([ts1, ts3])
|
|
|
|
assert set(tracker.subscriptions) == set([ts2])
|