-
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.
Add model validation tests for MainCommand
- Loading branch information
1 parent
9765b94
commit 7ae3aab
Showing
3 changed files
with
77 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from sqlmodel import SQLModel | ||
from sqlmodel._compat import get_config_value, set_config_value | ||
|
||
|
||
class BaseSQLModel(SQLModel): | ||
""" | ||
Base SQL Model class. It performs validation on the model unlike the default SQLModel class with table=True. | ||
""" | ||
|
||
def __init__(self, **data): | ||
is_table = get_config_value(model=self, parameter="table", default=False) | ||
set_config_value( | ||
model=self, parameter="table", value=False | ||
) # Makes it validate the model | ||
super().__init__(**data) | ||
set_config_value(model=self, parameter="table", value=is_table) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pytest | ||
from sqlmodel import Session | ||
from backend.data.data_models import MainCommand | ||
|
||
|
||
def test_main_command(): | ||
main_command = MainCommand( | ||
name="Test", | ||
params="param1,param2", | ||
format="int,int", | ||
data_size=2, | ||
total_size=2, | ||
) | ||
assert main_command.name == "Test" | ||
assert main_command.params == "param1,param2" | ||
assert main_command.format == "int,int" | ||
assert main_command.data_size == 2 | ||
assert main_command.total_size == 2 | ||
|
||
|
||
def test_main_command_no_params_and_no_format(): | ||
main_command = MainCommand( | ||
name="Test", | ||
data_size=2, | ||
total_size=2, | ||
) | ||
assert main_command.name == "Test" | ||
assert main_command.params is None | ||
assert main_command.format is None | ||
assert main_command.data_size == 2 | ||
assert main_command.total_size == 2 | ||
|
||
|
||
def test_main_command_no_format(db_engine): | ||
with pytest.raises(ValueError), Session(db_engine) as session: | ||
session.add( | ||
MainCommand( | ||
name="Test", | ||
params="param1,param2", | ||
data_size=2, | ||
total_size=2, | ||
) | ||
) | ||
session.commit() | ||
|
||
|
||
def test_main_command_no_params(db_engine): | ||
with pytest.raises(ValueError), Session(db_engine) as session: | ||
session.add( | ||
MainCommand( | ||
name="Test", | ||
format="int,int", | ||
data_size=2, | ||
total_size=2, | ||
) | ||
) | ||
session.commit() |