-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_ksl_cars.py
121 lines (96 loc) · 3.87 KB
/
test_ksl_cars.py
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
import unittest
from unittest.mock import patch
import ksl_cars
from ksl_cars import (
VehicleSearchFilters,
VehicleListing,
APIHandler,
semicolonize,
ksl_cars_search,
get_makes_models_trims,
)
ksl_cars.SLEEP_TIME = 0
class TestGetMakesModelsTrims(unittest.TestCase):
def test_get_makes_models_trims(self):
cars = {
"Toyota": {"Corolla": ["LE"]},
"Honda": {"Civic": ["Si"]},
"Ford": {"F-150": ["Lariat"]},
}
# Patch the APIHandler.post method at the class level
with patch.object(APIHandler, "post") as mock_post:
mock_post.return_value = cars
self.assertEqual(get_makes_models_trims(), ["Toyota", "Honda", "Ford"])
self.assertEqual(get_makes_models_trims(make="Toyota"), ["Corolla"])
self.assertEqual(
get_makes_models_trims(make="Toyota", model="Corolla"), ["LE"]
)
class TestVehicleSearchFilters(unittest.TestCase):
def test_valid_initialization(self):
filters = VehicleSearchFilters(sellerType="Dealership", priceTo=10000)
self.assertEqual(filters.sellerType, "Dealership")
self.assertEqual(filters.priceTo, 10000)
def test_invalid_initialization(self):
with self.assertRaises(TypeError):
VehicleSearchFilters(sellerType="Invalid Seller")
def test_set_invalid_attribute_type(self):
filters = VehicleSearchFilters()
with self.assertRaises(TypeError):
filters.priceTo = "Invalid Value"
def test_to_list(self):
filters = VehicleSearchFilters(priceTo=10000, transmission="Manual")
expected = ["priceTo", "10000", "transmission", "Manual"]
self.assertEqual(filters.to_list(), expected)
def test_semicolonize(self):
self.assertEqual(semicolonize(["A", "B", "C"]), "A;B;C")
self.assertEqual(semicolonize("A"), "A")
class TestVehicleListing(unittest.TestCase):
def test_initialization(self):
listing_json = {
"id": "12345",
"makeYear": "2010",
"price": "8000",
"make": "Toyota",
"model": "Corolla",
"displayTime": "1638467200",
}
listing = VehicleListing(listing_json)
self.assertEqual(listing.listing_title, "2010 Toyota Corolla")
self.assertEqual(listing.year, 2010)
self.assertEqual(listing.price, 8000)
self.assertEqual(listing.url, "https://cars.ksl.com/listing/12345")
class TestAPIHandler(unittest.TestCase):
@patch("requests.post")
def test_post_success(self, mock_post):
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {"data": {}}
api = APIHandler()
response = api.post("/classifieds/cars/search/searchByUrlParams")
self.assertIsInstance(response, dict)
@patch("requests.post")
def test_post_retry_on_503(self, mock_post):
mock_post.return_value.status_code = 503
api = APIHandler()
with self.assertRaises(ValueError):
api.post("/classifieds/cars/search/searchByUrlParams", max_retries=1)
class TestKslCarsSearch(unittest.TestCase):
@patch("ksl_cars.APIHandler.post")
def test_ksl_cars_search(self, mock_post):
mock_post.return_value = {
"items": [
{
"id": "12345",
"makeYear": "2010",
"price": "8000",
"make": "Toyota",
"model": "Corolla",
"displayTime": "1638467200",
}
]
}
filters = VehicleSearchFilters(make="Toyota", priceTo=10000)
listings = list(ksl_cars_search(filters=filters))
self.assertEqual(len(listings), 1)
self.assertEqual(listings[0].listing_title, "2010 Toyota Corolla")
if __name__ == "__main__":
unittest.main()