Skip to content

Commit

Permalink
add fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
prStudentka committed Mar 3, 2024
1 parent 05509d5 commit b2a5b86
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 84 deletions.
10 changes: 10 additions & 0 deletions task_manager/fixtures/statuses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"model": "statuses.Status",
"pk": 1,
"fields": {
"name": "New test status",
"created_at": "2024-03-02 00:00:00"
}
}
]
14 changes: 14 additions & 0 deletions task_manager/fixtures/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"model": "tasks.Task",
"pk": 1,
"fields": {
"author": 1,
"name": "Task test",
"description": "Just test task",
"executor": 1,
"status": 1,
"created_at": "2024-03-02 00:00:00"
}
}
]
13 changes: 13 additions & 0 deletions task_manager/fixtures/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"model": "users.CustomUser",
"pk": 1,
"fields": {
"first_name": "John",
"last_name": "London",
"username": "user_test1",
"password": "pass12345",
"created_at": "2024-03-02"
}
}
]
1 change: 1 addition & 0 deletions task_manager/labels/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def test_delete_label(self):
url = reverse_lazy('marked:delete label', kwargs={'pk': key})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, template_name='labels/delete.html')
response = self.client.delete(url)
with self.assertRaises(ObjectDoesNotExist):
Label.objects.get(pk=key)
Expand Down
20 changes: 0 additions & 20 deletions task_manager/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import os
import dj_database_url
# from urllib.parse import urlparse
from pathlib import Path
from dotenv import load_dotenv
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -99,25 +98,6 @@
conn_max_age=600),
}

'''
DB_URL = os.getenv('DATABASE_URL')
parsed_url = urlparse(DB_URL)
DATABASES = {
'default': {
"ENGINE": 'django.db.backends.postgresql',
'NAME': parsed_url.path[1:],
'USER': parsed_url.username,
'PASSWORD': parsed_url.password,
'HOST': parsed_url.hostname,
'PORT': parsed_url.port,
'CONN_MAX_AGE': 600,
},
'default_': {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}
'''

# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
Expand Down
6 changes: 4 additions & 2 deletions task_manager/statuses/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.urls import reverse_lazy
from task_manager.statuses.models import Status
from django.core.exceptions import ObjectDoesNotExist
from task_manager.statuses.views import StatusDeleteView


# Create your tests here.
Expand All @@ -14,7 +15,7 @@ def setUp(self):

def test_create_status(self):
data = {
'name': 'worked',
'name': 'new',
}
url = reverse_lazy('create status')
response = self.client.post(url, data, format='json')
Expand All @@ -24,7 +25,7 @@ def test_create_status(self):

def test_update_status(self):
update_data = {
'name': 'fix it'
'name': 'tested'
}
url = reverse_lazy('update status', args=[self.status_test.pk])
response = self.client.get(url)
Expand All @@ -41,6 +42,7 @@ def test_delete_status(self):
url = reverse_lazy('delete status', kwargs={'pk': key})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertIs(response.resolver_match.func.view_class, StatusDeleteView)
response = self.client.delete(url)
with self.assertRaises(ObjectDoesNotExist):
Status.objects.get(pk=key)
Expand Down
59 changes: 22 additions & 37 deletions task_manager/tasks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,58 @@
from task_manager.labels.models import Label
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from task_manager.tasks.filters import TaskFilter
from django.core.management import call_command


# Create your tests here.
class TaskCrudTest(TestCase):

fixtures = ['users.json', 'statuses.json', 'tasks.json']

def setUp(self):
self.test_author = CustomUser.objects.create_user(
first_name='Blue',
last_name='Joe',
username='user_test1',
password='12345'
)
call_command('loaddata', 'users.json', verbosity=2)
call_command('loaddata', 'statuses.json', verbosity=2)
call_command('loaddata', 'tasks.json', verbosity=2)
self.test_author = CustomUser.objects.get(pk=1)
self.status = Status.objects.get(pk=1)
self.executor = CustomUser.objects.create_user(
first_name='Pizza',
last_name='Delivery',
username='user_test',
password='i12345'
)
self.status = Status.objects.create(name='status_test')
self.label1 = Label.objects.create(name='label test one')
self.first_task = Task.objects.create(
name='Task first',
description='first description',
status=self.status,
executor=self.executor,
author=self.test_author
)
self.first_task = Task.objects.get(pk=1)
self.first_task.labels.add(self.label1)
self.login_data = {
'username': 'user_test1',
'password': '12345'
}

def test_create_task(self):
data = {
'name': 'Task test',
self.data = {
'name': 'Task was created for test',
'description': 'test description',
'status': self.status.pk,
'executor': self.executor.pk,
'author': self.test_author.pk
}

def test_create_task(self):
url = reverse_lazy('create task')
self.client.login(username=self.login_data['username'],
password=self.login_data['password'])
self.client.force_login(user=self.test_author)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
response = self.client.post(url, data, format='json')
response = self.client.post(url, self.data, format='json', follow=True)
url_back = reverse_lazy('tasks_index')
self.assertRedirects(response, url_back, status_code=302)
task_name = Task.objects.get(name=data['name'])
self.assertEqual(task_name.name, data['name'])
task_name = Task.objects.get(name=self.data['name'])
self.assertEqual(task_name.name, self.data['name'])

def test_update_task(self):
update_data = {
'name': 'Task update test',
'description': 'test description',
'status': self.status.pk,
'author': self.test_author.pk
}
self.client.login(username=self.login_data['username'],
password=self.login_data['password'])
old_name_task = self.first_task.name
self.client.force_login(user=self.test_author)
url = reverse_lazy('update task', args=[self.first_task.pk])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
response = self.client.post(url, data=update_data)
response = self.client.post(url, data=self.data)
self.first_task.refresh_from_db()
self.assertEqual(self.first_task.name, update_data['name'])
self.assertEqual(self.first_task.name, self.data['name'])
self.assertEqual(self.first_task.name != old_name_task, True)
url_back = reverse_lazy('tasks_index')
self.assertRedirects(response, url_back, status_code=302)

Expand Down
46 changes: 21 additions & 25 deletions task_manager/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@
from task_manager.users.forms import RegistrationForm
from django.core.exceptions import ObjectDoesNotExist
from task_manager.views import IndexView
from django.core.management import call_command


# Create your tests here.
class UserCrudTest(TestCase):

fixtures = ['users.json']

def setUp(self):
self.test_user = CustomUser.objects.create_user(
first_name='Blue',
last_name='Joe',
username='user_test1',
password='12345'
)
self.login_data = {
'username': 'user_test1',
'password': '12345'
}
call_command('loaddata', 'users.json', verbosity=2)
self.test_user = CustomUser.objects.get(pk=1)
self.data = {
'first_name': 'Petrov',
'last_name': 'Ivan',
Expand All @@ -37,7 +32,7 @@ def test_homepage(self):
def test_open_registration(self):
url = reverse_lazy('create')
response = self.client.get(url)
self.assertTemplateUsed(response, template_name='form.html')
self.assertTemplateUsed(response, template_name='users/create.html')
self.assertEqual(response.status_code, 200)

def test_form_valid(self):
Expand All @@ -52,28 +47,29 @@ def test_create_user(self):
self.assertEqual(user.first_name, self.data['first_name'])

def test_login_user(self):
new_password = '12345'
self.test_user.set_password(new_password)
self.test_user.save()
login_data = {
'username': self.test_user.username,
'password': new_password
}
url = reverse_lazy('log in')
self.client.login(username=self.login_data['username'],
password=self.login_data['password'])
response = self.client.post(url, self.login_data)
self.assertRedirects(response, reverse_lazy('index'), status_code=302)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
response = self.client.post(url, login_data, follow=True)
url_back = reverse_lazy('index')
self.assertRedirects(response, url_back, status_code=302)
self.assertTemplateUsed(response, template_name='index.html')

def test_update_user(self):

update_data = {
'first_name': 'Ivanov',
'last_name': 'Ivan',
'username': 'user_test',
'password1': 'abcd',
'password2': 'abcd'
}
self.client.force_login(user=self.test_user)
url = reverse_lazy('update', args=[self.test_user.pk])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
response = self.client.post(url, data=update_data)
response = self.client.post(url, data=self.data)
self.test_user.refresh_from_db()
self.assertEqual(self.test_user.first_name, update_data['first_name'])
self.assertEqual(self.test_user.first_name, self.data['first_name'])
url_back = reverse_lazy('users_index')
self.assertRedirects(response, url_back, status_code=302)

Expand Down

0 comments on commit b2a5b86

Please sign in to comment.