-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patherrors.py
More file actions
41 lines (32 loc) · 1.11 KB
/
errors.py
File metadata and controls
41 lines (32 loc) · 1.11 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
from decimal import Decimal
from nanodb import Table, Column, ColumnType, DataType
def test_errors() -> None:
"""Demonstrate selected constraint violations."""
person = Table(
"person",
[
Column("id", ColumnType(DataType.INT, not_null=True, unique=True)),
Column("name", ColumnType(DataType.TEXT, not_null=True)),
Column("salary", ColumnType(DataType.DECIMAL)),
],
primary_key=("id",),
)
person.insert(("id", "name", "salary"), (1, "Alice", Decimal("10.50")))
try:
person.insert(("id", "name"), (1, "Bob"))
except ValueError as exc:
print("Duplicate primary key:", exc)
try:
person.insert(("id", "name"), (2, None))
except ValueError as exc:
print("NOT NULL violation:", exc)
try:
person.insert(("id", "name", "salary"), (3, "Cyril", 10.5))
except TypeError as exc:
print("Invalid DECIMAL type:", exc)
try:
person.insert(("unknown_column",), (123,))
except ValueError as exc:
print("Unknown column:", exc)
if __name__ == "__main__":
test_errors()