From 0dd0634b45b1f2144c01025a31d660af4d2dcbd9 Mon Sep 17 00:00:00 2001 From: Kat Olaguer Date: Sat, 20 Jan 2024 01:21:14 -0800 Subject: [PATCH 1/3] Added coverage.py and increased coverage --- .gitignore | 1 + poetry.lock | 66 ++++++++++++++++++++++++- pyproject.toml | 1 + users/test.py | 129 +++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 181 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 9671b23..b705f60 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ node_modules **/dist/ /staticfiles/ +.coverage diff --git a/poetry.lock b/poetry.lock index baf8cbb..9050c34 100644 --- a/poetry.lock +++ b/poetry.lock @@ -175,6 +175,70 @@ files = [ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, ] +[[package]] +name = "coverage" +version = "7.4.0" +description = "Code coverage measurement for Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "coverage-7.4.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:36b0ea8ab20d6a7564e89cb6135920bc9188fb5f1f7152e94e8300b7b189441a"}, + {file = "coverage-7.4.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:0676cd0ba581e514b7f726495ea75aba3eb20899d824636c6f59b0ed2f88c471"}, + {file = "coverage-7.4.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d0ca5c71a5a1765a0f8f88022c52b6b8be740e512980362f7fdbb03725a0d6b9"}, + {file = "coverage-7.4.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:a7c97726520f784239f6c62506bc70e48d01ae71e9da128259d61ca5e9788516"}, + {file = "coverage-7.4.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:815ac2d0f3398a14286dc2cea223a6f338109f9ecf39a71160cd1628786bc6f5"}, + {file = "coverage-7.4.0-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:80b5ee39b7f0131ebec7968baa9b2309eddb35b8403d1869e08f024efd883566"}, + {file = "coverage-7.4.0-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:5b2ccb7548a0b65974860a78c9ffe1173cfb5877460e5a229238d985565574ae"}, + {file = "coverage-7.4.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:995ea5c48c4ebfd898eacb098164b3cc826ba273b3049e4a889658548e321b43"}, + {file = "coverage-7.4.0-cp310-cp310-win32.whl", hash = "sha256:79287fd95585ed36e83182794a57a46aeae0b64ca53929d1176db56aacc83451"}, + {file = "coverage-7.4.0-cp310-cp310-win_amd64.whl", hash = "sha256:5b14b4f8760006bfdb6e08667af7bc2d8d9bfdb648351915315ea17645347137"}, + {file = "coverage-7.4.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:04387a4a6ecb330c1878907ce0dc04078ea72a869263e53c72a1ba5bbdf380ca"}, + {file = "coverage-7.4.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:ea81d8f9691bb53f4fb4db603203029643caffc82bf998ab5b59ca05560f4c06"}, + {file = "coverage-7.4.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:74775198b702868ec2d058cb92720a3c5a9177296f75bd97317c787daf711505"}, + {file = "coverage-7.4.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:76f03940f9973bfaee8cfba70ac991825611b9aac047e5c80d499a44079ec0bc"}, + {file = "coverage-7.4.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:485e9f897cf4856a65a57c7f6ea3dc0d4e6c076c87311d4bc003f82cfe199d25"}, + {file = "coverage-7.4.0-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:6ae8c9d301207e6856865867d762a4b6fd379c714fcc0607a84b92ee63feff70"}, + {file = "coverage-7.4.0-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:bf477c355274a72435ceb140dc42de0dc1e1e0bf6e97195be30487d8eaaf1a09"}, + {file = "coverage-7.4.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:83c2dda2666fe32332f8e87481eed056c8b4d163fe18ecc690b02802d36a4d26"}, + {file = "coverage-7.4.0-cp311-cp311-win32.whl", hash = "sha256:697d1317e5290a313ef0d369650cfee1a114abb6021fa239ca12b4849ebbd614"}, + {file = "coverage-7.4.0-cp311-cp311-win_amd64.whl", hash = "sha256:26776ff6c711d9d835557ee453082025d871e30b3fd6c27fcef14733f67f0590"}, + {file = "coverage-7.4.0-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:13eaf476ec3e883fe3e5fe3707caeb88268a06284484a3daf8250259ef1ba143"}, + {file = "coverage-7.4.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:846f52f46e212affb5bcf131c952fb4075b55aae6b61adc9856222df89cbe3e2"}, + {file = "coverage-7.4.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:26f66da8695719ccf90e794ed567a1549bb2644a706b41e9f6eae6816b398c4a"}, + {file = "coverage-7.4.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:164fdcc3246c69a6526a59b744b62e303039a81e42cfbbdc171c91a8cc2f9446"}, + {file = "coverage-7.4.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:316543f71025a6565677d84bc4df2114e9b6a615aa39fb165d697dba06a54af9"}, + {file = "coverage-7.4.0-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:bb1de682da0b824411e00a0d4da5a784ec6496b6850fdf8c865c1d68c0e318dd"}, + {file = "coverage-7.4.0-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:0e8d06778e8fbffccfe96331a3946237f87b1e1d359d7fbe8b06b96c95a5407a"}, + {file = "coverage-7.4.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:a56de34db7b7ff77056a37aedded01b2b98b508227d2d0979d373a9b5d353daa"}, + {file = "coverage-7.4.0-cp312-cp312-win32.whl", hash = "sha256:51456e6fa099a8d9d91497202d9563a320513fcf59f33991b0661a4a6f2ad450"}, + {file = "coverage-7.4.0-cp312-cp312-win_amd64.whl", hash = "sha256:cd3c1e4cb2ff0083758f09be0f77402e1bdf704adb7f89108007300a6da587d0"}, + {file = "coverage-7.4.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:e9d1bf53c4c8de58d22e0e956a79a5b37f754ed1ffdbf1a260d9dcfa2d8a325e"}, + {file = "coverage-7.4.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:109f5985182b6b81fe33323ab4707011875198c41964f014579cf82cebf2bb85"}, + {file = "coverage-7.4.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3cc9d4bc55de8003663ec94c2f215d12d42ceea128da8f0f4036235a119c88ac"}, + {file = "coverage-7.4.0-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:cc6d65b21c219ec2072c1293c505cf36e4e913a3f936d80028993dd73c7906b1"}, + {file = "coverage-7.4.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5a10a4920def78bbfff4eff8a05c51be03e42f1c3735be42d851f199144897ba"}, + {file = "coverage-7.4.0-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:b8e99f06160602bc64da35158bb76c73522a4010f0649be44a4e167ff8555952"}, + {file = "coverage-7.4.0-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:7d360587e64d006402b7116623cebf9d48893329ef035278969fa3bbf75b697e"}, + {file = "coverage-7.4.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:29f3abe810930311c0b5d1a7140f6395369c3db1be68345638c33eec07535105"}, + {file = "coverage-7.4.0-cp38-cp38-win32.whl", hash = "sha256:5040148f4ec43644702e7b16ca864c5314ccb8ee0751ef617d49aa0e2d6bf4f2"}, + {file = "coverage-7.4.0-cp38-cp38-win_amd64.whl", hash = "sha256:9864463c1c2f9cb3b5db2cf1ff475eed2f0b4285c2aaf4d357b69959941aa555"}, + {file = "coverage-7.4.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:936d38794044b26c99d3dd004d8af0035ac535b92090f7f2bb5aa9c8e2f5cd42"}, + {file = "coverage-7.4.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:799c8f873794a08cdf216aa5d0531c6a3747793b70c53f70e98259720a6fe2d7"}, + {file = "coverage-7.4.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e7defbb9737274023e2d7af02cac77043c86ce88a907c58f42b580a97d5bcca9"}, + {file = "coverage-7.4.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:a1526d265743fb49363974b7aa8d5899ff64ee07df47dd8d3e37dcc0818f09ed"}, + {file = "coverage-7.4.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bf635a52fc1ea401baf88843ae8708591aa4adff875e5c23220de43b1ccf575c"}, + {file = "coverage-7.4.0-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:756ded44f47f330666843b5781be126ab57bb57c22adbb07d83f6b519783b870"}, + {file = "coverage-7.4.0-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:0eb3c2f32dabe3a4aaf6441dde94f35687224dfd7eb2a7f47f3fd9428e421058"}, + {file = "coverage-7.4.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:bfd5db349d15c08311702611f3dccbef4b4e2ec148fcc636cf8739519b4a5c0f"}, + {file = "coverage-7.4.0-cp39-cp39-win32.whl", hash = "sha256:53d7d9158ee03956e0eadac38dfa1ec8068431ef8058fe6447043db1fb40d932"}, + {file = "coverage-7.4.0-cp39-cp39-win_amd64.whl", hash = "sha256:cfd2a8b6b0d8e66e944d47cdec2f47c48fef2ba2f2dff5a9a75757f64172857e"}, + {file = "coverage-7.4.0-pp38.pp39.pp310-none-any.whl", hash = "sha256:c530833afc4707fe48524a44844493f36d8727f04dcce91fb978c414a8556cc6"}, + {file = "coverage-7.4.0.tar.gz", hash = "sha256:707c0f58cb1712b8809ece32b68996ee1e609f71bd14615bd8f87a1293cb610e"}, +] + +[package.extras] +toml = ["tomli"] + [[package]] name = "defusedxml" version = "0.7.1" @@ -1298,4 +1362,4 @@ testing = ["Pillow (>=9.1.0,<11.0.0)", "Wand (>=0.6,<1.0)", "black (==22.3.0)", [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "bf12aa81c6892567f64563734b7fadbe4226f5a8e0838799a7982977875bb59b" +content-hash = "4b9efddf98cc2719a9b336ef25dcf2e7e5aa3ff7ea7edfec15ec37bdb08b78a0" diff --git a/pyproject.toml b/pyproject.toml index e2a3aba..82b0e31 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,3 +38,4 @@ pytest-django = "^4.7.0" pytest-timeout = "^2.2.0" pytest-factoryboy = "^2.6.0" django-browser-reload = "^1.12.1" +coverage = "^7.4.0" diff --git a/users/test.py b/users/test.py index 7bc08de..f7af588 100644 --- a/users/test.py +++ b/users/test.py @@ -1,5 +1,6 @@ import pytest from django.contrib.auth import get_user_model +from django.db import IntegrityError from .factory import UserFactory @@ -8,22 +9,120 @@ @pytest.mark.django_db class TestUser: + """ + Tests the overriden User Model. + """ + def test_user_factory(self): user = UserFactory() + assert isinstance(user, User) - def test_user_model(self): - user1 = UserFactory() - user2 = UserFactory() - - assert user1.username == "user1" - assert user1.first_name == "first_name1" - assert user1.last_name == "last_name1" - assert user1.email == "email1@hotosm.com" - assert user1.check_password("password") - - assert user2.username == "user2" - assert user2.first_name == "first_name2" - assert user2.last_name == "last_name2" - assert user2.email == "email2@hotosm.com" - assert user2.check_password("password") + def test_user_str(self): + user = UserFactory(username="testuser") + + assert str(user) == "testuser" + + def test_user_is_not_anonymous(self): + user = UserFactory() + + assert not user.is_anonymous + + def test_user_is_authenticated(self): + user = UserFactory() + + assert user.is_authenticated + + def test_user_get_full_name(self): + user = UserFactory() + + assert user.get_full_name() == f"{user.first_name} {user.last_name}" + + def test_user_get_short_name(self): + user = UserFactory() + + assert user.get_short_name() == user.first_name + + def test_user_password_set_and_check(self): + user = UserFactory() + + user.set_password("newpassword") + + assert user.check_password("newpassword") + + def test_user_has_perm(self): + user = UserFactory() + + user.is_superuser = True + + assert user.has_perm("auth.view_user") + + def test_user_has_module_perms(self): + user = UserFactory() + + user.is_superuser = True + + assert user.has_module_perms("auth") + + def test_duplicate_username(self): + UserFactory(username="testuser") + + with pytest.raises(IntegrityError): + UserFactory(username="testuser") + + def test_superuser_and_staff_properties(self): + user = UserFactory(is_superuser=True, is_staff=True) + + assert user.is_superuser + assert user.is_staff + + def test_email_field(self): + user = UserFactory(email="test@example.com") + + assert user.email == "test@example.com" + + def test_missing_email(self): + with pytest.raises( + IntegrityError, + match=( + 'null value in column "email" of relation "users_user" violates not-null constraint' + ), + ): + UserFactory(email=None) + + def test_missing_username(self): + with pytest.raises( + IntegrityError, + match=( + 'null value in column "username" of relation "users_user" ' + "violates not-null constraint" + ), + ): + UserFactory(username=None) + + def test_get_username(self): + user = UserFactory(username="testuser") + + assert user.get_username() == "testuser" + + def test_is_active(self): + user = UserFactory(is_active=True) + + assert user.is_active + + def test_is_not_staff(self): + user = UserFactory(is_staff=False) + + assert not user.is_staff + + def test_is_not_superuser(self): + user = UserFactory(is_superuser=False) + + assert not user.is_superuser + + def test_set_unusable_password(self): + user = UserFactory() + + user.set_unusable_password() + + assert not user.has_usable_password() From 4d0b5e8929390d838719b0135f3da68a2334371e Mon Sep 17 00:00:00 2001 From: Kat Olaguer Date: Sat, 20 Jan 2024 01:49:59 -0800 Subject: [PATCH 2/3] Removed global test line --- users/test.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/users/test.py b/users/test.py index f7af588..f6640d1 100644 --- a/users/test.py +++ b/users/test.py @@ -4,8 +4,6 @@ from .factory import UserFactory -User = get_user_model() - @pytest.mark.django_db class TestUser: @@ -16,7 +14,7 @@ class TestUser: def test_user_factory(self): user = UserFactory() - assert isinstance(user, User) + assert isinstance(user, get_user_model()) def test_user_str(self): user = UserFactory(username="testuser") From 82321845f9a791b4cc025e886502bd56ca98a9a2 Mon Sep 17 00:00:00 2001 From: Kat Olaguer Date: Sun, 21 Jan 2024 21:08:49 -0800 Subject: [PATCH 3/3] Added coverage test to Makefile --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 76abd71..a1a5936 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,8 @@ build-prod: test: @docker compose -f docker-compose.dev.yml run --rm web pytest +cov: + @docker compose -f docker-compose.dev.yml run --rm web coverage run -m pytest up: @docker compose -f docker-compose.dev.yml up -d