Skip to content

Commit

Permalink
port todolists to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
jelly committed Aug 6, 2019
1 parent 6295a38 commit b9e3b8d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ endif
test-pdb: test


.PHONY: test lint
.PHONY: test lint coverage

lint:
pylint devel main mirrors news packages releng templates todolists visualize *.py
Expand Down
22 changes: 22 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import pytest

from django.contrib.auth.models import Group
from django.core.management import call_command

from devel.models import UserProfile


USERNAME = 'joeuser'
FIRSTNAME = 'Joe'
LASTNAME = 'User'
EMAIL = 'user1@example.com'


@pytest.fixture
def arches(db):
call_command('loaddata', 'main/fixtures/arches.json')
Expand All @@ -18,7 +25,10 @@ def repos(db):
@pytest.fixture
def package(db):
# TODO(jelle): create own parameter based version
from main.models import Package
print(list(Package.objects.all()))
call_command('loaddata', 'main/fixtures/package.json')
print(list(Package.objects.all()))


@pytest.fixture
Expand All @@ -38,3 +48,15 @@ def admin_user_profile(admin_user, arches, repos):
public_email="public@archlinux.org")
yield profile
profile.delete()


@pytest.fixture
def user_client(client, django_user_model, groups):
user = django_user_model.objects.create_user(username=USERNAME, password=USERNAME)
profile = UserProfile.objects.create(user=user,
public_email="{}@archlinux.org".format(user.username))
user.groups.add(Group.objects.get(name='Developers'))
client.login(username=USERNAME, password=USERNAME)
yield client
profile.delete()
user.delete()
2 changes: 2 additions & 0 deletions todolists/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


NAME = 'Boost rebuild'
SLUG = 'boost-rebuild'
DESCRIPTION = 'Boost 1.66 rebuild'
RAW = 'linux'

Expand All @@ -13,6 +14,7 @@
def todolist(admin_user, arches, repos, package):
todolist = Todolist.objects.create(name=NAME,
description=DESCRIPTION,
slug=SLUG,
creator=admin_user,
raw=RAW)
yield todolist
Expand Down
171 changes: 72 additions & 99 deletions todolists/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,103 +1,76 @@
from django.contrib.auth.models import User
from django.test import TestCase
from todolists.models import Todolist, TodolistPackage


from todolists.models import Todolist, TodolistPackage
def assert_create_todo(client):
response = client.post('/todo/add/', {
'name': 'Foo rebuild',
'description': 'The Foo Rebuild, please read the instructions',
'raw': 'linux',
}, follow=True)
assert response.status_code == 200


def test_todolist_overview(user_client, todolist):
response = user_client.get('/todo/')
assert response.status_code == 200
assert todolist.name in response.content.decode()


def test_todolist_detail(todolist, user_client):
response = user_client.get(todolist.get_absolute_url())
assert response.status_code == 200
assert todolist.name in response.content.decode()


def test_todolist_json(todolist, user_client):
response = user_client.get(todolist.get_absolute_url() + 'json')
assert response.status_code == 200
data = response.json()
assert data['name'] == todolist.name


def test_create_todolist(user_client):
assert_create_todo(user_client)
assert Todolist.objects.count() == 1
Todolist.objects.all().delete()


def test_flag_pkg(user_client, arches, repos, package):
assert_create_todo(user_client)

todolist = Todolist.objects.first()
package = todolist.packages().first()
assert package.status == TodolistPackage.INCOMPLETE

response = user_client.get('/todo/{}/flag/{}/'.format(todolist.slug, package.id))
assert response.status_code == 302

package = todolist.packages().first()
assert package.status == TodolistPackage.COMPLETE

Todolist.objects.all().delete()

def test_edit(user_client, arches, repos, package):
assert_create_todo(user_client)

todolist = Todolist.objects.first()
assert todolist.packages().count() == 1

response = user_client.post('/todo/{}/edit/'.format(todolist.slug), {
'name': 'Foo rebuild',
'description': 'The Foo Rebuild, please read the instructions',
'raw': 'linux\nglibc',
})
assert response.status_code == 302
todolist = Todolist.objects.first()
assert todolist.packages().count() == 2

Todolist.objects.all().delete()

def test_delete(user_client, arches, repos, package):
assert_create_todo(user_client)

class TestTodolist(TestCase):
fixtures = ['main/fixtures/arches.json', 'main/fixtures/repos.json',
'main/fixtures/package.json']

def setUp(self):
self.user = User.objects.create(username="joeuser", first_name="Joe",
last_name="User", email="user1@example.com")
self.todolist = Todolist.objects.create(name='Boost rebuild',
description='Boost 1.66 rebuid',
creator=self.user,
slug='boost-rebuild',
raw='linux')

def test_todolist_overview(self):
response = self.client.get('/todo/')
self.assertEqual(response.status_code, 200)
self.assertIn(self.todolist.name, response.content.decode())

def test_todolist_detail(self):
response = self.client.get(self.todolist.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertIn(self.todolist.name, response.content.decode())

def test_todolist_json(self):
response = self.client.get(self.todolist.get_absolute_url() + 'json')
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(data['name'], self.todolist.name)


class TestTodolistAdmin(TestCase):
fixtures = ['main/fixtures/arches.json', 'main/fixtures/repos.json',
'main/fixtures/package.json']

def setUp(self):
password = 'test'
self.user = User.objects.create_superuser("admin",
"admin@archlinux.org",
password)

self.client.post('/login/', {
'username': self.user.username,
'password': password
})

def tearDown(self):
Todolist.objects.all().delete()
self.user.delete()

def create_todo(self):
return self.client.post('/todo/add/', {
'name': 'Foo rebuild',
'description': 'The Foo Rebuild, please read the instructions',
'raw': 'linux',
})

def test_create_todolist(self):
response = self.create_todo()
self.assertEqual(response.status_code, 302)
self.assertEqual(len(Todolist.objects.all()), 1)

def test_flag_pkg(self):
response = self.create_todo()
self.assertEqual(response.status_code, 302)

todolist = Todolist.objects.first()
package = todolist.packages().first()
self.assertEqual(package.status, TodolistPackage.INCOMPLETE)

response = self.client.get('/todo/{}/flag/{}/'.format(todolist.slug, package.id))
self.assertEqual(response.status_code, 302)

package = todolist.packages().first()
self.assertEqual(package.status, TodolistPackage.COMPLETE)

def test_edit(self):
response = self.create_todo()
self.assertEqual(response.status_code, 302)
todolist = Todolist.objects.first()
self.assertEqual(len(todolist.packages().all()), 1)

response = self.client.post('/todo/{}/edit/'.format(todolist.slug), {
'name': 'Foo rebuild',
'description': 'The Foo Rebuild, please read the instructions',
'raw': 'linux\nglibc',
})
self.assertEqual(response.status_code, 302)
todolist = Todolist.objects.first()
self.assertEqual(len(todolist.packages().all()), 2)

def test_delete(self):
response = self.create_todo()
self.assertEqual(response.status_code, 302)
todolist = Todolist.objects.first()
response = self.client.post('/todo/{}/delete'.format(todolist.slug))
self.assertEqual(response.status_code, 301)
todolist = Todolist.objects.first()
response = user_client.post('/todo/{}/delete'.format(todolist.slug))
assert response.status_code == 301
Todolist.objects.all().delete()

0 comments on commit b9e3b8d

Please sign in to comment.