-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_analyze_tree_property.py
More file actions
62 lines (54 loc) · 2.71 KB
/
test_analyze_tree_property.py
File metadata and controls
62 lines (54 loc) · 2.71 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
import unittest
from unittest.mock import patch, mock_open
from pathlib import Path
from analyze_tree_property import TreePropertyAnalyzer
class TestTreePropertyAnalyzer(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.json_path = Path("test-dublin-trees.json")
cls.csv_path = Path("test-dublin-property.csv")
@patch("builtins.open", new_callable=mock_open, read_data="""{
"short": {"drive": {"abbey drive": 0, "coolrua drive": 10}},
"tall": {"gardens": {"temple gardens": 20}, "street": {"cork street": 10}}
}""")
@patch("pathlib.Path.exists", return_value=True)
def test_load_tree_data(self, mock_exists, mock_file):
analyzer = TreePropertyAnalyzer(self.json_path, self.csv_path)
short_streets, tall_streets = analyzer.load_tree_data()
self.assertEqual(short_streets, ["abbey drive", "coolrua drive"])
self.assertEqual(tall_streets, ["temple gardens", "cork street"])
@patch("builtins.open", new_callable=mock_open, read_data="""Street Name,Price
abbey drive,€100000
coolrua drive,€200000
temple gardens,€300000
cork street,€400000
unknown street,€500000""")
@patch("pathlib.Path.exists", return_value=True)
@patch("analyze_tree_property.TreePropertyAnalyzer.detect_file_encoding", return_value="utf-8")
def test_load_property_data(self, mock_encoding, mock_exists, mock_file):
analyzer = TreePropertyAnalyzer(self.json_path, self.csv_path)
properties = analyzer.load_property_data()
self.assertEqual(len(properties), 5)
self.assertEqual(properties[0]["Street Name"], "abbey drive")
self.assertEqual(properties[0]["Price"], "€100000")
@patch("builtins.open", side_effect=[
mock_open(read_data="""{
"short": {"drive": {"abbey drive": 0, "coolrua drive": 10}},
"tall": {"gardens": {"temple gardens": 20}, "street": {"cork street": 10}}
}""").return_value,
mock_open(read_data="""Street Name,Price
abbey drive,€100000
coolrua drive,€200000
temple gardens,€300000
cork street,€400000
unknown street,€500000""").return_value
])
@patch("pathlib.Path.exists", return_value=True)
@patch("analyze_tree_property.TreePropertyAnalyzer.detect_file_encoding", return_value="utf-8")
def test_analyze(self, mock_encoding, mock_exists, mock_files):
analyzer = TreePropertyAnalyzer(self.json_path, self.csv_path)
results = analyzer.analyze()
self.assertAlmostEqual(results["short_tree_avg"], 150000.0, places=2, msg="Short tree average is incorrect")
self.assertAlmostEqual(results["tall_tree_avg"], 350000.0, places=2, msg="Tall tree average is incorrect")
if __name__ == "__main__":
unittest.main()