Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Commit

Permalink
Merge branch feature/pytest into develop
Browse files Browse the repository at this point in the history
Close #3
Add tests using pytest
  • Loading branch information
winterjung committed Nov 14, 2018
2 parents ff53905 + 33445c7 commit 49802b5
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Expand Up @@ -10,6 +10,8 @@ python-dotenv = "*"
drf-extensions = {git = "https://github.com/chibisov/drf-extensions"}

[dev-packages]
pytest = "*"
pytest-django = "*"

[requires]
python_version = "3.6"
66 changes: 64 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions api/chat/tests.py

This file was deleted.

Empty file added api/chat/tests/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions api/chat/tests/conftest.py
@@ -0,0 +1,24 @@
import pytest

from api.chat.models import Message, Room, User


@pytest.fixture
def users(django_db_setup):
u1 = User.objects.create(username='001')
u2 = User.objects.create(username='002')
return (u1, u2)


@pytest.fixture
def room(users):
r = Room.objects.create(title='test001')
r.participants.set(users)
return r


@pytest.fixture
def msg(users, room):
m = Message(content='hello', room=room, sender=users[0])
m.save()
return m
24 changes: 24 additions & 0 deletions api/chat/tests/test_models.py
@@ -0,0 +1,24 @@
import pytest

from api.chat.models import Message, Room, User


@pytest.mark.django_db
class TestBasicModels:
def test_new_user(self, users):
assert users[0].username == '001'
assert users[1].username == '002'
assert User.objects.count() == 2

def test_new_room(self, users, room):
assert room.id == 1
assert room.title == 'test001'
assert room.participants.count() == 2
assert users[0].room_set.count() == 1
assert users[1].room_set.count() == 1

def test_new_msg(self, msg):
assert msg.id == 1
assert msg.content == 'hello'
assert msg.room.id == 1
assert msg.sender.id == 1
59 changes: 59 additions & 0 deletions api/chat/tests/test_views.py
@@ -0,0 +1,59 @@
import pytest
from django.test import Client

from api.chat.models import Room


@pytest.mark.django_db
class TestRoomView:
def test_room_list_without_data(self, client: Client):
res = client.get('/v1/rooms/')
assert res.status_code == 200
data = res.json()
assert data == []

def test_room_list_with_data(self, client: Client, room):
res = client.get('/v1/rooms/')
assert res.status_code == 200
data = res.json()
assert len(data) == 1

room = data[0]
assert room['id'] == 1
assert room['title'] == 'test001'
assert len(room['participants']) == 2
assert room['participants'][0] == 1
assert room['participants'][1] == 2

def test_room_detail(self, client: Client, room):
res = client.get('/v1/rooms/1/')
assert res.status_code == 200
data = res.json()
assert data['id'] == 1
assert data['title'] == 'test001'
assert len(data['participants']) == 2
assert data['participants'][0] == 1
assert data['participants'][1] == 2

def test_room_detail_non_exist(self, client: Client):
res = client.get('/v1/rooms/1/')
assert res.status_code == 404


@pytest.mark.django_db
class TestMessageView:
def test_msg_list_without_data(self, client: Client, room):
res = client.get('/v1/rooms/1/messages/')
assert res.status_code == 200
data = res.json()
assert data == []

def test_msg_list_with_data(self, client: Client, room, msg):
res = client.get('/v1/rooms/1/messages/')
assert res.status_code == 200
data = res.json()
assert len(data) == 1
msg = data[0]
assert msg['id'] == 1
assert msg['sender'] == 1
assert msg['content'] == 'hello'
3 changes: 3 additions & 0 deletions pytest.ini
@@ -0,0 +1,3 @@
[pytest]
DJANGO_SETTINGS_MODULE = api.settings.development
python_files = tests.py test_*.py *_tests.py
9 changes: 9 additions & 0 deletions requirements-dev.txt
@@ -0,0 +1,9 @@
-i https://pypi.org/simple
atomicwrites==1.1.5
attrs==18.1.0
more-itertools==4.3.0
pluggy==0.7.1; python_version >= '2.7'
py==1.5.4; python_version >= '2.7'
pytest-django==3.3.3
pytest==3.7.1
six==1.11.0

0 comments on commit 49802b5

Please sign in to comment.