Coverage for test/test_agent_concrete.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-05-04 13:14 +0700

1from copy import deepcopy 

2import pytest 

3from ..agent_model.agents import BaseAgent, ConcreteAgent 

4 

5@pytest.fixture 

6def minimum_kwargs(): 

7 return { 

8 'flows': { 

9 'in': { 

10 'co2': {'value': 0}, 

11 'caoh2': {'value': 0}, 

12 }, 

13 'out': { 

14 'caco3': {'value': 0}, 

15 'moisture': {'value': 0}, 

16 } 

17 }, 

18 'capacity': { 

19 'caoh2': 0, 

20 'caco3': 0, 

21 'moisture': 0 

22 }, 

23 } 

24 

25@pytest.fixture 

26def concrete_kwargs(): 

27 return { 

28 "flows": { 

29 "in": { 

30 "co2": { 

31 "value": 44.01, 

32 "weighted": ["carbonation_rate"], 

33 "connections": ["test_greenhouse"], 

34 }, 

35 "caoh2": { 

36 "value": 74.1, 

37 "weighted": ["carbonation_rate"], 

38 "requires": ["co2"], 

39 "connections": ["concrete"], 

40 } 

41 }, 

42 "out": { 

43 "caco3": { 

44 "value": 100.09, 

45 "weighted": ["carbonation_rate"], 

46 "requires": ["co2", "caoh2"], 

47 "connections": ["concrete"], 

48 }, 

49 "moisture": { 

50 "value": 18.02, 

51 "weighted": ["carbonation_rate"], 

52 "requires": ["co2", "caoh2"], 

53 "connections": ["concrete"], 

54 } 

55 } 

56 }, 

57 "capacity": { 

58 "caoh2": 1000, 

59 "caco3": 1000, 

60 "moisture": 1000 

61 }, 

62 } 

63 

64class MockModel: 

65 floating_point_precision = 6 

66 agents = {} 

67 currencies = { 

68 'o2': {'currency_type': 'currency'}, 

69 'co2': {'currency_type': 'currency'}, 

70 'caoh2': {'currency_type': 'currency'}, 

71 'caco3': {'currency_type': 'currency'}, 

72 'moisture': {'currency_type': 'currency'}, 

73 'atmosphere': {'currency_type': 'class', 'currencies': ['o2', 'co2']} 

74 } 

75 

76@pytest.fixture 

77def mock_model(concrete_kwargs): 

78 model = MockModel() 

79 model.agents = { 

80 'concrete': ConcreteAgent(model, 'concrete', **concrete_kwargs), 

81 'test_greenhouse': BaseAgent(model, 'test_greenhouse', 

82 storage={'o2': 999.65, 'co2': 0.35}, 

83 capacity={'o2': 1000, 'co2': 1000}) 

84 } 

85 return model 

86 

87class TestConcreteAgent: 

88 def test_agent_concrete_calc_max(self): 

89 lowest, highest = ConcreteAgent.ppm_range 

90 vals = [ConcreteAgent.calc_max_carbonation(ppm) 

91 for ppm in range(lowest, highest+50, 50)] 

92 for i, carbonation in enumerate(vals[1:]): 

93 assert carbonation > vals[i] 

94 

95 def test_agent_concrete_init(self, minimum_kwargs, concrete_kwargs): 

96 """Initialize attributes properly""" 

97 

98 # Missing kwarg 

99 model = object() 

100 incomplete_kwargs = deepcopy(minimum_kwargs) 

101 del incomplete_kwargs['flows']['in']['co2'] 

102 with pytest.raises(ValueError): 

103 concrete = ConcreteAgent(model, 'concrete', **incomplete_kwargs) 

104 

105 # Minimum kwargs 

106 model = object() 

107 concrete = ConcreteAgent(model, 'concrete', **minimum_kwargs) 

108 for k, v in ConcreteAgent.default_attributes.items(): 

109 assert concrete.attributes[k] == v 

110 

111 # All kwargs 

112 model = object() 

113 concrete = ConcreteAgent(model, 'concrete', **concrete_kwargs) 

114 assert True 

115 

116 # Initial carbonation 

117 half_carbonated = ConcreteAgent.calc_max_carbonation(3000) / 2 

118 carbo_kwargs = {**concrete_kwargs, 

119 'attributes': {'carbonation': half_carbonated}} 

120 concrete = ConcreteAgent(model, 'concrete', **carbo_kwargs) 

121 assert concrete.attributes['carbonation'] == pytest.approx(0.078267, 6) 

122 assert concrete.attributes['carbonation_rate'] == 0 

123 assert concrete.storage == { 

124 'caoh2': pytest.approx(5.799572, 6), 

125 'caco3': pytest.approx(7.783372, 6), 

126 'moisture': pytest.approx(1.410358, 6), 

127 } 

128 

129 def test_agent_concrete_step(self, mock_model): 

130 concrete_agent = mock_model.agents['concrete'] 

131 greenhouse_agent = mock_model.agents['test_greenhouse'] 

132 concrete_agent.register() 

133 greenhouse_agent.register() 

134 assert concrete_agent.attributes['carbonation'] == 0 

135 concrete_agent.step() 

136 carbonation_rate = concrete_agent.attributes['carbonation_rate'] 

137 assert carbonation_rate != 0 

138 assert concrete_agent.attributes['carbonation'] == carbonation_rate 

139