Fakultas Ilmu Komputer UI

Commit c78294ee authored by M. Abdurrahman's avatar M. Abdurrahman
Browse files

[GREEN] add tests for informasi_fasilitas views for lokasi

parent edfe3d50
Pipeline #42159 passed with stages
in 6 minutes and 17 seconds
from django.test import TestCase
from django.test import TestCase, Client
from django.db.utils import IntegrityError
from .models import Lokasi, Fasilitas, Komentar, KURSI_RODA, RUNNING_TEXT
from .serializers import LokasiSerializer
from registrasi.models import BisaGoUser
from django.contrib.auth.models import User
from django.urls import reverse, path, include
import json
import tempfile
# Create your tests here.
class InformasiFasilitasTest(TestCase):
class InformasiFasilitasModelTest(TestCase):
not_null_constraint_failed_message = 'NOT NULL constraint failed'
def test_models_lokasi_not_created(self):
with self.assertRaises(IntegrityError) as cm:
obj = Lokasi(name=None)
obj.save()
self.assertTrue(str(cm.exception).startswith(self.not_null_constraint_failed_message))
self.assertTrue(str(cm.exception).startswith(
self.not_null_constraint_failed_message))
def test_models_create_new_lokasi(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
......@@ -32,7 +36,8 @@ class InformasiFasilitasTest(TestCase):
with self.assertRaises(IntegrityError) as cm:
obj = Fasilitas(lokasi=None)
obj.save()
self.assertTrue(str(cm.exception).startswith(self.not_null_constraint_failed_message))
self.assertTrue(str(cm.exception).startswith(
self.not_null_constraint_failed_message))
def test_models_create_new_fasilitas(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
......@@ -69,7 +74,8 @@ class InformasiFasilitasTest(TestCase):
with self.assertRaises(IntegrityError) as cm:
obj = Komentar(fasilitas=None)
obj.save();
self.assertTrue(str(cm.exception).startswith(self.not_null_constraint_failed_message))
self.assertTrue(str(cm.exception).startswith(
self.not_null_constraint_failed_message))
def test_models_create_new_komentar(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
......@@ -118,4 +124,95 @@ def test_view_lokasi(self):
no_telp='081212123131',
image=image
)
lokasi.save()
\ No newline at end of file
lokasi.save()
class InformasiFasilitasViewTest(TestCase):
urlpatterns = [
path('informasi-fasilitas/', include('informasi_fasilitas.urls')),
]
def test_LokasiSerializer_valid(self):
serializer = LokasiSerializer(data = {
'name' : 'Mall',
'latitude' : 0.0,
'longitude' : 0.0,
'alamat' : 'Jl. Raya Bogor no.1, Jakarta',
'no_telp' : '081212123131'
})
self.assertTrue(serializer.is_valid)
def test_can_get_lokasi_list(self):
response = Client().get(reverse('lokasi-list'))
self.assertEqual(response.status_code, 200)
def test_cannot_post_lokasi_list(self):
response = Client().post(reverse('lokasi-list'))
self.assertEqual(response.status_code, 405)
def test_can_get_lokasi_details(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
response = Client().get(reverse('lokasi-details', kwargs={'name':'Mall'}))
self.assertEqual(response.status_code, 200)
def test_cannot_post_lokasi_details(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
response = Client().post(reverse('lokasi-details', kwargs={'name':'Mall'}))
self.assertEqual(response.status_code, 405)
def test_can_post_add_lokasi(self):
email='user@gmail.com'
password='hahagotim'
Client().post('/api/register/',
{'name':'name',
'email':email,
'phone_number':000000000,
'password':password})
token_response = Client().post('/api-token-auth/',
{'username' : email, 'password' : password})
content = json.loads(token_response.content.decode('utf-8'))
token = content['token']
client = Client(HTTP_AUTHORIZATION='token '+token)
response = client.post(reverse('add-lokasi'),
{'name' : 'Mall',
'latitude' : 0.0,
'longitude' : 0.0,
'alamat' : 'Jl. Raya Bogor no.1, Jakarta',
'no_telp' : '081212123131',
'image' : None})
self.assertEqual(response.status_code, 201)
def test_cannot_get_add_lokasi(self):
email='user@gmail.com'
password='hahagotim'
Client().post('/api/register/',
{'name':'name',
'email':email,
'phone_number':000000000,
'password':password})
token_response = Client().post('/api-token-auth/',
{'username' : email, 'password' : password})
content = json.loads(token_response.content.decode('utf-8'))
token = content['token']
client = Client(HTTP_AUTHORIZATION='token '+token)
response = client.get(reverse('add-lokasi'))
self.assertEqual(response.status_code, 405)
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