1 import pytest

2 import datetime

3 import numpy as np

4 from ..agent_model.agents import LampAgent

5

6 class MockModel:

7 agents = {}

8 time = datetime.datetime(2020, 1, 1, 0)

9

10 class MockAgent:

11 def __init__(self, lamp_id, photoperiod, par_baseline, active):

12 self.flows = {'in': {'par': {'connections': [lamp_id]}}}

13 self.properties = {'photoperiod': {'value': photoperiod},

14 'par_baseline': {'value': par_baseline}}

15 self.active = active

16

17@pytest.fixture

18 def mock_model():

19 model = MockModel()

20 model.agents = {

21 'lamp': LampAgent(model, 'lamp'),

22 'test_plant_1': MockAgent('lamp', 13, 1.5, 2),

23 'test_plant_2': MockAgent('lamp', 15, 1.2, 2),

24 }

25 return model

26

27 class TestAgentLamp:

28 def test_agent_lamp_init(self):

29 """Initialize attributes properly"""

30 model = object()

31 lamp = LampAgent(model, 'lamp')

32 for k, v in LampAgent.default_attributes.items():

33 assert lamp.attributes[k] == v

34

35 def test_agent_lamp_register_full(self, mock_model):

36 """Register lamp with model"""

37 lamp = mock_model.agents['lamp']

38 lamp.register(mock_model)

39 assert len(lamp.connected_plants) == 2

40 assert all(isinstance(p, str) for p in lamp.connected_plants)

41 assert lamp.active == 4

42

43 def test_agent_lamp_register_empty(self, mock_model):

44 del mock_model.agents['test_plant_1']

45 del mock_model.agents['test_plant_2']

46 lamp = mock_model.agents['lamp']

47 lamp.register(mock_model)

48 assert len(lamp.connected_plants) == 0

49 assert lamp.active == 0

50

51 def test_agent_lamp_update_attributes(self, mock_model):

52 lamp = mock_model.agents['lamp']

53 lamp.register(mock_model)

54

55 assert lamp.connected_plants == ['test_plant_1', 'test_plant_2']

56 assert lamp.lamp_configuration == {'test_plant_1': 2, 'test_plant_2': 2}

57 assert lamp.attributes['photoperiod'] == 15

58 expected_par_rate = 1.5 * 24/13 # Highest instantaneous of any connected plant

59 assert lamp.attributes['par_rate'] == expected_par_rate

60 expected_daily_growth = np.zeros(24)

61 expected_daily_growth[4:19] = expected_par_rate

62 assert str(lamp.daily_growth) == str(expected_daily_growth)

63 assert lamp.active == 4

64

65 def test_agent_lamp_step(self, mock_model):

66 lamp = mock_model.agents['lamp']

67 # lamp.register()

68 # Update daily_growth_factor and par storage

69 lamp.step()

70 assert lamp.attributes['daily_growth_factor'] == 0

71 assert lamp.storage['par'] == 0

72 # Daily_growth_factor updates with model time, storage reset

73 mock_model.time = datetime.datetime(2020, 1, 1, 4)

74 lamp.storage['par'] = 1

75 lamp.step()

76 assert lamp.attributes['daily_growth_factor'] == 1.5 * 24/13

77 assert lamp.storage['par'] == 0

78 # Update variables when connected plants change

79 mock_model.agents['test_plant_1'].active = 0

80 lamp.step()

81 assert lamp.active == 2

82 assert lamp.lamp_configuration == {'test_plant_1': 0, 'test_plant_2': 2}

83 assert lamp.attributes['photoperiod'] == 15

84 assert lamp.attributes['par_rate'] == 1.2 * 24/15

85