Passed
Branch master (0f4c76)
by Christophe
56s
created

MemoryIndividualTest.test_value()   A

Complexity

Conditions 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 21
rs 9.3142
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(
11
            definition={'mybool': bool, 'myint': int}
12
        )
13
        self.assertEqual(
14
            str(context),
15
            "{'population': [], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}",
16
            "The context has not been correctly initialized"
17
        )
18
        context = MemoryContext(
19
            definition={'mybool': bool, 'myint': int},
20
            individuals=['0', '1']
21
        )
22
        self.assertEqual(
23
            str(context.population),
24
            "['0', '1']",
25
            "The context has not been correctly initialized"
26
        )
27
        self.assertEqual(
28
            str(context.model),
29
            "{'mybool': <class 'bool'>, 'myint': <class 'int'>}",
30
            "The context has not been correctly initialized"
31
        )
32
        context = MemoryContext(
33
            definition={'mybool': bool, 'myint': int},
34
            individuals={'0': {'mybool': True}, '1': {'myint': 1}}
35
        )
36
        self.assertEqual(
37
            str(context.population['0']),
38
            "{'mybool': True, 'myint': 0}",
39
            "The context has not been correctly initialized"
40
        )
41
        self.assertEqual(
42
            str(context.population['1']),
43
            "{'mybool': False, 'myint': 1}",
44
            "The context has not been correctly initialized"
45
        )
46
        with self.assertRaises(TypeError):
47
            _ = MemoryContext(definition='bad value')
48
        with self.assertRaises(TypeError):
49
            _ = MemoryContext(individuals=1000)
50
51
    def test_population(self):
52
        context = MemoryContext(
53
            definition={'mybool': bool, 'myint': int}
54
        )
55
        self.assertTrue(
56
            isinstance(context.population, Population),
57
            "The property population is instance of Population"
58
        )
59
60
    def test_model(self):
61
        context = MemoryContext(
62
            definition={'mybool': bool, 'myint': int}
63
        )
64
        self.assertTrue(
65
            isinstance(context.model, Model),
66
            "The property model is instance of Model"
67
        )
68
69
    def test___contains__(self):
70
        context = MemoryContext(
71
            definition={'mybool': bool, 'myint': int},
72
            individuals=['0', '1']
73
        )
74
        self.assertTrue(
75
            context.population['0'] in context,
76
            "The first individual of the context is in the context"
77
        )
78
        self.assertTrue(
79
            context.model['mybool'] in context,
80
            "The first attribute of the context is in the context"
81
        )
82
        other = MemoryContext(
83
            definition={'mybool': bool, 'myint': int},
84
            individuals=['0', '1']
85
        )
86
        self.assertFalse(
87
            other.population['0'] in context,
88
            "An unknown element is not in the context"
89
        )
90
91
    def test___bool__(self):
92
        context = MemoryContext(
93
            definition={'mybool': bool, 'myint': int},
94
            individuals=['0', '1']
95
        )
96
        self.assertTrue(
97
            context,
98
            "The context is not empty"
99
        )
100
        context = MemoryContext(
101
            definition={'mybool': bool, 'myint': int}
102
        )
103
        self.assertFalse(
104
            context,
105
            "The context is empty"
106
        )
107
        context = MemoryContext(
108
            individuals=['0', '1']
109
        )
110
        self.assertFalse(
111
            context,
112
            "The context is empty"
113
        )
114
115
    def test___str__(self):
116
        context = MemoryContext(
117
            definition={'mybool': bool},
118
            individuals=['0']
119
        )
120
        self.assertEqual(
121
            str(context),
122
            "{'population': ['0'], 'model': {'mybool': <class 'bool'>}}",
123
            "The string representation of this context is not correct"
124
        )
125
126
127
class MemoryPopulationTest(TestCase):
128
    def test_context(self):
129
        context = MemoryContext(
130
            definition={'mybool': bool, 'myint': int},
131
            individuals=['0', '1']
132
        )
133
        self.assertEqual(
134
            context.population.context,
135
            context,
136
            "The context of the population must be the original context"
137
        )
138
139
    def test_model(self):
140
        context = MemoryContext(
141
            definition={'mybool': bool, 'myint': int},
142
            individuals=['0', '1']
143
        )
144
        self.assertEqual(
145
            context.population.model,
146
            context.model,
147
            "The model of the population must be the original model"
148
        )
149
150
    def test___getitem__(self):
151
        context = MemoryContext(
152
            definition={'mybool': bool, 'myint': int},
153
            individuals=['0', '1']
154
        )
155
        self.assertEqual(
156
            str(context.population['0']),
157
            "{'mybool': False, 'myint': 0}",
158
            "The textual representation of the first individual is not correct"
159
        )
160
161
    def test___setitem__(self):
162
        context = MemoryContext(
163
            definition={'mybool': bool, 'myint': int}
164
        )
165
        context.population['0'] = {}
166
        self.assertEqual(
167
            str(context.population['0']),
168
            "{'mybool': False, 'myint': 0}",
169
            "The population is not correct"
170
        )
171
        context.population['0'] = {'mybool': True}
172
        self.assertEqual(
173
            str(context.population['0']),
174
            "{'mybool': True, 'myint': 0}",
175
            "The population is not correct"
176
        )
177
        with self.assertRaises(KeyError):
178
            context.population['0'] = {'abc': 1}
179
180
    def test___delitem__(self):
181
        context = MemoryContext(
182
            definition={'mybool': bool, 'myint': int},
183
            individuals=['0']
184
        )
185
        del context.population['0']
186
        self.assertFalse(
187
            bool(context.population),
188
            "The population must be empty"
189
        )
190
191
    def test___len__(self):
192
        context = MemoryContext(
193
            definition={'mybool': bool, 'myint': int},
194
            individuals=['0', '1']
195
        )
196
        self.assertEqual(
197
            len(context.population),
198
            2,
199
            "The length of the population is not correct"
200
        )
201
202
    def test___bool__(self):
203
        context = MemoryContext(
204
            definition={'mybool': bool, 'myint': int}
205
        )
206
        self.assertFalse(
207
            bool(context.population),
208
            "The population must be empty"
209
        )
210
        context.population['0'] = {}
211
        self.assertTrue(
212
            bool(context.population),
213
            "The population must be not empty"
214
        )
215
216
    def test___iter__(self):
217
        context = MemoryContext(
218
            definition={'mybool': bool, 'myint': int},
219
            individuals=['0', '1']
220
        )
221
        for identifier, individual in context.population.items():
222
            self.assertTrue(
223
                isinstance(individual, Individual),
224
                "Each individual must be an instance of the Individual class"
225
            )
226
            self.assertTrue(
227
                isinstance(identifier, str),
228
                "Each identifier must be a str"
229
            )
230
231
    def test___str__(self):
232
        context = MemoryContext(
233
            definition={'mybool': bool, 'myint': int},
234
            individuals=['0', '1']
235
        )
236
        self.assertEqual(
237
            str(context.population),
238
            "['0', '1']",
239
            "The string representation of the population is not correct"
240
        )
241
242
243
class MemoryModelTest(TestCase):
244
    def test_context(self):
245
        context = MemoryContext(
246
            definition={'mybool': bool, 'myint': int}
247
        )
248
        self.assertEqual(
249
            context.model.context,
250
            context,
251
            "The context of the model must be the original context"
252
        )
253
254
    def test_population(self):
255
        context = MemoryContext(
256
            definition={'mybool': bool, 'myint': int}
257
        )
258
        self.assertEqual(
259
            context.model.population,
260
            context.population,
261
            "The population of the model must be the original population"
262
        )
263
264
    def test___getitem__(self):
265
        context = MemoryContext(
266
            definition={'mybool': bool, 'myint': int}
267
        )
268
        self.assertEqual(
269
            str(context.model['mybool']),
270
            "{'name': 'mybool', 'type': <class 'bool'>}",
271
            "The attribute mybool is not correct"
272
        )
273
274
    def test___setitem__(self):
275
        context = MemoryContext(
276
            definition={'mybool': bool, 'myint': int},
277
            individuals={'0': {'mybool': True}, '1': {'myint': 1}}
278
        )
279
        context.model['mybool'] = bool
280
        self.assertEqual(
281
            {identifier: str(individual) for identifier, individual in context.population.items()},
282
            {'0': "{'mybool': True, 'myint': 0}", '1': "{'mybool': False, 'myint': 1}"},
283
            "The individuals must remain unchanged"
284
        )
285
        context.model['mybool'] = int
286
        self.assertEqual(
287
            {identifier: str(individual) for identifier, individual in context.population.items()},
288
            {'0': "{'mybool': 1, 'myint': 0}", '1': "{'mybool': 0, 'myint': 1}"},
289
            "The individuals must change"
290
        )
291
        context.model['mybool'] = list
292
        self.assertEqual(
293
            {identifier: str(individual) for identifier, individual in context.population.items()},
294
            {'0': "{'mybool': [], 'myint': 0}", '1': "{'mybool': [], 'myint': 1}"},
295
            "The individuals must change"
296
        )
297
        del context.model['myint']
298
        context.model['myint2'] = int
299
        self.assertEqual(
300
            {identifier: str(individual) for identifier, individual in context.population.items()},
301
            {'0': "{'mybool': [], 'myint2': 0}", '1': "{'mybool': [], 'myint2': 0}"},
302
            "The individuals must change"
303
        )
304
305
    def test___delitem__(self):
306
        context = MemoryContext(
307
            definition={'mybool': bool, 'myint': int},
308
            individuals=['0', '1']
309
        )
310
        del context.model['mybool']
311
        self.assertEqual(
312
            str(context),
313
            "{'population': ['0', '1'], 'model': {'myint': <class 'int'>}}",
314
            "The context is not correct"
315
        )
316
        self.assertEqual(
317
            str(context.population['0']),
318
            "{'myint': 0}",
319
            "The context is not correct"
320
        )
321
322
    def test___len__(self):
323
        context = MemoryContext(
324
            definition={'mybool': bool, 'myint': int}
325
        )
326
        self.assertEqual(
327
            len(context.model),
328
            2,
329
            "The length of the model is not correct"
330
        )
331
332
    def test___bool__(self):
333
        context = MemoryContext(
334
            definition={'mybool': bool, 'myint': int}
335
        )
336
        self.assertTrue(
337
            bool(context.model),
338
            "The bool representation of the model is not correct"
339
        )
340
341
    def test___iter__(self):
342
        context = MemoryContext(
343
            definition={'mybool': bool, 'myint': int}
344
        )
345
        self.assertEqual(
346
            list(iter(context.model)),
347
            ['mybool', 'myint'],
348
            "The iteration over the model is not correct"
349
        )
350
351
    def test___str__(self):
352
        context = MemoryContext(
353
            definition={'mybool': bool, 'myint': int}
354
        )
355
        self.assertEqual(
356
            str(context.model),
357
            "{'mybool': <class 'bool'>, 'myint': <class 'int'>}",
358
            "The string representation of the model must be correct"
359
        )
360
361
362
class MemoryIndividualTest(TestCase):
363
    def test_identifier(self):
364
        context = MemoryContext(
365
            definition={'mybool': bool, 'myint': int},
366
            individuals=['0']
367
        )
368
        self.assertEqual(
369
            context.population['0'].identifier,
370
            '0',
371
            "The identifier must be '0'"
372
        )
373
374
    def test_population(self):
375
        context = MemoryContext(
376
            definition={'mybool': bool, 'myint': int},
377
            individuals=['0']
378
        )
379
        self.assertEqual(
380
            context.population['0'].population,
381
            context.population,
382
            "The population of the individual named '0' must be the context population"
383
        )
384
385
    def test_context(self):
386
        context = MemoryContext(
387
            definition={'mybool': bool, 'myint': int},
388
            individuals=['0']
389
        )
390
        self.assertEqual(
391
            context.population['0'].context,
392
            context,
393
            "The context of the individual named '0' must be the context"
394
        )
395
396
    def test_model(self):
397
        context = MemoryContext(
398
            definition={'mybool': bool, 'myint': int},
399
            individuals=['0']
400
        )
401
        self.assertEqual(
402
            context.population['0'].model,
403
            context.model,
404
            "The model of the individual named '0' must be the context model"
405
        )
406
407
    def test_value(self):
408
        context = MemoryContext(
409
            definition={'mybool': bool, 'myint': int},
410
            individuals=['0']
411
        )
412
        self.assertEqual(
413
            context.population['0'].value(context.model['mybool']),
414
            bool(),
415
            "The value 'mybool' of the individual named '0' must be the False"
416
        )
417
        self.assertEqual(
418
            context.population['0'].value(context.model['myint']),
419
            int(),
420
            "The value 'myint' of the individual named '0' must be the 0"
421
        )
422
        other = MemoryContext(
423
            definition={'mybool': bool, 'myint': int},
424
            individuals=['0']
425
        )
426
        with self.assertRaises(ValueError):
427
            _ = context.population['0'].value(other.model['myint'])
428
429
    def test___setitem__(self):
430
        context = MemoryContext(
431
            definition={'mybool': bool, 'myint': int},
432
            individuals=['0', '1']
433
        )
434
        context.population['0']['mybool'] = True
435
        context.population['0']['myint'] = 1
436
        self.assertEqual(
437
            str(context.population['0']),
438
            "{'mybool': True, 'myint': 1}",
439
            "The textual representation of the first individual is not correct"
440
        )
441
        context.population['1']['mybool'] = True
442
        context.population['1']['myint'] = 2
443
        self.assertEqual(
444
            str(context.population['1']),
445
            "{'mybool': True, 'myint': 2}",
446
            "The textual representation of the first individual is not correct"
447
        )
448
        with self.assertRaises(KeyError):
449
            context.population['0']['unknown'] = 1
450
        with self.assertRaises(ValueError):
451
            context.population['0']['myint'] = 'abc'
452
453
    def test___getitem__(self):
454
        context = MemoryContext(
455
            definition={'mybool': bool, 'myint': int},
456
            individuals=['0']
457
        )
458
        self.assertEqual(
459
            context.population['0']['mybool'],
460
            bool(),
461
            "The first value is not correct"
462
        )
463
        self.assertEqual(
464
            context.population['0']['myint'],
465
            int(),
466
            "The second value is not correct"
467
        )
468
        with self.assertRaises(KeyError):
469
            _ = context.population['0']['unknown']
470
471
    def test___delitem__(self):
472
        context = MemoryContext(
473
            definition={'mybool': bool, 'myint': int},
474
            individuals=['0']
475
        )
476
        with self.assertRaises(ValueError):
477
            del context.population['0']['myint']
478
479
    def test___len__(self):
480
        context = MemoryContext(
481
            definition={'mybool': bool, 'myint': int},
482
            individuals=['0']
483
        )
484
        self.assertEqual(
485
            len(context.population['0']),
486
            2,
487
            "The length of the individual is not correct"
488
        )
489
490
    def test___eq__(self):
491
        context = MemoryContext(
492
            definition={'mybool': bool, 'myint': int},
493
            individuals=['0', '1']
494
        )
495
        self.assertTrue(
496
            context.population['0'] == context.population['0'],
497
            "The individual is equal to itself"
498
        )
499
        self.assertFalse(
500
            context.population['0'] == context.population['1'],
501
            "Two different individuals are unequal"
502
        )
503
        other = MemoryContext(
504
            definition={'mybool': bool, 'myint': int},
505
            individuals=['0', '1']
506
        )
507
        self.assertFalse(
508
            context.population['0'] == other.population['0'],
509
            "Two individuals from two different contexts are unequal"
510
        )
511
512
    def test___iter__(self):
513
        context = MemoryContext(
514
            definition={'mybool': bool, 'myint': int},
515
            individuals=['0', '1']
516
        )
517
        self.assertEqual(
518
            list(iter(context.population['0'])),
519
            ['mybool', 'myint'],
520
            "The iteration over the individual is not correct"
521
        )
522
523
    def test___str__(self):
524
        context = MemoryContext(
525
            definition={'mybool': bool, 'myint': int},
526
            individuals=['0', '1']
527
        )
528
        self.assertEqual(
529
            str(context.population['0']),
530
            "{'mybool': False, 'myint': 0}",
531
            "The string representation of the individual is not correct"
532
        )
533
534
535
class MemoryAttributeTest(TestCase):
536
    def test_name(self):
537
        context = MemoryContext(
538
            definition={'mybool': bool, 'myint': int},
539
            individuals=['0']
540
        )
541
        self.assertEqual(
542
            context.model['mybool'].name,
543
            'mybool',
544
            "The name must be 'mybool'"
545
        )
546
547
    def test_type(self):
548
        context = MemoryContext(
549
            definition={'mybool': bool, 'myint': int},
550
            individuals=['0']
551
        )
552
        self.assertEqual(
553
            context.model['mybool'].type,
554
            bool,
555
            "The type must be bool"
556
        )
557
558
    def test_value(self):
559
        context = MemoryContext(
560
            definition={'mybool': bool, 'myint': int},
561
            individuals=['0', '1']
562
        )
563
        self.assertEqual(
564
            context.model['mybool'].value(context.population['0']),
565
            context.population['0'].value(context.model['mybool']),
566
            "The value must be correct"
567
        )
568
569
    def test___getitem__(self):
570
        context = MemoryContext(
571
            definition={'mybool': bool, 'myint': int},
572
            individuals=['0', '1']
573
        )
574
        self.assertEqual(
575
            context.model['mybool']['0'],
576
            context.population['0']['mybool'],
577
            "The value must be correct"
578
        )
579
580
    def test___setitem__(self):
581
        context = MemoryContext(
582
            definition={'mybool': bool, 'myint': int},
583
            individuals=['0', '1']
584
        )
585
        context.model['mybool']['0'] = True
586
        context.model['myint']['0'] = 1
587
        self.assertEqual(
588
            str(context.population['0']),
589
            "{'mybool': True, 'myint': 1}",
590
            "The textual representation of the first individual is not correct"
591
        )
592
        context.model['mybool']['1'] = True
593
        context.model['myint']['1'] = 2
594
        self.assertEqual(
595
            str(context.population['1']),
596
            "{'mybool': True, 'myint': 2}",
597
            "The textual representation of the first individual is not correct"
598
        )
599
        with self.assertRaises(KeyError):
600
            context.model['mybool']['unknown'] = 1
601
        with self.assertRaises(ValueError):
602
            context.model['myint']['0'] = 'abc'
603
604
    def test___len__(self):
605
        context = MemoryContext(
606
            definition={'mybool': bool, 'myint': int},
607
            individuals=['0', '1']
608
        )
609
        self.assertEqual(
610
            len(context.model['mybool']),
611
            2,
612
            "The length must be correct"
613
        )
614
615
    def test___eq__(self):
616
        context = MemoryContext(
617
            definition={'mybool': bool, 'myint': int},
618
            individuals=['0', '1']
619
        )
620
        self.assertTrue(
621
            context.model['mybool'] == context.model['mybool'],
622
            "The equality must be correct"
623
        )
624
625
    def test___iter__(self):
626
        context = MemoryContext(
627
            definition={'mybool': bool, 'myint': int},
628
            individuals=['0', '1']
629
        )
630
        self.assertEqual(
631
            list(iter(context.model['mybool'])),
632
            ['0', '1'],
633
            "The iter must be correct"
634
        )
635
636
    def test___str__(self):
637
        context = MemoryContext(
638
            definition={'mybool': bool, 'myint': int},
639
            individuals=['0', '1']
640
        )
641
        self.assertEqual(
642
            str(context.model['mybool']),
643
            "{'name': 'mybool', 'type': <class 'bool'>}",
644
            "The iter must be correct"
645
        )
646