Skip to content

Commit 90626a2

Browse files
Generated tests for organization and role using pytest.
1 parent 0d01c92 commit 90626a2

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

tests/test_organization.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# test_organization.py
2+
3+
import pytest
4+
from utils.models import Organization
5+
6+
def test_organization_creation():
7+
"""Test basic organization creation"""
8+
org = Organization("Test Org", "Test Description")
9+
assert org.name == "Test Org"
10+
assert org.description == "Test Description"
11+
assert org.roles == []
12+
assert org.members == []
13+
14+
def test_organization_add_role():
15+
"""Test adding a role to organization"""
16+
org = Organization("Test Org", "Test Description")
17+
role = {"name": "Admin", "permissions": ["read", "write"]}
18+
org.add_role(role)
19+
assert len(org.roles) == 1
20+
assert org.roles[0] == role
21+
22+
def test_organization_add_member():
23+
"""Test adding a member to organization"""
24+
org = Organization("Test Org", "Test Description")
25+
member = {"id": 1, "name": "John Doe"}
26+
org.add_member(member)
27+
assert len(org.members) == 1
28+
assert org.members[0] == member
29+
30+
def test_organization_remove_member():
31+
"""Test removing a member from organization"""
32+
org = Organization("Test Org", "Test Description")
33+
member = {"id": 1, "name": "John Doe"}
34+
org.add_member(member)
35+
org.remove_member(1)
36+
assert len(org.members) == 0
37+
38+
def test_organization_get_member():
39+
"""Test getting a member from organization"""
40+
org = Organization("Test Org", "Test Description")
41+
member = {"id": 1, "name": "John Doe"}
42+
org.add_member(member)
43+
retrieved_member = org.get_member(1)
44+
assert retrieved_member == member
45+
46+
def test_organization_get_nonexistent_member():
47+
"""Test getting a non-existent member"""
48+
org = Organization("Test Org", "Test Description")
49+
assert org.get_member(1) is None
50+
51+
# Additional tests for organization.py
52+
53+
def test_organization_invalid_name():
54+
"""Test organization creation with invalid name"""
55+
with pytest.raises(ValueError):
56+
Organization("", "Description")
57+
58+
def test_organization_none_name():
59+
"""Test organization creation with None name"""
60+
with pytest.raises(ValueError):
61+
Organization(None, "Description")

tests/test_role.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# test_role.py
2+
3+
import pytest
4+
from utils.models import Role
5+
6+
def test_role_creation():
7+
"""Test basic role creation"""
8+
role = Role("Admin", ["read", "write"])
9+
assert role.name == "Admin"
10+
assert role.permissions == ["read", "write"]
11+
12+
def test_role_add_permission():
13+
"""Test adding a permission to role"""
14+
role = Role("Admin", ["read"])
15+
role.add_permission("write")
16+
assert "write" in role.permissions
17+
assert len(role.permissions) == 2
18+
19+
def test_role_remove_permission():
20+
"""Test removing a permission from role"""
21+
role = Role("Admin", ["read", "write"])
22+
role.remove_permission("write")
23+
assert "write" not in role.permissions
24+
assert len(role.permissions) == 1
25+
26+
def test_role_has_permission():
27+
"""Test checking if role has specific permission"""
28+
role = Role("Admin", ["read", "write"])
29+
assert role.has_permission("read") is True
30+
assert role.has_permission("delete") is False
31+
32+
def test_role_add_existing_permission():
33+
"""Test adding a permission that already exists"""
34+
role = Role("Admin", ["read"])
35+
role.add_permission("read")
36+
assert len(role.permissions) == 1
37+
38+
def test_role_remove_nonexistent_permission():
39+
"""Test removing a permission that doesn't exist"""
40+
role = Role("Admin", ["read"])
41+
role.remove_permission("write")
42+
assert len(role.permissions) == 1
43+
assert role.permissions == ["read"]
44+
45+
# Additional tests for role.py
46+
47+
def test_role_invalid_name():
48+
"""Test role creation with invalid name"""
49+
with pytest.raises(ValueError):
50+
Role("", ["read"])
51+
52+
def test_role_none_permissions():
53+
"""Test role creation with None permissions"""
54+
with pytest.raises(ValueError):
55+
Role("Admin", None)
56+
57+
def test_role_empty_permissions():
58+
"""Test role creation with empty permissions list"""
59+
role = Role("Admin", [])
60+
assert len(role.permissions) == 0

0 commit comments

Comments
 (0)