Skip to content

Commit

Permalink
fixing poll tests & poll_suggestion
Browse files Browse the repository at this point in the history
Signed-off-by: rimonsh <[email protected]>
  • Loading branch information
rimonsh committed Jun 1, 2023
1 parent 6fa0096 commit 98c7574
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions poll/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_is_active_false(self, poll1):
assert not poll_from_db.is_active()

def test_show_suggestions(self, poll1):
times = [(datetime.now() + timedelta(minutes=10 * i)).time() for i in range(3)]
times = [(datetime.now() + timedelta(minutes=10 * i)) for i in range(1, 4)]
for time in times:
poll_suggestion = PollSuggestion(poll_id=poll1, time=time)
poll_suggestion.save()
Expand All @@ -122,8 +122,10 @@ def test_show_suggestions(self, poll1):
assert suggestions.count() == 3

suggestion_times = [suggestion.time for suggestion in suggestions]
datetime_objects = [dt.replace(tzinfo=None) for dt in suggestion_times]

for time in times:
assert time in suggestion_times
assert time in datetime_objects

def test_time_remaining_positive(self, poll1):
poll_from_db = Poll.objects.get(id=poll1.id)
Expand Down
5 changes: 3 additions & 2 deletions poll_suggestion/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from poll.models import Poll
from django.db.utils import IntegrityError
from users.models import Profile
from django.utils import timezone
from datetime import datetime


class PollSuggestion(models.Model):
Expand All @@ -12,9 +12,10 @@ class PollSuggestion(models.Model):
def save(self, *args, **kwargs):
all_suggestions = PollSuggestion.objects.all()
suggested_times = [poll_suggestion.time for poll_suggestion in all_suggestions]
current_time = datetime.now()
if self.time in suggested_times:
raise IntegrityError
if self.time < timezone.now():
if self.time < current_time:
raise IntegrityError
super().save(*args, **kwargs)

Expand Down

0 comments on commit 98c7574

Please sign in to comment.