| Total Complexity | 17 |
| Total Lines | 189 |
| Duplicated Lines | 8.47 % |
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 | import unittest |
||
| 8 | class TestJsonRenderer(unittest.TestCase): |
||
| 9 | |||
| 10 | def setUp(self): |
||
| 11 | self.concept = Concept() |
||
| 12 | self.concept.type = 'concept' |
||
| 13 | self.concept.id = 11 |
||
| 14 | self.concept.concept_id = 101 |
||
| 15 | self.concept.uri = 'urn:x-atramhasis-demo:TREES:101' |
||
| 16 | self.concept.conceptscheme_id = 1 |
||
| 17 | |||
| 18 | notes = [] |
||
| 19 | note = Note(note='test note', notetype_id='example', language_id='en') |
||
| 20 | note2 = Note(note='note def', notetype_id='definition', language_id='en') |
||
| 21 | notes.append(note) |
||
| 22 | notes.append(note2) |
||
| 23 | self.concept.notes = notes |
||
| 24 | |||
| 25 | labels = [] |
||
| 26 | label = Label(label='een label', labeltype_id='prefLabel', language_id='nl') |
||
| 27 | label2 = Label(label='other label', labeltype_id='altLabel', language_id='en') |
||
| 28 | label3 = Label(label='and some other label', labeltype_id='altLabel', language_id='en') |
||
| 29 | labels.append(label) |
||
| 30 | labels.append(label2) |
||
| 31 | labels.append(label3) |
||
| 32 | self.concept.labels = labels |
||
| 33 | |||
| 34 | sources = [] |
||
| 35 | source = Source('Van Daele K. 2009') |
||
| 36 | sources.append(source) |
||
| 37 | self.concept.sources = sources |
||
| 38 | |||
| 39 | matches = [] |
||
| 40 | match = Match() |
||
| 41 | match.matchtype = MatchType(name='closeMatch', description='test') |
||
| 42 | match.uri = 'urn:somethingelse:st1' |
||
| 43 | matches.append(match) |
||
| 44 | match2 = Match() |
||
| 45 | match2.matchtype = MatchType(name='closeMatch', description='test') |
||
| 46 | match2.uri = 'urn:somethingelse:st2' |
||
| 47 | matches.append(match2) |
||
| 48 | match3 = Match() |
||
| 49 | match3.matchtype = MatchType(name='exactMatch', description='test') |
||
| 50 | match3.uri = 'urn:something:thingy' |
||
| 51 | matches.append(match3) |
||
| 52 | self.concept.matches = matches |
||
| 53 | |||
| 54 | self.collection = Collection() |
||
| 55 | self.collection.type = 'collection' |
||
| 56 | self.collection.id = 12 |
||
| 57 | self.collection.concept_id = 102 |
||
| 58 | self.collection.uri = 'urn:x-atramhasis-demo:TREES:102' |
||
| 59 | self.collection.conceptscheme_id = 1 |
||
| 60 | |||
| 61 | self.conceptscheme = ConceptScheme() |
||
| 62 | self.conceptscheme.id = 1 |
||
| 63 | self.conceptscheme.labels = labels |
||
| 64 | self.conceptscheme.notes = notes |
||
| 65 | self.conceptscheme.sources = sources |
||
| 66 | self.conceptscheme.uri = None |
||
| 67 | |||
| 68 | self.regis = Registry() |
||
| 69 | self.regis.register_provider(trees) |
||
| 70 | self.request = testing.DummyRequest() |
||
| 71 | self.request.skos_registry = self.regis |
||
| 72 | self.request.matchdict = {'scheme_id': 'TREES'} |
||
| 73 | |||
| 74 | self.concept.member_of.add(self.collection) |
||
| 75 | self.collection.members.add(self.concept) |
||
| 76 | |||
| 77 | def test_label_adapter(self): |
||
| 78 | from atramhasis.renderers import label_adapter |
||
| 79 | l = self.concept.labels[2] |
||
| 80 | label = label_adapter(l, {}) |
||
| 81 | self.assertIsInstance(label, dict) |
||
| 82 | self.assertEqual(label['label'], 'and some other label') |
||
| 83 | self.assertEqual(label['type'], 'altLabel') |
||
| 84 | self.assertEqual(label['language'], 'en') |
||
| 85 | |||
| 86 | def test_note_adapter(self): |
||
| 87 | from atramhasis.renderers import note_adapter |
||
| 88 | n = self.concept.notes[0] |
||
| 89 | note = note_adapter(n, {}) |
||
| 90 | self.assertIsInstance(note, dict) |
||
| 91 | self.assertEqual(note['note'], 'test note') |
||
| 92 | self.assertEqual(note['type'], 'example') |
||
| 93 | self.assertEqual(note['language'], 'en') |
||
| 94 | |||
| 95 | def test_source_adapter(self): |
||
| 96 | from atramhasis.renderers import source_adapter |
||
| 97 | s = self.concept.sources[0] |
||
| 98 | source = source_adapter(s, {}) |
||
| 99 | self.assertIsInstance(source, dict) |
||
| 100 | self.assertEqual(source['citation'], 'Van Daele K. 2009') |
||
| 101 | |||
| 102 | def test_concept_adapter(self): |
||
| 103 | from atramhasis.renderers import concept_adapter |
||
| 104 | c = self.concept |
||
| 105 | concept = concept_adapter(c, {}) |
||
| 106 | self.assertIsInstance(concept, dict) |
||
| 107 | self.assertEqual(concept['id'], 101) |
||
| 108 | self.assertEqual(concept['type'], 'concept') |
||
| 109 | self.assertEqual(concept['uri'], 'urn:x-atramhasis-demo:TREES:101') |
||
| 110 | self.assertIsNotNone(concept['label'], 'een label') |
||
| 111 | self.assertIsInstance(concept['labels'], list) |
||
| 112 | self.assertEqual(len(concept['labels']), 3) |
||
| 113 | self.assertIsInstance(concept['notes'], list) |
||
| 114 | self.assertEqual(len(concept['notes']), 2) |
||
| 115 | self.assertIsInstance(concept['sources'], list) |
||
| 116 | self.assertEqual(len(concept['sources']), 1) |
||
| 117 | self.assertIsInstance(concept['member_of'], list) |
||
| 118 | self.assertEqual(len(concept['member_of']), 1) |
||
| 119 | self.assertIsInstance(concept['narrower'], list) |
||
| 120 | self.assertEqual(len(concept['narrower']), 0) |
||
| 121 | self.assertIsInstance(concept['broader'], list) |
||
| 122 | self.assertEqual(len(concept['broader']), 0) |
||
| 123 | self.assertIsInstance(concept['related'], list) |
||
| 124 | self.assertEqual(len(concept['related']), 0) |
||
| 125 | self.assertIsInstance(concept['matches'], dict) |
||
| 126 | self.assertEqual(len(concept['matches']['exact']), 1) |
||
| 127 | self.assertEqual(len(concept['matches']['close']), 2) |
||
| 128 | |||
| 129 | def test_collection_adapter(self): |
||
| 130 | from atramhasis.renderers import collection_adapter |
||
| 131 | c = self.collection |
||
| 132 | collection = collection_adapter(c, {}) |
||
| 133 | self.assertIsInstance(collection, dict) |
||
| 134 | self.assertEqual(collection['id'], 102) |
||
| 135 | self.assertEqual(collection['type'], 'collection') |
||
| 136 | self.assertEqual(collection['uri'], 'urn:x-atramhasis-demo:TREES:102') |
||
| 137 | self.assertIsNone(collection['label']) |
||
| 138 | self.assertIsInstance(collection['labels'], list) |
||
| 139 | self.assertEqual(len(collection['labels']), 0) |
||
| 140 | self.assertIsInstance(collection['member_of'], list) |
||
| 141 | self.assertEqual(len(collection['member_of']), 0) |
||
| 142 | self.assertIsInstance(collection['members'], list) |
||
| 143 | self.assertEqual(len(collection['members']), 1) |
||
| 144 | |||
| 145 | def test_map_relation_concept(self): |
||
| 146 | from atramhasis.renderers import map_relation |
||
| 147 | c = self.concept |
||
| 148 | relation = map_relation(c) |
||
| 149 | self.assertIsInstance(relation, dict) |
||
| 150 | self.assertEqual(relation['id'], 101) |
||
| 151 | self.assertEqual(relation['type'], 'concept') |
||
| 152 | self.assertEqual(relation['uri'], 'urn:x-atramhasis-demo:TREES:101') |
||
| 153 | self.assertIsNotNone(relation['label'], 'een label') |
||
| 154 | self.assertIsInstance(relation['labels'], list) |
||
| 155 | self.assertEqual(len(relation['labels']), 3) |
||
| 156 | self.assertRaises(KeyError, lambda: relation['notes']) |
||
| 157 | self.assertRaises(KeyError, lambda: relation['member_of']) |
||
| 158 | self.assertRaises(KeyError, lambda: relation['narrower']) |
||
| 159 | self.assertRaises(KeyError, lambda: relation['broader']) |
||
| 160 | self.assertRaises(KeyError, lambda: relation['related']) |
||
| 161 | |||
| 162 | def test_map_relation_collection(self): |
||
| 163 | from atramhasis.renderers import map_relation |
||
| 164 | c = self.collection |
||
| 165 | relation = map_relation(c) |
||
| 166 | self.assertIsInstance(relation, dict) |
||
| 167 | self.assertEqual(relation['id'], 102) |
||
| 168 | self.assertEqual(relation['type'], 'collection') |
||
| 169 | self.assertEqual(relation['uri'], 'urn:x-atramhasis-demo:TREES:102') |
||
| 170 | self.assertIsNone(relation['label']) |
||
| 171 | self.assertIsInstance(relation['labels'], list) |
||
| 172 | self.assertEqual(len(relation['labels']), 0) |
||
| 173 | self.assertRaises(KeyError, lambda: relation['members']) |
||
| 174 | self.assertRaises(KeyError, lambda: relation['member_of']) |
||
| 175 | |||
| 176 | def test_language_adaptor(self): |
||
| 177 | from atramhasis.renderers import language_adaptor |
||
| 178 | l = Language(id='af', name='Afrikaans') |
||
| 179 | res = language_adaptor(l, None) |
||
| 180 | self.assertIsNotNone(res) |
||
| 181 | self.assertIsInstance(res, dict) |
||
| 182 | self.assertEqual(res['id'], 'af') |
||
| 183 | self.assertEqual(res['name'], 'Afrikaans') |
||
| 184 | |||
| 185 | def test_conceptscheme_adapter(self): |
||
| 186 | from atramhasis.renderers import conceptscheme_adapter |
||
| 187 | c = self.conceptscheme |
||
| 188 | conceptscheme = conceptscheme_adapter(c, self.request) |
||
| 189 | self.assertGreater(len(conceptscheme['notes']), 0) |
||
| 190 | self.assertGreater(len(conceptscheme['labels']), 0) |
||
| 191 | self.assertGreater(len(conceptscheme['sources']), 0) |
||
| 192 | self.assertIsNone(conceptscheme['uri']) |
||
| 193 | self.assertEqual('een label', conceptscheme['label']) |
||
| 194 | self.assertEqual(1, conceptscheme['id']) |
||
| 195 | self.assertEqual(0, len(conceptscheme['subject'])) |
||
| 196 | self.assertIsInstance(conceptscheme, dict) |
||
| 197 | |||
| 198 |