Browse Source

Add test case for data upload

pull/33/head
Hironsan 6 years ago
parent
commit
907cecaca3
3 changed files with 62 additions and 32 deletions
  1. BIN
      app/db.sqlite3
  2. 32
      app/server/tests/test_doc_api.py
  3. 62
      app/server/tests/test_views.py

BIN
app/db.sqlite3

32
app/server/tests/test_doc_api.py

@ -63,35 +63,3 @@ class TestDocAPI(APITestCase):
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_create_doc_by_admin(self):
"""
Ensure we can create a new document object by admin.
"""
# user = self.create_superuser()
# project = self.create_project()
# project.users.add(user)
#
# data = {'text': 'doc', 'labels': []}
# url = reverse('docs', args=[project.id])
# self.client.login(username=self.username, password=self.password)
# response = self.client.post(url, data, format='json')
#
# self.assertEqual(response.status_code, status.HTTP_201_CREATED)
# self.assertEqual(Document.objects.count(), 1)
# self.assertEqual(Document.objects.get().text, 'doc')
def test_create_doc_by_user(self):
"""
Ensure we cannot create a new project object by user.
"""
user = self.create_user()
project = self.create_project()
project.users.add(user)
data = {'text': 'doc', 'labels': []}
url = reverse('docs', args=[project.id])
self.client.login(username=self.username, password=self.password)
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

62
app/server/tests/test_views.py

@ -0,0 +1,62 @@
import os
from django.urls import reverse
from django.test import TestCase, Client
from rest_framework import status
from mixer.backend.django import mixer
from ..models import *
class TestUpload(TestCase):
def setUp(self):
self.username, self.password = 'user', 'pass'
self.client = Client()
self.filepath = os.path.join(os.path.dirname(__file__), 'data/test.csv')
def create_user(self):
user = User.objects.create_user(username=self.username, password=self.password)
return user
def create_superuser(self):
user = User.objects.create_superuser(username=self.username,
password=self.password,
email='hoge@example.com')
return user
def create_project(self):
project = mixer.blend('server.Project')
return project
def test_create_doc_by_admin(self):
"""
Ensure we can create a new document object by admin.
"""
user = self.create_superuser()
project = self.create_project()
project.users.add(user)
with open(self.filepath) as f:
url = reverse('upload', args=[project.id])
self.client.login(username=self.username, password=self.password)
response = self.client.post(url, {'csv_file': f})
self.assertEqual(response.status_code, status.HTTP_302_FOUND)
self.assertEqual(Document.objects.count(), 3)
def test_create_doc_by_user(self):
"""
Ensure we cannot create a new project object by user.
"""
user = self.create_user()
project = self.create_project()
project.users.add(user)
with open(self.filepath) as f:
url = reverse('upload', args=[project.id])
self.client.login(username=self.username, password=self.password)
response = self.client.post(url, {'csv_file': f})
self.assertEqual(response.status_code, status.HTTP_302_FOUND)
self.assertEqual(Document.objects.count(), 0)
Loading…
Cancel
Save