From eb2f2ef13c86acf82fbe94df13a70a4857087285 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Thu, 20 Jan 2022 11:57:53 -0500 Subject: [PATCH] Handle invalid material conversions --- elite_engineering/trade.py | 12 ++++++++++-- pyproject.toml | 1 + tests/test_trade.py | 30 +++++++++++++++--------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/elite_engineering/trade.py b/elite_engineering/trade.py index 5fa3615..f7a3bf4 100644 --- a/elite_engineering/trade.py +++ b/elite_engineering/trade.py @@ -1,6 +1,8 @@ import dataclasses import fractions +from returns.maybe import Maybe, Some, Nothing + materials = { "raw": { "Raw Material Category 1": ["Carbon", "Vanadium", "Niobium", "Yttrium"], @@ -141,7 +143,9 @@ class Material: category: str grade: int - def trade_ratio(self, other: "Material") -> fractions.Fraction: + MAX_CAPACITY: int = 300 + + def trade_ratio(self, other: "Material") -> Maybe[fractions.Fraction]: grade_base = 3 if self.grade <= other.grade else 6 grade_ratio = fractions.Fraction( grade_base ** self.grade, grade_base ** other.grade @@ -151,4 +155,8 @@ class Material: if self.category == other.category else fractions.Fraction(6, 1) ) - return grade_ratio * category_ratio + ratio = grade_ratio * category_ratio + if ratio.numerator > self.MAX_CAPACITY or ratio.denominator > self.MAX_CAPACITY: + return Nothing + else: + return Some(ratio) diff --git a/pyproject.toml b/pyproject.toml index 36909da..83a3f41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,6 +7,7 @@ license = "MIT" [tool.poetry.dependencies] python = "^3.9" +returns = "^0.18.0" [tool.poetry.dev-dependencies] black = "^21.12b0" diff --git a/tests/test_trade.py b/tests/test_trade.py index 115dbb0..4b6112e 100644 --- a/tests/test_trade.py +++ b/tests/test_trade.py @@ -1,6 +1,8 @@ import fractions import unittest +from returns.maybe import Maybe + from elite_engineering import trade @@ -8,22 +10,21 @@ class TradeCalculationTests(unittest.TestCase): def format_ratio(self, ratio: fractions.Fraction) -> str: return "{} → {}".format(ratio.numerator, ratio.denominator) - def test_same_category_exchange(self): + def test_same_category_exchange(self) -> None: expected = [ ["1 → 1", "1 → 3", "1 → 9", "1 → 27", "1 → 81"], ["6 → 1", "1 → 1", "1 → 3", "1 → 9", "1 → 27"], ["36 → 1", "6 → 1", "1 → 1", "1 → 3", "1 → 9"], ["216 → 1", "36 → 1", "6 → 1", "1 → 1", "1 → 3"], - ["1296 → 1", "216 → 1", "36 → 1", "6 → 1", "1 → 1"], + ["-", "216 → 1", "36 → 1", "6 → 1", "1 → 1"], ] actual = [ [ - self.format_ratio( - trade.Material("name", "category", grade_in).trade_ratio( - trade.Material("name", "category", grade_out) - ) - ) + trade.Material("name", "category", grade_in) + .trade_ratio(trade.Material("name", "category", grade_out)) + .map(self.format_ratio) + .value_or("-") for grade_out in [1, 2, 3, 4, 5] ] for grade_in in [1, 2, 3, 4, 5] @@ -31,22 +32,21 @@ class TradeCalculationTests(unittest.TestCase): self.assertEqual(expected, actual) - def test_different_category_exchange(self): + def test_different_category_exchange(self) -> None: expected = [ ["6 → 1", "2 → 1", "2 → 3", "2 → 9", "2 → 27"], ["36 → 1", "6 → 1", "2 → 1", "2 → 3", "2 → 9"], ["216 → 1", "36 → 1", "6 → 1", "2 → 1", "2 → 3"], - ["1296 → 1", "216 → 1", "36 → 1", "6 → 1", "2 → 1"], - ["7776 → 1", "1296 → 1", "216 → 1", "36 → 1", "6 → 1"], + ["-", "216 → 1", "36 → 1", "6 → 1", "2 → 1"], + ["-", "-", "216 → 1", "36 → 1", "6 → 1"], ] actual = [ [ - self.format_ratio( - trade.Material("name", "category", grade_in).trade_ratio( - trade.Material("name", "other", grade_out) - ) - ) + trade.Material("name", "category", grade_in) + .trade_ratio(trade.Material("name", "other", grade_out)) + .map(self.format_ratio) + .value_or("-") for grade_out in [1, 2, 3, 4, 5] ] for grade_in in [1, 2, 3, 4, 5]