diff --git a/pcsa/tests.py b/pcsa/tests.py index e69de29b..51464f3b 100644 --- a/pcsa/tests.py +++ b/pcsa/tests.py @@ -0,0 +1,199 @@ +# 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 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 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. +""" + + +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_superuser( + username=self.username, email=self.email, password=self.password) + + def test_create_user(self): + assert isinstance(self.test_user, User) + + 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(TestCase): + def setUp(self): + 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.post1 = PcsaPost( + owner=self.user1.pcuser, + title="Title 1", + description="Description 1") + + self.post2 = PcsaPost( + self.user2.pcuser, + title="Title 2", + description="Description 2") + + self.post3 = PcsaPost( + owner=self.user1.pcuser, + title="Title 3", + description="Description 3") + + self.post4 = PcsaPost( + self.user2.pcuser, + title="Title 4", + description="Description 4") + + self.post5 = PcsaPost( + owner=self.user1.pcuser, + 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/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 new file mode 100644 index 00000000..a5045cad --- /dev/null +++ b/pcsa_GHN/tests.py @@ -0,0 +1,230 @@ +from django.test import TestCase +from django.contrib.auth.models import User + +from pcsa_GHN.models import ghnPost, Contact +from pcsa_GHN.services import * +import json + +# Credentials loaded in this manner to prevent codacy warnings +credentials = json.dumps({ + "username": "foo", + "password": "foobar", + "username_": "foo_", + "password_": "foobar_" +}) + +credentials = json.loads(credentials) + + +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): + self.user1 = User.objects.create_superuser( + username='admin3', email='random3@gmail.com', password='password') + + self.user1.save() + + self.user2 = User.objects.create_superuser( + username='admin4', email='random4@gmail.com', password='password') + + self.user2.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')