Commit 88635a06 authored by Ola Hermann Opheim's avatar Ola Hermann Opheim
Browse files

refac projects views

parent 948c66ab
import unittest
from django.test import Client
from test_helpers import create_projects_sorting_data, delete_test_data
class SortingIntegrationTest(unittest.TestCase):
def setUp(self):
self.client = Client()
create_projects_sorting_data()
self.request_data = {
'sort_form': '',
'sort_by': 't',
}
def tearDown(self):
delete_test_data()
def test_sort_by_title_asc(self):
response = self.client.post('/projects/', self.request_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['projects'][0].title, "A Project")
self.assertEqual(response.context['projects'][1].title, "B Project")
self.assertEqual(response.context['projects'][2].title, "C Project")
def test_sort_by_title_desc(self):
self.request_data['sort_by'] = '!t'
response = self.client.post('/projects/', self.request_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['projects'][0].title, "C Project")
self.assertEqual(response.context['projects'][1].title, "B Project")
self.assertEqual(response.context['projects'][2].title, "A Project")
def test_sort_by_owner_asc(self):
self.request_data['sort_by'] = 'o'
response = self.client.post('/projects/', self.request_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(str(response.context['projects'][0].user), "A User")
self.assertEqual(str(response.context['projects'][1].user), "B User")
self.assertEqual(str(response.context['projects'][2].user), "C User")
def test_sort_by_owner_desc(self):
self.request_data['sort_by'] = '!o'
response = self.client.post('/projects/', self.request_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(str(response.context['projects'][0].user), "C User")
self.assertEqual(str(response.context['projects'][1].user), "B User")
self.assertEqual(str(response.context['projects'][2].user), "A User")
\ No newline at end of file
......@@ -439,124 +439,6 @@ class AcceptingOfferOutputTest(unittest.TestCase):
# Task 3 - IV
class SortingTest(unittest.TestCase):
def setUp(self):
self.client = Client()
self.sorting_category = ProjectCategory.objects.create(name='sorting_category')
self.user_a = User.objects.create_user(
username='aUser',
email='test@test.com',
password='top_secret'
)
self.user_b = User.objects.create_user(
username='bUser',
email='test@test.com',
password='top_secret'
)
self.user_c = User.objects.create_user(
username='cUser',
email='test@test.com',
password='top_secret'
)
Project.objects.all().delete()
self.project_a = Project.objects.create(
user=self.user_c.profile,
title='A Project',
description='',
category=self.sorting_category,
status='o'
)
self.project_c = Project.objects.create(
user=self.user_b.profile,
title='C Project',
description='',
category=self.sorting_category,
status='o'
)
self.project_b = Project.objects.create(
user=self.user_a.profile,
title='B Project',
description='',
category=self.sorting_category,
status='o'
)
self.project_a_task = Task.objects.create(
project=self.project_a,
title='Test task 1',
description='',
budget=20,
status='ad',
feedback=''
)
self.project_b_task = Task.objects.create(
project=self.project_b,
title='Test task 1',
description='',
budget=20,
status='ad',
feedback=''
)
self.project_c_task = Task.objects.create(
project=self.project_c,
title='Test task 1',
description='',
budget=20,
status='ad',
feedback=''
)
def test_error_free_sorting(self):
# Test sorting by title ASC
response = self.client.post('/projects/', {
'sort_form': '',
'sort_by': 't',
})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['projects'][0], self.project_a)
self.assertEqual(response.context['projects'][1], self.project_b)
self.assertEqual(response.context['projects'][2], self.project_c)
# Test sorting by title DESC
response = self.client.post('/projects/', {
'sort_form': '',
'sort_by': '!t',
})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['projects'][0], self.project_c)
self.assertEqual(response.context['projects'][1], self.project_b)
self.assertEqual(response.context['projects'][2], self.project_a)
# Test sorting by owner username ASC
response = self.client.post('/projects/', {
'sort_form': '',
'sort_by': 'o',
})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['projects'][0], self.project_b)
self.assertEqual(response.context['projects'][1], self.project_c)
self.assertEqual(response.context['projects'][2], self.project_a)
# Test sorting by owner username DESC
response = self.client.post('/projects/', {
'sort_form': '',
'sort_by': '!o',
})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['projects'][0], self.project_a)
self.assertEqual(response.context['projects'][1], self.project_c)
self.assertEqual(response.context['projects'][2], self.project_b)
class EditTaskOfferTest(unittest.TestCase):
def setUp(self):
self.client = Client()
......
def get_username(elem):
return elem.user.user.username.lower()
def get_title(elem):
return elem.title.lower()
def get_timestamp(elem):
return elem.timestamp
def sort_projects(projects, sort_by):
if sort_by == 't':
projects = sorted(projects, key=get_title)
elif sort_by == '!t':
projects = sorted(projects, key=get_title, reverse=True)
elif sort_by == 'o':
projects = sorted(projects, key=get_username)
elif sort_by == '!o':
projects = sorted(projects, key=get_username, reverse=True)
elif sort_by == 'd':
projects = sorted(projects, key=get_timestamp)
elif sort_by == '!d':
projects = sorted(projects, key=get_timestamp, reverse=True)
return projects
......@@ -6,16 +6,9 @@ from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
def projects(request):
def get_username(elem):
return elem.user.user.username.lower()
from .utils import sort_projects
def get_title(elem):
return elem.title.lower()
def get_timestamp(elem):
return elem.timestamp
def projects(request):
projects = Project.objects.all()
project_categories = ProjectCategory.objects.all()
......@@ -24,19 +17,8 @@ def projects(request):
if request.method == 'POST' and 'sort_form' in request.POST:
sort_form = SortForm(request.POST)
if sort_form.is_valid():
sort_by = sort_form.save(commit=False)
if request.POST.getlist('sort_by')[0] == 't':
projects = sorted(projects, key=get_title)
elif request.POST.getlist('sort_by')[0] == '!t':
projects = sorted(projects, key=get_title, reverse=True)
elif request.POST.getlist('sort_by')[0] == 'o':
projects = sorted(projects, key=get_username)
elif request.POST.getlist('sort_by')[0] == '!o':
projects = sorted(projects, key=get_username, reverse=True)
elif request.POST.getlist('sort_by')[0] == 'd':
projects = sorted(projects, key=get_timestamp)
elif request.POST.getlist('sort_by')[0] == '!d':
projects = sorted(projects, key=get_timestamp, reverse=True)
sort_by = sort_form.save(commit=False)
projects = sort_projects(projects, request.POST.get('sort_by'))
return render(request,
'projects/projects.html',
......
......@@ -30,6 +30,48 @@ def create_task_status_data():
feedback=''
)
def create_projects_sorting_data():
#create_multiple_test_data()
test_user_a = User.objects.create_user(
username='A User',
email='test@test.com',
password='top_secret'
)
test_user_b = User.objects.create_user(
username='B User',
email='test@test.com',
password='top_secret'
)
test_user_c = User.objects.create_user(
username='C User',
email='test@test.com',
password='top_secret'
)
test_category = ProjectCategory.objects.create(name='Category')
test_project_a = Project.objects.create(
user=test_user_c.profile,
title='A Project',
description='',
category=test_category,
status='o'
)
test_project_b = Project.objects.create(
user=test_user_a.profile,
title='B Project',
description='',
category=test_category,
status='o'
)
test_project_c = Project.objects.create(
user=test_user_b.profile,
title='C Project',
description='',
category=test_category,
status='o'
)
def create_multiple_test_data():
test_user_1 = User.objects.create_user(
username='User1',
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment