This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch feature/pytest into develop
Close #3 Add tests using pytest
- Loading branch information
Showing
9 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[pytest] | ||
DJANGO_SETTINGS_MODULE = api.settings.development | ||
python_files = tests.py test_*.py *_tests.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |