Skip to content

Commit

Permalink
pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfleming committed Feb 9, 2023
1 parent 0cd50a0 commit 1d1094b
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"python.formatting.provider": "autopep8"
"python.formatting.provider": "autopep8",
"python.testing.pytestArgs": [
"chat"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
33 changes: 18 additions & 15 deletions chat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

utterance = models.ForeignKey(Utterance, on_delete=models.CASCADE)
response_text = models.CharField(max_length=8192)

def __str__(self):
return self.response_text
31 changes: 30 additions & 1 deletion chat/tests.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion chat/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('session/', views.session, name='session'),
Expand Down
2 changes: 1 addition & 1 deletion chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Binary file modified db.sqlite3
Binary file not shown.
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
DJANGO_SETTINGS_MODULE = Amy.settings
python_files = tests.py test_*.py *_tests.py

0 comments on commit 1d1094b

Please sign in to comment.