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

Issue#35: check if the title in GuaranteedAnalysis is minimal #47

Merged
merged 8 commits into from
Oct 9, 2024
10 changes: 9 additions & 1 deletion pipeline/inspection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from typing import List, Optional
from pydantic import BaseModel, Field, field_validator
from pydantic import BaseModel, Field, field_validator, model_validator

class npkError(ValueError):
pass
Expand Down Expand Up @@ -44,6 +44,7 @@ def convert_value(cls, v):
class GuaranteedAnalysis(BaseModel):
title: Optional[str] = None
nutrients: List[NutrientValue] = []
is_minimal: bool | None = None

@field_validator(
"nutrients",
Expand All @@ -53,6 +54,13 @@ def replace_none_with_empty_list(cls, v):
if v is None:
v = []
return v

@model_validator(mode="after")
def set_is_minimal(self):
pattern = r'\bminim\w*\b'
if self.title and re.search(pattern, self.title, re.IGNORECASE):
self.is_minimal = True
return self
k-allagbe marked this conversation as resolved.
Show resolved Hide resolved

class Specification(BaseModel):
humidity: Optional[float] = Field(..., alias='humidity')
Expand Down
23 changes: 22 additions & 1 deletion tests/test_inspection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from pipeline.inspection import FertilizerInspection, NutrientValue, Value, Specification
from pipeline.inspection import FertilizerInspection, GuaranteedAnalysis, NutrientValue, Value, Specification


class TestNutrientValue(unittest.TestCase):
Expand Down Expand Up @@ -175,6 +175,27 @@ def test_invalid_npk(self):
inspection = FertilizerInspection(npk=npk)
self.assertIsNone(inspection.npk, f"Expected None for npk with input {npk}")

class TestGuaranteedAnalysis(unittest.TestCase):

def setUp(self):
self.nutrient_1 = NutrientValue(nutrient="Nitrogen", value="2", unit="mg/L")
self.nutrient_2 = NutrientValue(nutrient="Organic matter", value="15", unit="mg/L")

def test_set_is_minimal(self):
guaranteed_analysis = GuaranteedAnalysis(
title="Guaranteed minimum analysis",
nutrients=[self.nutrient_1, self.nutrient_2]
)
self.assertTrue(guaranteed_analysis.is_minimal)

def test_set_is_not_minimal(self):
guaranteed_analysis = GuaranteedAnalysis(
title="Guaranteed analysis",
nutrients=[self.nutrient_1, self.nutrient_2]
)

self.assertIsNone(guaranteed_analysis.is_minimal)


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