Debian Codegraph @

tests/test_utils.py

raw symbols SCIP
1 from functools import partial
2
3 from django.contrib.auth.models import User
4 from django.test import TestCase
5 from django.test.utils import override_settings
6
7 from organizations.models import Organization
8 from organizations.utils import create_organization
9 from organizations.utils import model_field_attr
10 from test_abstract.models import CustomOrganization
11 from test_accounts.models import Account
12
13
14 @override_settings(USE_TZ=True)
15 class CreateOrgTests(TestCase):
16
17 fixtures = ["users.json", "orgs.json"]
18
19 def setUp(self):
20 self.user = User.objects.get(username="dave")
21
22 def test_create_organization(self):
23 acme = create_organization(
24 self.user, "Acme", org_defaults={"slug": "acme-slug"}
25 )
26 self.assertTrue(isinstance(acme, Organization))
27 self.assertEqual(self.user, acme.owner.organization_user.user)
28 self.assertTrue(acme.owner.organization_user.is_admin)
29
30 def test_create_custom_org(self):
31 custom = create_organization(self.user, "Custom", model=Account)
32 self.assertTrue(isinstance(custom, Account))
33 self.assertEqual(self.user, custom.owner.organization_user.user)
34
35 def test_create_custom_org_from_abstract(self):
36 custom = create_organization(self.user, "Custom", model=CustomOrganization)
37 self.assertTrue(isinstance(custom, CustomOrganization))
38 self.assertEqual(self.user, custom.owner.organization_user.user)
39
40 def test_defaults(self):
41 """Ensure models are created with defaults as specified"""
42 # Default models
43 org = create_organization(
44 self.user,
45 "Is Admin",
46 org_defaults={"slug": "is-admin-212", "is_active": False},
47 org_user_defaults={"is_admin": False},
48 )
49 self.assertFalse(org.is_active)
50 self.assertFalse(org.owner.organization_user.is_admin)
51
52 # Custom models
53 create_account = partial(
54 create_organization,
55 model=Account,
56 org_defaults={"monthly_subscription": 99},
57 org_user_defaults={"user_type": "B"},
58 )
59 myaccount = create_account(self.user, name="My New Account")
60 self.assertEqual(myaccount.monthly_subscription, 99)
61
62 def test_backwards_compat(self):
63 """Ensure old optional arguments still work"""
64 org = create_organization(self.user, "Is Admin", "my-slug", is_active=False)
65 self.assertFalse(org.is_active)
66
67 custom = create_organization(self.user, "Custom org", org_model=Account)
68 self.assertTrue(isinstance(custom, Account))
69
70
71 class AttributeUtilTests(TestCase):
72 def test_present_field(self):
73 self.assertTrue(model_field_attr(User, "username", "max_length"))
74
75 def test_absent_field(self):
76 self.assertRaises(KeyError, model_field_attr, User, "blahblah", "max_length")
77
78 def test_absent_attr(self):
79 self.assertRaises(
80 AttributeError, model_field_attr, User, "username", "mariopoints"
81 )