| Total Complexity | 43 |
| Total Lines | 181 |
| Duplicated Lines | 54.14 % |
| Changes | 0 | ||
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:
Complex classes like often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Integration tests for the package.""" |
||
| 150 | class TestStandard: |
||
| 151 | """Integration tests for standard attribute types.""" |
||
| 152 | |||
| 153 | @yorm.attr(status=yorm.types.Boolean) |
||
| 154 | class StatusDictionary(Dictionary): |
||
| 155 | pass |
||
| 156 | |||
| 157 | def test_decorator(self, tmpdir): |
||
| 158 | """Verify standard attribute types dump/parse correctly (decorator).""" |
||
| 159 | tmpdir.chdir() |
||
| 160 | sample = SampleStandardDecorated('sample') |
||
| 161 | assert "tmp/default/sample.yml" == sample.__mapper__.path |
||
| 162 | |||
| 163 | log("Checking object default values...") |
||
| 164 | assert {} == sample.object |
||
| 165 | assert [] == sample.array |
||
| 166 | assert "" == sample.string |
||
| 167 | assert 0 == sample.number_int |
||
| 168 | assert 0.0 == sample.number_real |
||
| 169 | assert True is sample.truthy |
||
| 170 | assert False is sample.falsey |
||
| 171 | assert None is sample.null |
||
| 172 | |||
| 173 | log("Changing object values...") |
||
| 174 | sample.object = {'key2': 'value'} |
||
| 175 | sample.array = [0, 1, 2] |
||
| 176 | sample.string = "Hello, world!" |
||
| 177 | sample.number_int = 42 |
||
| 178 | sample.number_real = 4.2 |
||
| 179 | sample.truthy = False |
||
| 180 | sample.falsey = True |
||
| 181 | |||
| 182 | log("Checking file contents...") |
||
| 183 | assert strip(""" |
||
| 184 | array: |
||
| 185 | - 0 |
||
| 186 | - 1 |
||
| 187 | - 2 |
||
| 188 | falsey: true |
||
| 189 | number_int: 42 |
||
| 190 | number_real: 4.2 |
||
| 191 | object: {} |
||
| 192 | string: Hello, world! |
||
| 193 | truthy: false |
||
| 194 | """) == sample.__mapper__.text |
||
| 195 | |||
| 196 | log("Changing file contents...") |
||
| 197 | refresh_file_modification_times() |
||
| 198 | sample.__mapper__.text = strip(""" |
||
| 199 | array: [4, 5, 6] |
||
| 200 | falsey: null |
||
| 201 | number_int: 42 |
||
| 202 | number_real: '4.2' |
||
| 203 | object: {'status': false} |
||
| 204 | string: "abc" |
||
| 205 | truthy: null |
||
| 206 | """) |
||
| 207 | |||
| 208 | log("Checking object values...") |
||
| 209 | assert {'status': False} == sample.object |
||
| 210 | assert [4, 5, 6] == sample.array |
||
| 211 | assert "abc" == sample.string |
||
| 212 | assert 42 == sample.number_int |
||
| 213 | assert 4.2 == sample.number_real |
||
| 214 | assert False is sample.truthy |
||
| 215 | assert False is sample.falsey |
||
| 216 | |||
| 217 | def test_function(self, tmpdir): |
||
| 218 | """Verify standard attribute types dump/parse correctly (function).""" |
||
| 219 | tmpdir.chdir() |
||
| 220 | _sample = SampleStandard() |
||
| 221 | View Code Duplication | attrs = {'object': self.StatusDictionary, |
|
| 222 | 'array': IntegerList, |
||
| 223 | 'string': String, |
||
| 224 | 'number_int': Integer, |
||
| 225 | 'number_real': Float, |
||
| 226 | 'truthy': Boolean, |
||
| 227 | 'falsey': Boolean} |
||
| 228 | sample = yorm.sync(_sample, "tmp/directory/sample.yml", attrs) |
||
| 229 | assert "tmp/directory/sample.yml" == sample.__mapper__.path |
||
| 230 | |||
| 231 | # check defaults |
||
| 232 | assert {'status': False} == sample.object |
||
| 233 | assert [] == sample.array |
||
| 234 | assert "" == sample.string |
||
| 235 | assert 0 == sample.number_int |
||
| 236 | assert 0.0 == sample.number_real |
||
| 237 | assert True is sample.truthy |
||
| 238 | assert False is sample.falsey |
||
| 239 | assert None is sample.null |
||
| 240 | |||
| 241 | # change object values |
||
| 242 | sample.object = {'key': 'value'} |
||
| 243 | sample.array = [1, 2, 3] |
||
| 244 | sample.string = "Hello, world!" |
||
| 245 | sample.number_int = 42 |
||
| 246 | sample.number_real = 4.2 |
||
| 247 | sample.truthy = None |
||
| 248 | sample.falsey = 1 |
||
| 249 | |||
| 250 | # check file values |
||
| 251 | assert strip(""" |
||
| 252 | array: |
||
| 253 | - 1 |
||
| 254 | - 2 |
||
| 255 | - 3 |
||
| 256 | falsey: true |
||
| 257 | number_int: 42 |
||
| 258 | number_real: 4.2 |
||
| 259 | object: |
||
| 260 | status: false |
||
| 261 | string: Hello, world! |
||
| 262 | truthy: false |
||
| 263 | """) == sample.__mapper__.text |
||
| 264 | |||
| 265 | def test_function_to_json(self, tmpdir): |
||
| 266 | """Verify standard attribute types dump/parse correctly (function).""" |
||
| 267 | tmpdir.chdir() |
||
| 268 | _sample = SampleStandard() |
||
| 269 | View Code Duplication | attrs = {'object': self.StatusDictionary, |
|
| 270 | 'array': IntegerList, |
||
| 271 | 'string': String, |
||
| 272 | 'number_int': Integer, |
||
| 273 | 'number_real': Float, |
||
| 274 | 'truthy': Boolean, |
||
| 275 | 'falsey': Boolean} |
||
| 276 | sample = yorm.sync(_sample, "tmp/directory/sample.json", attrs) |
||
| 277 | assert "tmp/directory/sample.json" == sample.__mapper__.path |
||
| 278 | |||
| 279 | # check defaults |
||
| 280 | assert {'status': False} == sample.object |
||
| 281 | assert [] == sample.array |
||
| 282 | assert "" == sample.string |
||
| 283 | assert 0 == sample.number_int |
||
| 284 | assert 0.0 == sample.number_real |
||
| 285 | assert True is sample.truthy |
||
| 286 | assert False is sample.falsey |
||
| 287 | assert None is sample.null |
||
| 288 | |||
| 289 | # change object values |
||
| 290 | sample.object = {'key': 'value'} |
||
| 291 | sample.array = [1, 2, 3] |
||
| 292 | sample.string = "Hello, world!" |
||
| 293 | sample.number_int = 42 |
||
| 294 | sample.number_real = 4.2 |
||
| 295 | sample.truthy = None |
||
| 296 | sample.falsey = 1 |
||
| 297 | |||
| 298 | # check file values |
||
| 299 | assert strip(""" |
||
| 300 | { |
||
| 301 | "array": [ |
||
| 302 | 1, |
||
| 303 | 2, |
||
| 304 | 3 |
||
| 305 | ], |
||
| 306 | "falsey": true, |
||
| 307 | "number_int": 42, |
||
| 308 | "number_real": 4.2, |
||
| 309 | "object": { |
||
| 310 | "status": false |
||
| 311 | }, |
||
| 312 | "string": "Hello, world!", |
||
| 313 | "truthy": false |
||
| 314 | } |
||
| 315 | """, tabs=2, end='') == sample.__mapper__.text |
||
| 316 | |||
| 317 | def test_auto_off(self, tmpdir): |
||
| 318 | """Verify file updates are disabled with auto save off.""" |
||
| 319 | tmpdir.chdir() |
||
| 320 | sample = SampleDecoratedAutoOff() |
||
| 321 | |||
| 322 | sample.string = "hello" |
||
| 323 | assert "" == sample.__mapper__.text |
||
| 324 | |||
| 325 | sample.__mapper__.auto_save = True |
||
| 326 | sample.string = "world" |
||
| 327 | |||
| 328 | assert strip(""" |
||
| 329 | string: world |
||
| 330 | """) == sample.__mapper__.text |
||
| 331 | |||
| 482 |