Skip to content

Commit

Permalink
Filling in some test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Jun 23, 2024
1 parent 53ad252 commit 7a92deb
Show file tree
Hide file tree
Showing 12 changed files with 708 additions and 80 deletions.
7 changes: 5 additions & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ omit=venv/*,\
booking/management/commands/update_categories.py,\
booking/management/commands/check_credits.py,\
booking/management/commands/reactivate_blocks.py,\
../*migrations*,../*tests*,../*wsgi*,../*__init__*,\
timetable/management/commands/create_pip_hire_sessions.py,manage.py
*/migrations/*,\
*/tests/*,\
pipsevents/wsgi.py,\
timetable/management/commands/create_pip_hire_sessions.py,\
manage.py
relative_files = True
3 changes: 3 additions & 0 deletions accounts/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from datetime import datetime, date
from datetime import timezone as dt_timezone

Expand Down Expand Up @@ -29,6 +31,7 @@
OVER_18_STATEMENT_AT_2020_05_01 = "I confirm that I am aged 18 or over"


@pytest.mark.skip(reason="Old migration tests")
class DisclaimerVersioningMingrationTests(MigrationTest):

before = [
Expand Down
37 changes: 35 additions & 2 deletions accounts/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,12 @@ def test_new_version_must_have_new_terms(self):
disclaimer_terms="foo", over_18_statement="bar", medical_treatment_terms="foobar",
version=None
)
with pytest.raises(ValidationError) as e:
with pytest.raises(ValidationError, match="No changes made to content; not saved") as e:
baker.make(
DisclaimerContent,
disclaimer_terms="foo", over_18_statement="bar", medical_treatment_terms="foobar",
version=None
)
assert str(e) == "No changes made to content; not saved"


class DisclaimerModelTests(TestCase):
Expand Down Expand Up @@ -199,6 +198,27 @@ def test_cannot_create_new_active_disclaimer(self):
with self.assertRaises(ValidationError):
baker.make(OnlineDisclaimer, user=user, version=disclaimer_content.version)

def test_expired_disclaimer(self):
user = baker.make_recipe('booking.user', username='testuser')
disclaimer_content = baker.make(
DisclaimerContent,
disclaimer_terms="foo", over_18_statement="bar", medical_treatment_terms="foobar",
version=None # ensure version is incremented from any existing ones
)
# disclaimer is out of date, so inactive
disclaimer = baker.make(
OnlineDisclaimer, user=user,
version=disclaimer_content.version
)

assert disclaimer.is_active

# manually mark as expired, even though stil in date
disclaimer.expired = True
disclaimer.save()

assert not disclaimer.is_active

def test_delete_online_disclaimer(self):
self.assertFalse(ArchivedDisclaimer.objects.exists())
disclaimer = baker.make(OnlineDisclaimer, name='Test 1')
Expand Down Expand Up @@ -243,6 +263,19 @@ def test_nonregistered_disclaimer_is_active(self):
)
self.assertFalse(old_disclaimer.is_active)

def test_nonregistered_disclaimer_expired(self):
disclaimer_content = baker.make(
DisclaimerContent, version=None # ensure version is incremented from any existing ones
)
disclaimer = baker.make(
NonRegisteredDisclaimer, first_name='Test', last_name='User', version=disclaimer_content.version
)
assert disclaimer.is_active

disclaimer.expired = True
disclaimer.save()
assert not disclaimer.is_active

def test_delete_nonregistered_disclaimer(self):
self.assertFalse(ArchivedDisclaimer.objects.exists())
disclaimer = baker.make(NonRegisteredDisclaimer, first_name='Test', last_name='User')
Expand Down
10 changes: 3 additions & 7 deletions booking/models/booking_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,7 @@ def can_cancel(self):
def has_available_block(self):
available_blocks = [
block for block in
Block.objects.select_related("user", "block_type").filter(
user=self.user, block_type__event_type=self.event.event_type
)
self.user.blocks.filter(block_type__event_type=self.event.event_type)
if block.active_block()
]
return bool(available_blocks)
Expand All @@ -773,10 +771,8 @@ def has_available_block(self):
def has_unpaid_block(self):
available_blocks = [
block for block in
Block.objects.select_related("user", "block_type").filter(
user=self.user, block_type__event_type=self.event.event_type
)
if not block.full and not block.expired and not block.paid
self.user.blocks.filter(block_type__event_type=self.event.event_type, paid=False)
if not block.full and not block.expired
]
return bool(available_blocks)

Expand Down
16 changes: 10 additions & 6 deletions booking/models/membership_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,21 +238,25 @@ def valid_for_event(self, event):

if self.end_date and self.end_date < event.date:
return False
# check quantities of classes already booked with this membership for this event type

# check quantities of classes already booked with this membership for this event type in the same month
allowed_numbers = membership_item.quantity
open_booking_count = self.bookings.filter(event__event_type=event.event_type, status="OPEN").count()
open_booking_count = self.bookings.filter(
event__event_type=event.event_type, event__date__month=event.date.month, event__date__year=event.date.year,
status="OPEN"
).count()
return open_booking_count < allowed_numbers

def hr_status(self):
return self.HR_STATUS.get(self.subscription_status, self.subscription_status.title())

def bookings_this_month(self):
return self.bookings.filter(status="OPEN", event__date__month=datetime.now().month)
return self.bookings.filter(status="OPEN", event__date__month=datetime.now().month, event__date__year=datetime.now().year)

def bookings_next_month(self):
next_month = (datetime.now().month - 12) % 12
return self.bookings.filter(status="OPEN", event__date__month=next_month)
next_month = (datetime.now().month + 1 - 12) % 12
year = datetime.now().year + 1 if next_month == 1 else datetime.now().year
return self.bookings.filter(status="OPEN", event__date__month=next_month, event__date__year=year)

@classmethod
def calculate_membership_end_date(cls, end_date=None):
Expand Down
14 changes: 14 additions & 0 deletions booking/tests/test_gift_voucher_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ def test_purchase_gift_voucher_block(self):

assert "paypal_form" in resp.context_data

def test_purchase_gift_voucher_invalid_email(self):
data = {
'voucher_type': self.block_voucher_type1.id,
'user_email': "[email protected]",
'user_email1': "[email protected]",
'recipient_name': 'Donald Duck',
'message': 'Quack'
}
resp = self.client.post(self.url, data)
assert resp.status_code == 200
assert resp.context_data["form"].errors == {
"user_email1": ["Email addresses do not match"]
}


class TestGiftVoucherUpdateView(GiftVoucherTestMixin, TestCase):

Expand Down
64 changes: 64 additions & 0 deletions booking/tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -3179,3 +3179,67 @@ def test_find_no_shows_defaults(self):
assert len(mail.outbox) == 2
assert f"{3}: {user.first_name} {user.last_name} - (id {user.id})" in mail.outbox[1].body
assert f"{user1.first_name} {user1.last_name} - (id {user1.id})" not in mail.outbox[1].body


import json
import pytest
@pytest.mark.django_db
def test_update_prices(tmp_path):
blocktype = baker.make_recipe("booking.blocktype5", size=3, identifier="standard", cost=20)
blocktype1 = baker.make_recipe("booking.blocktype5", size=3, identifier="not-standard", cost=20)
event_type = baker.make("booking.EventType", event_type="CL", subtype="Test class")
event_type1 = baker.make("booking.EventType", event_type="CL", subtype="Test class 1")
event = baker.make_recipe("booking.future_PC", event_type=event_type, cost=10)
event1 = baker.make_recipe("booking.future_PC", event_type=event_type1, cost=10)
tt_session = baker.make("timetable.Session", event_type=event_type, cost=10)
tt_session1 = baker.make("timetable.Session", event_type=event_type1, cost=10)

new_prices = {
"blocktype": [
{
"identifier": "standard",
"size": 3,
"price": 36.00
},
],
"event": [
{
"event_type": "CL",
"subtype": "Test class",
"price": 13.00
},
],
"session": [
{
"event_type": "CL",
"subtype": "Test class",
"price": 12.00
},
]
}
new_prices_filepath = tmp_path / "prices.json"
new_prices_filepath.write_text(json.dumps(new_prices))

# dry run
management.call_command("update_prices", new_prices_filepath)
for obj in [blocktype, blocktype1, event_type, event_type1, event, event1, tt_session, tt_session1]:
obj.refresh_from_db()
assert blocktype.cost == 20
assert blocktype1.cost == 20
assert event.cost == 10
assert event1.cost == 10
assert tt_session.cost == 10
assert tt_session1.cost == 10

# live run
management.call_command("update_prices", new_prices_filepath, live_run=True)
for obj in [blocktype, blocktype1, event_type, event_type1, event, event1, tt_session, tt_session1]:
obj.refresh_from_db()
assert blocktype.cost == 36
assert blocktype1.cost == 20
assert event.cost == 13
assert event1.cost == 10
assert tt_session.cost == 12
assert tt_session1.cost == 10


Loading

0 comments on commit 7a92deb

Please sign in to comment.