-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_landscape_structure.py
More file actions
616 lines (528 loc) · 27.3 KB
/
test_landscape_structure.py
File metadata and controls
616 lines (528 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
"""
This module provides tests for the Cell, HighLand, LowLand, Desert and Water classes.
"""
import itertools
import pytest
import random
from biosim.landscape_structure import HighLand, LowLand, Desert, Water
from biosim.animal_kingdom import Animal, Herbivore, Carnivore
__author__ = 'Mahrin Tasfe'
__email__ = 'mahrin.tasfe@nmbu.no'
# Overall parameters for probabilistic tests
SEED = 12345678 # random seed for tests
ALPHA = 0.01 # significance level for statistical tests
@pytest.fixture
def reset_animal_lists_fixture():
"""
The fixture resets class-level Herbivore & Carnivore numbers before leaving a test.
"""
yield
LowLand.herbivore_objects = []
LowLand.carnivore_objects = []
HighLand.herbivore_objects = []
HighLand.carnivore_objects = []
Desert.herbivore_objects = []
Desert.carnivore_objects = []
@pytest.fixture
def reset_params_fixture():
"""
The fixture resets the fodder amount before leaving a test.
"""
yield
HighLand.set_params({'f_max': 300})
LowLand.set_params({'f_max': 800})
Desert.set_params({'f_max': 0})
Water.set_params({'f_max': 0})
@pytest.fixture
def reset_animal_with_default_parameter_fixture():
"""
The fixture resets the Herbivore and Carnivore parameters before leaving a test.
"""
yield
Herbivore.reset_params()
Carnivore.reset_params()
@pytest.mark.parametrize('cell_type', [HighLand, LowLand, Desert, Water])
def test_default_cell_creation(cell_type):
"""
Test that a default Cell is created with the correct number of Herbivores & Carnivores.
"""
cell_obj = cell_type()
assert cell_obj.get_num_of_herbivores() == 0 and cell_obj.get_num_of_carnivores() == 0
@pytest.mark.parametrize('pop_list, herb_number, carn_number',
[
([{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Carnivore', 'age': 1, 'weight': 10.},
{'species': 'Carnivore', 'age': 1, 'weight': 10.}], 2, 2),
([{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Carnivore', 'age': 1, 'weight': 10.}], 2, 1)
])
def test_default_cell_creation_with_population(pop_list, herb_number, carn_number,
reset_animal_lists_fixture):
"""
Test that Cell is created with correct number of Herbivores & Carnivores.
"""
lowland_obj = LowLand(pop_list)
assert lowland_obj.get_num_of_herbivores() == herb_number \
and lowland_obj.get_num_of_carnivores() == carn_number
@pytest.mark.parametrize('individual_pop',
[
[{'species': 'Dragon', 'age': 1, 'weight': 10}],
[{'species': 'Ghost', 'age': 1, 'weight': 10}],
])
def test_cell_creation_illegal(individual_pop):
"""
Test if ValueError is raised when illegal species type is given for any animal
in the population dictionaries when the Cell was being created.
"""
with pytest.raises(ValueError):
LowLand(individual_pop)
@pytest.mark.parametrize('individual_pop',
[
{'speci': 'Herbivore', 'age': 1, 'weight': 10},
{'specie': 'Herbivore', 'age': 1, 'weight': 10},
{'species': 'Herbivore', 'hello': 1, 'weight': 10},
{'species': 'Herbivore', 'age': 1, 'weights': 10},
])
def test_valid_animal_keys_illegal(individual_pop):
"""
Test if False is returned when illegal keys are given in the population dictionaries.
"""
lowland_obj = LowLand()
assert lowland_obj.valid_animal_keys(individual_pop) == False
@pytest.mark.parametrize('individual_pop',
[
{'species': 'Herbivore', 'age': 1, 'weight': 10},
{'species': 'Carnivore', 'age': 1, 'weight': 10},
])
def test_valid_animal_keys(individual_pop, reset_animal_lists_fixture):
"""
Test if the given population dictionaries have correct keys, true is returned.
"""
lowland_obj = LowLand()
assert lowland_obj.valid_animal_keys(individual_pop)
@pytest.mark.parametrize('pop_list, result',
[
([{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Herbivore', 'age': 1, 'weight': 10.}], 2),
([{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Herbivore', 'age': 1, 'weight': 10.}], 3)
])
def test_get_num_of_herbivores(pop_list, result, reset_animal_lists_fixture):
"""
Test that a Cell created with a population consisting of only herbivores has
the correct number of Herbivores.
"""
lowland_obj = LowLand(pop_list)
assert lowland_obj.get_num_of_herbivores() == result
@pytest.mark.parametrize('pop_list, result',
[
([{'species': 'Carnivore', 'age': 1, 'weight': 10.},
{'species': 'Carnivore', 'age': 1, 'weight': 10.}], 2),
([{'species': 'Carnivore', 'age': 1, 'weight': 10.},
{'species': 'Carnivore', 'age': 1, 'weight': 10.},
{'species': 'Carnivore', 'age': 1, 'weight': 10.}], 3)
])
def test_get_num_of_carnivores(pop_list, result, reset_animal_lists_fixture):
"""
Test that a Cell created with a population consisting of only carnivores has the
correct number of carnivores.
"""
lowland_obj = LowLand(pop_list)
assert lowland_obj.get_num_of_carnivores() == result
@pytest.mark.parametrize('pop_list, herb_number, carn_number',
[
([{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Carnivore', 'age': 1, 'weight': 10.},
{'species': 'Carnivore', 'age': 1, 'weight': 10.}], 2, 2),
([{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Herbivore', 'age': 1, 'weight': 10.},
{'species': 'Carnivore', 'age': 1, 'weight': 10.}], 2, 1)
])
def test_get_animal_number_per_species_in_cell(pop_list, herb_number, carn_number,
reset_animal_lists_fixture):
"""
Test that a Cell created with a population consisting of animal(Herbivores and Carnivores)
has the correct number of animals in the dictionary format.
"""
lowland_obj = LowLand(pop_list)
assert lowland_obj.get_animal_number_per_species_in_cell() == {'Herbivore': herb_number,
'Carnivore': carn_number}
@pytest.mark.parametrize('landscape_type, new_params', [
(HighLand, {'f_max': 0, 'p_max': 10}), (HighLand, {'f_max': -10}),
(LowLand, {'f_max': 0, 'p_max': 10}), (LowLand, {'f_max': -10}),
(Desert, {'f_max': 0, 'p_max': 10}), (Desert, {'f_max': -10}),
(Water, {'f_max': 0, 'p_max': 10}), (Water, {'f_max': -10})])
def test_set_params_illegal(landscape_type, new_params, reset_params_fixture):
"""
Test if ValueError is raised when illegal parameters for a land type is given.
"""
landscape = landscape_type()
with pytest.raises(ValueError):
landscape.set_params(new_params)
@pytest.mark.parametrize('landscape_type, new_params', [(HighLand, {'f_max': 10}),
(LowLand, {'f_max': 10}),
(Desert, {'f_max': 10}),
(Water, {'f_max': 10})])
def test_set_params(landscape_type, new_params, reset_params_fixture):
"""
Test if the set_params() is able to set the new given parameters properly.
"""
landscape_type.set_params(new_params)
result = 10
assert landscape_type.params['f_max'] == result
@pytest.mark.parametrize('landscape_type', [HighLand, LowLand, Desert, Water])
def test_get_params(landscape_type, reset_params_fixture):
"""
Test if the get_params() is able to get the current parameters properly.
"""
result = {'f_max': 100}
landscape_type.set_params(result)
assert landscape_type.get_params() == result
@pytest.mark.parametrize('landscape_type, result',
[(HighLand, True), (LowLand, True), (Desert, True), (Water, False)])
def test_allow_animals_in_cell(landscape_type, result):
"""
Test whether a particular Cell type allow animals.
"""
landscape = landscape_type()
assert landscape.allow_animals_in_cell() == result
@pytest.mark.parametrize('landscape_type, result',
[(HighLand, 10), (LowLand, 10), (Desert, 10), (Water, 10)])
def test_update_fodder(landscape_type, result, reset_params_fixture):
"""
Test whether a particular Cell's fodder amount gets updated after the Herbivores eat.
"""
landscape_type.set_params({'f_max': 90})
landscape_obj = landscape_type()
eaten_amount = 80
landscape_obj.update_fodder(eaten_amount)
assert landscape_obj.get_current_fodder() == result
@pytest.mark.parametrize('landscape_type, result',
[(HighLand, 10), (LowLand, 10), (Desert, 10), (Water, 10)])
def test_get_current_fodder(landscape_type, result, reset_params_fixture):
"""
Test whether a particular Cell's current fodder amount is returned properly.
"""
landscape_type.set_params({'f_max': 10})
landscape_obj = landscape_type()
assert landscape_obj.get_current_fodder() == result
@pytest.mark.parametrize('landscape_type, result',
[(HighLand, 300), (LowLand, 800)])
def test_reset_fodder(landscape_type, result, reset_params_fixture):
"""
Test whether a particular Cell's current fodder amount resets properly.
"""
landscape_object = landscape_type()
eaten_amount = 100
landscape_object.update_fodder(eaten_amount)
landscape_object.reset_fodder()
assert landscape_object.get_current_fodder() == result
@pytest.mark.parametrize('herbivore_population, carnivore_population',
[
[[{'species': 'Herbivore', 'age': 0, 'weight': 1}
for _ in range(10)],
[{'species': 'Carnivore', 'age': 0, 'weight': 5}
for _ in range(5)]],
[[{'species': 'Herbivore', 'age': 0, 'weight': 40}
for _ in range(10)],
[{'species': 'Carnivore', 'age': 0, 'weight': 2}
for _ in range(3)]],
[[{'species': 'Herbivore', 'age': 0, 'weight': 10}
for _ in range(50)],
[{'species': 'Carnivore', 'age': 0, 'weight': 60}
for _ in range(5)]],
])
class TestCellMethodCalls:
"""
Test related to method calls in the Cell class.
"""
@pytest.fixture(autouse=True)
def create_cell(self, herbivore_population, carnivore_population):
"""
The fixture creates a LowLand cell with the herbivore and carnivore population given
in the parameter before entering a test.
"""
self.herbivore_number = len(herbivore_population)
self.carnivore_number = len(carnivore_population)
self.cell = LowLand()
self.cell.add_only_population(herbivore_population)
self.cell.add_only_population(carnivore_population)
@pytest.fixture
def reset_animal_defaults_for_class(self):
"""
The fixture resets the Herbivore and Carnivore parameters before and after leaving a test.
"""
reset_animal_with_default_parameter_fixture()
@pytest.fixture
def reset_animal_lists_for_class(self):
"""
The fixture resets class-level Herbivore & Carnivore numbers before entering and leaving a
test.
"""
reset_animal_lists_fixture()
def test_cell_aging_calls(self, mocker):
"""
Test that cell aging() method calls Animal class's yearly_age_increment() method
the correct number of times.
"""
# mocker.spy wraps Animal.yearly_age_increment() so that we can get a call count.
mocker.spy(Animal, 'yearly_age_increment')
self.cell.aging()
# noinspection PyUnresolvedReferences
assert Animal.yearly_age_increment.call_count == self.herbivore_number + \
self.carnivore_number
def test_cell_weight_loss_calls(self, mocker):
"""
Test that cell weight_loss() method calls Animal class's yearly_weight_decrease()
method the correct number of times.
"""
# mocker.spy wraps Animal.yearly_weight_decrease() so that we can get a call count.
mocker.spy(Animal, 'yearly_weight_decrease')
self.cell.weight_loss()
# noinspection PyUnresolvedReferences
assert Animal.yearly_weight_decrease.call_count == self.herbivore_number + \
self.carnivore_number
def test_cell_feeding_calls_herbivore(self, mocker):
"""
Test that cell feeding() method calls Herbivore class's get_eaten_fodder_amount() method
the correct number of times.
"""
# mocker.spy wraps Herbivore.get_eaten_fodder_amount() so that we can get a call count.
mocker.spy(Herbivore, 'get_eaten_fodder_amount')
self.cell.feeding()
# noinspection PyUnresolvedReferences
assert Herbivore.get_eaten_fodder_amount.call_count == self.herbivore_number
def test_cell_feeding_calls_carnivore(self, mocker):
"""
Test that cell feeding() method calls Carnivore class's carnivore_eating_herbivore()
method the correct number of times. For each carnivore, it keeps eating until is
has eaten F amount or has tried to kill every herbivore in that cell. So the call
count will always be grater than or equal to the number of carnivores present
in that cell.
"""
# mocker.spy wraps Carnivore.carnivore_eating_herbivore() so that we can get a call count.
mocker.spy(Carnivore, 'carnivore_eating_herbivore')
self.cell.feeding()
# noinspection PyUnresolvedReferences
assert Carnivore.carnivore_eating_herbivore.call_count >= self.carnivore_number
def test_cell_aging(self):
"""
Test that animal age correctly via the cell.aging().
"""
self.cell.aging()
# All animals had age 0 initially, must now be 1 after 1 calling of aging()
assert [animal_obj.age for animal_obj in
itertools.chain(self.cell.herbivore_objects,
self.cell.carnivore_objects)] == [1] * (
self.herbivore_number + self.carnivore_number)
def test_cell_dying_calls(self, mocker):
"""
Test that cell dying method calls Animal yearly_death_checker method
the correct number of times.
"""
# mocker.spy wraps Animal.yearly_death_checker() so that we can get a call count.
mocker.spy(Animal, 'yearly_death_checker')
self.cell.dying()
# noinspection PyUnresolvedReferences
assert Animal.yearly_death_checker.call_count == self.herbivore_number + \
self.carnivore_number
@pytest.mark.parametrize('herbivore_population, carnivore_population',
[[[{'species': 'Herbivore', 'age': 10, 'weight': 34} for _ in range(30)],
[{'species': 'Carnivore', 'age': 10, 'weight': 25} for _ in range(5)]],
[[{'species': 'Herbivore', 'age': 10, 'weight': 40} for _ in range(10)],
[{'species': 'Carnivore', 'age': 10, 'weight': 30} for _ in range(3)]],
[[{'species': 'Herbivore', 'age': 2, 'weight': 34} for _ in range(100)],
[{'species': 'Carnivore', 'age': 2, 'weight': 45} for _ in range(100)]]])
def test_procreating(herbivore_population, carnivore_population, reset_animal_lists_fixture,
reset_params_fixture):
"""
Test that during the procreating() method call in Cell class, the number of the herbivore and
the carnivore number never decreases.
"""
herbivore_number_initial = len(herbivore_population)
carnivore_number_initial = len(carnivore_population)
cell = LowLand()
cell.add_only_population(herbivore_population)
cell.add_only_population(carnivore_population)
herbivore_number_old = cell.get_num_of_herbivores()
carnivore_number_old = cell.get_num_of_carnivores()
for _ in range(10):
cell.procreating()
herbivore_number = cell.get_num_of_herbivores()
carnivore_number = cell.get_num_of_carnivores()
# herbivore_number and carnivore_number must never decrease
assert herbivore_number >= herbivore_number_old
assert carnivore_number >= carnivore_number_old
herbivore_number_old, carnivore_number_old = herbivore_number, carnivore_number
# after 10 rounds of procreation checking, no decrease found in the numbers
assert cell.get_num_of_herbivores() >= herbivore_number_initial and \
cell.get_num_of_carnivores() >= carnivore_number_initial
@pytest.mark.parametrize('herbivore_population, carnivore_population',
[[[{'species': 'Herbivore', 'age': 130, 'weight': 1} for _ in range(10)],
[{'species': 'Carnivore', 'age': 110, 'weight': 5} for _ in range(5)]],
[[{'species': 'Herbivore', 'age': 80, 'weight': 40} for _ in range(10)],
[{'species': 'Carnivore', 'age': 10, 'weight': 2} for _ in range(3)]],
[[{'species': 'Herbivore', 'age': 2, 'weight': 10}
for _ in range(10)],
[{'species': 'Carnivore', 'age': 5, 'weight': 5} for _ in range(3)]]])
class TestDying:
"""
Test related to death in the Cell class.
"""
@pytest.fixture(autouse=True)
def create_cell(self, herbivore_population, carnivore_population):
"""
The fixture creates a LowLand cell with the herbivore and carnivore population given
in the parameter before entering a test.
"""
self.herbivore_number = len(herbivore_population)
self.carnivore_number = len(carnivore_population)
self.cell = LowLand()
self.cell.add_only_population(herbivore_population)
self.cell.add_only_population(carnivore_population)
yield
@pytest.fixture
def reset_animal_defaults_for_class(self):
"""
The fixture resets the Herbivore and Carnivore parameters before and after leaving a test.
"""
reset_animal_with_default_parameter_fixture()
def test_dying(self):
"""
Test that during the dying() method call in Cell class, the number of the herbivore and
the carnivore number never increases.
"""
herbivore_number_old = self.cell.get_num_of_herbivores()
carnivore_number_old = self.cell.get_num_of_carnivores()
for _ in range(10):
self.cell.dying()
herbivore_number = self.cell.get_num_of_herbivores()
carnivore_number = self.cell.get_num_of_carnivores()
# herbivore_number and carnivore_number must never increase
assert herbivore_number <= herbivore_number_old
assert carnivore_number <= carnivore_number_old
herbivore_number_old, carnivore_number_old = herbivore_number, carnivore_number
# after 10 rounds of death checking, no increase found in the numbers
assert self.cell.get_num_of_herbivores() < self.herbivore_number and \
self.cell.get_num_of_carnivores() < self.carnivore_number
def test_all_die(self, mocker):
"""
Test that during the dying() method call in Cell class,
if the survival_probability is set to 0, the animal always die.
"""
# Making the survival_probability = 0, so that the calculated probability is
# always greater which ensures the animal will always die.
survival_probability = 0
mocker.patch('random.random', return_value=survival_probability)
self.cell.dying()
assert self.cell.get_num_of_herbivores() == 0 and self.cell.get_num_of_carnivores() == 0
@pytest.mark.parametrize('population, landscape_type, result',
[
([{'species': 'Herbivore', 'age': 5, 'weight': 10.},
{'species': 'Herbivore', 'age': 5, 'weight': 5},
{'species': 'Herbivore', 'age': 1, 'weight': 50},
{'species': 'Carnivore', 'age': 2, 'weight': 3}], HighLand, True),
([], HighLand, False),
([], Water, False),
([], Desert, False)
])
def test_should_run_cycle(population, landscape_type, result, reset_animal_lists_fixture):
"""Test if should_run_cycle() method of landscape works properly.
It should return False if the Cell type is water or if it does not
have any animals in it."""
landscape = landscape_type()
landscape.add_only_population(population)
assert landscape.should_run_cycle() == result
@pytest.mark.parametrize('pop_list, result',
[([{'species': 'Herbivore', 'age': 5, 'weight': 10.},
{'species': 'Herbivore', 'age': 5, 'weight': 5},
{'species': 'Herbivore', 'age': 1, 'weight': 50},
{'species': 'Carnivore', 'age': 2, 'weight': 3}],
([5, 5, 1], [2])),
([{'species': 'Herbivore', 'age': 2, 'weight': 10.},
{'species': 'Carnivore', 'age': 50, 'weight': 2.},
{'species': 'Herbivore', 'age': 1, 'weight': 3},
{'species': 'Carnivore', 'age': 20, 'weight': 2.},
{'species': 'Herbivore', 'age': 10, 'weight': 2.}],
([2, 1, 10], [50, 20]))])
def test_get_animal_ages_from_cell(pop_list, result, reset_animal_lists_fixture):
"""
Test to check whether the get_animal_ages_from_cell() works properly.
This method is supposed to return a tuple of lists with age of the herbivores in the 1st
element and age of the carnivores in the 2nd element.
"""
low_land = LowLand()
low_land.add_only_population(pop_list)
obtained_result = low_land.get_animal_ages_from_cell()
assert obtained_result == result
@pytest.mark.parametrize('pop_list, result', [([{'species': 'Herbivore', 'age': 5, 'weight': 10.},
{'species': 'Herbivore', 'age': 5, 'weight': 5},
{'species': 'Herbivore', 'age': 1, 'weight': 50},
{'species': 'Carnivore', 'age': 2, 'weight': 3}],
([10.0, 5, 50], [3])),
([{'species': 'Herbivore', 'age': 2, 'weight': 10},
{'species': 'Carnivore', 'age': 50, 'weight': 2},
{'species': 'Herbivore', 'age': 1, 'weight': 3},
{'species': 'Carnivore', 'age': 20, 'weight': 2.},
{'species': 'Herbivore', 'age': 10, 'weight': 2.}],
([10, 3, 2.0], [2, 2.0]))])
def test_get_animal_weights_from_cell(pop_list, result, reset_animal_lists_fixture):
"""
Test to check whether the get_animal_weights_from_cell() works properly.
This method is supposed to return a tuple of lists with weight of the herbivores in the 1st
element and weight of the carnivores in the 2nd element.
"""
low_land = LowLand()
low_land.add_only_population(pop_list)
obtained_result = low_land.get_animal_weights_from_cell()
assert obtained_result == result
@pytest.mark.parametrize('pop_list, result',
[
([{'species': 'Herbivore', 'age': 5, 'weight': 10.},
{'species': 'Herbivore', 'age': 5, 'weight': 5},
{'species': 'Herbivore', 'age': 1, 'weight': 50},
{'species': 'Carnivore', 'age': 2, 'weight': 3}],
([0.5, 0.3775, 0.982], [0.4013])),
([{'species': 'Herbivore', 'age': 2, 'weight': 10},
{'species': 'Carnivore', 'age': 50, 'weight': 2},
{'species': 'Herbivore', 'age': 1, 'weight': 3},
{'species': 'Carnivore', 'age': 20, 'weight': 2.},
{'species': 'Herbivore', 'age': 10, 'weight': 2.}],
([0.5, 0.3318, 0.31], [0.0147, 0.3093]),
)
])
def test_get_animal_fitnesses_from_cell(pop_list, result, reset_animal_lists_fixture):
"""
Test to check whether the get_animal_fitnesses_from_cell() works properly.
This method is supposed to return a tuple of lists with fitness of the herbivores in the 1st
element and fitness of the carnivores in the 2nd element.
"""
low_land = LowLand()
low_land.add_only_population(pop_list)
obtained_result = low_land.get_animal_fitnesses_from_cell()
assert obtained_result == result
@pytest.mark.parametrize('pop_list, herb_cnt, carn_cnt',
[
([{'species': 'Herbivore', 'age': 5, 'weight': 25.},
{'species': 'Herbivore', 'age': 5, 'weight': 30}],
19, 0),
([{'species': 'Herbivore', 'age': 2, 'weight': 20},
{'species': 'Carnivore', 'age': 5, 'weight': 35},
{'species': 'Herbivore', 'age': 10, 'weight': 1}],
11, 1)
])
def test_annual_cycle_on_cell(pop_list, herb_cnt, carn_cnt, reset_animal_lists_fixture):
"""
Test to check whether the test_annual_cycle_on_cell() can generate the expected
number of herbivores and carnivores after a year cycle with a random seed set.
"""
random.seed(SEED)
low_land = LowLand()
low_land.add_only_population(pop_list)
low_land.annual_cycle_on_cell()
assert low_land.get_num_of_herbivores() == herb_cnt \
and low_land.get_num_of_carnivores() == carn_cnt