diff --git a/.vscode/launch.json b/.vscode/launch.json index bf1e98d..7651209 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,13 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Debug Single Test", + "type": "python", + "request": "launch", + "program": "C:\\ProgramData\\Anaconda3\\envs\\django\\Scripts\\pytest.exe", + "django": true + }, { "name": "Python: Django", "type": "python", diff --git a/.vscode/settings.json b/.vscode/settings.json index 09395a1..63d992a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,8 @@ { - "python.formatting.provider": "autopep8" + "python.formatting.provider": "autopep8", + "python.testing.pytestArgs": [ + "chat" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true } \ No newline at end of file diff --git a/chat/models.py b/chat/models.py index 33d7339..7c37bfc 100644 --- a/chat/models.py +++ b/chat/models.py @@ -4,20 +4,23 @@ # Create your models here. + class Utterance(models.Model): - utterance_text = models.CharField(max_length=8192) - time = models.DateTimeField('Time') - - def __str__(self): - return self.utterance_text - - def isRecent(self): - return self.time >= timezone.now() - datetime.timedelta(days=1) - + utterance_text = models.CharField(max_length=8192) + time = models.DateTimeField('Time') + + def __str__(self): + return self.utterance_text + + def is_recent(self): + now = timezone.now() + recent = now - datetime.timedelta(days=1) <= self.time <= now + return recent + + class Response(models.Model): - utterance = models.ForeignKey(Utterance, on_delete=models.CASCADE) - response_text = models.CharField(max_length=8192) - - def __str__(self): - return self.response_text - \ No newline at end of file + utterance = models.ForeignKey(Utterance, on_delete=models.CASCADE) + response_text = models.CharField(max_length=8192) + + def __str__(self): + return self.response_text diff --git a/chat/tests.py b/chat/tests.py index de8bdc0..7a34e49 100644 --- a/chat/tests.py +++ b/chat/tests.py @@ -1,3 +1,32 @@ from django.test import TestCase +from django.utils import timezone +from django.urls import reverse +import datetime -# Create your tests here. +from .models import Utterance + + +class UtteranceModelTests(TestCase): + def test_is_recent_with_future_time(self): + """ + is_recent() returns False for future utterances + """ + future = timezone.now() + datetime.timedelta(days=30) + future_utterance = Utterance(time=future, utterance_text='test') + self.assertIs(future_utterance.is_recent(), False) + + def test_is_recent_with_old_utterance(self): + """ + is_recent() returns False for utterances whose time is older than 1 day + """ + day_old = timezone.now() - datetime.timedelta(days=1, seconds=1) + old_utterance = Utterance(time=day_old, utterance_text='test') + self.assertIs(old_utterance.is_recent(), False) + + def test_is_recent_with_recent_utterance(self): + """ + is_recent() returns True for utterances whose time is within the last day + """ + recent = timezone.now() - datetime.timedelta(hours=23, minutes=59) + recent_utterance = Utterance(time=recent, utterance_text='test') + self.assertIs(recent_utterance.is_recent(), True) diff --git a/chat/urls.py b/chat/urls.py index 5576e84..91b3eab 100644 --- a/chat/urls.py +++ b/chat/urls.py @@ -4,7 +4,8 @@ app_name = 'chat' urlpatterns = [ - path('', views.IndexView.as_view(), name='index'), + path('', views.session, name='session'), + path('list', views.IndexView.as_view(), name='list'), path('/', views.DetailView.as_view(), name='detail'), path('/results/', views.ResultsView.as_view(), name='results'), path('session/', views.session, name='session'), diff --git a/chat/views.py b/chat/views.py index 56d033b..8cb20a9 100644 --- a/chat/views.py +++ b/chat/views.py @@ -12,7 +12,7 @@ class IndexView(generic.ListView): context_object_name = 'recent_utterances' def get_queryset(self): - return Utterance.objects.order_by('-time')[:5] + return Utterance.objects.filter(time__lte=timezone.now()).order_by('-time')[:5] class DetailView(generic.DetailView): diff --git a/db.sqlite3 b/db.sqlite3 index ffb15d9..53ba1ed 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..be93e6c --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +DJANGO_SETTINGS_MODULE = Amy.settings +python_files = tests.py test_*.py *_tests.py \ No newline at end of file