-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99b4b28
commit 7390f84
Showing
8 changed files
with
162 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework.routers import DefaultRouter | ||
from .views import * | ||
|
||
router = DefaultRouter() | ||
|
||
router.register(r'authors', AuthorViewSet, basename='author_list') | ||
router.register(r'books', BookViewSet, basename='book_list') | ||
|
||
urlpatterns = router.urls |
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
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
Django/Group_3/3_Django_REST/api/tests/test_book_serializer.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,71 @@ | ||
from django.test import TestCase | ||
|
||
from ..models import Author, Book | ||
from ..serializers import BookSerializer | ||
|
||
|
||
class BookSerializerTest(TestCase): | ||
def setUp(self) -> None: | ||
self.author = Author.objects.create( | ||
first_name='test_name', | ||
last_name='test_last_name' | ||
) | ||
|
||
self.book = Book.objects.create( | ||
title='test_title', | ||
author_id=self.author | ||
) | ||
|
||
self.serializer_data = { | ||
'title': 'test_title', | ||
'author_id': self.author.id | ||
} | ||
|
||
def test_serializer_contains_expected_fields(self): | ||
serializer = BookSerializer(instance=self.book) | ||
data = serializer.data | ||
|
||
self.assertEqual(set(data.keys()), {'id', 'title', 'author_id'}) | ||
self.assertEqual(data['title'], self.book.title) | ||
self.assertEqual(data['author_id'], self.book.author_id.id) | ||
|
||
def test_serializer_create_valid_date(self): | ||
serializer = BookSerializer(instance=self.book, data=self.serializer_data) | ||
|
||
self.assertTrue(serializer.is_valid()) | ||
new_book = serializer.save() | ||
self.assertEqual(new_book.author_id.id, self.serializer_data['author_id']) | ||
self.assertEqual(new_book.title, self.serializer_data['title']) | ||
|
||
def test_serializer_create_invalid_data(self): | ||
invalid_data = { | ||
'title': 'test_title', | ||
'author_id': None | ||
} | ||
|
||
serializer = BookSerializer(instance=self.book, data=invalid_data) | ||
self.assertFalse(serializer.is_valid()) | ||
|
||
def test_serializer_update_valid_data(self): | ||
updated_data = { | ||
'title': 'updated_title', | ||
'author_id': self.author | ||
} | ||
|
||
serializer = BookSerializer(instance=self.book, data=self.serializer_data) | ||
self.assertTrue(serializer.is_valid()) | ||
serializer.save() | ||
|
||
serializer.update(instance=self.book, validated_data=updated_data) | ||
self.assertTrue(serializer.is_valid()) | ||
self.assertEqual(serializer.data['title'], updated_data['title']) | ||
self.assertEqual(serializer.data['author_id'], updated_data['author_id'].id) | ||
|
||
def test_serializer_update_invalid_data(self): | ||
invalid_data = { | ||
'title': 'test_title', | ||
'author_id': None | ||
} | ||
|
||
serializer = BookSerializer(instance=self.book, data=invalid_data, partial=True) | ||
self.assertFalse(serializer.is_valid()) |
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,43 @@ | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
from rest_framework.test import APIClient | ||
|
||
from ..models import Author, Book | ||
from ..serializers import BookSerializer | ||
|
||
|
||
class BookViewTest(TestCase): | ||
def setUp(self) -> None: | ||
self.author = Author.objects.create( | ||
first_name='test_name', | ||
last_name='test_last_name' | ||
) | ||
|
||
self.book1 = Book.objects.create( | ||
title='test_title_1', | ||
author_id=self.author | ||
) | ||
|
||
self.book2 = Book.objects.create( | ||
title='test_title_2', | ||
author_id=self.author | ||
) | ||
|
||
self.client = APIClient() | ||
|
||
def test_list_books(self): | ||
response = self.client.get(reverse('book_list')) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
|
||
expected_data = BookSerializer([self.book1, self.book2], many=True).data | ||
|
||
self.assertEqual(response.data, expected_data) | ||
|
||
def test_list_books_empty(self): | ||
Book.objects.all().delete() | ||
|
||
response = self.client.get(reverse('book_list')) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data, []) |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .author_view import AuthorListView | ||
from .book_view import BookListView | ||
from .author_view import AuthorListView, AuthorViewSet | ||
from .book_view import BookListView, BookViewSet | ||
from .borrow_view import BorrowListView, BorrowRetrieveDestroyView, BorrowUpdateView, BorrowReturnBookUpdateView | ||
from .user_view import UserCreateView |
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
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