Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding tests and logic #104

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion poll_suggestion/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Migration(migrations.Migration):
name='PollSuggestion',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('time', models.TimeField()),
('time', models.DateTimeField()),
('poll_id', models.ForeignKey(to='poll.Poll', on_delete=models.CASCADE)),
],
),
Expand Down
6 changes: 5 additions & 1 deletion poll_suggestion/models.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe consider to add a field that tracks the number of votes each suggestion receives. This addition can provide valuable data for evaluating the popularity for each suggestion. Or you thought of alternative way for determining the chosen suggestion?

Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
from poll.models import Poll
from django.db.utils import IntegrityError
from users.models import Profile
from datetime import datetime


class PollSuggestion(models.Model):
time = models.TimeField()
time = models.DateTimeField()
poll_id = models.ForeignKey(Poll, on_delete=models.CASCADE)

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 < current_time:
raise IntegrityError
super().save(*args, **kwargs)


Expand Down
79 changes: 79 additions & 0 deletions poll_suggestion/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,82 @@
# from django.test import TestCase

# Create your tests here.
# from django.test import TestCase

# Create your tests here.
from poll.models import Poll, get_default_end_date
from .models import PollSuggestion
from datetime import datetime, timedelta
from django.utils import timezone
from event.models import Event
from category.models import Category
from location.models import Location
from category_location.models import CategoryLocation
import pytest

EVENT_NAME = "weight lifting"
CATEGORY = "Gym1"
LOCATION = "Holmes place Netanya"
MAX_PART = 10
IS_PRIVATE = False
DATETIME = datetime.now()
MAX_SUGEGSTIONS = 5
DATETIME = timezone.now()


@pytest.fixture
def setup_category_location():
loc = Location(
name=LOCATION,
city="test city",
street="test street",
street_number=1,
indoor=False,
description="test description",
)
loc.save()
cat = Category(name=CATEGORY)
cat.save()
cat_loc = CategoryLocation(location=loc, category=cat)
cat_loc.save()
return cat_loc


@pytest.fixture
def setup_event(setup_category_location):
event = Event(
category=setup_category_location.category,
location=setup_category_location.location,
poll=None,
name=EVENT_NAME,
max_participants=MAX_PART,
start_time=DATETIME + timedelta(days=2),
end_time=DATETIME + timedelta(days=3),
is_private=IS_PRIVATE,
)
event.save()
return event


@pytest.fixture
def setup_poll(setup_event):
poll = Poll(event_id=setup_event, max_suggestions=MAX_SUGEGSTIONS, end_time=get_default_end_date())
poll.save()
return poll


@pytest.fixture
def set_up_poll_suggestion(setup_poll):
poll_s = PollSuggestion.objects.create(time=datetime.now() + timedelta(days=3), poll_id=setup_poll)
rimonsh marked this conversation as resolved.
Show resolved Hide resolved
poll_s.save()
return poll_s


@pytest.mark.django_db
class TestPollSuggestionModel:

def test_create_poll_suggestion(self, set_up_poll_suggestion):
poll_suggestion1 = PollSuggestion.objects.get(id=set_up_poll_suggestion.id)
poll = poll_suggestion1.poll_id
poll_s_from_db = poll.show_suggestions()
return poll_s_from_db == poll_suggestion1