Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#14 validate extra fields #16

Merged
merged 3 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions bpx/schema.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from typing import List, Literal, Union, Dict

from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, Extra

from bpx import Function, InterpolatedTable

FloatFunctionTable = Union[float, Function, InterpolatedTable]


class Header(BaseModel):
class ExtraBaseModel(BaseModel):
class Config:
extra = Extra.forbid


class Header(ExtraBaseModel):
bpx: float = Field(
alias="BPX",
example=1.0,
Expand Down Expand Up @@ -38,7 +43,7 @@ class Header(BaseModel):
)


class Cell(BaseModel):
class Cell(ExtraBaseModel):
electrode_area: float = Field(
alias="Electrode area [m2]",
description="Electrode cross-sectional area",
Expand Down Expand Up @@ -113,7 +118,7 @@ class Cell(BaseModel):
)


class Electrolyte(BaseModel):
class Electrolyte(ExtraBaseModel):
initial_concentration: float = Field(
alias="Initial concentration [mol.m-3]",
example=1000,
Expand Down Expand Up @@ -153,7 +158,7 @@ class Electrolyte(BaseModel):
)


class Contact(BaseModel):
class Contact(ExtraBaseModel):
thickness: float = Field(
alias="Thickness [m]",
example=85.2e-6,
Expand Down Expand Up @@ -243,7 +248,7 @@ class Electrode(Contact):
)


class Experiment(BaseModel):
class Experiment(ExtraBaseModel):
time: List[float] = Field(
alias="Time [s]",
example=[0, 0.1, 0.2, 0.3, 0.4],
Expand All @@ -267,7 +272,7 @@ class Experiment(BaseModel):
)


class Parameterisation(BaseModel):
class Parameterisation(ExtraBaseModel):
cell: Cell = Field(
alias="Cell",
)
Expand All @@ -285,7 +290,7 @@ class Parameterisation(BaseModel):
)


class BPX(BaseModel):
class BPX(ExtraBaseModel):
header: Header = Field(
alias="Header",
)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ def test_to_python_function(self):
pyfunct = funct.to_python_function()
self.assertEqual(pyfunct(2.0), 4.0)

def test_bad_input(self):
test = copy.copy(self.base)
test["Parameterisation"]["Electrolyte"]["bad"] = "this shouldn't be here"
with self.assertRaises(ValidationError):
parse_obj_as(BPX, test)

def test_validation_data(self):
test = copy.copy(self.base)
test["Validation"] = {
"Experiment 1": {
"Time [s]": [0, 1000, 2000],
"Current [A]": [-0.625, -0.625, -0.625],
"Voltage [V]": [4.19367569, 4.1677888, 4.14976386],
"Temperature [K]": [298.15, 298.15, 298.15],
},
"Experiment 2": {
"Time [s]": [0, 1000],
"Current [A]": [-0.625, -0.625],
"Voltage [V]": [4.19367569, 4.1677888],
"Temperature [K]": [298.15, 298.15],
},
}


if __name__ == "__main__":
unittest.main()