-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model.py
More file actions
88 lines (61 loc) · 2.77 KB
/
test_model.py
File metadata and controls
88 lines (61 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Specific tests for the Model and it's methods."""
import pytest
from sqliter.model.model import BaseDBModel
class TestBaseDBModel:
"""Test the Model and it's methods."""
def test_should_create_pk(self) -> None:
"""Test that 'should_create_pk' returns True."""
assert BaseDBModel.should_create_pk() is True
def test_get_primary_key(self) -> None:
"""Test that 'get_primary_key' returns 'pk'."""
assert BaseDBModel.get_primary_key() == "pk"
def test_get_table_name_default(self) -> None:
"""Test that 'get_table_name' returns the default table name."""
class TestModel(BaseDBModel):
pass
assert TestModel.get_table_name() == "tests"
def test_get_table_name_custom(self) -> None:
"""Test that 'get_table_name' returns the custom table name."""
class TestModel(BaseDBModel):
class Meta:
table_name = "custom_table"
assert TestModel.get_table_name() == "custom_table"
def test_get_table_name_invalid_characters(self) -> None:
"""Test that 'get_table_name' rejects invalid characters."""
class TestModel(BaseDBModel):
class Meta:
table_name = "invalid-table-name"
with pytest.raises(ValueError, match="Invalid table name"):
TestModel.get_table_name()
def test_get_table_name_sql_injection_attempt(self) -> None:
"""Test that 'get_table_name' prevents SQL injection."""
class TestModel(BaseDBModel):
class Meta:
table_name = 'users"; DROP TABLE users; --'
with pytest.raises(ValueError, match="Invalid table name"):
TestModel.get_table_name()
def test_get_table_name_starts_with_number(self) -> None:
"""Test that 'get_table_name' rejects names starting with numbers."""
class TestModel(BaseDBModel):
class Meta:
table_name = "123table"
with pytest.raises(
ValueError, match=r"Invalid table name.*must start with"
):
TestModel.get_table_name()
def test_get_table_name_with_underscore(self) -> None:
"""Test that 'get_table_name' allows underscores."""
class TestModel(BaseDBModel):
class Meta:
table_name = "my_table_name"
assert TestModel.get_table_name() == "my_table_name"
def test_model_validate_partial(self) -> None:
"""Test 'model_validate_partial' with partial data."""
class TestModel(BaseDBModel):
name: str
age: int | None
data = {"name": "John"}
model_instance = TestModel.model_validate_partial(data)
assert model_instance.name == "John"
with pytest.raises(AttributeError):
_ = model_instance.age