Skip to content

Commit e7f54b1

Browse files
committed
Added test for sponsership model
1 parent cd28fee commit e7f54b1

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

backend/tests/nest/model/__init__.py

Whitespace-only changes.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
from unittest.mock import Mock, patch
2+
3+
import pytest
4+
from django.utils import timezone
5+
6+
from apps.github.models.issue import Issue
7+
from apps.nest.models.sponsorship import Sponsorship
8+
9+
PRICE_USD = 100.0
10+
11+
12+
class TestSponsorshipModel:
13+
@pytest.mark.parametrize(
14+
("price_usd", "deadline_at", "slack_user_id"),
15+
[
16+
(100.0, timezone.now(), "U12345"),
17+
(200.0, None, "U67890"),
18+
],
19+
)
20+
@patch.object(Sponsorship, "save")
21+
def test_update_data(self, mock_save, price_usd, deadline_at, slack_user_id):
22+
"""Test the update_data method of the Sponsorship model."""
23+
sponsorship = Sponsorship()
24+
25+
updated_sponsorship = Sponsorship.update_data(
26+
sponsorship,
27+
price_usd=price_usd,
28+
deadline_at=deadline_at,
29+
slack_user_id=slack_user_id,
30+
)
31+
32+
assert updated_sponsorship.price_usd == price_usd
33+
assert updated_sponsorship.deadline_at == deadline_at
34+
assert updated_sponsorship.slack_user_id == slack_user_id
35+
mock_save.assert_called_once()
36+
37+
@patch.object(Sponsorship, "save")
38+
def test_update_data_save_called(self, mock_save):
39+
"""Test that the save method is called when updating sponsorship data."""
40+
sponsorship = Sponsorship()
41+
42+
Sponsorship.update_data(
43+
sponsorship,
44+
price_usd=PRICE_USD,
45+
deadline_at=timezone.now(),
46+
slack_user_id="U12345",
47+
)
48+
49+
mock_save.assert_called_once()
50+
51+
@pytest.mark.parametrize(
52+
("initial_price", "updated_price", "initial_deadline", "updated_deadline"),
53+
[
54+
(50.0, 100.0, timezone.now(), timezone.now()),
55+
(200.0, 150.0, None, timezone.now()),
56+
],
57+
)
58+
@patch.object(Sponsorship, "save")
59+
def test_update_data_partial_updates(
60+
self, mock_save, initial_price, updated_price, initial_deadline, updated_deadline
61+
):
62+
"""Test partial updates using the update_data method."""
63+
sponsorship = Sponsorship(price_usd=initial_price, deadline_at=initial_deadline)
64+
65+
Sponsorship.update_data(sponsorship, price_usd=updated_price)
66+
assert sponsorship.price_usd == updated_price
67+
assert sponsorship.deadline_at == initial_deadline
68+
mock_save.assert_called_once()
69+
70+
mock_save.reset_mock()
71+
72+
# Update only the deadline
73+
Sponsorship.update_data(sponsorship, deadline_at=updated_deadline)
74+
assert sponsorship.price_usd == updated_price
75+
assert sponsorship.deadline_at == updated_deadline
76+
mock_save.assert_called_once()
77+
78+
@patch("apps.nest.models.sponsorship.Sponsorship.objects.create")
79+
def test_sponsorship_creation(self, mock_create):
80+
"""Test creating a Sponsorship instance in the database."""
81+
issue = Mock(spec=Issue, title="Test Issue", url="https://github.com/OWASP/Nest/issues/1")
82+
issue._state = Mock()
83+
84+
mock_create.return_value = Sponsorship(
85+
issue=issue,
86+
price_usd=PRICE_USD,
87+
slack_user_id="U12345",
88+
)
89+
90+
sponsorship = Sponsorship.objects.create(
91+
issue=issue,
92+
price_usd=100.0,
93+
slack_user_id="U12345",
94+
)
95+
96+
assert sponsorship.issue == issue
97+
assert sponsorship.price_usd == PRICE_USD
98+
assert sponsorship.slack_user_id == "U12345"
99+
assert sponsorship.deadline_at is None
100+
mock_create.assert_called_once_with(
101+
issue=issue,
102+
price_usd=100.0,
103+
slack_user_id="U12345",
104+
)
105+
106+
@patch("apps.nest.models.sponsorship.Sponsorship.objects.create")
107+
def test_sponsorship_with_deadline(self, mock_create):
108+
"""Test creating a Sponsorship instance with a deadline."""
109+
# Mock the Issue instance with _state attribute
110+
issue = Mock(spec=Issue, title="Test Issue", url="https://github.com/OWASP/Nest/issues/1")
111+
issue._state = Mock()
112+
113+
deadline = timezone.now()
114+
115+
# Mock the return value of Sponsorship.objects.create
116+
mock_create.return_value = Sponsorship(
117+
issue=issue,
118+
price_usd=100.0,
119+
slack_user_id="U12345",
120+
deadline_at=deadline,
121+
)
122+
123+
sponsorship = Sponsorship.objects.create(
124+
issue=issue,
125+
price_usd=100.0,
126+
slack_user_id="U12345",
127+
deadline_at=deadline,
128+
)
129+
130+
assert sponsorship.deadline_at == deadline
131+
mock_create.assert_called_once_with(
132+
issue=issue,
133+
price_usd=100.0,
134+
slack_user_id="U12345",
135+
deadline_at=deadline,
136+
)

0 commit comments

Comments
 (0)