From 3c6321fb70bfdefb15aab512f5c8ab7444d99793 Mon Sep 17 00:00:00 2001 From: Souperhero Date: Tue, 23 Oct 2018 19:15:27 -0400 Subject: [PATCH] Pycodestyle changes made to Flashcards --- flash/src/flashcards/api/permissions.py | 5 +- flash/src/flashcards/api/serializers.py | 68 ++++++++++++------- flash/src/flashcards/api/urls.py | 57 ++++++++++++---- flash/src/flashcards/api/views.py | 53 ++++++++------- flash/src/flashcards/apps.py | 1 + .../src/flashcards/migrations/0001_initial.py | 43 ------------ flash/src/flashcards/models.py | 5 +- .../test_crud_flashcards/test_crud_card.py | 51 +++++++++----- .../test_crud_flashcards/test_crud_deck.py | 51 +++++++++----- .../test_deck_functions.py | 66 ++++++++++++------ .../test_flashcards/test_card_create.py | 15 ++-- .../test_flashcards/test_card_delete.py | 26 ++++--- .../test_flashcards/test_card_put.py | 33 ++++++--- .../test_flashcards/test_deck_create.py | 19 +++--- .../test_flashcards/test_deck_delete.py | 21 ++++-- .../test_flashcards/test_deck_detail.py | 31 ++++++--- .../test_flashcards/test_deck_put.py | 23 +++++-- .../test_flashcards/test_flashcards_get.py | 8 ++- .../test_flashcards_retrieve.py | 37 +++++++--- flash/src/manage.py | 5 +- 20 files changed, 384 insertions(+), 234 deletions(-) delete mode 100644 flash/src/flashcards/migrations/0001_initial.py diff --git a/flash/src/flashcards/api/permissions.py b/flash/src/flashcards/api/permissions.py index ff5f689..27a9678 100644 --- a/flash/src/flashcards/api/permissions.py +++ b/flash/src/flashcards/api/permissions.py @@ -2,6 +2,7 @@ from accounts.models import UserProfile from flashcards.models import Deck + class IsDeckOwner(permissions.BasePermission): def has_object_permission(self, request, view, obj): @@ -12,6 +13,7 @@ def has_object_permission(self, request, view, obj): return obj.parent_user == request.user + class IsCardOwner(permissions.BasePermission): def has_object_permission(self, request, view, obj): @@ -20,4 +22,5 @@ def has_object_permission(self, request, view, obj): if request.method in permissions.SAFE_METHODS: return True - return (obj.parent_deck.parent_user == request.user) or (request.user.is_superuser) + return (obj.parent_deck.parent_user == request.user) or ( + request.user.is_superuser) diff --git a/flash/src/flashcards/api/serializers.py b/flash/src/flashcards/api/serializers.py index 3599e12..1ae19da 100644 --- a/flash/src/flashcards/api/serializers.py +++ b/flash/src/flashcards/api/serializers.py @@ -7,26 +7,27 @@ Date Modified: 4/22/2018 """ +import ModelSerializer +import SerializerMethodField +import Deck +import Card + +from rest_framework.serializers +from ..models # Deck Fields Generalization -DECK_UNIQUE_ID = 'unique_id' -DECK_PARENT_USER = 'parent_user' -DECK_PARENT_COURSE = 'parent_course' -DECK_TITLE = 'title' +DECK_UNIQUE_ID = 'unique_id' +DECK_PARENT_USER = 'parent_user' +DECK_PARENT_COURSE = 'parent_course' +DECK_TITLE = 'title' DECK_DECK_DESCRIPTION = 'deck_description' # Card Fields Generalization -CARD_UNIQUE_ID = 'unique_id' -CARD_PARENT_DECK = 'parent_deck' -CARD_FRONT = 'front' -CARD_BACK = 'back' - +CARD_UNIQUE_ID = 'unique_id' +CARD_PARENT_DECK = 'parent_deck' +CARD_FRONT = 'front' +CARD_BACK = 'back' -from rest_framework.serializers import ( - ModelSerializer, - SerializerMethodField, -) -from ..models import Deck, Card class DeckSerializer(ModelSerializer): """ @@ -34,7 +35,9 @@ class DeckSerializer(ModelSerializer): """ class Meta: model = Deck - fields = (DECK_UNIQUE_ID, DECK_PARENT_USER, DECK_PARENT_COURSE, DECK_TITLE, DECK_DECK_DESCRIPTION) + fields = ( + DECK_UNIQUE_ID, DECK_PARENT_USER, + DECK_PARENT_COURSE, DECK_TITLE, DECK_DECK_DESCRIPTION) class DeckOutputSerializer(ModelSerializer): @@ -47,7 +50,9 @@ class DeckOutputSerializer(ModelSerializer): class Meta: model = Deck - fields = (DECK_UNIQUE_ID, DECK_PARENT_USER, DECK_PARENT_COURSE, DECK_TITLE, DECK_DECK_DESCRIPTION, 'parent_course_url') + fields = ( + DECK_UNIQUE_ID, DECK_PARENT_USER, DECK_PARENT_COURSE, + DECK_TITLE, DECK_DECK_DESCRIPTION, 'parent_course_url') def get_parent_user(self, obj): return obj.parent_user.username @@ -56,7 +61,8 @@ def get_parent_course(self, obj): return obj.parent_course.unique_id def get_parent_course_url(self, obj): - return "/courses/api/course/retrieve/{}".format(obj.parent_course.unique_id) + return "/courses/api/course/retrieve/{}".format( + obj.parent_course.unique_id) class DeckDetailSerializer(ModelSerializer): @@ -67,11 +73,18 @@ class DeckDetailSerializer(ModelSerializer): parent_course = SerializerMethodField(source='get_parent_course') parent_course_url = SerializerMethodField(source='get_parent_course_url') cards = SerializerMethodField(source='get_cards') - + class Meta: model = Deck - fields = (DECK_UNIQUE_ID, DECK_PARENT_USER, DECK_PARENT_COURSE, DECK_TITLE, DECK_DECK_DESCRIPTION, 'parent_course_url', 'cards') - + fields = ( + DECK_UNIQUE_ID, + DECK_PARENT_USER, + DECK_PARENT_COURSE, + DECK_TITLE, + DECK_DECK_DESCRIPTION, + 'parent_course_url', + 'cards') + def get_parent_user(self, obj): return obj.parent_user.username @@ -79,8 +92,9 @@ def get_parent_course(self, obj): return obj.parent_course.unique_id def get_parent_course_url(self, obj): - return "/courses/api/course/retrieve/{}".format(obj.parent_course.unique_id) - + return "/courses/api/course/retrieve/{}".format( + obj.parent_course.unique_id) + def get_cards(self, obj): cards_queryset = obj.card_set.all() return CardOutputSerializer(cards_queryset, many=True).data @@ -94,6 +108,7 @@ class Meta: model = Card fields = (CARD_UNIQUE_ID, CARD_PARENT_DECK, CARD_FRONT, CARD_BACK) + class CardOutputSerializer(ModelSerializer): """ This serializes the Card model for Output @@ -103,10 +118,15 @@ class CardOutputSerializer(ModelSerializer): class Meta: model = Card - fields = (CARD_UNIQUE_ID, CARD_PARENT_DECK, CARD_FRONT, CARD_BACK, 'parent_deck_url') + fields = ( + CARD_UNIQUE_ID, + CARD_PARENT_DECK, + CARD_FRONT, CARD_BACK, + 'parent_deck_url') def get_parent_deck(self, obj): return obj.parent_deck.unique_id def get_parent_deck_url(self, obj): - return "/flashcards/api/deck/retrieve/{}".format(obj.parent_deck.unique_id) + return "/flashcards/api/deck/retrieve/{}".format( + obj.parent_deck.unique_id) diff --git a/flash/src/flashcards/api/urls.py b/flash/src/flashcards/api/urls.py index e295f49..24de977 100644 --- a/flash/src/flashcards/api/urls.py +++ b/flash/src/flashcards/api/urls.py @@ -1,8 +1,6 @@ """ FlashCourses Decks & Cards REST API Class-Based URLs - File Path: /flash/src/flashcards/api/urls.py - Modified By: Patrick R. McElhiney Date Modified: 4/16/2018 """ @@ -27,16 +25,47 @@ app_name = 'flashcards_api' urlpatterns = [ - path('deck/create/', views.CreateDeckAPIView.as_view(), name='deck_create'), - path('deck/retrieve/', views.RetrieveDeckAPIView.as_view(), name='deck_retrieve'), - path('deck/list/', views.ListDeckAPIView.as_view(), name='deck_list'), - path('deck/delete/', views.DestroyDeckAPIView.as_view(), name='deck_delete'), - path('deck/update/', views.UpdateDeckAPIView.as_view(), name='deck_update'), - path('deck/detail/', views.DetailDeckAPIView.as_view(), name='deck_detail'), - path('card/create/', views.CreateCardAPIView.as_view(), name='card_create'), - path('card/retrieve/', views.RetrieveCardAPIView.as_view(), name='card_retrieve'), - path('card/list/', views.ListCardAPIView.as_view(), name='card_list'), - path('card/delete/', views.DestroyCardAPIView.as_view(), name='card_delete'), - path('card/update/', views.UpdateCardAPIView.as_view(), name='card_update') + path( + 'deck/create/', + views.CreateDeckAPIView.as_view(), + name='deck_create'), + path( + 'deck/retrieve/', + views.RetrieveDeckAPIView.as_view(), + name='deck_retrieve'), + path( + 'deck/list/', + views.ListDeckAPIView.as_view(), + name='deck_list'), + path( + 'deck/delete/', + views.DestroyDeckAPIView.as_view(), + name='deck_delete'), + path( + 'deck/update/', + views.UpdateDeckAPIView.as_view(), + name='deck_update'), + path( + 'deck/detail/', + views.DetailDeckAPIView.as_view(), + name='deck_detail'), + path( + 'card/create/', + views.CreateCardAPIView.as_view(), + name='card_create'), + path( + 'card/retrieve/', + views.RetrieveCardAPIView.as_view(), + name='card_retrieve'), + path( + 'card/list/', + views.ListCardAPIView.as_view(), + name='card_list'), + path( + 'card/delete/', + views.DestroyCardAPIView.as_view(), + name='card_delete'), + path('card/update/', + views.UpdateCardAPIView.as_view(), + name='card_update') ] - diff --git a/flash/src/flashcards/api/views.py b/flash/src/flashcards/api/views.py index 3748dd0..6a3fbf7 100644 --- a/flash/src/flashcards/api/views.py +++ b/flash/src/flashcards/api/views.py @@ -9,6 +9,15 @@ NOTE: Uncomment the commented code below to enable authentication with JWT. """ +import DeckSerializer +import DeckOutputSerializer +import DeckDetailSerializer +import CardSerializer +import CardOutputSerializer +from flashcards.models import Deck, Card +from rest_framework import generics +from .permissions import IsDeckOwner, IsCardOwner +from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAdminUser # Deck Fields Generalization DECK_UNIQUE_ID = 'unique_id' @@ -17,25 +26,14 @@ CARD_UNIQUE_ID = 'unique_id' -from .serializers import ( - DeckSerializer, - DeckOutputSerializer, - DeckDetailSerializer, - CardSerializer, - CardOutputSerializer, -) -from flashcards.models import Deck, Card -from rest_framework import generics -from .permissions import IsDeckOwner, IsCardOwner -from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAdminUser - class CreateDeckAPIView(generics.CreateAPIView): """ This API endpoint is for creating a new Deck. """ queryset = Deck.objects.all() serializer_class = DeckSerializer - #permission_classes = (IsAuthenticatedOrReadOnly,) + # permission_classes = (IsAuthenticatedOrReadOnly,) + class RetrieveDeckAPIView(generics.RetrieveAPIView): """ @@ -45,7 +43,8 @@ class RetrieveDeckAPIView(generics.RetrieveAPIView): lookup_field = DECK_UNIQUE_ID queryset = Deck.objects.all() serializer_class = DeckOutputSerializer - #permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + # permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + class ListDeckAPIView(generics.ListAPIView): """ @@ -53,7 +52,8 @@ class ListDeckAPIView(generics.ListAPIView): """ queryset = Deck.objects.all().order_by('title') serializer_class = DeckOutputSerializer - #permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + # permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + class DestroyDeckAPIView(generics.DestroyAPIView): """ @@ -63,7 +63,8 @@ class DestroyDeckAPIView(generics.DestroyAPIView): lookup_field = DECK_UNIQUE_ID queryset = Deck.objects.all() serializer_class = DeckSerializer - #permission_classes = (IsDeckOwner,IsAdminUser) + # permission_classes = (IsDeckOwner,IsAdminUser) + class UpdateDeckAPIView(generics.UpdateAPIView): """ @@ -73,7 +74,8 @@ class UpdateDeckAPIView(generics.UpdateAPIView): lookup_field = DECK_UNIQUE_ID queryset = Deck.objects.all() serializer_class = DeckSerializer - #permission_classes = (IsDeckOwner,IsAdminUser) + # permission_classes = (IsDeckOwner,IsAdminUser) + class DetailDeckAPIView(generics.RetrieveAPIView): """ @@ -83,7 +85,8 @@ class DetailDeckAPIView(generics.RetrieveAPIView): lookup_field = DECK_UNIQUE_ID queryset = Deck.objects.all() serializer_class = DeckDetailSerializer - #permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + # permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + class CreateCardAPIView(generics.CreateAPIView): """ @@ -91,7 +94,8 @@ class CreateCardAPIView(generics.CreateAPIView): """ queryset = Card.objects.all() serializer_class = CardSerializer - #permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + # permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + class RetrieveCardAPIView(generics.RetrieveAPIView): """ @@ -101,7 +105,8 @@ class RetrieveCardAPIView(generics.RetrieveAPIView): lookup_field = CARD_UNIQUE_ID queryset = Card.objects.all() serializer_class = CardOutputSerializer - #permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + # permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + class ListCardAPIView(generics.ListAPIView): """ @@ -109,7 +114,8 @@ class ListCardAPIView(generics.ListAPIView): """ queryset = Card.objects.all() serializer_class = CardOutputSerializer - #permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + # permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + class DestroyCardAPIView(generics.DestroyAPIView): """ @@ -119,7 +125,8 @@ class DestroyCardAPIView(generics.DestroyAPIView): lookup_field = CARD_UNIQUE_ID queryset = Card.objects.all() serializer_class = CardSerializer - #permission_classes = (IsCardOwner, IsAdminUser) + # permission_classes = (IsCardOwner, IsAdminUser) + class UpdateCardAPIView(generics.UpdateAPIView): """ @@ -129,4 +136,4 @@ class UpdateCardAPIView(generics.UpdateAPIView): lookup_field = CARD_UNIQUE_ID queryset = Card.objects.all() serializer_class = CardSerializer - #permission_classes = (IsCardOwner, IsAdminUser) + # permission_classes = (IsCardOwner, IsAdminUser) diff --git a/flash/src/flashcards/apps.py b/flash/src/flashcards/apps.py index 0cf37c6..65f5f44 100644 --- a/flash/src/flashcards/apps.py +++ b/flash/src/flashcards/apps.py @@ -7,5 +7,6 @@ from django.apps import AppConfig + class FlashcardsConfig(AppConfig): name = 'flashcards' diff --git a/flash/src/flashcards/migrations/0001_initial.py b/flash/src/flashcards/migrations/0001_initial.py deleted file mode 100644 index b834955..0000000 --- a/flash/src/flashcards/migrations/0001_initial.py +++ /dev/null @@ -1,43 +0,0 @@ -# Generated by Django 2.0.3 on 2018-04-15 16:55 - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion -import uuid - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - ('courses', '0001_initial'), - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ] - - operations = [ - migrations.CreateModel( - name='Card', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('front', models.TextField()), - ('back', models.TextField()), - ('unique_id', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)), - ], - ), - migrations.CreateModel( - name='Deck', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('title', models.CharField(max_length=64)), - ('unique_id', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)), - ('parent_course', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='courses.Course')), - ('parent_user', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), - ], - ), - migrations.AddField( - model_name='card', - name='parent_deck', - field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='flashcards.Deck'), - ), - ] diff --git a/flash/src/flashcards/models.py b/flash/src/flashcards/models.py index e84138e..b3b1ea1 100644 --- a/flash/src/flashcards/models.py +++ b/flash/src/flashcards/models.py @@ -11,6 +11,7 @@ import uuid + class Deck(models.Model): """ Deck model @@ -36,7 +37,7 @@ class Deck(models.Model): blank=False) unique_id = models.UUIDField( default=uuid.uuid4, - editable= False, + editable=False, unique=True) def __str__(self): @@ -88,7 +89,7 @@ class Card(models.Model): back = models.TextField() unique_id = models.UUIDField( default=uuid.uuid4, - editable= False, + editable=False, unique=True) def __str__(self): diff --git a/flash/src/flashcards/test_crud_flashcards/test_crud_card.py b/flash/src/flashcards/test_crud_flashcards/test_crud_card.py index 188e227..fdbc265 100644 --- a/flash/src/flashcards/test_crud_flashcards/test_crud_card.py +++ b/flash/src/flashcards/test_crud_flashcards/test_crud_card.py @@ -3,7 +3,8 @@ 04.23.2018 Testing CRUD operations for models -Relative File Path: /flash/src/flashcards/test_crud_flashcards/test_crud_card.py +Relative File Path: +/flash/src/flashcards/test_crud_flashcards/test_crud_card.py """ from django.test import TestCase @@ -18,23 +19,41 @@ class CRUDCardTest(TestCase): - institution = None; - course = None; + institution = None + course = None deck = None - card = None; - user = None; + card = None + user = None def setUp(self): """ - setUp needed to perform the tests, it is called before every test function. + setUp needed to perform the tests, + it is called before every test function. """ - self.institution = Institution.objects.create(institution_name='test_name', location='test_location') - self.institution1 = Institution.objects.create(institution_name='test_name1', location='test_location1') - self.course = Course.objects.create(course_id= 'test_id', course_title= 'title_123', course_description= 'description') - self.course1 = Course.objects.create(course_id= 'test_id1', course_title = 'title_12', course_description= 'description1') - self.deck = Deck.objects.create(title='deck1', deck_description= 'desc') - self.card = Card.objects.create(front='card1', back='card2') - self.user = User.objects.create(first_name = 'first_name', last_name = 'last_name', password='password') + self.institution = Institution.objects.create( + institution_name='test_name', + location='test_location') + self.institution1 = Institution.objects.create( + institution_name='test_name1', + location='test_location1') + self.course = Course.objects.create( + course_id='test_id', + course_title='title_123', + course_description='description') + self.course1 = Course.objects.create( + course_id='test_id1', + course_title='title_12', + course_description='description1') + self.deck = Deck.objects.create( + title='deck1', + deck_description='desc') + self.card = Card.objects.create( + front='card1', + back='card2') + self.user = User.objects.create( + first_name='first_name', + last_name='last_name', + password='password') def test_Card_create(self): """ @@ -54,8 +73,8 @@ def test_Card_update(self): """ Testing object update operaions for Card model. """ - c= Card.objects.filter(front='card1').update(back='card1') - self.card= Card.objects.get(id=self.card.id) + c = Card.objects.filter(front='card1').update(back='card1') + self.card = Card.objects.get(id=self.card.id) self.assertEqual(self.card.back, 'card1') def test_Card_delete(self): @@ -63,4 +82,4 @@ def test_Card_delete(self): Testing object delete operaions for Card model. """ Card.objects.filter(front='card1').delete() - self.assertEqual(Card.objects.count(),0) + self.assertEqual(Card.objects.count(), 0) diff --git a/flash/src/flashcards/test_crud_flashcards/test_crud_deck.py b/flash/src/flashcards/test_crud_flashcards/test_crud_deck.py index 35f77e1..5911cbe 100644 --- a/flash/src/flashcards/test_crud_flashcards/test_crud_deck.py +++ b/flash/src/flashcards/test_crud_flashcards/test_crud_deck.py @@ -3,7 +3,8 @@ 04.23.2018 Testing CRUD operations for models -Relative File Path: /flash/src/flashcards/test_crud_flashcards/test_crud_deck.py +Relative File Path: +/flash/src/flashcards/test_crud_flashcards/test_crud_deck.py """ from django.test import TestCase @@ -18,23 +19,41 @@ class CRUDDeckTest(TestCase): - institution = None; - course = None; + institution = None + course = None deck = None - card = None; - user = None; + card = None + user = None def setUp(self): """ - setUp needed to perform the tests, it is called before every test function. + setUp needed to perform the tests, + it is called before every test function. """ - self.institution = Institution.objects.create(institution_name='test_name', location='test_location') - self.institution1 = Institution.objects.create(institution_name='test_name1', location='test_location1') - self.course = Course.objects.create(course_id= 'test_id', course_title= 'title_123', course_description= 'description') - self.course1 = Course.objects.create(course_id= 'test_id1', course_title = 'title_12', course_description= 'description1') - self.deck = Deck.objects.create(title='deck1', deck_description= 'desc') - self.card = Card.objects.create(front='card1', back='card2') - self.user = User.objects.create(first_name = 'first_name', last_name = 'last_name', password='password') + self.institution = Institution.objects.create( + institution_name='test_name', + location='test_location') + self.institution1 = Institution.objects.create( + institution_name='test_name1', + location='test_location1') + self.course = Course.objects.create( + course_id='test_id', + course_title='title_123', + course_description='description') + self.course1 = Course.objects.create( + course_id='test_id1', + course_title='title_12', + course_description='description1') + self.deck = Deck.objects.create( + title='deck1', + deck_description='desc') + self.card = Card.objects.create( + front='card1', + back='card2') + self.user = User.objects.create( + first_name='first_name', + last_name='last_name', + password='password') def test_Deck_create(self): """ @@ -53,8 +72,8 @@ def test_Deck_update(self): """ Testing object update operations for Deck model """ - d= Deck.objects.filter(title='deck1').update(title='deck2') - self.deck= Deck.objects.get(id=self.deck.id) + d = Deck.objects.filter(title='deck1').update(title='deck2') + self.deck = Deck.objects.get(id=self.deck.id) self.assertEqual(self.deck.title, 'deck2') def test_deck_delete(self): @@ -62,4 +81,4 @@ def test_deck_delete(self): Testing object delete operations for Deck model """ Deck.objects.filter(title='deck1').delete() - self.assertEqual(Deck.objects.count(),0) + self.assertEqual(Deck.objects.count(), 0) diff --git a/flash/src/flashcards/test_crud_flashcards/test_deck_functions.py b/flash/src/flashcards/test_crud_flashcards/test_deck_functions.py index d1181be..67cd7f2 100644 --- a/flash/src/flashcards/test_crud_flashcards/test_deck_functions.py +++ b/flash/src/flashcards/test_crud_flashcards/test_deck_functions.py @@ -2,7 +2,8 @@ FlashCards- Test cases for deck model functions Created By: Swechchha Tiwari 4/25/2018 -Relative File Path: /flash/src/flashcards/test_crud_flashcards/test_deck_functions.py +Relative File Path: +/flash/src/flashcards/test_crud_flashcards/test_deck_functions.py Modified Date: 4/25/2018 """ @@ -18,49 +19,70 @@ from rest_framework.test import APITestCase # Create your tests here. + + class CardTest(TestCase): def setUp(self): """ - setUp needed to perform the tests, it is called before every test function. + setUp needed to perform the tests, + it is called before every test function. """ - self.user = User.objects.create_user(username = 'Swechchha', email = 'swechchha@gmail.com', password = 'imppwdswe') - self.course_one = Course.objects.create(course_title = 'test', course_id = '2', course_description = 'this is a test data') - self.deck_one = Deck.objects.create(parent_user = self.user, title = 'test title', deck_description = 'this is a test') - self.card = Card.objects.create(front = 'test', back = 'testsback', parent_deck = self.deck_one) - self.card = Card.objects.create(front = 'test', back = 'testsback', parent_deck = self.deck_one) - self.card = Card.objects.create(front = 'testfront', back = 'testsback', parent_deck = self.deck_one) + self.user = User.objects.create_user( + username='Swechchha', + email='swechchha@gmail.com', + password='imppwdswe') + self.course_one = Course.objects.create( + course_title='test', + course_id='2', + course_description='this is a test data') + self.deck_one = Deck.objects.create( + parent_user=self.user, + title='test title', + deck_description='this is a test') + self.card = Card.objects.create( + front='test', + back='testsback', + parent_deck=self.deck_one) + self.card = Card.objects.create( + front='test', + back='testsback', + parent_deck=self.deck_one) + self.card = Card.objects.create( + front='testfront', + back='testsback', + parent_deck=self.deck_one) def test_get_number_cards(self): """ Test for get_number_cards method defined in models.py """ - d_one = Deck.objects.first() + d_one = Deck.objects.first() result = d_one.get_number_cards() - self.assertEqual(result,3) + self.assertEqual(result, 3) def test_has_duplicates(self, card=None): """ Test for has_duplicates method defined in models.py """ - if card is None: - d_one = Deck.objects.first() - c_one = Card.objects.all() - self.assertEqual(Card.objects.count(), 3) - result = d_one.has_duplicates(self.card) - self.assertIs(result, False) + if card is None: + d_one = Deck.objects.first() + c_one = Card.objects.all() + self.assertEqual(Card.objects.count(), 3) + result = d_one.has_duplicates(self.card) + self.assertIs(result, False) def test_is_owner(self, user=None): """ Test for is_owner method defined in models.py """ - if user is None: - self.user = User.objects.first() - self.assertEqual(User.objects.count(), 1) - d_one = Deck.objects.first() - actual = d_one.is_owner(self.user) - self.assertTrue(actual,'Swechchha') + if user is None: + self.user = User.objects.first() + self.assertEqual(User.objects.count(), 1) + d_one = Deck.objects.first() + actual = d_one.is_owner(self.user) + self.assertTrue(actual, 'Swechchha') diff --git a/flash/src/flashcards/test_flashcards/test_card_create.py b/flash/src/flashcards/test_flashcards/test_card_create.py index e112178..83dc47b 100644 --- a/flash/src/flashcards/test_flashcards/test_card_create.py +++ b/flash/src/flashcards/test_flashcards/test_card_create.py @@ -17,6 +17,7 @@ from rest_framework import status from rest_framework.test import APITestCase + class APIStatusCodeCardCreate(APITestCase): """ @@ -28,14 +29,14 @@ def setUp(self): Sets up testing environment. Add endpoint to be tested """ - self.post_method_endpoints_card =[ + self.post_method_endpoints_card = [ reverse('flashcards:flashcards_api:card_create') ] def test_card_endpoint_post_method_with_valid_data(self): """ - Create a request to card create endpoint in post_method_endpoints. Ensure returns a 201 - response status code + Create a request to card create endpoint in post_method_endpoints. + Ensure returns a 201 response status code """ c = Client() @@ -52,15 +53,15 @@ def test_card_endpoint_post_method_with_valid_data(self): def test_card_endpoint_post_method_with_invalid_data(self): """ - Create a request to card create endpoint in post_method_endpoints. Ensure returns a 400 for invalid data - response status code + Create a request to card create endpoint in post_method_endpoints. + Ensure returns a 400 for invalid data response status code. """ c = Client() invalid_data = { - "front" : "test front", - "back" : "" + "front": "test front", + "back": "" } diff --git a/flash/src/flashcards/test_flashcards/test_card_delete.py b/flash/src/flashcards/test_flashcards/test_card_delete.py index c1945e3..ff3a48d 100644 --- a/flash/src/flashcards/test_flashcards/test_card_delete.py +++ b/flash/src/flashcards/test_flashcards/test_card_delete.py @@ -17,6 +17,7 @@ from rest_framework import status from rest_framework.test import APITestCase + class APIdeleteStatusCodeCard(APITestCase): """ @@ -27,21 +28,30 @@ def setUp(self): """ Sets up testing environment. Add all endpoints to be tested """ - userone = User.objects.create_user('Swechchha', 'swechchha@gmail.com', 'imppwdswe') - course_tbl = Course.objects.create(course_title = 'test', course_id = '2', course_description = 'this is a test data') - deck_tbl = Deck.objects.create(title = 'testone', deck_description = 'testdescription') - card_tbl = Card.objects.create(front = 'test', back = 'testone') - + userone = User.objects.create_user( + 'Swechchha', 'swechchha@gmail.com', 'imppwdswe') + course_tbl = Course.objects.create( + course_title='test', + course_id='2', + course_description='this is a test data') + deck_tbl = Deck.objects.create( + title='testone', + deck_description='testdescription') + card_tbl = Card.objects.create( + front='test', + back='testone') self.delete_method_endpoints_card = [ - reverse('flashcards:flashcards_api:card_delete', kwargs = {'unique_id': Card.objects.first().unique_id}), + reverse( + 'flashcards:flashcards_api:card_delete', + kwargs={'unique_id': Card.objects.first().unique_id}), ] def test_self_delete_method_endpoints_card(self): """ - Create a request to every endpoint in delete_method_endpoints. Ensure returns a 204 - response status code + Create a request to every endpoint in delete_method_endpoints. + Ensure returns a 204 response status code """ c = Client() diff --git a/flash/src/flashcards/test_flashcards/test_card_put.py b/flash/src/flashcards/test_flashcards/test_card_put.py index 1764f77..949abf9 100644 --- a/flash/src/flashcards/test_flashcards/test_card_put.py +++ b/flash/src/flashcards/test_flashcards/test_card_put.py @@ -17,6 +17,7 @@ from rest_framework import status from rest_framework.test import APITestCase + class APIputStatusCodeTestsCard(APITestCase): """ @@ -28,28 +29,38 @@ def setUp(self): Sets up testing environment. Add all endpoints to be tested """ - user = User.objects.create_user('Swechchha', 'swechchha@gmail.com', 'imppwdswe') - course_tbl = Course.objects.create(course_title = 'test', course_id = '2', course_description = 'this is a test data') - deck_tbl = Deck.objects.create(title = 'test title', deck_description = 'testing') - card_tbl = Card.objects.create(front = 'test', back = 'testback') - + user = User.objects.create_user( + 'Swechchha', 'swechchha@gmail.com', 'imppwdswe') + course_tbl = Course.objects.create( + course_title='test', + course_id='2', + course_description='this is a test data') + deck_tbl = Deck.objects.create( + title='test title', + deck_description='testing') + card_tbl = Card.objects.create( + front='test', + back='testback') def test_card_endpoint_put_method(self): """ - Create a request to every endpoint in put_method_endpoints. Ensure returns a 200 - response status code + Create a request to every endpoint in put_method_endpoints. + Ensure returns a 200 response status code """ c = Client() card = Card.objects.first() data = { - "unique_id": card.unique_id , - "front" : card.front, - "back" : "testback one" + "unique_id": card.unique_id, + "front": card.front, + "back": "testback one" } self.assertEqual(card.back, "testback") - response = self.client.put(reverse('flashcards:flashcards_api:card_update', kwargs = {'unique_id': card.unique_id}), data) + response = self.client.put( + reverse( + 'flashcards:flashcards_api:card_update', + kwargs={'unique_id': card.unique_id}), data) card = Card.objects.first() self.assertEqual(card.back, 'testback one') self.assertEqual(response.status_code, 200) diff --git a/flash/src/flashcards/test_flashcards/test_deck_create.py b/flash/src/flashcards/test_flashcards/test_deck_create.py index 11cbffe..da01734 100644 --- a/flash/src/flashcards/test_flashcards/test_deck_create.py +++ b/flash/src/flashcards/test_flashcards/test_deck_create.py @@ -17,6 +17,7 @@ from rest_framework import status from rest_framework.test import APITestCase + class APIStatusCodeDeckCreate(APITestCase): """ @@ -25,17 +26,17 @@ class APIStatusCodeDeckCreate(APITestCase): def setUp(self): """ - Sets up testing environment. Add endpoint to be tested + Sets up testing environment. Add endpoint to be tested. """ - self.post_method_endpoints_deck =[ + self.post_method_endpoints_deck = [ reverse('flashcards:flashcards_api:deck_create') ] def test_deck_endpoint_post_method_with_valid_data(self): """ - Create a request to deck create endpoint in post_method_endpoints. Ensure returns a 201 - response status code + Create a request to deck create endpoint in post_method_endpoints. + Ensure returns a 201 response status code """ c = Client() @@ -43,8 +44,8 @@ def test_deck_endpoint_post_method_with_valid_data(self): "username": "Test", "email": "test@gmail.com", "password": "somepassword", - "deck_description" : "testdescription", - "title" : "Web application" + "deck_description": "testdescription", + "title": "Web application" } for endpoint in self.post_method_endpoints_deck: @@ -53,8 +54,8 @@ def test_deck_endpoint_post_method_with_valid_data(self): def test_deck_endpoint_post_method_with_invalid_data(self): """ - Create a request to deck create endpoint in post_method_endpoints. Ensure returns a 400 for invalid data - response status code + Create a request to deck create endpoint in post_method_endpoints. + Ensure returns a 400 for invalid data response status code """ c = Client() @@ -62,7 +63,7 @@ def test_deck_endpoint_post_method_with_invalid_data(self): "username": "Test", "email": "swe@gmail.com", "password": "guestpassword", - "title" : "" + "title": "" } for endpoint in self.post_method_endpoints_deck: diff --git a/flash/src/flashcards/test_flashcards/test_deck_delete.py b/flash/src/flashcards/test_flashcards/test_deck_delete.py index 628d22d..0318cfa 100644 --- a/flash/src/flashcards/test_flashcards/test_deck_delete.py +++ b/flash/src/flashcards/test_flashcards/test_deck_delete.py @@ -17,6 +17,7 @@ from rest_framework import status from rest_framework.test import APITestCase + class APIdeleteStatusCodeDeck(APITestCase): """ @@ -28,19 +29,27 @@ def setUp(self): Sets up testing environment. Add all endpoints to be tested """ - userone = User.objects.create_user('Swechchha', 'swechchha@gmail.com', 'imppwdswe') - course_tbl = Course.objects.create(course_title = 'test', course_id = '2', course_description = 'this is a test data') - deck_tbl = Deck.objects.create(title = 'testone', deck_description = 'testdescription') + userone = User.objects.create_user( + 'Swechchha', 'swechchha@gmail.com', 'imppwdswe') + course_tbl = Course.objects.create( + course_title='test', + course_id='2', + course_description='this is a test data') + deck_tbl = Deck.objects.create( + title='testone', + deck_description='testdescription') self.delete_method_endpoints_deck = [ - reverse('flashcards:flashcards_api:deck_delete', kwargs = {'unique_id': Deck.objects.first().unique_id}), + reverse( + 'flashcards:flashcards_api:deck_delete', + kwargs={'unique_id': Deck.objects.first().unique_id}), ] def test_self_delete_method_endpointsdeck(self): """ - Create a request to every endpoint in delete_method_endpoints. Ensure returns a 204 - response status code + Create a request to every endpoint in delete_method_endpoints. + Ensure returns a 204 response status code """ c = Client() diff --git a/flash/src/flashcards/test_flashcards/test_deck_detail.py b/flash/src/flashcards/test_flashcards/test_deck_detail.py index 0cb3db1..35ae815 100644 --- a/flash/src/flashcards/test_flashcards/test_deck_detail.py +++ b/flash/src/flashcards/test_flashcards/test_deck_detail.py @@ -1,5 +1,6 @@ """ -FlashCourses- Test cases for API endpoints for get and retrieve for card and deck endpoints +FlashCourses- Test cases for API endpoints for get and retrieve +for card and deck endpoints Created By: Swechchha Tiwari 4/21/2018 Relative File Path: /flash/src/flashcards/test_flashcards/test_deck_detail.py @@ -18,6 +19,7 @@ from rest_framework.test import APITestCase from accounts.models import UserProfile + class APIgetStatusCodeDeckdetail(APITestCase): """ @@ -26,21 +28,32 @@ class APIgetStatusCodeDeckdetail(APITestCase): def setUp(self): """ - Sets up testing environment for GET method for card_retrieve and deck_retrieve based on unique_id. + Sets up testing environment for GET method for card_retrieve + and deck_retrieve based on unique_id. """ - user = User.objects.create_user('Swechchha', 'swechchha@gmail.com', 'imppwdswe') - course_tbl = Course.objects.create(course_title = 'test', course_id = '2', course_description = 'this is a test data') - deck_tbl = Deck.objects.create(title = 'test title', deck_description = 'this is a test') - card_tbl = Card.objects.create(front = 'test', back = 'testsback') + user = User.objects.create_user( + 'Swechchha', 'swechchha@gmail.com', 'imppwdswe') + course_tbl = Course.objects.create( + course_title='test', + course_id='2', + course_description='this is a test data') + deck_tbl = Deck.objects.create( + title='test title', + deck_description='this is a test') + card_tbl = Card.objects.create( + front='test', + back='testsback') self.detail_method_endpoint = [ - reverse('flashcards:flashcards_api:deck_detail', kwargs = {'unique_id': Deck.objects.first().unique_id}), + reverse( + 'flashcards:flashcards_api:deck_detail', + kwargs={'unique_id': Deck.objects.first().unique_id}), ] def test_deck_endpoint_detail_method(self): """ - Create a request to every endpoint in retrieve_method_endpoints. Ensure returns a 200 - response status code + Create a request to every endpoint in retrieve_method_endpoints. + Ensure returns a 200 response status code """ c = Client() diff --git a/flash/src/flashcards/test_flashcards/test_deck_put.py b/flash/src/flashcards/test_flashcards/test_deck_put.py index e9c08f8..1f82ffa 100644 --- a/flash/src/flashcards/test_flashcards/test_deck_put.py +++ b/flash/src/flashcards/test_flashcards/test_deck_put.py @@ -17,6 +17,7 @@ from rest_framework import status from rest_framework.test import APITestCase + class APIputStatusCodeTestsDeck(APITestCase): """ @@ -27,15 +28,20 @@ def setUp(self): """ Sets up testing environment. Add all endpoints to be tested """ - - user = User.objects.create_user('Swechchha', 'swechchha@gmail.com', 'imppwdswe') - course_tbl = Course.objects.create(course_title = 'test', course_id = '2', course_description = 'this is a test data') - deck_tbl = Deck.objects.create(title = 'test title', deck_description = 'testing') + user = User.objects.create_user( + 'Swechchha', 'swechchha@gmail.com', 'imppwdswe') + course_tbl = Course.objects.create( + course_title='test', + course_id='2', + course_description='this is a test data') + deck_tbl = Deck.objects.create( + title='test title', + deck_description='testing') def test_deck_endpoint_put_method(self): """ - Create a request to every endpoint in put_method_endpoints. Ensure returns a 200 - response status code + Create a request to every endpoint in put_method_endpoints. + Ensure returns a 200 response status code """ c = Client() @@ -49,7 +55,10 @@ def test_deck_endpoint_put_method(self): } self.assertEqual(deck.title, "test title") - response = self.client.put(reverse('flashcards:flashcards_api:deck_update', kwargs = {'unique_id': deck.unique_id}), data) + response = self.client.put( + reverse( + 'flashcards:flashcards_api:deck_update', + kwargs={'unique_id': deck.unique_id}), data) deck = Deck.objects.first() self.assertEqual(deck.title, 'some new title') self.assertEqual(response.status_code, 200) diff --git a/flash/src/flashcards/test_flashcards/test_flashcards_get.py b/flash/src/flashcards/test_flashcards/test_flashcards_get.py index 4bf5c15..d85c323 100644 --- a/flash/src/flashcards/test_flashcards/test_flashcards_get.py +++ b/flash/src/flashcards/test_flashcards/test_flashcards_get.py @@ -2,7 +2,8 @@ FlashCourses- Test cases for API endpoints for get- card and deck endpoints Created By: Swechchha Tiwari 4/6/2018 -Relative File Path: /flash/src/flashcards/test_flashcards/test_flashcards_get.py +Relative File Path: +/flash/src/flashcards/test_flashcards/test_flashcards_get.py Modified Date: 4/17/2018 """ @@ -18,6 +19,7 @@ from rest_framework.test import APITestCase from accounts.models import UserProfile + class APIgetStatusCodeDeckCard(APITestCase): """ @@ -37,8 +39,8 @@ def setUp(self): def test_flashcards_endpoint_get_method(self): """ - Crate a request to every endpoint in get_method_endpoints. Ensure returns a 200 - response status code + Crate a request to every endpoint in get_method_endpoints. + Ensure returns a 200 response status code """ c = Client() diff --git a/flash/src/flashcards/test_flashcards/test_flashcards_retrieve.py b/flash/src/flashcards/test_flashcards/test_flashcards_retrieve.py index 327ba07..f8a8463 100644 --- a/flash/src/flashcards/test_flashcards/test_flashcards_retrieve.py +++ b/flash/src/flashcards/test_flashcards/test_flashcards_retrieve.py @@ -1,8 +1,10 @@ """ -FlashCourses- Test cases for API endpoints for get and retrieve for card and deck endpoints +FlashCourses- Test cases for API endpoints for get and retrieve +for card and deck endpoints Created By: Swechchha Tiwari 4/6/2018 -Relative File Path: /flash/src/flashcards/test_flashcards/test_flashcards_retrieve.py +Relative File Path: +/flash/src/flashcards/test_flashcards/test_flashcards_retrieve.py Modified Date: 4/17/2018 """ @@ -18,6 +20,7 @@ from rest_framework.test import APITestCase from accounts.models import UserProfile + class APIgetStatusCodeDeckCard(APITestCase): """ @@ -26,25 +29,37 @@ class APIgetStatusCodeDeckCard(APITestCase): def setUp(self): """ - Sets up testing environment for GET method for card_retrieve and deck_retrieve based on unique_id. + Sets up testing environment for GET method for card_retrieve + and deck_retrieve based on unique_id. """ - user = User.objects.create_user('Swechchha', 'swechchha@gmail.com', 'imppwdswe') - course_tbl = Course.objects.create(course_title = 'test', course_id = '2', course_description = 'this is a test data') - deck_tbl = Deck.objects.create(title = 'rofl', deck_description = 'this is a test') - card_tbl = Card.objects.create(front = 'test', back = 'testsback') + user = User.objects.create_user( + 'Swechchha', 'swechchha@gmail.com', 'imppwdswe') + course_tbl = Course.objects.create( + course_title='test', + course_id='2', + course_description='this is a test data') + deck_tbl = Deck.objects.create( + title='rofl', + deck_description='this is a test') + card_tbl = Card.objects.create( + front='test', back='testsback') self.retrieve_method_endpoints = [ - reverse('flashcards:flashcards_api:deck_retrieve', kwargs = {'unique_id': Deck.objects.first().unique_id}), + reverse( + 'flashcards:flashcards_api:deck_retrieve', + kwargs={'unique_id': Deck.objects.first().unique_id}), - reverse('flashcards:flashcards_api:card_retrieve', kwargs = {'unique_id': Card.objects.first().unique_id}), + reverse( + 'flashcards:flashcards_api:card_retrieve', + kwargs={'unique_id': Card.objects.first().unique_id}), ] def test_flashcards_endpoint_retrieve_method(self): """ - Create a request to every endpoint in retrieve_method_endpoints. Ensure returns a 200 - response status code + Create a request to every endpoint in retrieve_method_endpoints. + Ensure returns a 200 response status code """ c = Client() diff --git a/flash/src/manage.py b/flash/src/manage.py index c8a54b2..27261a7 100755 --- a/flash/src/manage.py +++ b/flash/src/manage.py @@ -6,12 +6,13 @@ for development """ -#!/usr/bin/env python +# !/usr/bin/env python import os import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE","FlashCourses.settings.common") + os.environ.setdefault( + "DJANGO_SETTINGS_MODULE", "FlashCourses.settings.common") try: from django.core.management import execute_from_command_line except ImportError as exc: