chore: correct tests so they function and cleanup correctly

ref: #731
This commit is contained in:
2025-04-23 18:32:47 +09:30
parent 7c9819efd1
commit daf30de835
10 changed files with 85 additions and 32 deletions

17
.vscode/launch.json vendored
View File

@ -88,6 +88,21 @@
],
"console": "integratedTerminal",
"justMyCode": false
}
},
{
"name": "Python Debugger: Local Attach",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
]
}

View File

@ -4,6 +4,10 @@
"cSpell.enableFiletypes": [
"!python"
],
"python.testing.pytestArgs": [
"--override-ini", "addopts=",
"app",
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"testing.coverageToolbarEnabled": true,

View File

@ -2,6 +2,7 @@ import pytest
from unittest.mock import patch
from django.apps import apps
from django.core.exceptions import ObjectDoesNotExist
from django.test import TestCase
from django.urls.exceptions import NoReverseMatch
@ -633,7 +634,7 @@ class TenancyObjectTest(
@classmethod
def setUpTestData(self):
class TestModel(TenancyObject):
class MockTenancyObjectModel(TenancyObject):
class Meta:
@ -641,9 +642,23 @@ class TenancyObjectTest(
verbose_name = 'Test Model'
self.model = MockTenancyObjectModel
self.model = TestModel
self.item = TestModel()
self.item = MockTenancyObjectModel()
@classmethod
def tearDownClass(self):
self.item = None
del apps.all_models['access']['mocktenancyobjectmodel']
self.model = None
super().tearDownClass()

View File

@ -108,17 +108,17 @@ class APIFieldsTestCases:
user = request.cls.view_user
)
yield
yield
with django_db_blocker.unblock():
with django_db_blocker.unblock():
team_user.delete()
team_user.delete()
view_team.delete()
view_team.delete()
request.cls.view_user.delete()
request.cls.view_user.delete()
del request.cls.kwargs_create_item
del request.cls.kwargs_create_item
@pytest.fixture( scope = 'class')
@ -139,9 +139,9 @@ class APIFieldsTestCases:
request.cls.api_data = response.data
yield
yield
del request.cls.url_view_kwargs['pk']
del request.cls.url_view_kwargs['pk']

View File

@ -9,6 +9,14 @@ from access.models.organization import Organization
def pytest_configure(config):
print("\n--- Pytest Launch Arguments ---")
print(f"Command-line arguments: {config.invocation_params.args}")
print(f"Config file options: {config.getini('addopts')}")
print("\n-------------------------------")
def pytest_pycollect_makeitem(collector, name, obj):
"""PyTest Test Creation
@ -81,13 +89,13 @@ def create_model(request, django_db_blocker):
**request.cls.kwargs_create_item
)
request.cls.item = item
request.cls.item = item
yield item
with django_db_blocker.unblock():
request.cls.item.delete()
item.delete()

View File

@ -1,3 +1,4 @@
from django.apps import apps
from django.db import models
from django.test import TestCase
@ -134,7 +135,7 @@ class SaveHistoryMixinTest(
@classmethod
def setUpTestData(self):
class MockModel(self.model):
class MockSaveHistoryModel(SaveHistory):
class Meta:
@ -142,7 +143,15 @@ class SaveHistoryMixinTest(
verbose_name = 'unit test'
self.item = MockModel()
self.item = MockSaveHistoryModel()
@classmethod
def tearDownClass(self):
self.item = None
del apps.all_models['core']['mocksavehistorymodel']

View File

@ -23,37 +23,36 @@ class APITestCases(
@pytest.fixture( scope = 'class')
def setup_model(self, request, django_db_blocker,
model,
organization_one,
):
with django_db_blocker.unblock():
request.cls.entity_user = Entity.objects.create(
organization = organization_one,
organization = request.cls.organization,
model_notes = 'asdas'
)
project = Project.objects.create(
organization = organization_one,
organization = request.cls.organization,
name = 'project'
)
parent_ticket = request.cls.model.objects.create(
organization = organization_one,
organization = request.cls.organization,
title = 'parent ticket',
description = 'bla bla',
opened_by = request.cls.view_user,
)
project_milestone = ProjectMilestone.objects.create(
organization = organization_one,
organization = request.cls.organization,
name = 'project milestone one',
project = project
)
request.cls.kwargs_create_item.update({
'category': TicketCategory.objects.create(
organization = organization_one,
organization = request.cls.organization,
name = 'a category'
),
'opened_by': request.cls.view_user,
@ -78,12 +77,12 @@ class APITestCases(
request.cls.entity_user.delete()
parent_ticket.delete()
project_milestone.delete()
project.delete()
parent_ticket.delete()
request.cls.kwargs_create_item['category'].delete()
if 'ticket_model' in request.cls.url_view_kwargs:

View File

@ -32,8 +32,6 @@ class ProjectMilestonePermissions(TestCase, ModelPermissions):
url_name_delete = '_project_milestone_delete'
url_delete_response = reverse('Project Management:_project_view', kwargs={'pk': 1}) + '?tab=milestones'
@classmethod
def setUpTestData(self):
"""Setup Test
@ -57,6 +55,8 @@ class ProjectMilestonePermissions(TestCase, ModelPermissions):
organization = self.organization
)
self.url_delete_response = reverse('Project Management:_project_view', kwargs={'pk': self.project.id}) + '?tab=milestones'
self.item = self.model.objects.create(
name = 'test_item_' + self.model._meta.model_name,
organization = self.organization,

View File

@ -39,14 +39,14 @@ docs: docs-lint
lint: markdown-mkdocs-lint
test:
pytest -s --cov --cov-branch --cov-report term --cov-report xml:artifacts/coverage_unit_functional.xml --cov-report html:artifacts/coverage/unit_functional/ --junit-xml=artifacts/unit_functional.JUnit.xml app/**/tests/unit app/**/tests/functional
pytest --cov-report xml:artifacts/coverage_unit_functional.xml --cov-report html:artifacts/coverage/unit_functional/ --junit-xml=artifacts/unit_functional.JUnit.xml app/**/tests/unit app/**/tests/functional
test-functional:
pytest -s --cov --cov-branch --cov-report term --cov-report xml:artifacts/coverage_functional.xml --cov-report html:artifacts/coverage/functional/ --junit-xml=artifacts/functional.JUnit.xml app/**/tests/functional
pytest --cov-report xml:artifacts/coverage_functional.xml --cov-report html:artifacts/coverage/functional/ --junit-xml=artifacts/functional.JUnit.xml app/**/tests/functional
test-unit:
pytest -s --cov --cov-branch --cov-report term --cov-report xml:artifacts/coverage_unit.xml --cov-report html:artifacts/coverage/unit/ --junit-xml=artifacts/unit.JUnit.xml app/**/tests/unit
pytest --cov-report xml:artifacts/coverage_unit.xml --cov-report html:artifacts/coverage/unit/ --junit-xml=artifacts/unit.JUnit.xml app/**/tests/unit

View File

@ -5,7 +5,6 @@ disable = [
enable = [
"variables"
]
source-roots = [
"app/"
]
@ -16,11 +15,15 @@ allow-global-unused-variables = true
init-import = true
[tool.pytest.ini_options]
addopts = '-s app'
addopts = '--cov --cov-branch --cov-report term --tb=long --verbosity=2 --full-trace --showlocals app'
DJANGO_SETTINGS_MODULE = 'app.settings'
junit_family = 'xunit2'
# -- recommended but optional:
# python_files = 'tests.py test_*.py *_tests.py'
log_cli = true
@ -32,4 +35,4 @@ log_cli_date_format = '%Y-%m-%d %H:%M:%S'
# 'tests/*.py',
# # 'tests/functional/**/*.py',
# # 'tests/unit/**/*.py',
# ]
# ]