Skip to content

Commit

Permalink
Add user model tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dylantjb committed Dec 2, 2022
1 parent 93b112b commit 04c4f08
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
branch = True
omit = *__init__.py,manage.py,*migrations*,*settings.py,*tests*
31 changes: 31 additions & 0 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Django CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.8]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
python manage.py test
45 changes: 0 additions & 45 deletions blogs/migrations/0001_initial.py

This file was deleted.

48 changes: 0 additions & 48 deletions blogs/migrations/0002_alter_user_bio_alter_user_email_and_more.py

This file was deleted.

Empty file removed blogs/migrations/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions blogs/tests/fixtures/default_user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"model": "blogs.user",
"pk": 1,
"fields": {
"first_name": "John",
"last_name": "Smith",
"username": "johnsmith",
"email": "[email protected]",
"bio": "Hello, I'm John Smith.",
"password": "pbkdf2_sha256$260000$4BNvFuAWoTT1XVU8D6hCay$KqDCG+bHl8TwYcvA60SGhOMluAheVOnF1PMz0wClilc="
}
}
]
38 changes: 38 additions & 0 deletions blogs/tests/fixtures/other_users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"model": "blogs.user",
"pk": 2,
"fields": {
"first_name": "Jane",
"last_name": "Doe",
"username": "janedoe",
"email": "[email protected]",
"password": "pbkdf2_sha256$260000$iGkOGC00dvNVncr3rtGWdL$IkIyONd+R4mPoN4APRvVqk5tGVLa3xocSQ2qJur+VZo=",
"bio": "Hi, my name is Jane."
}
},
{
"model": "blogs.user",
"pk": 3,
"fields": {
"first_name": "Adam",
"last_name": "Johnson",
"username": "adamjohnson",
"email": "[email protected]",
"password": "pbkdf2_sha256$260000$iGkOGC00dvNVncr3rtGWdL$IkIyONd+R4mPoN4APRvVqk5tGVLa3xocSQ2qJur+VZo=",
"bio": "Hello, my name is Adam."
}
},
{
"model": "blogs.user",
"pk": 4,
"fields": {
"first_name": "James",
"last_name": "Brown",
"username": "jamesbrown",
"email": "[email protected]",
"password": "pbkdf2_sha256$260000$iGkOGC00dvNVncr3rtGWdL$IkIyONd+R4mPoN4APRvVqk5tGVLa3xocSQ2qJur+VZo=",
"bio": "Hello, my name is James."
}
}
]
121 changes: 121 additions & 0 deletions blogs/tests/test-user-model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""Unit tests for the User model"""
from django.core.exceptions import ValidationError
from django.test import TestCase

from blogs.models import User


class UserModelTestCase(TestCase):
"""Unit tests of the user model."""

fixtures = [
"blogs/tests/fixtures/default_user.json",
"blogs/tests/fixtures/other_users.json",
]

def setUp(self):
self.user = User.objects.get(username="johnsmith")

def _assert_user_is_valid(self):
try:
self.user.full_clean()
except ValidationError:
self.fail("Test user should be valid")

def _assert_user_is_invalid(self):
with self.assertRaises(ValidationError):
self.user.full_clean()

def test_username_must_have_at_least_three_alphanumericals(self):
"""Test username must have at least three alphanumericals."""
self.user.username = "ab"
self._assert_user_is_invalid()

def test_username_cannot_be_blank(self):
"""Test username cannot be blank."""
self.user.username = ""
self._assert_user_is_invalid()

def test_username_can_be_30_characters_long(self):
"""Test username can be 30 characters long."""
self.user.username = "x" * 30
self._assert_user_is_valid()

def test_username_cannot_be_over_30_characters_long(self):
"""Test username cannot be over 30 characters long."""
self.user.username = "x" * 31
self._assert_user_is_invalid()

def test_username_must_be_unique(self):
"""Test username must be unique."""
second_user = User.objects.get(username="janedoe")
self.user.username = second_user.username
self._assert_user_is_invalid()

def test_username_has_numbers(self):
"""Test username has numbers."""
self.user.username = "j0hnd03"
self._assert_user_is_valid()

def test_first_name_is_not_blank(self):
"""Test first name must not be blank."""
self.user.first_name = ""
self._assert_user_is_invalid()

def test_first_name_is_not_unique(self):
"""Test first name is not unique."""
second_user = User.objects.get(username="janedoe")
self.user.first_name = second_user.first_name
self._assert_user_is_valid()

def test_first_name_can_have_less_than_50_characters(self):
"""Test first name can have less than 50 characters."""
self.user.first_name = "x" * 50
self._assert_user_is_valid()

def test_first_name_cannot_have_more_than_50_characters(self):
"""Test first name cannot have more than 50 characters."""
self.user.first_name = "x" * 51
self._assert_user_is_invalid()

def test_last_name_is_not_blank(self):
"""Test last name is not blank."""
self.user.last_name = ""
self._assert_user_is_invalid()

def test_last_name_is_not_unique(self):
"""Test last name is not unique."""
second_user = User.objects.get(username="janedoe")
self.user.last_name = second_user.last_name
self._assert_user_is_valid()

def test_last_name_can_have_less_than_50_characters(self):
"""Test last name can have less than 50 characters."""
self.user.last_name = "x" * 50
self._assert_user_is_valid()

def test_last_name_cannot_have_more_than_50_characters(self):
"""Test last name cannot have more than 50 characters."""
self.user.last_name = "x" * 51
self._assert_user_is_invalid()

def test_bio_can_be_blank(self):
"""Test bio can be blank."""
self.user.bio = ""
self._assert_user_is_valid()

def test_bio_may_not_be_unique(self):
"""Test bio may not be unique."""
second_user = User.objects.get(username="janedoe")
self.user.bio = second_user.bio
self._assert_user_is_valid()

def test_bio_may_have_520_chars(self):
"""Test bio may have 520 characters."""
self.user.bio = "x" * 520
self._assert_user_is_valid()

def test_bio_cannot_have_over_520_chars(self):
"""Test bio cannot have over 520 characters."""
self.user.bio = "x" * 521
self._assert_user_is_invalid()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ asgiref==3.5.2
backports.zoneinfo==0.2.1
Django==4.1.3
sqlparse==0.4.3
coverage==6.5.0

0 comments on commit 04c4f08

Please sign in to comment.