Skip to content

Commit

Permalink
tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
lnxfsf committed Nov 11, 2023
1 parent 6138830 commit 94a91d3
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 14 deletions.
Binary file modified backend/animanga_project/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file modified backend/animanga_project/__pycache__/urls.cpython-311.pyc
Binary file not shown.
9 changes: 7 additions & 2 deletions backend/animanga_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@

# 3rd party
'rest_framework',
'corsheaders',

# local
'api',
]

MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand All @@ -57,6 +60,8 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

CORS_ORIGIN_ALLOW_ALL = True

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

ROOT_URLCONF = 'animanga_project.urls'
Expand Down Expand Up @@ -142,5 +147,5 @@
}


MEDIA_ROOT = 'images/'
MEDIA_URL= 'images/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'images')
MEDIA_URL= '/images/'
Binary file modified backend/api/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file modified backend/api/__pycache__/serializers.cpython-311.pyc
Binary file not shown.
Binary file added backend/api/__pycache__/tests.cpython-311.pyc
Binary file not shown.
Binary file modified backend/api/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified backend/api/__pycache__/views.cpython-311.pyc
Binary file not shown.
4 changes: 3 additions & 1 deletion backend/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Anime(models.Model):
start_date = models.DateField(default=None)
end_date = models.DateField(default=None)


# TODO , implement this ImageField, when you have more knowledge about django..
image = models.ImageField(default=None, upload_to='images')
background_image = models.ImageField(default=None, upload_to='images')

Expand All @@ -21,7 +23,7 @@ class Anime(models.Model):

episodes = models.IntegerField(default=0)

yt_trailer = models.TextField(default=None)
yt_trailer = models.URLField(default=None)



Expand Down
6 changes: 5 additions & 1 deletion backend/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
class AnimeSerializer(serializers.ModelSerializer):
class Meta:
model = Anime
fields = ('id', 'title','genre', 'seasons','start_date', 'end_date', 'image','background_image','num_of_fav','description', 'studio', 'episodes', 'yt_trailer', )
fields = ('id', 'title','genre', 'seasons','start_date', 'end_date','num_of_fav','description', 'studio', 'episodes', 'yt_trailer',)
# fields = '__all__'



2 changes: 0 additions & 2 deletions backend/api/templates/api/show_image.html

This file was deleted.

166 changes: 165 additions & 1 deletion backend/api/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,167 @@

from django.test import TestCase
from .models import Anime


# api endpoints test
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient


# test for models, database
class ApiModelTest(TestCase):

# create database
@classmethod
def setUpTestData(cls):
Anime.objects.create(
title="hello",
genre="horror",
seasons=20,
start_date="2023-03-17",
end_date="2023-03-17",
num_of_fav=3,
description="My anime",
studio="Shinka",
episodes=35,
yt_trailer="https://yts.mx/"
)

# and then check (test) each field
def test_title_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.title}'
self.assertEquals(expected_object_name, 'hello')

def test_genre_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.genre}'
self.assertEquals(expected_object_name, 'horror')

def test_season_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.seasons}'
self.assertEquals(expected_object_name, '20')

def test_start_date_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.start_date}'
self.assertEquals(expected_object_name, "2023-03-17")

def test_end_date_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.end_date}'
self.assertEquals(expected_object_name, "2023-03-17")

def test_num_of_fav_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.num_of_fav}'
self.assertEquals(expected_object_name, '3')

def test_description_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.description}'
self.assertEquals(expected_object_name, "My anime")

def test_studio_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.studio}'
self.assertEquals(expected_object_name, "Shinka")

def test_episodes_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.episodes}'
self.assertEquals(expected_object_name, '35')

def test_yt_trailer_content(self):
api = Anime.objects.get(id=1)
expected_object_name = f'{api.yt_trailer}'
self.assertEquals(expected_object_name, "https://yts.mx/")




# api endpoints test
class ApiTests(TestCase):
def setUp(self):
self.client = APIClient()
self.api_url = reverse('anime_list')

def test_create_and_retrieve_anime(self):
# Test POST request to create an anime
data = {
"title": "hello",
"genre": "horror",
"seasons": 20,
"start_date": "2023-03-17",
"end_date": "2023-03-17",
"num_of_fav": 3,
"description": "My anime",
"studio": "Shinka",
"episodes": 35,
"yt_trailer": "https://yts.mx/"
}
response = self.client.post(self.api_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
anime_id = response.data['id']

# Test GET request to retrieve the created anime
response = self.client.get(reverse('anime_detail', kwargs={'pk': anime_id}))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['title'], 'hello')

def test_update_anime(self):
# Create an anime to update
anime = Anime.objects.create(
title="hello",
genre="horror",
seasons=20,
start_date="2023-03-17",
end_date="2023-03-17",
num_of_fav=3,
description="My anime",
studio="Shinka",
episodes=35,
yt_trailer="https://yts.mx/"
)

# Test PUT request to update the anime
data = {
"title": "Updated Anime",
"genre": "Action",
"seasons": 6,
"start_date": "2023-01-01",
"end_date": "2023-01-01",
"num_of_fav": 5,
"description": "Updated description",
"studio": "Updated Studio",
"episodes": 42,
"yt_trailer": "https://updated-yts.mx/"
}
response = self.client.put(reverse('anime_detail', kwargs={'pk': anime.id}), data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['title'], 'Updated Anime')

def test_delete_anime(self):
# Create an anime to delete
anime = Anime.objects.create(
title="hello",
genre="horror",
seasons=20,
start_date="2023-03-17",
end_date="2023-03-17",
num_of_fav=3,
description="My anime",
studio="Shinka",
episodes=35,
yt_trailer="https://yts.mx/"
)

# Test DELETE request to delete the anime
response = self.client.delete(reverse('anime_detail', kwargs={'pk': anime.id}))
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

# Create your tests here.
# Verify that the anime is deleted
with self.assertRaises(Anime.DoesNotExist):
Anime.objects.get(id=anime.id)
4 changes: 2 additions & 2 deletions backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from .views import ListAnime,DetailAnime

urlpatterns = [
path('<int:pk>/', DetailAnime.as_view()),
path('', ListAnime.as_view()),
path('<int:pk>/', DetailAnime.as_view(), name="anime_detail"),
path('', ListAnime.as_view(), name="anime_list"),
]
4 changes: 0 additions & 4 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@







class DetailAnime(generics.RetrieveUpdateDestroyAPIView):
queryset = Anime.objects.all()
serializer_class = AnimeSerializer
Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ gunicorn
packaging
whitenoise
djangorestframework
Pillow
Pillow
django-cors-headers

0 comments on commit 94a91d3

Please sign in to comment.