| Total Complexity | 54 |
| Total Lines | 101 |
| Duplicated Lines | 17.82 % |
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 TestCapakeyCachedGateway 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 | # -*- coding: utf-8 -*- |
||
| 33 | @pytest.mark.skipif( |
||
| 34 | not pytest.config.getoption('--capakey-integration'), |
||
| 35 | reason = 'No CAPAKEY Integration tests required' |
||
| 36 | ) |
||
| 37 | class TestCapakeyCachedGateway: |
||
| 38 | |||
| 39 | def test_cache_is_configured(self, capakey_gateway): |
||
| 40 | from dogpile.cache.backends.memory import MemoryBackend |
||
| 41 | assert isinstance( |
||
| 42 | capakey_gateway.caches['permanent'].backend, |
||
| 43 | MemoryBackend |
||
| 44 | ) |
||
| 45 | assert capakey_gateway.caches['permanent'].is_configured |
||
| 46 | |||
| 47 | def test_list_gemeenten(self, capakey_gateway): |
||
| 48 | res = capakey_gateway.list_gemeenten() |
||
| 49 | assert isinstance(res, list) |
||
| 50 | assert capakey_gateway.caches['permanent'].get('ListAdmGemeenten#1') == res |
||
| 51 | |||
| 52 | def test_list_gemeenten_different_sort(self, capakey_gateway): |
||
| 53 | res = capakey_gateway.list_gemeenten(2) |
||
| 54 | assert isinstance(res, list) |
||
| 55 | assert capakey_gateway.caches['permanent'].get('ListAdmGemeenten#2') == res |
||
| 56 | from dogpile.cache.api import NO_VALUE |
||
| 57 | assert capakey_gateway.caches['permanent'].get('ListAdmGemeenten#1') == NO_VALUE |
||
| 58 | |||
| 59 | def test_get_gemeente_by_id(self, capakey_gateway): |
||
| 60 | res = capakey_gateway.get_gemeente_by_id(44021) |
||
| 61 | assert isinstance(res, Gemeente) |
||
| 62 | assert capakey_gateway.caches['long'].get('GetAdmGemeenteByNiscode#44021') == res |
||
| 63 | |||
| 64 | def test_list_afdelingen(self, capakey_gateway): |
||
| 65 | res = capakey_gateway.list_kadastrale_afdelingen() |
||
| 66 | assert isinstance(res, list) |
||
| 67 | assert capakey_gateway.caches['permanent'].get('ListKadAfdelingen#1') == res |
||
| 68 | |||
| 69 | def test_list_afdelingen_by_gemeente(self, capakey_gateway): |
||
| 70 | g = capakey_gateway.get_gemeente_by_id(44021) |
||
| 71 | assert capakey_gateway.caches['long'].get('GetAdmGemeenteByNiscode#44021') == g |
||
| 72 | res = capakey_gateway.list_kadastrale_afdelingen_by_gemeente(g) |
||
| 73 | assert isinstance(res, list) |
||
| 74 | assert capakey_gateway.caches['permanent'].get('ListKadAfdelingenByNiscode#44021#1') == res |
||
| 75 | |||
| 76 | def test_get_kadastrale_afdeling_by_id(self, capakey_gateway): |
||
| 77 | res = capakey_gateway.get_kadastrale_afdeling_by_id(44021) |
||
| 78 | assert isinstance(res, Afdeling) |
||
| 79 | assert res.id == 44021 |
||
| 80 | assert isinstance(res.gemeente, Gemeente) |
||
| 81 | assert res.gemeente.id == 44021 |
||
| 82 | assert capakey_gateway.caches['long'].get('GetKadAfdelingByKadAfdelingcode#44021') == res |
||
| 83 | |||
| 84 | def test_list_secties_by_afdeling_id(self, capakey_gateway): |
||
| 85 | res = capakey_gateway.list_secties_by_afdeling(44021) |
||
| 86 | assert isinstance(res, list) |
||
| 87 | assert (len(res), 1) |
||
| 88 | assert capakey_gateway.caches['long'].get('ListKadSectiesByKadAfdelingcode#44021') == res |
||
| 89 | |||
| 90 | def test_get_sectie_by_id_and_afdeling(self, capakey_gateway): |
||
| 91 | a = capakey_gateway.get_kadastrale_afdeling_by_id(44021) |
||
| 92 | res = capakey_gateway.get_sectie_by_id_and_afdeling('A', a) |
||
| 93 | assert isinstance(res, Sectie) |
||
| 94 | assert res.id == 'A' |
||
| 95 | assert res.afdeling.id == 44021 |
||
| 96 | assert capakey_gateway.caches['long'].get('GetKadSectieByKadSectiecode#44021#A') == res |
||
| 97 | |||
| 98 | def test_list_percelen_by_sectie(self, capakey_gateway): |
||
| 99 | s = capakey_gateway.get_sectie_by_id_and_afdeling('A', 44021) |
||
| 100 | res = capakey_gateway.list_percelen_by_sectie(s) |
||
| 101 | assert isinstance(res, list) |
||
| 102 | assert len(res) > 0 |
||
| 103 | assert capakey_gateway.caches['short'].get('ListKadPerceelsnummersByKadSectiecode#44021#A#1') == res |
||
| 104 | |||
| 105 | def test_get_perceel_by_id_and_sectie(self, capakey_gateway): |
||
| 106 | s = capakey_gateway.get_sectie_by_id_and_afdeling('A', 44021) |
||
| 107 | percelen = capakey_gateway.list_percelen_by_sectie(s) |
||
| 108 | perc = percelen[0] |
||
| 109 | res = capakey_gateway.get_perceel_by_id_and_sectie(perc.id, s) |
||
| 110 | assert isinstance(res, Perceel) |
||
| 111 | assert res.sectie.id =='A' |
||
| 112 | assert res.sectie.afdeling.id == 44021 |
||
| 113 | assert capakey_gateway.caches['short'].get('GetKadPerceelsnummerByKadPerceelsnummer#44021#A#%s' % perc.id) == res |
||
| 114 | |||
| 115 | def test_get_perceel_by_capakey(self, capakey_gateway): |
||
| 116 | s = capakey_gateway.get_sectie_by_id_and_afdeling('A', 44021) |
||
| 117 | percelen = capakey_gateway.list_percelen_by_sectie(s) |
||
| 118 | perc = percelen[0] |
||
| 119 | res = capakey_gateway.get_perceel_by_capakey(perc.capakey) |
||
| 120 | assert isinstance(res, Perceel) |
||
| 121 | assert res.sectie.id == 'A' |
||
| 122 | assert res.sectie.afdeling.id, 44021 |
||
| 123 | assert capakey_gateway.caches['short'].get('GetKadPerceelsnummerByCaPaKey#%s' % perc.capakey) == res |
||
| 124 | |||
| 125 | def test_get_perceel_by_percid(self, capakey_gateway): |
||
| 126 | s = capakey_gateway.get_sectie_by_id_and_afdeling('A', 44021) |
||
| 127 | percelen = capakey_gateway.list_percelen_by_sectie(s) |
||
| 128 | perc = percelen[0] |
||
| 129 | res = capakey_gateway.get_perceel_by_percid(perc.percid) |
||
| 130 | assert isinstance(res, Perceel) |
||
| 131 | assert res.sectie.id == 'A' |
||
| 132 | assert res.sectie.afdeling.id == 44021 |
||
| 133 | assert capakey_gateway.caches['short'].get('GetKadPerceelsnummerByPERCID#%s' % perc.percid) == res |
||
| 134 |