-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ncstate-sat/LIBS-14-add-a-pydantic-models-…
…module Libs 14 add a pydantic models module
- Loading branch information
Showing
13 changed files
with
429 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[run] | ||
source = sat | ||
omit = */tests/*, sat/ldap.py | ||
omit = */tests/* |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi" | |
|
||
[project] | ||
name = "sat-utils" | ||
version = "1.4.3" | ||
version = "1.5.0" | ||
authors = [ | ||
{ name="Ryan Semmler", email="[email protected]" }, | ||
{ name="Shawn Taylor", email="[email protected]" }, | ||
|
@@ -21,7 +21,9 @@ dependencies = [ | |
"cx_Oracle==8.3.0", | ||
"oracledb==2.0.1", | ||
"pyodbc==5.1.0", | ||
"requests_oauthlib==1.3.1" | ||
"requests_oauthlib==1.3.1", | ||
"pydantic==2.6.4", | ||
"pydantic[email]==2.6.4" | ||
] | ||
|
||
[project.optional-dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[pytest] | ||
testpaths = tests | ||
python_files = tests.py test_*.py *_tests.py | ||
addopts = -p no:warnings --cov-config=.coveragerc --cov-fail-under=85 --cov=sat --cov-report=html --cov-report=term-missing:skip-covered -vvv | ||
addopts = -p no:warnings --cov-config=.coveragerc --cov-fail-under=86 --cov=sat --cov-report=html --cov-report=term-missing:skip-covered -vvv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pydantic import UUID4, BaseModel | ||
|
||
from sat.models.ccure.types import FILLED_STRING | ||
|
||
|
||
class Clearance(BaseModel): | ||
object_id: int | ||
guid: UUID4 | ||
name: FILLED_STRING | ||
|
||
|
||
class Credential(BaseModel): | ||
card_number: int | ||
patron_id: int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from pydantic import UUID4, BaseModel | ||
|
||
from sat.models.ccure.types import ASSET_TYPES, FILLED_STRING | ||
|
||
|
||
class Asset(BaseModel): | ||
object_id: int | ||
name: FILLED_STRING | ||
guid: UUID4 | ||
asset_type: ASSET_TYPES |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from enum import Enum | ||
|
||
from pydantic import AfterValidator | ||
from typing_extensions import Annotated | ||
|
||
|
||
def validate_filled_string(value): | ||
if not value: | ||
raise ValueError("This string may not be empty") | ||
|
||
|
||
ASSET_TYPES = Enum("ASSET_TYPES", ["Door", "Elevator"]) | ||
|
||
FILLED_STRING = Annotated[str, AfterValidator(validate_filled_string)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from uuid import uuid4 | ||
|
||
import pytest | ||
from sat.models.ccure.types import ASSET_TYPES | ||
|
||
|
||
@pytest.fixture | ||
def clearance(): | ||
def _clearance(multiple: int = None): | ||
clearance = { | ||
"name": "John Doe", | ||
"object_id": 1234, | ||
"guid": str(uuid4()), | ||
} | ||
if multiple: | ||
return [clearance for _ in range(multiple)] | ||
else: | ||
return clearance | ||
|
||
return _clearance | ||
|
||
|
||
@pytest.fixture | ||
def credential(): | ||
def _credential(multiple: int = None): | ||
credential = { | ||
"card_number": 1234567890, | ||
"patron_id": 1234567890, | ||
} | ||
if multiple: | ||
return [credential for _ in range(multiple)] | ||
else: | ||
return credential | ||
|
||
return _credential | ||
|
||
|
||
@pytest.fixture | ||
def asset(): | ||
def _asset(multiple: int = None): | ||
asset = { | ||
"name": "Asset Name", | ||
"object_id": 1234, | ||
"guid": str(uuid4()), | ||
"asset_type": ASSET_TYPES.Door, | ||
} | ||
if multiple: | ||
return [asset for _ in range(multiple)] | ||
else: | ||
return asset | ||
|
||
return _asset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import pydantic | ||
import pytest | ||
from sat.models.ccure.access import Clearance, Credential | ||
from sat.models.ccure.assets import Asset | ||
|
||
|
||
def test_valid_clearance(clearance): | ||
clr = clearance() | ||
assert Clearance(**clr) | ||
|
||
|
||
def test_invalid_clearance_uuid(clearance): | ||
clr = clearance() | ||
clr["guid"] = "asldkfj-aslkdjf-aldlas-asldkj" | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Clearance(**clr) | ||
assert "Input should be a valid UUID" in str(ve) | ||
|
||
|
||
def test_invalid_clearance_name(clearance): | ||
clr = clearance() | ||
clr["name"] = "" | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Clearance(**clr) | ||
assert "This string may not be empty" in str(ve) | ||
|
||
|
||
def test_invalid_clearance_name_non_string(clearance): | ||
clr = clearance() | ||
clr["name"] = 12 | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Clearance(**clr) | ||
assert "Input should be a valid string" in str(ve) | ||
|
||
|
||
def test_invalid_clearance_object_id(clearance): | ||
clr = clearance() | ||
clr["object_id"] = "1234b" | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Clearance(**clr) | ||
assert "Input should be a valid integer" in str(ve) | ||
|
||
|
||
def test_valid_credential(credential): | ||
cred = credential() | ||
assert Credential(**cred) | ||
|
||
|
||
def test_invalid_credential_card_number(credential): | ||
cred = credential() | ||
cred["card_number"] = "1234567890b" | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Credential(**cred) | ||
assert "Input should be a valid integer" in str(ve) | ||
|
||
|
||
def test_invalid_credential_patron_id(credential): | ||
cred = credential() | ||
cred["patron_id"] = "1234567890b" | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Credential(**cred) | ||
assert "Input should be a valid integer" in str(ve) | ||
|
||
|
||
def test_asset_valid(asset): | ||
ast = asset() | ||
assert Asset(**ast) | ||
|
||
|
||
def test_asset_invalid_name(asset): | ||
ast = asset() | ||
ast["name"] = "" | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Asset(**ast) | ||
assert "This string may not be empty" in str(ve) | ||
|
||
|
||
def test_asset_invalid_object_id(asset): | ||
ast = asset() | ||
ast["object_id"] = "1234b" | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Asset(**ast) | ||
assert "Input should be a valid integer" in str(ve) | ||
|
||
|
||
def test_asset_invalid_guid(asset): | ||
ast = asset() | ||
ast["guid"] = "asldkfj-aslkdjf-aldlas-asldkj" | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Asset(**ast) | ||
assert "Input should be a valid UUID" in str(ve) | ||
|
||
|
||
def test_asset_invalid_asset_type(asset): | ||
ast = asset() | ||
ast["asset_type"] = "Door" | ||
with pytest.raises(pydantic.ValidationError) as ve: | ||
Asset(**ast) | ||
assert "Input should be 1 or 2" in str(ve) |