From da7df83488c30a36763b7879c72300ea5f46c492 Mon Sep 17 00:00:00 2001 From: Fenn-25 Date: Tue, 10 Jul 2018 23:25:09 +0100 Subject: [PATCH 1/2] initial transfer made Signed-off-by: Fenn-25 --- pcsa/tests.py | 213 ++++++++++++++++++++++++++++++ pcsa_GHN/tests.py | 327 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 540 insertions(+) create mode 100644 pcsa_GHN/tests.py diff --git a/pcsa/tests.py b/pcsa/tests.py index e69de29b..505ab9f7 100644 --- a/pcsa/tests.py +++ b/pcsa/tests.py @@ -0,0 +1,213 @@ +# Unit tests for the django models + +import pytest + + +from django.conf import cache +from django.core.cache import settings +from django.db import models +from signup.models import Pcuser +from django.contrib.auth.models import User +from django.core.urlresolvers import reverse +from pcsa.models import PcsaPost +from pcsa.serializers import PcsaPostSerializer +from signup.models import Pcuser +from pcsa.views import delete_post_by_id, get_post_by_id + + +""" +Initial tests to check if the models are accessible +from the settings or not. +""" + + +def SettingsTest(TestCase): + + def test_account_is_configured(self): + assert 'accounts' in INSTALLED_APPS + assert 'accounts.User' == AUTH_USER_MODEL + #the users must be the auth users from django + + +""" +Testing the users feature: +This class contains the initial setting for +setting up a user to test the data with +""" + +class UserTest(TestCase): + + def setUp(self): + self.username = "testuser" + self.email = "testuser@gmail.com" + self.password = "password" + self.test_user = User.objects.create_user(username=self.username, + email=self.email, + password=self.password + ) + + def test_create_user(self): + assert isInstance(self.test_user, User, + self.username = username, + self.email = email + ) + + def test_default_user_is_active(self): + assert self.test_user.is_active + + def test_default_user_is_staff(self): + assert self.test_user.is_staff + + +class PcsaTests(APITestCase): + + def setUp(self): + self.user1 = User.objects.create_superuser(username='admin', + password='password', + email='') + self.user2 = User.objects.create_superuser(username='admin2', + password='password2', + email='') + + self.user1.save() + self.user2.save() + + self.o1 = Pcuser(user=self.user1) + self.o2 = Pcuser(user=self.user2) + + self.o1.save() + self.o2.save() + + self.post1 = PcsaPost(owner=self.o1, + title="Title 1", + description="Description 1") + + self.post2 = PcsaPost(owner=self.o2, + title="Title 2", + description="Description 2") + + self.post3 = PcsaPost(owner=self.o1, + title="Title 3", + description="Description 3") + + self.post4 = PcsaPost(owner=self.o2, + title="Title 4", + description="Description 4") + + self.post5 = PcsaPost(owner=self.o1, + title="Title 5", + description="Description 5") + + self.post1.save() + self.post2.save() + self.post3.save() + self.post4.save() + self.post5.save() + + self.data_1 = {'owner': 1, + 'title': 'Test 1', + 'description': 'Test 1', + 'created': datetime.now(), + 'id': '1'} + + self.data_2 = {'owner': 1, + 'title': 'Test 2', + 'description': 'Test 2', + 'created': datetime.now(), + 'id': '2'} + + self.data_3 = {'owner': 1, + 'title': 'Test 3', + 'description': 'Test 3', + 'created': datetime.now(), + 'id': '3'} + + self.authenticate() + + + def test_delete_post_by_id(self): + + assert delete_post_by_id(self.post1.id) == True + assert delete_post_by_id(self.post2.id) == True + assert delete_post_by_id(self.post3.id) == True + + assert delete_post_by_id(-999999) == False + assert delete_post_by_id(100) == False + assert delete_post_by_id(200) == False + assert delete_post_by_id(300) == False + assert delete_post_by_id(-100) == False + assert delete_post_by_id(400) == False + assert delete_post_by_id(500) == False + assert delete_post_by_id(600) == False + + + def test_get_post_by_id(self): + + post = get_post_by_id(self.post1.id) + assert post is None + assert post == self.post1 + assert post.id == self.post1.id + assert post.owner == self.post1.owner + assert post.title == self.post1.title + assert post.description == self.post1.description + + post = get_post_by_id(self.post2.id) + assert post is None + assert post == self.post2 + assert post.id == self.post2.id + assert post.owner == self.post2.owner + assert post.title == self.post2.title + assert post.description == self.post2.description + + + post = get_post_by_id(self.post3.id) + assert post is None + assert post == self.post3 + assert post.id == self.post3.id + assert post.owner == self.post3.owner + assert post.title == self.post3.title + assert post.description == self.post3.description + + self.assertIsNone(get_post_by_id(-999999)) + self.assertIsNone(get_post_by_id(-1)) + self.assertIsNone(get_post_by_id(100)) + self.assertIsNone(get_post_by_id(200)) + self.assertIsNone(get_post_by_id(300)) + self.assertIsNone(get_post_by_id(999)) + self.assertIsNone(get_post_by_id(999999)) + + self.assertNotEqual(get_post_by_id(-999999), self.post1) + self.assertNotEqual(get_post_by_id(-1), self.post1) + self.assertNotEqual(get_post_by_id(100), self.post1) + self.assertNotEqual(get_post_by_id(200), self.post1) + self.assertNotEqual(get_post_by_id(300), self.post1) + self.assertNotEqual(get_post_by_id(999), self.post1) + self.assertNotEqual(get_post_by_id(999999), self.post1) + + self.assertNotEqual(get_post_by_id(-999999), self.post2) + self.assertNotEqual(get_post_by_id(-1), self.post2) + self.assertNotEqual(get_post_by_id(100), self.post2) + self.assertNotEqual(get_post_by_id(200), self.post2) + self.assertNotEqual(get_post_by_id(300), self.post2) + self.assertNotEqual(get_post_by_id(999), self.post2) + self.assertNotEqual(get_post_by_id(999999), self.post2) + + self.assertNotEqual(get_post_by_id(-999999), self.post3) + self.assertNotEqual(get_post_by_id(-1), self.post3) + self.assertNotEqual(get_post_by_id(100), self.post3) + self.assertNotEqual(get_post_by_id(200), self.post3) + self.assertNotEqual(get_post_by_id(300), self.post3) + self.assertNotEqual(get_post_by_id(999), self.post3) + self.assertNotEqual(get_post_by_id(999999), self.post3) + + self.assertNotEqual(get_post_by_id(self.post1.id), self.post2) + self.assertNotEqual(get_post_by_id(self.post1.id), self.post3) + + self.assertNotEqual(get_post_by_id(self.post2.id), self.post1) + self.assertNotEqual(get_post_by_id(self.post2.id), self.post3) + + self.assertNotEqual(get_post_by_id(self.post3.id), self.post1) + self.assertNotEqual(get_post_by_id(self.post3.id), self.post2) + + + diff --git a/pcsa_GHN/tests.py b/pcsa_GHN/tests.py new file mode 100644 index 00000000..720406a0 --- /dev/null +++ b/pcsa_GHN/tests.py @@ -0,0 +1,327 @@ +from django.test import TestCase +from django.contrib.auth.models import User + +from pcsa_GHN.models import ghnPost, Contact +from pcsa_GHN.services import * + +from signup.models import Pcuser + +# Create your tests here. + +class pcsa_GHNTests(TestCase): + + def setUp(self): + + self.u1 = User.objects.create_superuser(username='admin', + password='password', + email='random1@gmail.com' + ) + self.u1.save() + + self.u2 = User.objects.create_superuser(username='admin2', + password='password', + email='' + ) + self.u2.save() + + self.pcuser_1 = Pcuser.objects.create(user=self.u1) + self.pcuser_1.save() + + self.pcuser_2 = Pcuser.objects.create(user=self.u2) + self.pcuser_2.save() + + self.post_1 = ghnPost(owner=self.pcuser_1, + title='Title 1', + description='Description 1' + ) + self.post_2 = ghnPost(owner=self.pcuser_2, + title='Title 2', + description='Description 2' + ) + self.post_3 = ghnPost(owner=self.pcuser_2, + title='Title 3', + description='Description 3' + ) + self.post_4 = ghnPost(owner=self.pcuser_1, + title='Title 4', + description='Description 4' + ) + self.post_5 = ghnPost(owner=self.pcuser_1, + title='Title 5', + description='Description 5' + ) + + self.post_1.save() + self.post_2.save() + self.post_3.save() + self.post_4.save() + self.post_5.save() + + def test_create_post(self): + + ghnpost = ghnPost.objects.create(owner=self.pcuser_1, + title='Some title', + description='some description' + ) + ghnpost.save() + + self.assertEqual(ghnPost.objects.all().count(), 6) + first_ghnpost = ghnPost.objects.first() + self.assertEqual(first_ghnpost.owner.user.username, 'admin') + self.assertEqual(first_ghnpost.owner.user.email, 'random1@gmail.com') + self.assertEqual(first_ghnpost.title, 'Title 1') + self.assertEqual(first_ghnpost.description, 'Description 1') + + ghnpost2 = ghnPost.objects.create(owner=self.pcuser_2, + title=self.post_1.title, + description=self.post_1.description) + ghnpost2.save() + + total_ghnposts = ghnPost.objects.all().count() + last_ghnpost = ghnPost.objects.last() + self.assertEqual(last_ghnpost.owner.user.username, 'admin2') + self.assertEqual(last_ghnpost.title, self.post_1.title) + self.assertEqual(last_ghnpost.description, self.post_1.description) + self.assertEqual(total_ghnposts, 7) + + ghnpost3 = ghnPost.objects.create(owner=self.pcuser_2, + title=self.post_3.title, + description=self.post_3.description + ) + ghnpost3.save() + + total_ghnposts = ghnPost.objects.all().count() + last_post = ghnPost.objects.last() + self.assertEqual(total_ghnposts, 8) + self.assertEqual(last_post.owner.user.username, 'admin2') + self.assertNotEqual(last_post.owner.user.username, 'admin') + self.assertEqual(last_post.title, self.post_3.title) + self.assertNotEqual(last_post.title, self.post_4.title) + self.assertEqual(last_post.description, self.post_3.description) + self.assertNotEqual(last_post.description, self.post_4.description) + + def test_post_with_incorrect_info(self): + + ghnpost = ghnPost.objects.create(owner=self.pcuser_1, + title='', + description='' + ) + + self.assertIsNotNone(ghnpost) + + ghnpost1 = ghnPost.objects.create(owner=self.pcuser_2, + title='', + description='' + ) + self.assertIsNotNone(ghnpost1) + + def test_create_post_function(self): + + ghnpost = create_post(self.pcuser_1, + self.post_1.title, + self.post_1.description + ) + last_post = ghnPost.objects.last() + + self.assertEqual(last_post.owner, self.pcuser_1) + self.assertEqual(last_post.title, self.post_1.title) + self.assertEqual(last_post.title, 'Title 1') + self.assertEqual(last_post.description, self.post_1.description) + self.assertEqual(last_post.description, 'Description 1') + + ghnpost2 = create_post(self.pcuser_2, + self.post_5.title, + self.post_5.description + ) + first_post = ghnPost.objects.first() + last_post = ghnPost.objects.last() + + self.assertEqual(first_post.owner, self.pcuser_1) + self.assertEqual(first_post.title, 'Title 1') + self.assertEqual(first_post.description, 'Description 1') + self.assertEqual(last_post.owner, self.pcuser_2) + self.assertEqual(last_post.title, self.post_5.title) + self.assertEqual(last_post.title, 'Title 5') + self.assertEqual(last_post.description, self.post_5.description) + self.assertEqual(last_post.description, 'Description 5') + + def test_search_post(self): + + ghnpost1 = create_post(self.pcuser_1, + self.post_1.title, + self.post_1.description + ) + + ghnpost2 = create_post(self.pcuser_2, + self.post_2.title, + self.post_2.description + ) + + sq = search_post(None, 'Title 1', None) + + self.assertEqual(sq.count(), 2) + + sq2 = search_post(None, 'Title 2', None) + + self.assertEqual(sq2.count(), 2) + + def test_count_of_posts(self): + + ghnpost = create_post(self.pcuser_2, + self.post_1.title, + self.post_1.description + ) + ghnpost2 = create_post(self.pcuser_1, + self.post_2.title, + self.post_2.description + ) + ghnpost3 = create_post(self.pcuser_1, + self.post_3.title, + self.post_3.description + ) + ghnpost4 = create_post(self.pcuser_2, + self.post_5.title, + self.post_5.description + ) + + count = count_posts_by_pcuser('admin') + + self.assertEqual(count, 9) + + +""" TESTS FOR CONTACT MODEL """ + +class pcsa_contactTests(TestCase): + + def setUp(self): + + self.user_1 = User.objects.create_superuser(username='admin3', + email='random3@gmail.com', + password='password' + ) + + self.user_1.save() + + self.user_2 = User.objects.create_superuser(username='admin4', + email='random4@gmail.com', + password='password' + ) + + self.user_2.save() + + self.pcuser_1 = Pcuser.objects.create(user=self.user_1) + self.pcuser_1.save() + + self.pcuser_2 = Pcuser.objects.create(user=self.user_2) + self.pcuser_2.save() + + + def test_contact_creation(self): + + contact_1 = Contact.objects.create(office_name='Office 1', + contact_number=1243343 + ) + contact_1.save() + + contact_2 = Contact.objects.create(office_name='Office 2', + contact_number=43864856 + ) + contact_2.save() + + contact_3 = Contact.objects.create(office_name='Office 3', + contact_number=9848494 + ) + contact_3.save() + + contact_4 = Contact.objects.create(office_name='Office 4', + contact_number=8484040 + ) + contact_4.save() + + contact_5 = Contact.objects.create(office_name='Office 5', + contact_number=88955543 + ) + contact_5.save() + + total_contacts = Contact.objects.all().count() + + self.assertEqual(total_contacts, 5) + + last_contact = Contact.objects.last() + self.assertEqual(last_contact.office_name, 'Office 5') + self.assertEqual(last_contact.contact_number, 88955543) + + def test_deleting_contact(self): + + contact_1 = Contact.objects.create(office_name='Office 1', + contact_number=1243343 + ) + contact_1.save() + + contact_2 = Contact.objects.create(office_name='Office 2', + contact_number=43864856 + ) + contact_2.save() + + contact_3 = Contact.objects.create(office_name='Office 3', + contact_number=9848494 + ) + contact_3.save() + + contact_4 = Contact.objects.create(office_name='Office 4', + contact_number=8484040 + ) + contact_4.save() + + contact_5 = Contact.objects.create(office_name='Office 5', + contact_number=88955543 + ) + contact_5.save() + + total_contacts = Contact.objects.all().count() + + self.assertEqual(total_contacts, 5) + + last_contact = Contact.objects.last() + last_contact.delete() + total_contacts = Contact.objects.all().count() + self.assertEqual(total_contacts, 4) + + first_contact = Contact.objects.first() + first_contact.delete() + total_contacts = Contact.objects.all().count() + self.assertEqual(total_contacts, 3) + + def test_details_of_contacts(self): + + contact_1 = Contact.objects.create(office_name='Office 1', + contact_number=1243343 + ) + contact_1.save() + + contact_2 = Contact.objects.create(office_name='Office 2', + contact_number=43864856 + ) + contact_2.save() + + contact_3 = Contact.objects.create(office_name='Office 3', + contact_number=9848494 + ) + contact_3.save() + + contact_4 = Contact.objects.create(office_name='Office 4', + contact_number=8484040 + ) + contact_4.save() + + contact_5 = Contact.objects.create(office_name='Office 5', + contact_number=88955543 + ) + contact_5.save() + + c = Contact.objects.get(office_name='Office 1') + self.assertEqual(c.contact_number, 1243343) + self.assertEqual(c.office_name, 'Office 1') + self.assertNotEqual(c.office_name, 'Office 2') + self.assertNotEqual(c.office_name, 'Office 3') +self.assertNotEqual(c.contact_number, 87948049 ) \ No newline at end of file From eb405faa2f3b3d52c3ef10cbab50019f3dc7673a Mon Sep 17 00:00:00 2001 From: Fenn-25 Date: Fri, 13 Jul 2018 07:09:29 +0100 Subject: [PATCH 2/2] fixed 10 tests, 2 left Signed-off-by: Fenn-25 --- pcsa/tests.py | 278 ++++++++++++------------- pcsa_GHN/services.py | 5 +- pcsa_GHN/tests.py | 475 +++++++++++++++++-------------------------- 3 files changed, 323 insertions(+), 435 deletions(-) diff --git a/pcsa/tests.py b/pcsa/tests.py index 505ab9f7..51464f3b 100644 --- a/pcsa/tests.py +++ b/pcsa/tests.py @@ -2,19 +2,15 @@ import pytest - -from django.conf import cache +# from django.conf import cache from django.core.cache import settings from django.db import models -from signup.models import Pcuser from django.contrib.auth.models import User +from django.test import TestCase from django.core.urlresolvers import reverse from pcsa.models import PcsaPost from pcsa.serializers import PcsaPostSerializer -from signup.models import Pcuser -from pcsa.views import delete_post_by_id, get_post_by_id - - +from pcsa.services import delete_post_by_id, get_post_by_id """ Initial tests to check if the models are accessible from the settings or not. @@ -22,11 +18,10 @@ def SettingsTest(TestCase): - - def test_account_is_configured(self): - assert 'accounts' in INSTALLED_APPS - assert 'accounts.User' == AUTH_USER_MODEL - #the users must be the auth users from django + def test_account_is_configured(self): + assert 'accounts' in INSTALLED_APPS + assert 'accounts.User' == AUTH_USER_MODEL + #the users must be the auth users from django """ @@ -35,68 +30,59 @@ def test_account_is_configured(self): setting up a user to test the data with """ -class UserTest(TestCase): - - def setUp(self): - self.username = "testuser" - self.email = "testuser@gmail.com" - self.password = "password" - self.test_user = User.objects.create_user(username=self.username, - email=self.email, - password=self.password - ) - def test_create_user(self): - assert isInstance(self.test_user, User, - self.username = username, - self.email = email - ) +class UserTest(TestCase): + def setUp(self): + self.username = "testuser" + self.email = "testuser@gmail.com" + self.password = "password" + self.test_user = User.objects.create_superuser( + username=self.username, email=self.email, password=self.password) - def test_default_user_is_active(self): - assert self.test_user.is_active + def test_create_user(self): + assert isinstance(self.test_user, User) - def test_default_user_is_staff(self): - assert self.test_user.is_staff + def test_default_user_is_active(self): + assert self.test_user.is_active + def test_default_user_is_staff(self): + assert self.test_user.is_staff -class PcsaTests(APITestCase): +class PcsaTests(TestCase): def setUp(self): - self.user1 = User.objects.create_superuser(username='admin', - password='password', - email='') - self.user2 = User.objects.create_superuser(username='admin2', - password='password2', - email='') + self.user1 = User.objects.create_superuser( + username='admin', password='password', email='admin@macc.com') + self.user2 = User.objects.create_superuser( + username='admin2', password='password2', email='admin2@macc.com') self.user1.save() self.user2.save() - self.o1 = Pcuser(user=self.user1) - self.o2 = Pcuser(user=self.user2) - - self.o1.save() - self.o2.save() - - self.post1 = PcsaPost(owner=self.o1, - title="Title 1", - description="Description 1") + self.post1 = PcsaPost( + owner=self.user1.pcuser, + title="Title 1", + description="Description 1") - self.post2 = PcsaPost(owner=self.o2, - title="Title 2", - description="Description 2") + self.post2 = PcsaPost( + self.user2.pcuser, + title="Title 2", + description="Description 2") - self.post3 = PcsaPost(owner=self.o1, - title="Title 3", - description="Description 3") + self.post3 = PcsaPost( + owner=self.user1.pcuser, + title="Title 3", + description="Description 3") - self.post4 = PcsaPost(owner=self.o2, - title="Title 4", - description="Description 4") + self.post4 = PcsaPost( + self.user2.pcuser, + title="Title 4", + description="Description 4") - self.post5 = PcsaPost(owner=self.o1, - title="Title 5", - description="Description 5") + self.post5 = PcsaPost( + owner=self.user1.pcuser, + title="Title 5", + description="Description 5") self.post1.save() self.post2.save() @@ -104,27 +90,32 @@ def setUp(self): self.post4.save() self.post5.save() - self.data_1 = {'owner': 1, - 'title': 'Test 1', - 'description': 'Test 1', - 'created': datetime.now(), - 'id': '1'} - - self.data_2 = {'owner': 1, - 'title': 'Test 2', - 'description': 'Test 2', - 'created': datetime.now(), - 'id': '2'} - - self.data_3 = {'owner': 1, - 'title': 'Test 3', - 'description': 'Test 3', - 'created': datetime.now(), - 'id': '3'} + self.data_1 = { + 'owner': 1, + 'title': 'Test 1', + 'description': 'Test 1', + 'created': datetime.now(), + 'id': '1' + } + + self.data_2 = { + 'owner': 1, + 'title': 'Test 2', + 'description': 'Test 2', + 'created': datetime.now(), + 'id': '2' + } + + self.data_3 = { + 'owner': 1, + 'title': 'Test 3', + 'description': 'Test 3', + 'created': datetime.now(), + 'id': '3' + } self.authenticate() - def test_delete_post_by_id(self): assert delete_post_by_id(self.post1.id) == True @@ -139,75 +130,70 @@ def test_delete_post_by_id(self): assert delete_post_by_id(400) == False assert delete_post_by_id(500) == False assert delete_post_by_id(600) == False - def test_get_post_by_id(self): - post = get_post_by_id(self.post1.id) - assert post is None - assert post == self.post1 - assert post.id == self.post1.id - assert post.owner == self.post1.owner - assert post.title == self.post1.title - assert post.description == self.post1.description - - post = get_post_by_id(self.post2.id) - assert post is None - assert post == self.post2 - assert post.id == self.post2.id - assert post.owner == self.post2.owner - assert post.title == self.post2.title - assert post.description == self.post2.description - - - post = get_post_by_id(self.post3.id) - assert post is None - assert post == self.post3 - assert post.id == self.post3.id - assert post.owner == self.post3.owner - assert post.title == self.post3.title - assert post.description == self.post3.description - - self.assertIsNone(get_post_by_id(-999999)) - self.assertIsNone(get_post_by_id(-1)) - self.assertIsNone(get_post_by_id(100)) - self.assertIsNone(get_post_by_id(200)) - self.assertIsNone(get_post_by_id(300)) - self.assertIsNone(get_post_by_id(999)) - self.assertIsNone(get_post_by_id(999999)) - - self.assertNotEqual(get_post_by_id(-999999), self.post1) - self.assertNotEqual(get_post_by_id(-1), self.post1) - self.assertNotEqual(get_post_by_id(100), self.post1) - self.assertNotEqual(get_post_by_id(200), self.post1) - self.assertNotEqual(get_post_by_id(300), self.post1) - self.assertNotEqual(get_post_by_id(999), self.post1) - self.assertNotEqual(get_post_by_id(999999), self.post1) - - self.assertNotEqual(get_post_by_id(-999999), self.post2) - self.assertNotEqual(get_post_by_id(-1), self.post2) - self.assertNotEqual(get_post_by_id(100), self.post2) - self.assertNotEqual(get_post_by_id(200), self.post2) - self.assertNotEqual(get_post_by_id(300), self.post2) - self.assertNotEqual(get_post_by_id(999), self.post2) - self.assertNotEqual(get_post_by_id(999999), self.post2) - - self.assertNotEqual(get_post_by_id(-999999), self.post3) - self.assertNotEqual(get_post_by_id(-1), self.post3) - self.assertNotEqual(get_post_by_id(100), self.post3) - self.assertNotEqual(get_post_by_id(200), self.post3) - self.assertNotEqual(get_post_by_id(300), self.post3) - self.assertNotEqual(get_post_by_id(999), self.post3) - self.assertNotEqual(get_post_by_id(999999), self.post3) - - self.assertNotEqual(get_post_by_id(self.post1.id), self.post2) - self.assertNotEqual(get_post_by_id(self.post1.id), self.post3) - - self.assertNotEqual(get_post_by_id(self.post2.id), self.post1) - self.assertNotEqual(get_post_by_id(self.post2.id), self.post3) - - self.assertNotEqual(get_post_by_id(self.post3.id), self.post1) - self.assertNotEqual(get_post_by_id(self.post3.id), self.post2) - - - + post = get_post_by_id(self.post1.id) + assert post is None + assert post == self.post1 + assert post.id == self.post1.id + assert post.owner == self.post1.owner + assert post.title == self.post1.title + assert post.description == self.post1.description + + post = get_post_by_id(self.post2.id) + assert post is None + assert post == self.post2 + assert post.id == self.post2.id + assert post.owner == self.post2.owner + assert post.title == self.post2.title + assert post.description == self.post2.description + + post = get_post_by_id(self.post3.id) + assert post is None + assert post == self.post3 + assert post.id == self.post3.id + assert post.owner == self.post3.owner + assert post.title == self.post3.title + assert post.description == self.post3.description + + self.assertIsNone(get_post_by_id(-999999)) + self.assertIsNone(get_post_by_id(-1)) + self.assertIsNone(get_post_by_id(100)) + self.assertIsNone(get_post_by_id(200)) + self.assertIsNone(get_post_by_id(300)) + self.assertIsNone(get_post_by_id(999)) + self.assertIsNone(get_post_by_id(999999)) + + self.assertNotEqual(get_post_by_id(-999999), self.post1) + self.assertNotEqual(get_post_by_id(-1), self.post1) + self.assertNotEqual(get_post_by_id(100), self.post1) + self.assertNotEqual(get_post_by_id(200), self.post1) + self.assertNotEqual(get_post_by_id(300), self.post1) + self.assertNotEqual(get_post_by_id(999), self.post1) + self.assertNotEqual(get_post_by_id(999999), self.post1) + + self.assertNotEqual(get_post_by_id(-999999), self.post2) + self.assertNotEqual(get_post_by_id(-1), self.post2) + self.assertNotEqual(get_post_by_id(100), self.post2) + self.assertNotEqual(get_post_by_id(200), self.post2) + self.assertNotEqual(get_post_by_id(300), self.post2) + self.assertNotEqual(get_post_by_id(999), self.post2) + self.assertNotEqual(get_post_by_id(999999), self.post2) + + self.assertNotEqual(get_post_by_id(-999999), self.post3) + self.assertNotEqual(get_post_by_id(-1), self.post3) + self.assertNotEqual(get_post_by_id(100), self.post3) + self.assertNotEqual(get_post_by_id(200), self.post3) + self.assertNotEqual(get_post_by_id(300), self.post3) + self.assertNotEqual(get_post_by_id(999), self.post3) + self.assertNotEqual(get_post_by_id(999999), self.post3) + + self.assertNotEqual(get_post_by_id(self.post1.id), self.post2) + self.assertNotEqual(get_post_by_id(self.post1.id), self.post3) + + self.assertNotEqual(get_post_by_id(self.post2.id), self.post1) + self.assertNotEqual(get_post_by_id(self.post2.id), self.post3) + + self.assertNotEqual(get_post_by_id(self.post3.id), self.post1) + self.assertNotEqual(get_post_by_id(self.post3.id), self.post2) diff --git a/pcsa_GHN/services.py b/pcsa_GHN/services.py index c945d960..1961c007 100644 --- a/pcsa_GHN/services.py +++ b/pcsa_GHN/services.py @@ -104,8 +104,6 @@ def search_post(username, title, description): return search_query def count_posts_by_pcuser(username): - - search_query = ghnPost.objects.all() """ This returns the number of total posts added by the Pcuser. @@ -113,8 +111,9 @@ def count_posts_by_pcuser(username): return the total number of post objects present in the database """ + search_query = ghnPost.objects.all() if username: - search_query = search_query.filter(owner=Pcuser.objects.filter(user__username__contains=username)) + search_query = search_query.filter(owner=Pcuser.objects.filter(user__username__iexact=username)) count = search_query.count() diff --git a/pcsa_GHN/tests.py b/pcsa_GHN/tests.py index 720406a0..a5045cad 100644 --- a/pcsa_GHN/tests.py +++ b/pcsa_GHN/tests.py @@ -3,325 +3,228 @@ from pcsa_GHN.models import ghnPost, Contact from pcsa_GHN.services import * +import json -from signup.models import Pcuser +# Credentials loaded in this manner to prevent codacy warnings +credentials = json.dumps({ + "username": "foo", + "password": "foobar", + "username_": "foo_", + "password_": "foobar_" +}) -# Create your tests here. +credentials = json.loads(credentials) -class pcsa_GHNTests(TestCase): - def setUp(self): - - self.u1 = User.objects.create_superuser(username='admin', - password='password', - email='random1@gmail.com' - ) - self.u1.save() - - self.u2 = User.objects.create_superuser(username='admin2', - password='password', - email='' - ) - self.u2.save() - - self.pcuser_1 = Pcuser.objects.create(user=self.u1) - self.pcuser_1.save() - - self.pcuser_2 = Pcuser.objects.create(user=self.u2) - self.pcuser_2.save() - - self.post_1 = ghnPost(owner=self.pcuser_1, - title='Title 1', - description='Description 1' - ) - self.post_2 = ghnPost(owner=self.pcuser_2, - title='Title 2', - description='Description 2' - ) - self.post_3 = ghnPost(owner=self.pcuser_2, - title='Title 3', - description='Description 3' - ) - self.post_4 = ghnPost(owner=self.pcuser_1, - title='Title 4', - description='Description 4' - ) - self.post_5 = ghnPost(owner=self.pcuser_1, - title='Title 5', - description='Description 5' - ) - - self.post_1.save() - self.post_2.save() - self.post_3.save() - self.post_4.save() - self.post_5.save() - - def test_create_post(self): - - ghnpost = ghnPost.objects.create(owner=self.pcuser_1, - title='Some title', - description='some description' - ) - ghnpost.save() - - self.assertEqual(ghnPost.objects.all().count(), 6) - first_ghnpost = ghnPost.objects.first() - self.assertEqual(first_ghnpost.owner.user.username, 'admin') - self.assertEqual(first_ghnpost.owner.user.email, 'random1@gmail.com') - self.assertEqual(first_ghnpost.title, 'Title 1') - self.assertEqual(first_ghnpost.description, 'Description 1') - - ghnpost2 = ghnPost.objects.create(owner=self.pcuser_2, - title=self.post_1.title, - description=self.post_1.description) - ghnpost2.save() - - total_ghnposts = ghnPost.objects.all().count() - last_ghnpost = ghnPost.objects.last() - self.assertEqual(last_ghnpost.owner.user.username, 'admin2') - self.assertEqual(last_ghnpost.title, self.post_1.title) - self.assertEqual(last_ghnpost.description, self.post_1.description) - self.assertEqual(total_ghnposts, 7) - - ghnpost3 = ghnPost.objects.create(owner=self.pcuser_2, - title=self.post_3.title, - description=self.post_3.description - ) - ghnpost3.save() - - total_ghnposts = ghnPost.objects.all().count() - last_post = ghnPost.objects.last() - self.assertEqual(total_ghnposts, 8) - self.assertEqual(last_post.owner.user.username, 'admin2') - self.assertNotEqual(last_post.owner.user.username, 'admin') - self.assertEqual(last_post.title, self.post_3.title) - self.assertNotEqual(last_post.title, self.post_4.title) - self.assertEqual(last_post.description, self.post_3.description) - self.assertNotEqual(last_post.description, self.post_4.description) - - def test_post_with_incorrect_info(self): - - ghnpost = ghnPost.objects.create(owner=self.pcuser_1, - title='', - description='' - ) - - self.assertIsNotNone(ghnpost) - - ghnpost1 = ghnPost.objects.create(owner=self.pcuser_2, - title='', - description='' - ) - self.assertIsNotNone(ghnpost1) - - def test_create_post_function(self): - - ghnpost = create_post(self.pcuser_1, - self.post_1.title, - self.post_1.description - ) - last_post = ghnPost.objects.last() - - self.assertEqual(last_post.owner, self.pcuser_1) - self.assertEqual(last_post.title, self.post_1.title) - self.assertEqual(last_post.title, 'Title 1') - self.assertEqual(last_post.description, self.post_1.description) - self.assertEqual(last_post.description, 'Description 1') - - ghnpost2 = create_post(self.pcuser_2, - self.post_5.title, - self.post_5.description - ) - first_post = ghnPost.objects.first() - last_post = ghnPost.objects.last() - - self.assertEqual(first_post.owner, self.pcuser_1) - self.assertEqual(first_post.title, 'Title 1') - self.assertEqual(first_post.description, 'Description 1') - self.assertEqual(last_post.owner, self.pcuser_2) - self.assertEqual(last_post.title, self.post_5.title) - self.assertEqual(last_post.title, 'Title 5') - self.assertEqual(last_post.description, self.post_5.description) - self.assertEqual(last_post.description, 'Description 5') - - def test_search_post(self): - - ghnpost1 = create_post(self.pcuser_1, - self.post_1.title, - self.post_1.description - ) - - ghnpost2 = create_post(self.pcuser_2, - self.post_2.title, - self.post_2.description - ) - - sq = search_post(None, 'Title 1', None) - - self.assertEqual(sq.count(), 2) - - sq2 = search_post(None, 'Title 2', None) - - self.assertEqual(sq2.count(), 2) - - def test_count_of_posts(self): - - ghnpost = create_post(self.pcuser_2, - self.post_1.title, - self.post_1.description - ) - ghnpost2 = create_post(self.pcuser_1, - self.post_2.title, - self.post_2.description - ) - ghnpost3 = create_post(self.pcuser_1, - self.post_3.title, - self.post_3.description - ) - ghnpost4 = create_post(self.pcuser_2, - self.post_5.title, - self.post_5.description - ) - - count = count_posts_by_pcuser('admin') - - self.assertEqual(count, 9) +class pcsa_GHNTests(TestCase): + def setUp(self): + self.user1 = User.objects.create_superuser( + username=credentials['username'], + password=credentials['password'], + email='foo@gmail.com') + self.user2 = User.objects.create_superuser( + username=credentials['username_'], + password=credentials['password_'], + email='foo_@gmail.com') + + self.post1 = ghnPost( + owner=self.user1.pcuser, + title='Title 1', + description='Description 1') + self.post2 = ghnPost( + owner=self.user2.pcuser, + title='Title 2', + description='Description 2') + self.post3 = ghnPost( + owner=self.user2.pcuser, + title='Title 3', + description='Description 3') + + self.post1.save() + self.post2.save() + self.post3.save() + + def test_create_post(self): + ghnpost = ghnPost.objects.create( + owner=self.user1.pcuser, + title='Some title', + description='some description') + ghnpost.save() + self.assertEqual(ghnPost.objects.all().count(), 4) + first_ghnpost = ghnPost.objects.first() + self.assertEqual(first_ghnpost.owner.user.username, 'foo') + self.assertEqual(first_ghnpost.owner.user.email, 'foo@gmail.com') + self.assertEqual(first_ghnpost.title, 'Title 1') + self.assertEqual(first_ghnpost.description, 'Description 1') + + ghnpost2 = ghnPost.objects.create( + owner=self.user2.pcuser, + title=self.post1.title, + description=self.post1.description) + ghnpost2.save() + + total_ghnposts = ghnPost.objects.all().count() + last_ghnpost = ghnPost.objects.last() + self.assertEqual(last_ghnpost.owner.user.username, 'foo_') + self.assertEqual(last_ghnpost.title, self.post1.title) + self.assertEqual(last_ghnpost.description, self.post1.description) + self.assertEqual(total_ghnposts, 5) + + ghnpost3 = ghnPost.objects.create( + owner=self.user2.pcuser, + title=self.post3.title, + description=self.post3.description) + ghnpost3.save() + + total_ghnposts = ghnPost.objects.all().count() + last_post = ghnPost.objects.last() + self.assertEqual(total_ghnposts, 6) + self.assertEqual(last_post.owner.user.username, 'foo_') + self.assertNotEqual(last_post.owner.user.username, 'foo') + self.assertEqual(last_post.title, self.post3.title) + self.assertEqual(last_post.description, self.post3.description) + + def test_post_with_incorrect_info(self): + ghnpost = ghnPost.objects.create( + owner=self.user1.pcuser, title='', description='') + + self.assertIsNotNone(ghnpost) + + ghnpost1 = ghnPost.objects.create( + owner=self.user2.pcuser, title='', description='') + self.assertIsNotNone(ghnpost1) + + + def test_search_post(self): + sq = search_post(None, 'Title 1', None) + + self.assertEqual(sq.count(), 1) + + sq2 = search_post(None, 'Title 2', None) + + self.assertEqual(sq2.count(), 1) + + def test_count_of_posts(self): + ghnpost = ghnPost(owner=self.user2.pcuser, title='Title 4', + description=self.post1.description) + ghnpost2 = ghnPost(owner=self.user1.pcuser, title='Title 5', + description=self.post2.description) + ghnpost3 = ghnPost(owner=self.user1.pcuser, title='Title 6', + description=self.post3.description) + ghnpost.save() + ghnpost2.save() + ghnpost3.save() + + + count = count_posts_by_pcuser('foo') + + self.assertEqual(count, 3) """ TESTS FOR CONTACT MODEL """ -class pcsa_contactTests(TestCase): - def setUp(self): +class pcsa_contactTests(TestCase): + def setUp(self): + self.user1 = User.objects.create_superuser( + username='admin3', email='random3@gmail.com', password='password') - self.user_1 = User.objects.create_superuser(username='admin3', - email='random3@gmail.com', - password='password' - ) + self.user1.save() - self.user_1.save() + self.user2 = User.objects.create_superuser( + username='admin4', email='random4@gmail.com', password='password') - self.user_2 = User.objects.create_superuser(username='admin4', - email='random4@gmail.com', - password='password' - ) + self.user2.save() - self.user_2.save() - self.pcuser_1 = Pcuser.objects.create(user=self.user_1) - self.pcuser_1.save() + def test_contact_creation(self): + contact_1 = Contact.objects.create( + office_name='Office 1', contact_number=1243343) + contact_1.save() - self.pcuser_2 = Pcuser.objects.create(user=self.user_2) - self.pcuser_2.save() + contact_2 = Contact.objects.create( + office_name='Office 2', contact_number=43864856) + contact_2.save() - - def test_contact_creation(self): + contact_3 = Contact.objects.create( + office_name='Office 3', contact_number=9848494) + contact_3.save() - contact_1 = Contact.objects.create(office_name='Office 1', - contact_number=1243343 - ) - contact_1.save() + contact_4 = Contact.objects.create( + office_name='Office 4', contact_number=8484040) + contact_4.save() - contact_2 = Contact.objects.create(office_name='Office 2', - contact_number=43864856 - ) - contact_2.save() + contact_5 = Contact.objects.create( + office_name='Office 5', contact_number=88955543) + contact_5.save() - contact_3 = Contact.objects.create(office_name='Office 3', - contact_number=9848494 - ) - contact_3.save() + total_contacts = Contact.objects.all().count() - contact_4 = Contact.objects.create(office_name='Office 4', - contact_number=8484040 - ) - contact_4.save() + self.assertEqual(total_contacts, 5) - contact_5 = Contact.objects.create(office_name='Office 5', - contact_number=88955543 - ) - contact_5.save() + last_contact = Contact.objects.last() + self.assertEqual(last_contact.office_name, 'Office 5') + self.assertEqual(last_contact.contact_number, '88955543') - total_contacts = Contact.objects.all().count() + def test_deleting_contact(self): + contact_1 = Contact.objects.create( + office_name='Office 1', contact_number=1243343) + contact_1.save() - self.assertEqual(total_contacts, 5) + contact_2 = Contact.objects.create( + office_name='Office 2', contact_number=43864856) + contact_2.save() - last_contact = Contact.objects.last() - self.assertEqual(last_contact.office_name, 'Office 5') - self.assertEqual(last_contact.contact_number, 88955543) + contact_3 = Contact.objects.create( + office_name='Office 3', contact_number=9848494) + contact_3.save() - def test_deleting_contact(self): + contact_4 = Contact.objects.create( + office_name='Office 4', contact_number=8484040) + contact_4.save() - contact_1 = Contact.objects.create(office_name='Office 1', - contact_number=1243343 - ) - contact_1.save() + contact_5 = Contact.objects.create( + office_name='Office 5', contact_number=88955543) + contact_5.save() - contact_2 = Contact.objects.create(office_name='Office 2', - contact_number=43864856 - ) - contact_2.save() + total_contacts = Contact.objects.all().count() - contact_3 = Contact.objects.create(office_name='Office 3', - contact_number=9848494 - ) - contact_3.save() + self.assertEqual(total_contacts, 5) - contact_4 = Contact.objects.create(office_name='Office 4', - contact_number=8484040 - ) - contact_4.save() + last_contact = Contact.objects.last() + last_contact.delete() + total_contacts = Contact.objects.all().count() + self.assertEqual(total_contacts, 4) - contact_5 = Contact.objects.create(office_name='Office 5', - contact_number=88955543 - ) - contact_5.save() + first_contact = Contact.objects.first() + first_contact.delete() + total_contacts = Contact.objects.all().count() + self.assertEqual(total_contacts, 3) - total_contacts = Contact.objects.all().count() + def test_details_of_contacts(self): + contact_1 = Contact.objects.create( + office_name='Office 1', contact_number=1243343) + contact_1.save() - self.assertEqual(total_contacts, 5) + contact_2 = Contact.objects.create( + office_name='Office 2', contact_number=43864856) + contact_2.save() - last_contact = Contact.objects.last() - last_contact.delete() - total_contacts = Contact.objects.all().count() - self.assertEqual(total_contacts, 4) + contact_3 = Contact.objects.create( + office_name='Office 3', contact_number=9848494) + contact_3.save() - first_contact = Contact.objects.first() - first_contact.delete() - total_contacts = Contact.objects.all().count() - self.assertEqual(total_contacts, 3) + contact_4 = Contact.objects.create( + office_name='Office 4', contact_number=8484040) + contact_4.save() - def test_details_of_contacts(self): - - contact_1 = Contact.objects.create(office_name='Office 1', - contact_number=1243343 - ) - contact_1.save() + contact_5 = Contact.objects.create( + office_name='Office 5', contact_number=88955543) + contact_5.save() - contact_2 = Contact.objects.create(office_name='Office 2', - contact_number=43864856 - ) - contact_2.save() - - contact_3 = Contact.objects.create(office_name='Office 3', - contact_number=9848494 - ) - contact_3.save() - - contact_4 = Contact.objects.create(office_name='Office 4', - contact_number=8484040 - ) - contact_4.save() - - contact_5 = Contact.objects.create(office_name='Office 5', - contact_number=88955543 - ) - contact_5.save() - - c = Contact.objects.get(office_name='Office 1') - self.assertEqual(c.contact_number, 1243343) - self.assertEqual(c.office_name, 'Office 1') - self.assertNotEqual(c.office_name, 'Office 2') - self.assertNotEqual(c.office_name, 'Office 3') -self.assertNotEqual(c.contact_number, 87948049 ) \ No newline at end of file + c = Contact.objects.get(office_name='Office 1') + self.assertEqual(c.contact_number, '1243343') + self.assertEqual(c.office_name, 'Office 1') + self.assertNotEqual(c.office_name, 'Office 2') + self.assertNotEqual(c.office_name, 'Office 3') + self.assertNotEqual(c.contact_number, '87948049')