Skip to content

Commit

Permalink
test: raise_test_coverage_to_100%
Browse files Browse the repository at this point in the history
  • Loading branch information
saladgg committed Aug 28, 2022
1 parent d76704a commit 8a1dfa7
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 6 deletions.
Empty file added apps/core/tests/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions apps/core/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from unittest import TestCase

import pytest
from django.conf import settings
from faker import Faker
from model_bakery import baker

from apps.sql_data.models import SQLDatabaseSource
from apps.users.models import User

fake = Faker()
pytestmark = pytest.mark.django_db


class AuditBaseModelTest(TestCase):
"""Test for AuditBase."""

def setUp(self):
self.user = baker.make(settings.AUTH_USER_MODEL, email=fake.email())
super().setUp()

def test_created_and_created_by(self):
sql_upload_metadata = SQLDatabaseSource(
pk=None,
name="name",
description="myql db",
database_name="sql",
database_vendor="MySQL",
)
sql_upload_metadata.save(user=User.objects.get(pk=self.user.pk))
38 changes: 36 additions & 2 deletions apps/dashboards/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from django.urls import reverse
from faker import Faker
from model_bakery import baker
from rest_framework.test import APIRequestFactory, APITestCase
from rest_framework.test import (
APIRequestFactory,
APITestCase,
force_authenticate,
)

from apps.common.tests.test_common import LoggedInMixin
from apps.dashboards.apiviews import DashboardViewSet, VisualizationViewSet
from apps.dashboards.models import Dashboard, Visualization

fake = Faker()
factory = APIRequestFactory()


class TestVisualizationViewSet(LoggedInMixin, APITestCase):
Expand Down Expand Up @@ -92,3 +96,33 @@ def test_delete(self):
url = reverse("dashboard-detail", kwargs={"pk": fake_data.id})
response = self.client.delete(url)
assert response.status_code == 204


class Test_Dashboard_and_VisualizationViewSet_non_Staff_Request(
LoggedInMixin, APITestCase
):
def setUp(self):
self.factory = APIRequestFactory()
super().setUp()

def test_dashboard_list_view_as_non_staff(self):
request = self.factory.get(
reverse("dashboard-list"),
)
request.user = self.user
request.user.is_staff = False
view = DashboardViewSet.as_view(actions={"get": "list"})
force_authenticate(request, user=self.user)
response = view(request)
assert response.status_code == 200

def test_visualization_list_view_as_non_staff(self):
request = self.factory.get(
reverse("visualization-list"),
)
request.user = self.user
request.user.is_staff = False
view = VisualizationViewSet.as_view(actions={"get": "list"})
force_authenticate(request, user=self.user)
response = view(request)
assert response.status_code == 200
24 changes: 24 additions & 0 deletions apps/dashboards/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from faker import Faker
from model_bakery import baker
from rest_framework.test import APITestCase

from apps.common.tests.test_common import LoggedInMixin
from apps.dashboards.models import Dashboard, Visualization

pytestmark = pytest.mark.django_db

fake = Faker()


class InitializeTestData(LoggedInMixin, APITestCase):
def setUp(self):
self.dashboard = baker.make(Dashboard)
self.visualization = baker.make(Visualization)
super().setUp()

def test_dashboard_str(self):
assert str(self.dashboard) == str(self.dashboard.title)

def test_visualization_str(self):
assert str(self.visualization) == str(self.visualization.title)
Empty file added apps/frontend/tests/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions apps/frontend/tests/test_context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from faker import Faker
from rest_framework.test import APIRequestFactory, APITestCase

from apps.common.tests.test_common import LoggedInMixin
from apps.frontend.context_processors import dashboards

pytestmark = pytest.mark.django_db

fake = Faker()


class InitializeTestData(LoggedInMixin, APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
super().setUp()

def test_context_processor_dashboards_as_staff(self):
request = self.factory.request()
request.user = self.user
response = dashboards(request)
assert response.get("dashboards") != ""

def test_context_processor_dashboards_as_non_staff(self):
request = self.factory.request()
request.user = self.user
request.user.is_staff = False
response = dashboards(request)
assert len(response.get("dashboards")) >= 0
13 changes: 13 additions & 0 deletions apps/frontend/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from unittest import TestCase

from apps.common.tests.test_common import LoggedInMixin
from apps.frontend.views import HomeView


class TestViews(LoggedInMixin, TestCase):
def setUp(self):
super().setUp()

def test_home_view_context(self):
view = HomeView()
view.get_context_data()
Empty file added apps/misc/tests/__init__.py
Empty file.
Empty file added apps/misc/tests/test_views.py
Empty file.
40 changes: 40 additions & 0 deletions apps/sql_data/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
from django.contrib.admin.sites import AdminSite
from faker import Faker
from model_bakery import baker
from rest_framework.test import APIRequestFactory, APITestCase

from apps.common.tests.test_common import LoggedInMixin
from apps.sql_data.admin import DataSourceVersionAdmin
from apps.sql_data.models import DataSourceVersion, SQLDatabaseSource

pytestmark = pytest.mark.django_db

fake = Faker()


class InitializeTestData(LoggedInMixin, APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.data_source_version = baker.make(DataSourceVersion)
self.sql_data_source = baker.make(SQLDatabaseSource)
super().setUp()

def test_audit_base_model_audit_field_set_property(self):
admin = DataSourceVersionAdmin(
model=DataSourceVersion, admin_site=AdminSite()
)
return admin.audit_details_fieldset

def test_save_model(self):
data_source_admin = DataSourceVersionAdmin(
model=DataSourceVersion, admin_site=AdminSite()
)
request = self.factory.request()
request.user = self.user
data_source_admin.save_model(
obj=self.data_source_version,
request=request,
form=None,
change=None,
)
18 changes: 18 additions & 0 deletions apps/sql_data/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ def test_update(self):
response = self.client.patch(url, {"chunk-index": 4})
assert response.status_code == 200

def test_start_chunk_upload_fail(self):
data = {"bad_data": 4}
fake_data = baker.make(SQLUploadChunk)
url = reverse(
"sqluploadmetadata-start-chunk-upload", kwargs={"pk": fake_data.id}
)
response = self.client.post(url, data)
assert response.status_code == 400


class TestSQLUploadMetadataViewSet(LoggedInMixin, APITestCase):
def setUp(self):
Expand All @@ -46,3 +55,12 @@ def test_create(self):
)
response = self.client.post(url, data, format="json")
assert response.status_code == 201

def test_upload_completion(self):
data = {"chunk_index": 1}
url = reverse(
"sqluploadmetadata-mark-as-complete",
kwargs={"pk": self.upload_metadata.pk},
)
response = self.client.patch(url, data, format="json")
assert response.status_code == 200
22 changes: 19 additions & 3 deletions apps/sql_data/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

class InitializeTestData(LoggedInMixin, APITestCase):
def setUp(self):
self.upload_chunk = baker.make(
SQLUploadChunk, chunk_content=fake.file_name()
)
self.data_source_version = baker.make(DataSourceVersion)
self.extract_metadata = baker.make(SQLExtractMetadata)
self.sql_upload_metadata = baker.make(
SQLUploadMetadata, extract_metadata=self.extract_metadata
)
self.upload_chunk = baker.make(
SQLUploadChunk,
upload_metadata=self.sql_upload_metadata,
chunk_content=fake.file_name(),
)

super().setUp()

Expand All @@ -35,6 +37,17 @@ def test_upload_metadata(self):
self.extract_metadata.data_source.name
== self.sql_upload_metadata.data_source_name
)
assert (
self.sql_upload_metadata.chunks_count
== self.upload_chunk.upload_metadata.chunks_count
)
assert self.sql_upload_metadata.mark_as_complete(
user=self.user
) == self.upload_chunk.upload_metadata.mark_as_complete(user=self.user)
assert self.sql_upload_metadata.is_complete
assert str(self.sql_upload_metadata.name) == str(
self.sql_upload_metadata
)

def test_uploaded_chunk_str(self):
assert str(self.upload_chunk) == str(
Expand All @@ -60,3 +73,6 @@ def test_data_source_version_str(self):
+ str(self.data_source_version.data_source_version)
+ ")"
)

def test_sql_extract_metadata_str(self):
assert str(self.extract_metadata.name) == str(self.extract_metadata)
30 changes: 30 additions & 0 deletions apps/users/tests/test_adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from faker import Faker
from rest_framework.test import APIRequestFactory, APITestCase

from apps.common.tests.test_common import LoggedInMixin
from apps.users.adapters import AccountAdapter, SocialAccountAdapter

pytestmark = pytest.mark.django_db

fake = Faker()


class InitializeTestData(LoggedInMixin, APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
super().setUp()

def test_account_adapter(self):
request = self.factory.request()
request.user = self.user
response = AccountAdapter.is_open_for_signup(self, request)
assert response

def test_social_account_adapter(self):
request = self.factory.request()
request.user = self.user
response = SocialAccountAdapter.is_open_for_signup(
self, request, sociallogin=None
)
assert response
19 changes: 19 additions & 0 deletions apps/users/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest
from faker import Faker
from rest_framework.test import APIRequestFactory, APITestCase

from apps.common.tests.test_common import LoggedInMixin
from apps.users.signals import assign_basic_permissions

pytestmark = pytest.mark.django_db

fake = Faker()


class InitializeTestData(LoggedInMixin, APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
super().setUp()

def test_account_adapter(self):
assert assign_basic_permissions(user=self.user) != ""
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"rest_framework",
"rest_framework.authtoken",
"django_filters",
"django_extensions",
]

LOCAL_APPS = [
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ typeCheckingMode = "basic"


[tool.pytest.ini_options]
addopts = "--ds=config.settings.test --cov-fail-under=85 --cov --cov-config=pyproject.toml --cov-report=html --cov-report=term-missing -n auto --junitxml='junitxml_report/report.xml' -v --durations=10 --cache-clear --create-db"
addopts = "--ds=config.settings.test --cov-fail-under=99 --cov --cov-config=pyproject.toml --cov-report=html --cov-report=term-missing -n auto --junitxml='junitxml_report/report.xml' -v --durations=10 --cache-clear --create-db"
console_output_style = "progress"
log_cli = 1
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
Expand Down

0 comments on commit 8a1dfa7

Please sign in to comment.