Skip to content

Commit

Permalink
removal of extra enum structure to use existing mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
markj0hnst0n committed Jul 4, 2024
1 parent 043f23a commit f6a75bd
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 44 deletions.
14 changes: 2 additions & 12 deletions mail/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,8 @@ class UnitMapping(enum.Enum):
MCG = 116 # microgram
MCL = 110 # microlitre

@classmethod
def serializer_choices(cls):
# Used by the API serializer for validation.
return list(cls.__members__.keys())


class LegacyUnitMapping(enum.Enum):
# These units below are the old values that are not used going forward
# They are kept here for backwards compatibility
# These units below are the old values or LegacyUnitMapping that are
# not used going forward. They are kept here for backwards compatibility
MIM = 111 # milligram
MCM = 116 # microgram
MIR = 74 # millilitre
Expand All @@ -219,9 +212,6 @@ def serializer_choices(cls):
# Used by the API serializer for validation.
return list(cls.__members__.keys())

def get_class_attributes(cls):
return [attr for attr in dir(cls)]


class MailReadStatuses(TextChoices):
READ = "READ"
Expand Down
6 changes: 1 addition & 5 deletions mail/libraries/lite_to_edifact_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
LicenceActionEnum,
LicenceTypeEnum,
UnitMapping,
LegacyUnitMapping,
)
from mail.libraries import chiefprotocol, chieftypes
from mail.libraries.edifact_validator import (
Expand Down Expand Up @@ -137,10 +136,7 @@ def generate_lines_for_licence(licence: LicencePayload) -> Iterable[chieftypes._
controlled_by = "Q" # usage is controlled by quantity only
quantity = commodity.get("quantity")

if commodity["unit"] in LegacyUnitMapping.get_class_attributes(LegacyUnitMapping):
qunit = LegacyUnitMapping[commodity["unit"]]
else:
qunit = UnitMapping[commodity["unit"]]
qunit = UnitMapping[commodity["unit"]]

if qunit == UnitMapping.NAR:
quantity = int(quantity)
Expand Down
5 changes: 1 addition & 4 deletions mail/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ class GoodSerializer(serializers.Serializer):
name = serializers.CharField()
description = serializers.CharField(max_length=2000, allow_blank=True)
quantity = serializers.DecimalField(decimal_places=3, max_digits=13)
combined_unit_mapping_choices = (
enums.UnitMapping.serializer_choices() + enums.LegacyUnitMapping.serializer_choices()
)
unit = serializers.ChoiceField(choices=combined_unit_mapping_choices)
unit = serializers.ChoiceField(choices=enums.UnitMapping.serializer_choices())


class CountrySerializer(serializers.Serializer):
Expand Down
24 changes: 1 addition & 23 deletions mail/tests/test_enums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from mail.enums import UnitMapping, LegacyUnitMapping
from mail.enums import UnitMapping


class UnitMappingTests(unittest.TestCase):
Expand All @@ -25,34 +25,12 @@ def test_convert_code(self):
with self.subTest(code=code, value=value):
self.assertEqual(value, UnitMapping[code].value)

def test_convert_legacy_code(self):
data = [
("MIM", 111),
("MCM", 116),
("MIR", 74),
("MCR", 110),
]

for code, value in data:
with self.subTest(code=code, value=value):
self.assertEqual(value, LegacyUnitMapping[code].value)

def test_convert_none(self):
with self.assertRaises(KeyError):
UnitMapping[None]

def test_convert_none_old(self):
with self.assertRaises(KeyError):
LegacyUnitMapping[None]

def test_serializer_choices(self):
choices = UnitMapping.serializer_choices()
expected = ["NAR", "GRM", "KGM", "MTK", "MTR", "LTR", "MTQ", "MLT", "ITG", "MGM", "TON", "MCG", "MCL"]

self.assertEqual(choices, expected)

def test_serializer_choices_old(self):
choices = LegacyUnitMapping.serializer_choices()
expected = ["MIM", "MCM", "MIR", "MCR"]

self.assertEqual(choices, expected)

0 comments on commit f6a75bd

Please sign in to comment.