From ddddf6775ce774a6a90c82667a97747ecb93f839 Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Sun, 6 Aug 2023 19:21:33 +0200 Subject: [PATCH] fix tests --- common/tests.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- loot/tests.py | 4 +++- users/models.py | 8 ++++++++ utils/diff.py | 6 +++++- 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/common/tests.py b/common/tests.py index aac50a0..f7495ea 100644 --- a/common/tests.py +++ b/common/tests.py @@ -1,11 +1,36 @@ from decimal import Decimal from django.test import SimpleTestCase +from django_tenants.test.cases import FastTenantTestCase +from loot.models import Loot +from users.models import TenantUser from utils.assets import get_css from utils.colors import gamma_correction, get_percieved_lightness, is_bright_color -from utils.markdown import md_to_html +from utils.markdown import md_to_html, autolink from utils.money import format_money +from utils.urls import name2url + + +class BaseTest(FastTenantTestCase): + @classmethod + def setup_tenant(cls, tenant): + # Creates the tenant user needed to set up tenant.owner_id + + tenant_user = TenantUser.objects.get_or_create(email="test@example.com") + tenant.name = "public" + tenant.email = "test2@example.com" + + # For django-tenant-users + + tenant.owner_id = 1 + tenant.tenants = [1] + return tenant + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.setUpTestData() class ColorsTests(SimpleTestCase): @@ -46,6 +71,30 @@ class MarkdownTests(SimpleTestCase): ) +class AutoLinkTests(BaseTest): + @classmethod + def setUpTestData(cls): + cls.some_loot = Loot.objects.create(name="Torches", quantity=2, value_gold=0.2) + + def test_name2url(self): + self.assertEqual( + name2url(), + {'Torches': ('/loot/1/edit', self.some_loot)} + ) + + def test_autolink(self): + self.assertEqual( + autolink("these Torches"), + ("these [Torches](/loot/1/edit)", {"loo1"}) + ) + + def test_basic_markdown(self): + self.assertHTMLEqual( + md_to_html("most Torches we found")[0], + '

mostTorcheswe found

' + ) + + class MoneyTests(SimpleTestCase): def test_money_conversion(self): self.assertEqual(format_money(Decimal("0.1")), "1 SP") diff --git a/loot/tests.py b/loot/tests.py index e997377..a005662 100644 --- a/loot/tests.py +++ b/loot/tests.py @@ -1,9 +1,11 @@ from django_tenants.test.cases import TenantTestCase + +from common.tests import BaseTest # Create your tests here. from loot.models import Loot -class LootTests(TenantTestCase): +class LootTests(BaseTest): @classmethod def setUpTestData(cls): diff --git a/users/models.py b/users/models.py index 2aebc2a..b82f3d5 100644 --- a/users/models.py +++ b/users/models.py @@ -38,3 +38,11 @@ class TenantUser(UserProfile): @property def graphkey(self): return f"use{self.pk}" + + @property + def groups(self): + return self.tenant_perms.groups + + @property + def user_permissions(self): + return self.tenant_perms.user_permissions diff --git a/utils/diff.py b/utils/diff.py index 4886b7c..44242fa 100644 --- a/utils/diff.py +++ b/utils/diff.py @@ -12,7 +12,11 @@ def print_diff_call(str1: str, str2: str, title: str) -> None: tmp1.flush() tmp2.write(str2) tmp2.flush() - subprocess.run(["git", "--no-pager", "diff", "--color-words=.", tmp1.name, tmp2.name]) + out = subprocess.run( + ["git", "--no-pager", "diff", "--color-words=.", tmp1.name, tmp2.name], + capture_output=True + ) + print(out.stdout.decode()) def print_diff(str1: str, str2: str) -> None: