| 1 | import logging |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | import pbcommand |
| 6 | from pbcommand.models.common import (PacBioFloatChoiceOption, PacBioIntOption, |
| 7 | PacBioStringOption, |
| 8 | PacBioStringChoiceOption, |
| 9 | PacBioIntChoiceOption, |
| 10 | PacBioBooleanOption, PacBioFloatOption) |
| 11 | |
| 12 | log = logging.getLogger(__name__) |
| 13 | |
| 14 | |
| 15 | def _to_i(s): |
| 16 | return "test.task_options.{}".format(s) |
| 17 | |
| 18 | |
| 19 | def get_or(i, value): |
| 20 | return value if i is None else i |
| 21 | |
| 22 | |
| 23 | class TestPacBioBasicOptionTest: |
| 24 | OPT_KLASS = PacBioIntOption |
| 25 | OPT_ID = "alpha" |
| 26 | OPT_NAME = "Alpha" |
| 27 | OPT_DESC = "Alpha description" |
| 28 | OPT_DEFAULT = 2 |
| 29 | |
| 30 | def _to_opt(self, i=None, n=None, v=None, d=None): |
| 31 | ix = get_or(i, _to_i(self.OPT_ID)) |
| 32 | name = get_or(n, self.OPT_NAME) |
| 33 | value = get_or(v, self.OPT_DEFAULT) |
| 34 | description = get_or(d, self.OPT_DESC) |
| 35 | return self.OPT_KLASS(ix, name, value, description) |
| 36 | |
| 37 | def test_sanity_option(self): |
| 38 | o = self._to_opt() |
| 39 | log.debug("Created option {o}".format(o=o)) |
| 40 | |
| 41 | assert o.option_id == "test.task_options.{}".format(self.OPT_ID) |
| 42 | assert o.name == self.OPT_NAME |
| 43 | assert o.default == self.OPT_DEFAULT |
| 44 | assert o.description == self.OPT_DESC |
| 45 | |
| 46 | |
| 47 | class TestPacBioIntOptionTest(TestPacBioBasicOptionTest): |
| 48 | |
| 49 | def test_bad_value_string(self): |
| 50 | with pytest.raises(TypeError): |
| 51 | _ = self._to_opt(v="bad-string") |
| 52 | |
| 53 | def test_bad_value_float(self): |
| 54 | with pytest.raises(TypeError): |
| 55 | _ = self._to_opt(v=3.124) |
| 56 | |
| 57 | def test_bad_value_boolean(self): |
| 58 | with pytest.raises(TypeError): |
| 59 | _ = self._to_opt(v=True) |
| 60 | |
| 61 | |
| 62 | class TestPacBioBooleanOptionTest(TestPacBioBasicOptionTest): |
| 63 | OPT_KLASS = PacBioBooleanOption |
| 64 | OPT_DEFAULT = True |
| 65 | |
| 66 | def test_bad_value_int(self): |
| 67 | with pytest.raises(TypeError): |
| 68 | _ = self._to_opt(v=1) |
| 69 | |
| 70 | def test_bad_value_float(self): |
| 71 | with pytest.raises(TypeError): |
| 72 | _ = self._to_opt(v=1.10) |
| 73 | |
| 74 | def test_bad_value_string(self): |
| 75 | with pytest.raises(TypeError): |
| 76 | _ = self._to_opt(v="bad-string") |
| 77 | |
| 78 | |
| 79 | class TestPacBioFloatOptionTest(TestPacBioBasicOptionTest): |
| 80 | OPT_KLASS = PacBioFloatOption |
| 81 | OPT_DEFAULT = 3.1415 |
| 82 | |
| 83 | def test_coerced_value_int(self): |
| 84 | o = self._to_opt(v=1) |
| 85 | assert o.default == 1.0 |
| 86 | |
| 87 | def test_bad_value_boolean(self): |
| 88 | with pytest.raises(TypeError): |
| 89 | _ = self._to_opt(v=True) |
| 90 | |
| 91 | def test_bad_value_string(self): |
| 92 | with pytest.raises(TypeError): |
| 93 | _ = self._to_opt(v="bad-string") |
| 94 | |
| 95 | def test_bad_value_float_tuple(self): |
| 96 | with pytest.raises(TypeError): |
| 97 | _ = self._to_opt(v=(1.0, 2.0)) |
| 98 | |
| 99 | |
| 100 | class TestPacBioStringOptionTest(TestPacBioBasicOptionTest): |
| 101 | OPT_KLASS = PacBioStringOption |
| 102 | OPT_DEFAULT = "gamma" |
| 103 | |
| 104 | def test_bad_value_int(self): |
| 105 | with pytest.raises(TypeError): |
| 106 | _ = self._to_opt(v=1) |
| 107 | |
| 108 | def test_bad_value_float(self): |
| 109 | with pytest.raises(TypeError): |
| 110 | _ = self._to_opt(v=1.10) |
| 111 | |
| 112 | |
| 113 | class TestPacBioBasicChoiceTest(TestPacBioBasicOptionTest): |
| 114 | OPT_KLASS = PacBioStringChoiceOption |
| 115 | OPT_CHOICES = ("alpha", "beta", "gamma") |
| 116 | OPT_DEFAULT = "beta" |
| 117 | OPT_BAD_OPTION = "delta" |
| 118 | |
| 119 | def _to_opt(self, i=None, n=None, v=None, d=None, c=None): |
| 120 | ix = get_or(i, _to_i(self.OPT_ID)) |
| 121 | name = get_or(n, self.OPT_NAME) |
| 122 | value = get_or(v, self.OPT_DEFAULT) |
| 123 | description = get_or(d, self.OPT_DESC) |
| 124 | choices = get_or(c, self.OPT_CHOICES) |
| 125 | return self.OPT_KLASS(ix, name, value, description, choices) |
| 126 | |
| 127 | def test_sanity_choice_option(self): |
| 128 | o = self._to_opt() |
| 129 | assert o.choices == self.OPT_CHOICES |
| 130 | |
| 131 | def test_bad_invalid_choice(self): |
| 132 | with pytest.raises(ValueError): |
| 133 | _ = self._to_opt(v=self.OPT_BAD_OPTION) |
| 134 | |
| 135 | |
| 136 | class TestPacBioChoiceStringOptionTest(TestPacBioBasicChoiceTest): |
| 137 | OPT_KLASS = PacBioStringChoiceOption |
| 138 | OPT_DEFAULT = "gamma" |
| 139 | OPT_BAD_OPTION = "Bad-value" |
| 140 | |
| 141 | |
| 142 | class TestPacBioIntChoiceOptionTest(TestPacBioBasicChoiceTest): |
| 143 | OPT_KLASS = PacBioIntChoiceOption |
| 144 | OPT_CHOICES = (1, 2, 7) |
| 145 | OPT_DEFAULT = 2 |
| 146 | OPT_BAD_OPTION = 3 |
| 147 | |
| 148 | |
| 149 | class TestPacBioFloatChoiceOptionTest(TestPacBioBasicChoiceTest): |
| 150 | OPT_KLASS = PacBioFloatChoiceOption |
| 151 | OPT_CHOICES = (1.0, 2.0, 7.0) |
| 152 | OPT_DEFAULT = 2.0 |
| 153 | OPT_BAD_OPTION = -1.0 |
| 154 | |
| 155 | def test_coerce_float_choices(self): |
| 156 | choices = (10, 12123, 12) |
| 157 | o = self._to_opt(c=choices, v=12) |
| 158 | |
| 159 | def test_bad_choices(self): |
| 160 | choices = (1, 2.0, "bad-value") |
| 161 | with pytest.raises(TypeError): |
| 162 | _ = self._to_opt(c=choices) |