| Total Complexity | 21 |
| Total Lines | 107 |
| Duplicated Lines | 30.84 % |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | # pylint: disable=missing-docstring,no-self-use,attribute-defined-outside-init,protected-access,misplaced-comparison-constant |
||
| 86 | class TestMappable: |
||
| 87 | """Unit tests for the `Mappable` class.""" |
||
| 88 | |||
| 89 | def setup_method(self, _): |
||
| 90 | """Create an mappable instance for tests.""" |
||
| 91 | self.sample = SampleMappable() |
||
| 92 | |||
| 93 | def test_init(self): |
||
| 94 | """Verify files are created after initialized.""" |
||
| 95 | text = self.sample.__mapper__._read() |
||
| 96 | assert strip(""" |
||
| 97 | var1: '' |
||
| 98 | var2: 0 |
||
| 99 | var3: false |
||
| 100 | var4: [] |
||
| 101 | var5: |
||
| 102 | status: false |
||
| 103 | """) == text |
||
| 104 | |||
| 105 | View Code Duplication | def test_set(self): |
|
| 106 | """Verify the file is written to after setting an attribute.""" |
||
| 107 | self.sample.var1 = "abc123" |
||
| 108 | self.sample.var2 = 1 |
||
| 109 | self.sample.var3 = True |
||
| 110 | self.sample.var4 = [42] |
||
| 111 | self.sample.var5 = {'status': True} |
||
| 112 | text = self.sample.__mapper__._read() |
||
| 113 | assert strip(""" |
||
| 114 | var1: abc123 |
||
| 115 | var2: 1 |
||
| 116 | var3: true |
||
| 117 | var4: |
||
| 118 | - 42 |
||
| 119 | var5: |
||
| 120 | status: true |
||
| 121 | """) == text |
||
| 122 | |||
| 123 | View Code Duplication | def test_set_converted(self): |
|
| 124 | """Verify conversion occurs when setting attributes.""" |
||
| 125 | self.sample.var1 = 42 |
||
| 126 | self.sample.var2 = "1" |
||
| 127 | self.sample.var3 = 'off' |
||
| 128 | self.sample.var4 = None |
||
| 129 | self.sample.var5 = {'status': 1} |
||
| 130 | text = self.sample.__mapper__._read() |
||
| 131 | assert strip(""" |
||
| 132 | var1: 42 |
||
| 133 | var2: 1 |
||
| 134 | var3: false |
||
| 135 | var4: [] |
||
| 136 | var5: |
||
| 137 | status: true |
||
| 138 | """) == text |
||
| 139 | |||
| 140 | def test_set_error(self): |
||
| 141 | """Verify an exception is raised when a value cannot be converted.""" |
||
| 142 | with pytest.raises(ValueError): |
||
| 143 | self.sample.var2 = "abc" |
||
| 144 | |||
| 145 | def test_get(self): |
||
| 146 | """Verify the file is read from before getting an attribute.""" |
||
| 147 | text = strip(""" |
||
| 148 | var1: def456 |
||
| 149 | var2: 42 |
||
| 150 | var3: off |
||
| 151 | """) |
||
| 152 | self.sample.__mapper__._write(text) |
||
| 153 | assert"def456" == self.sample.var1 |
||
| 154 | assert 42 == self.sample.var2 |
||
| 155 | assert False is self.sample.var3 |
||
| 156 | |||
| 157 | def test_error_invalid_yaml(self): |
||
| 158 | """Verify an exception is raised on invalid YAML.""" |
||
| 159 | text = strip(""" |
||
| 160 | invalid: - |
||
| 161 | """) |
||
| 162 | self.sample.__mapper__._write(text) |
||
| 163 | with pytest.raises(ValueError): |
||
| 164 | print(self.sample.var1) |
||
| 165 | |||
| 166 | def test_error_unexpected_yaml(self): |
||
| 167 | """Verify an exception is raised on unexpected YAML.""" |
||
| 168 | text = strip(""" |
||
| 169 | not a dictionary |
||
| 170 | """) |
||
| 171 | self.sample.__mapper__._write(text) |
||
| 172 | with pytest.raises(ValueError): |
||
| 173 | print(self.sample.var1) |
||
| 174 | |||
| 175 | def test_new(self): |
||
| 176 | """Verify new attributes are added to the object.""" |
||
| 177 | self.sample.__mapper__.strict = False |
||
| 178 | text = strip(""" |
||
| 179 | new: 42 |
||
| 180 | """) |
||
| 181 | self.sample.__mapper__._write(text) |
||
| 182 | assert 42 == self.sample.new |
||
| 183 | |||
| 184 | def test_new_unknown(self): |
||
| 185 | """Verify an exception is raised on new attributes w/ unknown types""" |
||
| 186 | self.sample.__mapper__.strict = False |
||
| 187 | text = strip(""" |
||
| 188 | new: !!timestamp 2001-12-15T02:59:43.1Z |
||
| 189 | """) |
||
| 190 | self.sample.__mapper__._write(text) |
||
| 191 | with pytest.raises(ValueError): |
||
| 192 | print(self.sample.var1) |
||
| 193 | |||
| 274 |