Skip to content

Commit b0a6582

Browse files
committed
tests: use unique identifiers for all tests
1 parent def37c7 commit b0a6582

17 files changed

+266
-325
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import annotations
2+
3+
import random
4+
import string
5+
6+
7+
def name() -> str:
8+
"""Generate a unique name."""
9+
return "".join(random.choices(string.ascii_letters, k=8))
10+
11+
12+
def email() -> str:
13+
"""Generate a unique email address."""
14+
name = "".join(random.choices(string.ascii_lowercase, k=8))
15+
return f"{name}@example.com"
16+
17+
18+
def username() -> str:
19+
"""Generate a unique username."""
20+
return "".join(random.choices(string.ascii_letters, k=8))
21+
22+
23+
def password() -> str:
24+
"""Generate a secure password."""
25+
characters = string.ascii_letters + string.digits + string.punctuation
26+
return "".join(random.choices(characters, k=12)) # 12-character password

integration/tests/posit/connect/oauth/test_associations.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from posit import connect
77

8-
from .. import CONNECT_VERSION
8+
from .. import CONNECT_VERSION, fixtures
99

1010

1111
@pytest.mark.skipif(
@@ -17,7 +17,7 @@ class TestAssociations:
1717
def setup_class(cls):
1818
cls.client = connect.Client()
1919
cls.integration = cls.client.oauth.integrations.create(
20-
name="example integration",
20+
name=fixtures.name(),
2121
description="integration description",
2222
template="custom",
2323
config={
@@ -32,7 +32,7 @@ def setup_class(cls):
3232
)
3333

3434
cls.another_integration = cls.client.oauth.integrations.create(
35-
name="another example integration",
35+
name=fixtures.name(),
3636
description="another integration description",
3737
template="custom",
3838
config={
@@ -48,26 +48,19 @@ def setup_class(cls):
4848

4949
# create content
5050
# requires full bundle deployment to produce an interactive content type
51-
cls.content = cls.client.content.create(name="example-flask-minimal")
51+
cls.content = cls.client.content.create(name=fixtures.name())
52+
5253
# create bundle
5354
path = Path("../../../../resources/connect/bundles/example-flask-minimal/bundle.tar.gz")
5455
path = (Path(__file__).parent / path).resolve()
5556
bundle = cls.content.bundles.create(str(path))
57+
5658
# deploy bundle
5759
task = bundle.deploy()
5860
task.wait_for()
5961

6062
cls.content.oauth.associations.update(cls.integration["guid"])
6163

62-
@classmethod
63-
def teardown_class(cls):
64-
cls.integration.delete()
65-
cls.another_integration.delete()
66-
assert len(cls.client.oauth.integrations.find()) == 0
67-
68-
cls.content.delete()
69-
assert cls.client.content.count() == 0
70-
7164
def test_find_by_integration(self):
7265
associations = self.integration.associations.find()
7366
assert len(associations) == 1

integration/tests/posit/connect/oauth/test_integrations.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from posit import connect
55

6-
from .. import CONNECT_VERSION
6+
from .. import CONNECT_VERSION, fixtures
77

88

99
@pytest.mark.skipif(
@@ -15,7 +15,7 @@ class TestIntegrations:
1515
def setup_class(cls):
1616
cls.client = connect.Client()
1717
cls.integration = cls.client.oauth.integrations.create(
18-
name="example integration",
18+
name=fixtures.name(),
1919
description="integration description",
2020
template="custom",
2121
config={
@@ -30,7 +30,7 @@ def setup_class(cls):
3030
)
3131

3232
cls.another_integration = cls.client.oauth.integrations.create(
33-
name="another example integration",
33+
name=fixtures.name(),
3434
description="another integration description",
3535
template="custom",
3636
config={
@@ -44,27 +44,21 @@ def setup_class(cls):
4444
},
4545
)
4646

47-
@classmethod
48-
def teardown_class(cls):
49-
cls.integration.delete()
50-
cls.another_integration.delete()
51-
assert len(cls.client.oauth.integrations.find()) == 0
52-
5347
def test_get(self):
5448
result = self.client.oauth.integrations.get(self.integration["guid"])
5549
assert result == self.integration
5650

5751
def test_find(self):
5852
results = self.client.oauth.integrations.find()
59-
assert len(results) == 2
60-
assert results[0] == self.integration
61-
assert results[1] == self.another_integration
53+
assert len(results) >= 2
54+
integration_guids = {integration["guid"] for integration in results}
55+
assert self.integration["guid"] in integration_guids
56+
assert self.another_integration["guid"] in integration_guids
6257

6358
def test_create_update_delete(self):
6459
# create a new integration
65-
6660
integration = self.client.oauth.integrations.create(
67-
name="new integration",
61+
name=fixtures.name(),
6862
description="new integration description",
6963
template="custom",
7064
config={
@@ -82,7 +76,7 @@ def test_create_update_delete(self):
8276
assert created == integration
8377

8478
all_integrations = self.client.oauth.integrations.find()
85-
assert len(all_integrations) == 3
79+
assert len(all_integrations) >= 3
8680

8781
# update the new integration
8882

@@ -94,4 +88,4 @@ def test_create_update_delete(self):
9488

9589
created.delete()
9690
all_integrations_after_delete = self.client.oauth.integrations.find()
97-
assert len(all_integrations_after_delete) == 2
91+
assert len(all_integrations_after_delete) == len(all_integrations) - 1

integration/tests/posit/connect/test_bundles.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ def setup_class(cls):
2424
)
2525
cls.bundle_path = (Path(__file__).parent / bundle_path).resolve()
2626

27-
@classmethod
28-
def teardown_class(cls):
29-
cls.content.delete()
30-
3127
def test_create_bundle(self):
3228
"""Test creating a bundle."""
3329
bundle = self.content.bundles.create(str(self.bundle_path))

integration/tests/posit/connect/test_content.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from posit import connect
77

8-
from . import CONNECT_VERSION
8+
from . import CONNECT_VERSION, fixtures
99

1010

1111
class TestContent:
@@ -14,13 +14,9 @@ def setup_class(cls):
1414
cls.client = connect.Client()
1515
cls.content = cls.client.content.create()
1616

17-
@classmethod
18-
def teardown_class(cls):
19-
cls.content.delete()
20-
assert cls.client.content.count() == 0
21-
2217
def test_count(self):
23-
assert self.client.content.count() == 1
18+
# Assert that count works. We don't care what the count is.
19+
assert self.client.content.count()
2420

2521
def test_get(self):
2622
assert self.client.content.get(self.content["guid"]) == self.content
@@ -52,7 +48,7 @@ def test_content_item_owner_from_include(self):
5248
)
5349
def test_restart(self):
5450
# create content
55-
content = self.client.content.create(name="example-flask-minimal")
51+
content = self.client.content.create(name=fixtures.name())
5652
# create bundle
5753
path = Path("../../../resources/connect/bundles/example-flask-minimal/bundle.tar.gz")
5854
path = (Path(__file__).parent / path).resolve()
@@ -71,7 +67,7 @@ def test_restart(self):
7167
)
7268
def test_render(self):
7369
# create content
74-
content = self.client.content.create(name="example-quarto-minimal")
70+
content = self.client.content.create(name=fixtures.name())
7571
# create bundle
7672
path = Path("../../../resources/connect/bundles/example-quarto-minimal/bundle.tar.gz")
7773
path = (Path(__file__).parent / path).resolve()

integration/tests/posit/connect/test_content_item_permissions.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from posit import connect
77

8+
from .fixtures import email, name, password, username
9+
810
if TYPE_CHECKING:
911
from posit.connect.content import ContentItem
1012
from posit.connect.permissions import Permission
@@ -16,42 +18,33 @@ class TestContentPermissions:
1618
@classmethod
1719
def setup_class(cls):
1820
cls.client = connect.Client()
19-
cls.content = cls.client.content.create(name="example")
21+
cls.content = cls.client.content.create(name=name())
2022

21-
cls.user_aron = cls.client.users.create(
22-
username="permission_aron",
23-
email="permission_aron@example.com",
24-
password="permission_s3cur3p@ssword",
25-
)
26-
cls.user_bill = cls.client.users.create(
27-
username="permission_bill",
28-
email="permission_bill@example.com",
29-
password="permission_s3cur3p@ssword",
23+
cls.alice = cls.client.users.create(
24+
username=name(),
25+
email=email(),
26+
password=password(),
3027
)
3128

32-
cls.group_friends = cls.client.groups.create(name="Friends")
33-
34-
@classmethod
35-
def teardown_class(cls):
36-
cls.content.delete()
37-
assert cls.client.content.count() == 0
29+
cls.bob = cls.client.users.create(
30+
username=username(),
31+
email=email(),
32+
password=password(),
33+
)
3834

39-
cls.group_friends.delete()
40-
assert cls.client.groups.count() == 0
35+
cls.group = cls.client.groups.create(name=name())
4136

4237
def test_permissions_add_destroy(self):
43-
assert self.client.groups.count() == 1
44-
assert self.client.users.count() == 3
4538
assert self.content.permissions.find() == []
4639

4740
# Add permissions
4841
self.content.permissions.create(
49-
principal_guid=self.user_aron["guid"],
42+
principal_guid=self.alice["guid"],
5043
principal_type="user",
5144
role="viewer",
5245
)
5346
self.content.permissions.create(
54-
principal_guid=self.group_friends["guid"],
47+
principal_guid=self.group["guid"],
5548
principal_type="group",
5649
role="owner",
5750
)
@@ -63,22 +56,22 @@ def assert_permissions_match_guids(permissions: list[Permission], objs_with_guid
6356
# Prove they have been added
6457
assert_permissions_match_guids(
6558
self.content.permissions.find(),
66-
[self.user_aron, self.group_friends],
59+
[self.alice, self.group],
6760
)
6861

6962
# Remove permissions (and from some that isn't an owner)
70-
self.content.permissions.destroy(self.user_aron)
63+
self.content.permissions.destroy(self.alice)
7164
with pytest.raises(ValueError):
72-
self.content.permissions.destroy(self.user_bill)
65+
self.content.permissions.destroy(self.bob)
7366

7467
# Prove they have been removed
7568
assert_permissions_match_guids(
7669
self.content.permissions.find(),
77-
[self.group_friends],
70+
[self.group],
7871
)
7972

8073
# Remove the last permission
81-
self.content.permissions.destroy(self.group_friends)
74+
self.content.permissions.destroy(self.group)
8275

8376
# Prove they have been removed
8477
assert self.content.permissions.find() == []

integration/tests/posit/connect/test_content_item_repository.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from posit.connect.content import ContentItem
66
from posit.connect.repository import ContentItemRepository
77

8-
from . import CONNECT_VERSION
8+
from . import CONNECT_VERSION, fixtures
99

1010

1111
class TestContentItemRepository:
@@ -14,12 +14,7 @@ class TestContentItemRepository:
1414
@classmethod
1515
def setup_class(cls):
1616
cls.client = connect.Client()
17-
cls.content = cls.client.content.create(name="example")
18-
19-
@classmethod
20-
def teardown_class(cls):
21-
cls.content.delete()
22-
assert cls.client.content.count() == 0
17+
cls.content = cls.client.content.create(name=fixtures.name())
2318

2419
@property
2520
def repo_repository(self):

integration/tests/posit/connect/test_env.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
from posit import connect
22

3+
from . import fixtures
4+
35

46
class TestEnvVars:
57
@classmethod
68
def setup_class(cls):
79
cls.client = connect.Client()
810
cls.content = cls.client.content.create(
9-
name="Sample",
11+
name=fixtures.name(),
1012
description="Simple sample content for testing",
1113
access_type="acl",
1214
)
1315

14-
@classmethod
15-
def teardown_class(cls):
16-
cls.content.delete()
17-
assert cls.client.content.count() == 0
18-
1916
def test_clear(self):
2017
self.content.environment_variables.create("KEY", "value")
2118
assert self.content.environment_variables.find() == ["KEY"]

integration/tests/posit/connect/test_environments.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ def setup_class(cls):
2020
cluster_name="Kubernetes",
2121
)
2222

23-
@classmethod
24-
def teardown_class(cls):
25-
cls.environment.destroy()
26-
assert len(cls.client.environments) == 0
27-
2823
def test_find(self):
2924
uid = self.environment["guid"]
3025
environment = self.client.environments.find(uid)

0 commit comments

Comments
 (0)