|
| 1 | +from enum import Enum |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from newtype import NewType, newtype_exclude |
| 6 | + |
| 7 | + |
| 8 | +class ENV(NewType(str), Enum): |
| 9 | + |
| 10 | + LOCAL = "LOCAL" |
| 11 | + DEV = "DEV" |
| 12 | + SIT = "SIT" |
| 13 | + UAT = "UAT" |
| 14 | + PREPROD = "PREPROD" |
| 15 | + PROD = "PROD" |
| 16 | + |
| 17 | +class RegularENV(str, Enum): |
| 18 | + |
| 19 | + LOCAL = "LOCAL" |
| 20 | + DEV = "DEV" |
| 21 | + SIT = "SIT" |
| 22 | + UAT = "UAT" |
| 23 | + PREPROD = "PREPROD" |
| 24 | + PROD = "PROD" |
| 25 | + |
| 26 | +class ENVVariant(str): |
| 27 | + |
| 28 | + __VALID_MEMBERS__ = ["LOCAL", "DEV", "SIT", "UAT", "PREPROD", "PROD"] |
| 29 | + |
| 30 | + def __new__(cls, value: str): |
| 31 | + members = ENVVariant.__VALID_MEMBERS__ |
| 32 | + # if isinstance(value, RollYourOwnNewTypeEnum): |
| 33 | + # value_as_str = str(value.value) |
| 34 | + # else: |
| 35 | + value_as_str = str(value) |
| 36 | + if value_as_str not in members: |
| 37 | + raise ValueError(f"`value` = {value} must be one of `{members}`; `value_as_str` = {value_as_str}") |
| 38 | + return super().__new__(cls, value_as_str) |
| 39 | + |
| 40 | + # why not i write my own `.replace(..)` |
| 41 | + # yes, you can but how? |
| 42 | + def my_replace(self, old: "ENVVariant", new: "ENVVariant", count: int=-1): |
| 43 | + return ENVVariant(str(self).replace(str(old), str(new), count)) |
| 44 | + |
| 45 | +class RollYourOwnNewTypeEnum(ENVVariant, Enum): |
| 46 | + |
| 47 | + LOCAL = "LOCAL" |
| 48 | + DEV = "DEV" |
| 49 | + SIT = "SIT" |
| 50 | + UAT = "UAT" |
| 51 | + PREPROD = "PREPROD" |
| 52 | + PROD = "PROD" |
| 53 | + |
| 54 | + |
| 55 | +def test_nt_env_replace(): |
| 56 | + |
| 57 | + env = ENV.LOCAL |
| 58 | + |
| 59 | + assert env is ENV.LOCAL |
| 60 | + assert env is not ENV.DEV |
| 61 | + assert isinstance(env, ENV) |
| 62 | + |
| 63 | + # let's say now we want to replace the environment |
| 64 | + # nevermind about the reason why we want to do so |
| 65 | + env = env.replace(ENV.LOCAL, ENV.DEV) |
| 66 | + |
| 67 | + # replacement is successful |
| 68 | + assert env is ENV.DEV |
| 69 | + assert env is not ENV.LOCAL |
| 70 | + |
| 71 | + # still an `ENV` |
| 72 | + assert isinstance(env, ENV) |
| 73 | + assert isinstance(env, str) |
| 74 | + |
| 75 | + with pytest.raises(ValueError): |
| 76 | + # cannot replace with something that is not a `ENV` |
| 77 | + env = env.replace(ENV.DEV, "NotAnEnv") |
| 78 | + |
| 79 | + with pytest.raises(ValueError): |
| 80 | + # cannot even make 'DEV' -> 'dev' |
| 81 | + env = env.lower() |
| 82 | + |
| 83 | +def test_reg_env_replace(): |
| 84 | + |
| 85 | + env = RegularENV.LOCAL |
| 86 | + |
| 87 | + # expected outcomes |
| 88 | + assert env is RegularENV.LOCAL # pass |
| 89 | + assert env is not RegularENV.DEV # pass |
| 90 | + assert isinstance(env, RegularENV) # pass |
| 91 | + |
| 92 | + # now we try to replace |
| 93 | + env = env.replace(RegularENV.LOCAL, RegularENV.DEV) |
| 94 | + |
| 95 | + # we are hoping that it will continue to be a `RegularENV.DEV` but it is not |
| 96 | + assert env is not RegularENV.DEV # pass, no longer a `RegularENV` |
| 97 | + assert env is not RegularENV.LOCAL # pass, no longer a `RegularENV` |
| 98 | + assert not isinstance(env, RegularENV) |
| 99 | + assert isinstance(env, str) # 'downcast' (?) to `str` |
| 100 | + |
| 101 | +def test_ryont_env_replace(): |
| 102 | + |
| 103 | + env = RollYourOwnNewTypeEnum.LOCAL |
| 104 | + |
| 105 | + # expected outcomes |
| 106 | + assert env is RollYourOwnNewTypeEnum.LOCAL # pass |
| 107 | + assert env is not RollYourOwnNewTypeEnum.DEV # pass |
| 108 | + assert isinstance(env, RollYourOwnNewTypeEnum) # pass |
| 109 | + |
| 110 | + # now we try to replace |
| 111 | + env = env.replace(RollYourOwnNewTypeEnum.LOCAL, RollYourOwnNewTypeEnum.DEV) |
| 112 | + |
| 113 | + # we are hoping that it will continue to be a `RollYourOwnNewTypeEnum.DEV` but it is not |
| 114 | + assert env is not RollYourOwnNewTypeEnum.DEV # pass, no longer a `RollYourOwnNewTypeEnum` |
| 115 | + assert env is not RollYourOwnNewTypeEnum.LOCAL # pass, no longer a `RollYourOwnNewTypeEnum` |
| 116 | + assert not isinstance(env, RollYourOwnNewTypeEnum) |
| 117 | + assert isinstance(env, str) # 'downcast' (?) to `str` |
| 118 | + |
| 119 | + with pytest.raises(AssertionError): |
| 120 | + assert env is RollYourOwnNewTypeEnum.DEV |
| 121 | + |
| 122 | + with pytest.raises(AssertionError): |
| 123 | + assert env is RollYourOwnNewTypeEnum.DEV |
| 124 | + |
| 125 | + with pytest.raises(AssertionError): |
| 126 | + assert isinstance(env, RollYourOwnNewTypeEnum) |
| 127 | + |
| 128 | + env = env.replace("DEV", "NotAnEnv") |
| 129 | + assert env == "NotAnEnv" # this 'shouldn't' pass but it does |
| 130 | + |
| 131 | + env = RollYourOwnNewTypeEnum.LOCAL |
| 132 | + |
| 133 | + # env = env.my_replace(RollYourOwnNewTypeEnum.LOCAL, RollYourOwnNewTypeEnum.PREPROD) |
| 134 | + |
| 135 | + assert isinstance(env, str) |
| 136 | + assert env is not RollYourOwnNewTypeEnum.PREPROD |
| 137 | + assert isinstance(env, RollYourOwnNewTypeEnum) |
0 commit comments