Passed
Branch master (0f5d95)
by Christophe
01:59 queued 01:08
created

MemoryContextTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 16.09 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 87
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A test_model() 0 5 1
A test_population() 0 5 1
A test___contains__() 14 14 1
A test___str__() 0 6 1
A test___bool__() 0 15 1
B test___init__() 0 31 1

How to fix   Duplicated Code   

Duplicated Code

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
# This Python file uses the following encoding: utf-8
2
3
from unittest import TestCase
4
5
from galactic.context.memory import *
6
7
8
class MemoryContextTest(TestCase):
9
    def test___init__(self):
10
        context = MemoryContext({'mybool': bool, 'myint': int})
11
        self.assertEqual(
12
            str(context),
13
            "{'population': [], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}",
14
            "The context has not been correctly initialized"
15
        )
16
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
17
        self.assertEqual(
18
            str(context.population),
19
            "['0', '1']",
20
            "The context has not been correctly initialized"
21
        )
22
        self.assertEqual(
23
            str(context.model),
24
            "{'mybool': <class 'bool'>, 'myint': <class 'int'>}",
25
            "The context has not been correctly initialized"
26
        )
27
        context = MemoryContext(
28
            {'mybool': bool, 'myint': int},
29
            {'0': {'mybool': True}, '1': {'myint': 1}}
30
        )
31
        self.assertEqual(
32
            str(context.population['0']),
33
            "{'mybool': True, 'myint': 0}",
34
            "The context has not been correctly initialized"
35
        )
36
        self.assertEqual(
37
            str(context.population['1']),
38
            "{'mybool': False, 'myint': 1}",
39
            "The context has not been correctly initialized"
40
        )
41
42
    def test_population(self):
43
        context = MemoryContext({'mybool': bool, 'myint': int})
44
        self.assertTrue(
45
            isinstance(context.population, Population),
46
            "The property population is instance of Population"
47
        )
48
49
    def test_model(self):
50
        context = MemoryContext({'mybool': bool, 'myint': int})
51
        self.assertTrue(
52
            isinstance(context.model, Model),
53
            "The property model is instance of Model"
54
        )
55
56 View Code Duplication
    def test___contains__(self):
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
57
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
58
        self.assertTrue(
59
            context.population['0'] in context,
60
            "The first individual of the context is in the context"
61
        )
62
        self.assertTrue(
63
            context.model['mybool'] in context,
64
            "The first attribute of the context is in the context"
65
        )
66
        other = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
67
        self.assertFalse(
68
            other.population['0'] in context,
69
            "An unknown element is not in the context"
70
        )
71
72
    def test___bool__(self):
73
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
74
        self.assertTrue(
75
            context,
76
            "The context is not empty"
77
        )
78
        context = MemoryContext({'mybool': bool, 'myint': int})
79
        self.assertFalse(
80
            context,
81
            "The context is empty"
82
        )
83
        context = MemoryContext({}, ['0', '1'])
84
        self.assertFalse(
85
            context,
86
            "The context is empty"
87
        )
88
89
    def test___str__(self):
90
        context = MemoryContext({'mybool': bool}, ['0'])
91
        self.assertEqual(
92
            str(context),
93
            "{'population': ['0'], 'model': {'mybool': <class 'bool'>}}",
94
            "The string representation of this context is not correct"
95
        )
96
97
98
class MemoryPopulationTest(TestCase):
99
    def test_context(self):
100
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
101
        self.assertEqual(
102
            context.population.context,
103
            context,
104
            "The context of the population must be the original context"
105
        )
106
107
    def test_model(self):
108
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
109
        self.assertEqual(
110
            context.population.model,
111
            context.model,
112
            "The model of the population must be the original model"
113
        )
114
115
    def test___getitem__(self):
116
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
117
        self.assertEqual(
118
            str(context.population['0']),
119
            "{'mybool': False, 'myint': 0}",
120
            "The textual representation of the first individual is not correct"
121
        )
122
123
    def test___setitem__(self):
124
        context = MemoryContext({'mybool': bool, 'myint': int})
125
        context.population['0'] = {}
126
        self.assertEqual(
127
            str(context.population['0']),
128
            "{'mybool': False, 'myint': 0}",
129
            "The population is not correct"
130
        )
131
        context.population['0'] = {'mybool': True}
132
        self.assertEqual(
133
            str(context.population['0']),
134
            "{'mybool': True, 'myint': 0}",
135
            "The population is not correct"
136
        )
137
        with self.assertRaises(KeyError):
138
            context.population['0'] = {'abc': 1}
139
140
    def test___delitem__(self):
141
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
142
        del context.population['0']
143
        self.assertFalse(
144
            bool(context.population),
145
            "The population must be empty"
146
        )
147
148
    def test___len__(self):
149
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
150
        self.assertEqual(
151
            len(context.population),
152
            2,
153
            "The length of the population is not correct"
154
        )
155
156
    def test___bool__(self):
157
        context = MemoryContext({'mybool': bool, 'myint': int})
158
        self.assertFalse(
159
            bool(context.population),
160
            "The population must be empty"
161
        )
162
        context.population['0'] = {}
163
        self.assertTrue(
164
            bool(context.population),
165
            "The population must be not empty"
166
        )
167
168
    def test___iter__(self):
169
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
170
        for identifier, individual in context.population.items():
171
            self.assertTrue(
172
                isinstance(individual, Individual),
173
                "Each individual must be an instance of the Individual class"
174
            )
175
            self.assertTrue(
176
                isinstance(identifier, str),
177
                "Each identifier must be a str"
178
            )
179
180
    def test___str__(self):
181
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
182
        self.assertEqual(
183
            str(context.population),
184
            "['0', '1']",
185
            "The string representation of the population is not correct"
186
        )
187
188
189
class MemoryModelTest(TestCase):
190
    def test_context(self):
191
        context = MemoryContext({'mybool': bool, 'myint': int})
192
        self.assertEqual(
193
            context.model.context,
194
            context,
195
            "The context of the model must be the original context"
196
        )
197
198
    def test_population(self):
199
        context = MemoryContext({'mybool': bool, 'myint': int})
200
        self.assertEqual(
201
            context.model.population,
202
            context.population,
203
            "The population of the model must be the original population"
204
        )
205
206
    def test___getitem__(self):
207
        context = MemoryContext({'mybool': bool, 'myint': int})
208
        self.assertEqual(
209
            str(context.model['mybool']),
210
            "{'name': 'mybool', 'type': <class 'bool'>}",
211
            "The attribute mybool is not correct"
212
        )
213
214
    def test___setitem__(self):
215
        context = MemoryContext(
216
            {'mybool': bool, 'myint': int},
217
            {'0': {'mybool': True}, '1': {'myint': 1}}
218
        )
219
        context.model['mybool'] = bool
220
        self.assertEqual(
221
            {identifier: str(individual) for identifier, individual in context.population.items()},
222
            {'0': "{'mybool': True, 'myint': 0}", '1': "{'mybool': False, 'myint': 1}"},
223
            "The individuals must remain unchanged"
224
        )
225
        context.model['mybool'] = int
226
        self.assertEqual(
227
            {identifier: str(individual) for identifier, individual in context.population.items()},
228
            {'0': "{'mybool': 1, 'myint': 0}", '1': "{'mybool': 0, 'myint': 1}"},
229
            "The individuals must change"
230
        )
231
        context.model['mybool'] = list
232
        self.assertEqual(
233
            {identifier: str(individual) for identifier, individual in context.population.items()},
234
            {'0': "{'mybool': [], 'myint': 0}", '1': "{'mybool': [], 'myint': 1}"},
235
            "The individuals must change"
236
        )
237
        del context.model['myint']
238
        context.model['myint2'] = int
239
        self.assertEqual(
240
            {identifier: str(individual) for identifier, individual in context.population.items()},
241
            {'0': "{'mybool': [], 'myint2': 0}", '1': "{'mybool': [], 'myint2': 0}"},
242
            "The individuals must change"
243
        )
244
245
    def test___delitem__(self):
246
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
247
        del context.model['mybool']
248
        self.assertEqual(
249
            str(context),
250
            "{'population': ['0', '1'], 'model': {'myint': <class 'int'>}}",
251
            "The context is not correct"
252
        )
253
        self.assertEqual(
254
            str(context.population['0']),
255
            "{'myint': 0}",
256
            "The context is not correct"
257
        )
258
259
    def test___len__(self):
260
        context = MemoryContext({'mybool': bool, 'myint': int})
261
        self.assertEqual(
262
            len(context.model),
263
            2,
264
            "The length of the model is not correct"
265
        )
266
267
    def test___bool__(self):
268
        context = MemoryContext({'mybool': bool, 'myint': int})
269
        self.assertTrue(
270
            bool(context.model),
271
            "The bool representation of the model is not correct"
272
        )
273
274
    def test___iter__(self):
275
        context = MemoryContext({'mybool': bool, 'myint': int})
276
        self.assertEqual(
277
            list(iter(context.model)),
278
            ['mybool', 'myint'],
279
            "The iteration over the model is not correct"
280
        )
281
282
    def test___str__(self):
283
        context = MemoryContext({'mybool': bool, 'myint': int})
284
        self.assertEqual(
285
            str(context.model),
286
            "{'mybool': <class 'bool'>, 'myint': <class 'int'>}",
287
            "The string representation of the model must be correct"
288
        )
289
290
291
class MemoryIndividualTest(TestCase):
292
    def test_identifier(self):
293
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
294
        self.assertEqual(
295
            context.population['0'].identifier,
296
            '0',
297
            "The identifier must be '0'"
298
        )
299
300
    def test_population(self):
301
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
302
        self.assertEqual(
303
            context.population['0'].population,
304
            context.population,
305
            "The population of the individual named '0' must be the context population"
306
        )
307
308
    def test_context(self):
309
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
310
        self.assertEqual(
311
            context.population['0'].context,
312
            context,
313
            "The context of the individual named '0' must be the context"
314
        )
315
316
    def test_model(self):
317
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
318
        self.assertEqual(
319
            context.population['0'].model,
320
            context.model,
321
            "The model of the individual named '0' must be the context model"
322
        )
323
324
    def test_value(self):
325
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
326
        self.assertEqual(
327
            context.population['0'].value(context.model['mybool']),
328
            bool(),
329
            "The value 'mybool' of the individual named '0' must be the False"
330
        )
331
        self.assertEqual(
332
            context.population['0'].value(context.model['myint']),
333
            int(),
334
            "The value 'myint' of the individual named '0' must be the 0"
335
        )
336
        other = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
337
        with self.assertRaises(ValueError):
338
            _ = context.population['0'].value(other.model['myint'])
339
340 View Code Duplication
    def test___setitem__(self):
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
341
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
342
        context.population['0']['mybool'] = True
343
        context.population['0']['myint'] = 1
344
        self.assertEqual(
345
            str(context.population['0']),
346
            "{'mybool': True, 'myint': 1}",
347
            "The textual representation of the first individual is not correct"
348
        )
349
        context.population['1']['mybool'] = True
350
        context.population['1']['myint'] = 2
351
        self.assertEqual(
352
            str(context.population['1']),
353
            "{'mybool': True, 'myint': 2}",
354
            "The textual representation of the first individual is not correct"
355
        )
356
        with self.assertRaises(KeyError):
357
            context.population['0']['unknown'] = 1
358
        with self.assertRaises(ValueError):
359
            context.population['0']['myint'] = 'abc'
360
361
    def test___getitem__(self):
362
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
363
        self.assertEqual(
364
            context.population['0']['mybool'],
365
            bool(),
366
            "The first value is not correct"
367
        )
368
        self.assertEqual(
369
            context.population['0']['myint'],
370
            int(),
371
            "The second value is not correct"
372
        )
373
        with self.assertRaises(KeyError):
374
            _ = context.population['0']['unknown']
375
376
    def test___delitem__(self):
377
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
378
        with self.assertRaises(ValueError):
379
            del context.population['0']['myint']
380
381
    def test___len__(self):
382
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
383
        self.assertEqual(
384
            len(context.population['0']),
385
            2,
386
            "The length of the individual is not correct"
387
        )
388
389 View Code Duplication
    def test___eq__(self):
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
390
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
391
        self.assertTrue(
392
            context.population['0'] == context.population['0'],
393
            "The individual is equal to itself"
394
        )
395
        self.assertFalse(
396
            context.population['0'] == context.population['1'],
397
            "Two different individuals are unequal"
398
        )
399
        other = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
400
        self.assertFalse(
401
            context.population['0'] == other.population['0'],
402
            "Two individuals from two different contexts are unequal"
403
        )
404
405
    def test___iter__(self):
406
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
407
        self.assertEqual(
408
            list(iter(context.population['0'])),
409
            ['mybool', 'myint'],
410
            "The iteration over the individual is not correct"
411
        )
412
413
    def test___str__(self):
414
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
415
        self.assertEqual(
416
            str(context.population['0']),
417
            "{'mybool': False, 'myint': 0}",
418
            "The string representation of the individual is not correct"
419
        )
420
421
422
class MemoryAttributeTest(TestCase):
423
    def test_name(self):
424
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
425
        self.assertEqual(
426
            context.model['mybool'].name,
427
            'mybool',
428
            "The name must be 'mybool'"
429
        )
430
431
    def test_type(self):
432
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0'])
433
        self.assertEqual(
434
            context.model['mybool'].type,
435
            bool,
436
            "The type must be bool"
437
        )
438
439
    def test_value(self):
440
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
441
        self.assertEqual(
442
            context.model['mybool'].value(context.population['0']),
443
            context.population['0'].value(context.model['mybool']),
444
            "The value must be correct"
445
        )
446
447
    def test___getitem__(self):
448
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
449
        self.assertEqual(
450
            context.model['mybool']['0'],
451
            context.population['0']['mybool'],
452
            "The value must be correct"
453
        )
454
455 View Code Duplication
    def test___setitem__(self):
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
456
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
457
        context.model['mybool']['0'] = True
458
        context.model['myint']['0'] = 1
459
        self.assertEqual(
460
            str(context.population['0']),
461
            "{'mybool': True, 'myint': 1}",
462
            "The textual representation of the first individual is not correct"
463
        )
464
        context.model['mybool']['1'] = True
465
        context.model['myint']['1'] = 2
466
        self.assertEqual(
467
            str(context.population['1']),
468
            "{'mybool': True, 'myint': 2}",
469
            "The textual representation of the first individual is not correct"
470
        )
471
        with self.assertRaises(KeyError):
472
            context.model['mybool']['unknown'] = 1
473
        with self.assertRaises(ValueError):
474
            context.model['myint']['0'] = 'abc'
475
476
    def test___len__(self):
477
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
478
        self.assertEqual(
479
            len(context.model['mybool']),
480
            2,
481
            "The length must be correct"
482
        )
483
484
    def test___eq__(self):
485
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
486
        self.assertTrue(
487
            context.model['mybool'] == context.model['mybool'],
488
            "The equality must be correct"
489
        )
490
491
    def test___iter__(self):
492
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
493
        self.assertEqual(
494
            list(iter(context.model['mybool'])),
495
            ['0', '1'],
496
            "The iter must be correct"
497
        )
498
499
    def test___str__(self):
500
        context = MemoryContext({'mybool': bool, 'myint': int}, ['0', '1'])
501
        self.assertEqual(
502
            str(context.model['mybool']),
503
            "{'name': 'mybool', 'type': <class 'bool'>}",
504
            "The iter must be correct"
505
        )
506
507