dodani testovi
This commit is contained in:
Binary file not shown.
BIN
plovidba_aplikacija/__pycache__/tests.cpython-38.pyc
Normal file
BIN
plovidba_aplikacija/__pycache__/tests.cpython-38.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,10 +1,10 @@
|
||||
from rest_framework import serializers
|
||||
from .models import ObjektSigurnosti, Log
|
||||
|
||||
class ObjektSigurnostiSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = ObjektSigurnosti
|
||||
fields = '__all__'
|
||||
from django.contrib.gis.geos import Point
|
||||
from .models import ObjektSigurnosti
|
||||
# class ObjektSigurnostiSerializer(serializers.ModelSerializer):
|
||||
# class Meta:
|
||||
# model = ObjektSigurnosti
|
||||
# fields = '__all__'
|
||||
|
||||
# class LogSerializer(serializers.ModelSerializer):
|
||||
|
||||
@@ -12,4 +12,32 @@ class ObjektSigurnostiSerializer(serializers.ModelSerializer):
|
||||
|
||||
# class Meta:
|
||||
# model = Log
|
||||
# fields = ('id', 'user', 'user_full_name', 'timestamp', 'akcija', 'opis')
|
||||
# fields = ('id', 'user', 'user_full_name', 'timestamp', 'akcija', 'opis')
|
||||
|
||||
class PointSerializer(serializers.Serializer):
|
||||
lat = serializers.FloatField()
|
||||
lon = serializers.FloatField()
|
||||
|
||||
def to_representation(self, instance):
|
||||
return {'lat': instance.y, 'lon': instance.x}
|
||||
|
||||
class ObjektSigurnostiSerializer(serializers.ModelSerializer):
|
||||
lokacija = PointSerializer()
|
||||
|
||||
class ObjektSigurnostiSerializer(serializers.ModelSerializer):
|
||||
lokacija = PointSerializer()
|
||||
|
||||
class Meta:
|
||||
model = ObjektSigurnosti
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
representation['lokacija'] = PointSerializer(instance.lokacija).data
|
||||
return representation
|
||||
|
||||
def create(self, validated_data):
|
||||
lokacija_data = validated_data.pop('lokacija')
|
||||
lokacija = Point(lokacija_data['lon'], lokacija_data['lat'], srid=3765)
|
||||
objekt = ObjektSigurnosti.objects.create(lokacija=lokacija, **validated_data)
|
||||
return objekt
|
||||
@@ -1,3 +1,69 @@
|
||||
from django.test import TestCase
|
||||
from django.contrib.gis.geos import Point
|
||||
from django.urls import reverse
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
from plovidba_aplikacija.models import ObjektSigurnosti
|
||||
from plovidba_aplikacija.serializers import PointSerializer
|
||||
|
||||
# Create your tests here.
|
||||
# Testiranje listanja objekata
|
||||
class ObjektSigurnostiListTest(APITestCase):
|
||||
def test_list_objekti_sigurnosti(self):
|
||||
url = reverse('objektisigurnosti-list')
|
||||
response = self.client.get(url)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('results', response.data)
|
||||
|
||||
# Testiranje stvaranja objekata
|
||||
class ObjektSigurnostiCreateTest(APITestCase):
|
||||
def setUp(self):
|
||||
self.url = reverse('objektisigurnosti-list')
|
||||
|
||||
def test_create_objekt_sigurnosti(self):
|
||||
data = {
|
||||
'lokacija': {'lat': 45.123, 'lon': 18.456},
|
||||
'naziv': 'test-naziv',
|
||||
}
|
||||
response = self.client.post(self.url, data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
|
||||
# Testiranje dohvaćanja pojedinog objekata
|
||||
class ObjektSigurnostiDetailTest(APITestCase):
|
||||
def setUp(self):
|
||||
self.objekt = ObjektSigurnosti.objects.create(
|
||||
lokacija=Point(18.456, 45.123),
|
||||
naziv='test-naziv',
|
||||
)
|
||||
self.url = reverse('objektisigurnosti-detail', args=[self.objekt.pk])
|
||||
|
||||
|
||||
def test_get_objekt_sigurnosti(self):
|
||||
response = self.client.get(self.url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# def test_update_objekt_sigurnosti(self):
|
||||
# data = {
|
||||
# 'lokacija': {'lat': 15.17517, 'lon': 44.01113},
|
||||
# 'naziv' : 'updated-naziv',
|
||||
|
||||
# }
|
||||
# response = self.client.patch(self.url, data, format='json')
|
||||
# self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
# # Reload the object from the database
|
||||
# updated_objekt = ObjektSigurnosti.objects.get(pk=self.objekt.pk)
|
||||
|
||||
# # Check if the values are updated correctly
|
||||
# self.assertEqual(updated_objekt.lokacija.x, 13.79758)
|
||||
# self.assertEqual(updated_objekt.lokacija.y, 44.9254)
|
||||
# self.assertEqual(updated_objekt.naziv, 'updated-naziv')
|
||||
|
||||
|
||||
# def test_retrieve_objekt_sigurnosti(self):
|
||||
# url = reverse('objektisigurnosti-detail', args=[self.objekt.id])
|
||||
# response = self.client.get(url)
|
||||
|
||||
# self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
from django.urls import path, include
|
||||
from .views import ObjektSigurnostiList, ObjektSigurnostiDetail, Log
|
||||
from .views import ObjektSigurnostiList, ObjektSigurnostiDetail
|
||||
from plovidba_aplikacija import views
|
||||
|
||||
urlpatterns = [
|
||||
|
||||
@@ -4,7 +4,7 @@ from rest_framework.generics import ListAPIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from rest_framework import generics
|
||||
from .models import ObjektSigurnosti, Log
|
||||
from .models import ObjektSigurnosti
|
||||
from .serializers import ObjektSigurnostiSerializer
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.pagination import LimitOffsetPagination
|
||||
@@ -34,8 +34,8 @@ def perform_create(self, serializer):
|
||||
#used to customize the behavior when creating an object, and in this case,
|
||||
#it sets the operater field and creates a log entry for the created object.
|
||||
serializer.save(operater=self.request.user)
|
||||
instance = serializer.instance
|
||||
self.create_log(instance)
|
||||
# instance = serializer.instance
|
||||
# self.create_log(instance)
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.request.method == "GET":
|
||||
|
||||
Reference in New Issue
Block a user