From 62ac56befb2baf905ca05977ed7aeb5093e260f5 Mon Sep 17 00:00:00 2001 From: Eero af Heurlin Date: Fri, 22 Feb 2019 11:05:05 +0200 Subject: [PATCH 1/3] Make sure conditional_add works too --- project/creditor/models.py | 1 + project/creditor/tests/test_recurring.py | 53 +++++++++++++++++------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/project/creditor/models.py b/project/creditor/models.py index 7315ed5c..6d81f95f 100644 --- a/project/creditor/models.py +++ b/project/creditor/models.py @@ -125,6 +125,7 @@ def in_timescope(self, timescope=None): scope_start_ts, scope_end_ts = self.resolve_timescope(timescope) scope_start = scope_start_ts.date() scope_end = scope_end_ts.date() + return (self.start <= scope_end and (not self.end or self.end >= scope_end)) diff --git a/project/creditor/tests/test_recurring.py b/project/creditor/tests/test_recurring.py index c0fabe9b..36341d66 100644 --- a/project/creditor/tests/test_recurring.py +++ b/project/creditor/tests/test_recurring.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import calendar import datetime +from django.utils import timezone import pytest import pytz @@ -10,10 +11,11 @@ def year_start_end(timescope=None): if timescope is None: timescope = datetime.datetime.now() - start = start = datetime.datetime(timescope.year, 1, 1).date() + start = datetime.datetime(timescope.year, 1, 1).date() end = datetime.datetime(start.year, 12, calendar.monthrange(start.year, 12)[1]).date() return (start, end) + def quarter_start_end(timescope=None): if timescope is None: timescope = datetime.datetime.now() @@ -29,9 +31,10 @@ def quarter_start_end(timescope=None): end = datetime.datetime(start.year, end_month, calendar.monthrange(start.year, end_month)[1]).date() return (start, end) + def month_start_end(timescope=None): if timescope is None: - timescope = datetime.datetime.now() + timescope = timezone.now() start = datetime.datetime(timescope.year, timescope.month, 1).date() end = datetime.datetime(start.year, start.month, calendar.monthrange(start.year, start.month)[1]).date() return (start, end) @@ -39,47 +42,63 @@ def month_start_end(timescope=None): @pytest.mark.django_db def test_yearly_in_scope_with_end(): - now = datetime.datetime.now() + now = timezone.now() start, end = year_start_end(now) t = MembershipfeeFactory(start=start, end=end) assert t.in_timescope(now) + ret1 = t.conditional_add_transaction(now) + assert ret1 + ret2 = t.conditional_add_transaction(now) + assert not ret2 @pytest.mark.django_db def test_yearly_in_scope_without_end(): - now = datetime.datetime.now() + now = timezone.now() start, end = year_start_end(now - datetime.timedelta(weeks=60)) end = None t = MembershipfeeFactory(start=start, end=end) assert t.in_timescope(now) + ret1 = t.conditional_add_transaction(now) + assert ret1 + ret2 = t.conditional_add_transaction(now) + assert not ret2 @pytest.mark.django_db def test_yearly_not_in_scope_ended(): - now = datetime.datetime.now() + now = timezone.now() start, end = year_start_end(now - datetime.timedelta(weeks=60)) t = MembershipfeeFactory(start=start, end=end) assert not t.in_timescope(now) + ret2 = t.conditional_add_transaction(now) + assert not ret2 @pytest.mark.django_db def test_yearly_not_in_scope_notstarted(): - now = datetime.datetime.now() + now = timezone.now() start, end = year_start_end(now + datetime.timedelta(weeks=60)) t = MembershipfeeFactory(start=start, end=end) assert not t.in_timescope(now) + ret2 = t.conditional_add_transaction(now) + assert not ret2 + @pytest.mark.django_db def test_quarterly_in_scope_with_end(): - now = datetime.datetime.now() + now = timezone.now() + start, end = quarter_start_end(now) end += datetime.timedelta(weeks=15) t = QuarterlyFactory(start=start, end=end) assert t.in_timescope(now) + @pytest.mark.django_db def test_quarterly_in_scope_without_end(): - now = datetime.datetime.now() + now = timezone.now() + start, end = month_start_end(now - datetime.timedelta(weeks=15)) end = None t = QuarterlyFactory(start=start, end=end) @@ -87,21 +106,24 @@ def test_quarterly_in_scope_without_end(): @pytest.mark.django_db def test_quarterly_not_in_scope_ended(): - now = datetime.datetime.now() + now = timezone.now() + start, end = month_start_end(now - datetime.timedelta(weeks=25)) t = QuarterlyFactory(start=start, end=end) assert not t.in_timescope(now) @pytest.mark.django_db def test_quarterly_not_in_scope_notstarted(): - now = datetime.datetime.now() + now = timezone.now() + start, end = month_start_end(now + datetime.timedelta(weeks=25)) t = QuarterlyFactory(start=start, end=end) assert not t.in_timescope(now) @pytest.mark.django_db def test_monthly_in_scope_with_end(): - now = datetime.datetime.now() + now = timezone.now() + start, end = month_start_end(now) end += datetime.timedelta(weeks=6) t = KeyholderfeeFactory(start=start, end=end) @@ -110,7 +132,8 @@ def test_monthly_in_scope_with_end(): @pytest.mark.django_db def test_monthly_in_scope_without_end(): - now = datetime.datetime.now() + now = timezone.now() + start, end = month_start_end(now - datetime.timedelta(weeks=6)) end = None t = KeyholderfeeFactory(start=start, end=end) @@ -119,7 +142,8 @@ def test_monthly_in_scope_without_end(): @pytest.mark.django_db def test_monthly_not_in_scope_ended(): - now = datetime.datetime.now() + now = timezone.now() + start, end = month_start_end(now - datetime.timedelta(weeks=10)) t = KeyholderfeeFactory(start=start, end=end) assert not t.in_timescope(now) @@ -127,7 +151,8 @@ def test_monthly_not_in_scope_ended(): @pytest.mark.django_db def test_monthly_not_in_scope_notstarted(): - now = datetime.datetime.now() + now = timezone.now() + start, end = month_start_end(now + datetime.timedelta(weeks=10)) t = KeyholderfeeFactory(start=start, end=end) assert not t.in_timescope(now) From 2a04444c5f3e0993a56a6923639be4f2c0cf04e6 Mon Sep 17 00:00:00 2001 From: Eero af Heurlin Date: Fri, 22 Feb 2019 18:44:44 +0200 Subject: [PATCH 2/3] Improve transaction_exists and timescope checks --- project/creditor/models.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/project/creditor/models.py b/project/creditor/models.py index 6d81f95f..e23cf429 100644 --- a/project/creditor/models.py +++ b/project/creditor/models.py @@ -128,15 +128,19 @@ def in_timescope(self, timescope=None): return (self.start <= scope_end and (not self.end - or self.end >= scope_end)) + or self.end >= scope_start)) @transaction.atomic() def transaction_exists(self, timescope=None): start, end = self.resolve_timescope(timescope) uid_source = self.make_uid_source(timescope) uid = hashlib.sha1(uid_source.encode('UTF-8')).hexdigest() + # Check for uid match + if Transaction.objects.filter(unique_id=uid).count(): + return True + # check for time based match qs = Transaction.objects.filter( - owner=self.owner, tag=self.tag, unique_id=uid, stamp__gte=start, stamp__lte=end + owner=self.owner, tag=self.tag, stamp__gte=start, stamp__lte=end ) if qs.count(): return True From f79a57e38c0d464500da1099c2c3d7d28078cc50 Mon Sep 17 00:00:00 2001 From: Eero af Heurlin Date: Fri, 22 Feb 2019 18:44:21 +0200 Subject: [PATCH 3/3] Test multiple creations --- project/creditor/tests/test_recurring.py | 26 +++++++++++++----------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/project/creditor/tests/test_recurring.py b/project/creditor/tests/test_recurring.py index 36341d66..93ed0ba8 100644 --- a/project/creditor/tests/test_recurring.py +++ b/project/creditor/tests/test_recurring.py @@ -44,12 +44,13 @@ def month_start_end(timescope=None): def test_yearly_in_scope_with_end(): now = timezone.now() start, end = year_start_end(now) - t = MembershipfeeFactory(start=start, end=end) - assert t.in_timescope(now) - ret1 = t.conditional_add_transaction(now) - assert ret1 - ret2 = t.conditional_add_transaction(now) - assert not ret2 + for x in range(5): + t = MembershipfeeFactory(start=start, end=end) + assert t.in_timescope(now) + ret1 = t.conditional_add_transaction(now) + assert ret1 + ret2 = t.conditional_add_transaction(now) + assert not ret2 @pytest.mark.django_db @@ -57,12 +58,13 @@ def test_yearly_in_scope_without_end(): now = timezone.now() start, end = year_start_end(now - datetime.timedelta(weeks=60)) end = None - t = MembershipfeeFactory(start=start, end=end) - assert t.in_timescope(now) - ret1 = t.conditional_add_transaction(now) - assert ret1 - ret2 = t.conditional_add_transaction(now) - assert not ret2 + for x in range(5): + t = MembershipfeeFactory(start=start, end=end) + assert t.in_timescope(now) + ret1 = t.conditional_add_transaction(now) + assert ret1 + ret2 = t.conditional_add_transaction(now) + assert not ret2 @pytest.mark.django_db