class WolfSheepTestCase(TestCase): def setUp(self): self.wolfsheep = WolfSheep(MODEL_NAME, grp_struct=wolfsheep_grps) self.wolf = Agent(name=WOLF_GRP_NM, exec_key=self.wolfsheep.exec_key) self.sheep = Agent(name=SHEEP_GRP_NM, exec_key=self.wolfsheep.exec_key) def tearDown(self): self.wolfsheep = None def test_run(self): """ Does running the model work? (return of 0) """ self.assertEqual(0, self.wolfsheep.run()) def test_main(self): """ Does the main func of the model work? (return of 0) """ self.assertEqual(0, main()) def test_wolf_action(self): """ Wolves act by eating a random sheep from the meadow """ # Run once to initialize the attributes wolf_action(self.wolf) # Get TIME_TO_REPRODUCE attribute time_to_repro = self.wolf.get_attr(TIME_TO_REPRODUCE) # Run again wolf_action(self.wolf) if time_to_repro == 1: self.assertEqual(self.wolf.get_attr(TIME_TO_REPRODUCE), WOLF_REPRO_PERIOD) else: self.assertEqual(self.wolf.get_attr(TIME_TO_REPRODUCE), time_to_repro - 1) def test_sheep_action(self): """ Sheep act by moving around the meadow and reproducing """ # Run once to initialize the attributes sheep_action(self.sheep) # Get TIME_TO_REPRODUCE attribute time_to_repro = self.sheep.get_attr(TIME_TO_REPRODUCE) # Run again sheep_action(self.sheep) if time_to_repro == 1: self.assertEqual(self.sheep.get_attr(TIME_TO_REPRODUCE), SHEEP_REPRO_PERIOD) else: self.assertEqual(self.sheep.get_attr(TIME_TO_REPRODUCE), time_to_repro - 1) @skip("This test is nonsense: can't ask a sheep to act outside of model.") def test_sheep_reproduce(self): """ Check if sheep can reproduce """ # Run once to initialize the attributes sheep_action(self.sheep) # Set TIME_TO_REPRODUCE to 0 for triggering reproduction self.sheep.set_attr(TIME_TO_REPRODUCE, 0) # Let sheep reproduce reproduce(self.sheep) self.assertEqual(self.sheep.get_attr(TIME_TO_REPRODUCE), DEF_TIME_TO_REPRO) @skip("This test is nonsense: can't ask a wolf to act outside of model.") def test_wolf_reproduce(self): """ Check if wolf can reproduce """ # Run once to initialize the attributes wolf_action(self.wolf) # Set TIME_TO_REPRODUCE to 0 for triggering reproduction self.wolf.set_attr(TIME_TO_REPRODUCE, 0) # Let wolf reproduce reproduce(self.wolf) self.assertEqual(self.wolf.get_attr(TIME_TO_REPRODUCE), DEF_TIME_TO_REPRO)
class FireflyTestCase(TestCase): def setUp(self): self.ff = Firefly(MODEL_NAME, grp_struct=firefly_grps) self.firefly = Agent(name="firefly", exec_key=self.ff.exec_key) def tearDown(self): self.firefly = None def test_run(self): """ Does running the model work? (return of 0) """ self.assertEqual(0, self.ff.run()) def test_main(self): """ Does the main func of the model work? (return of 0) """ self.assertEqual(0, main()) def test_firefly_action(self): """ Fireflies have to move all the time! """ self.assertEqual(firefly_action(self.firefly), MOVE) def test_blink_turn_on(self): """ Test the firefly_blink function and see if flips between FIREFLY_OFF and FIREFLY_ON based on the BLINK_FREQUENCY """ # Set duration to a known value for testing purposes self.firefly.duration = 100 # Set attributes to known values self.firefly.set_prim_group(FIREFLY_OFF) self.firefly.set_attr(BLINK_FREQUENCY, 10) self.firefly.set_attr(LAST_BLINKED_AT, 0) # Run the target function firefly_blink(self.firefly) # Last blink time needs to be updated self.assertEqual(self.firefly.get_attr(LAST_BLINKED_AT), self.firefly.duration) # The agent should be "blinked" since we were well over its # blinking frequency self.assertEqual(self.firefly.group_name(), FIREFLY_ON) def test_blink_turn_off(self): """ Test if firefly_blink function immediately turns OFF an ON firefly """ # Set duration to a known value for testing purposes self.firefly.duration = 100 # Set attributes to known values self.firefly.set_prim_group(FIREFLY_ON) self.firefly.set_attr(BLINK_FREQUENCY, 10) self.firefly.set_attr(LAST_BLINKED_AT, 0) # Run the target function firefly_blink(self.firefly) # The firefly needs to be turned off immediately, if it is ON self.assertEqual(self.firefly.group_name(), FIREFLY_OFF) def test_adjust_blink_frequency(self): """ Test adjust_blink_frequency function to see if it initializes the BLINK_FREQUENCY """ # Set attributes to known values self.firefly.duration = 100 self.firefly.set_attr(BLINK_FREQUENCY, None) self.firefly.set_attr(LAST_BLINKED_AT, None) # Run the target function adjust_blink_frequency(self.firefly) # A random blink frequency should have been generated self.assertGreater(self.firefly.get_attr(BLINK_FREQUENCY), 0) # Last blink time should be updated self.assertEqual(self.firefly.get_attr(LAST_BLINKED_AT), self.firefly.duration) def test_env_action_dict_empty(self): """ Test environment action when the BLINK_FREQUENCIES dictionary is empty """ BLINK_FREQUENCIES = {} std = env_action(None) self.assertEqual(std, None) def test_env_action_dict_less_than_two(self): """ Test environment action when there are less than 2 items in the BLINK_FREQUENCIES dictionary """ BLINK_FREQUENCIES = {"agent1": 3} std = env_action(None) self.assertEqual(std, None)