add tests
This commit is contained in:
62
tests/test_encar.py
Normal file
62
tests/test_encar.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from encar_scraper.encar import EncarMapper, EncarScraper, ENCAR_DETAIL_URL_TEMPLATE
|
||||
|
||||
|
||||
class TestEncarMapper(unittest.TestCase):
|
||||
def test_map_to_car_record(self):
|
||||
payload = {
|
||||
"Id": 41421262,
|
||||
"Manufacturer": "Hyundai",
|
||||
"Model": "G90 (RS4)",
|
||||
"FormYear": "2023",
|
||||
"Price": 8590,
|
||||
"Mileage": 37923,
|
||||
"Photos": [
|
||||
{"location": "/carpicture01/pic4141/41412663_001.jpg", "ordering": 1},
|
||||
{"location": "/carpicture01/pic4141/41412663_003.jpg", "ordering": 3},
|
||||
],
|
||||
}
|
||||
|
||||
record = EncarMapper().map_to_car_record(
|
||||
ENCAR_DETAIL_URL_TEMPLATE.format(vehicle_id="41421262"),
|
||||
payload,
|
||||
)
|
||||
|
||||
self.assertEqual(record.origin_id, "encar:41421262")
|
||||
self.assertEqual(record.parser_id, "encar:41421262")
|
||||
self.assertEqual(record.brand, "Hyundai")
|
||||
self.assertEqual(record.model, "G90 (RS4)")
|
||||
self.assertEqual(record.year, 2023)
|
||||
self.assertEqual(record.price, 85900000) # 8590 만원 × 10000
|
||||
self.assertEqual(record.mileage, 37923)
|
||||
self.assertEqual(record.country, "KR")
|
||||
self.assertEqual(record.currency, "KRW")
|
||||
self.assertEqual(len(record.images), 2)
|
||||
self.assertTrue(record.images[0].fullres_image.endswith("_001.jpg"))
|
||||
|
||||
def test_extract_vehicle_id_from_url(self):
|
||||
url = ENCAR_DETAIL_URL_TEMPLATE.format(vehicle_id="41421262")
|
||||
self.assertEqual(EncarMapper()._extract_vehicle_id(url), "41421262")
|
||||
|
||||
|
||||
class TestEncarScraper(unittest.TestCase):
|
||||
def test_collect_listing_limits_results(self):
|
||||
scraper = EncarScraper()
|
||||
scraper._fetch_listing_page = MagicMock(side_effect=[
|
||||
{"SearchResults": [{"Id": 1}, {"Id": 2}]},
|
||||
])
|
||||
|
||||
result = scraper.collect_listing(limit=1, page_size=2)
|
||||
self.assertEqual(result["items_collected"], 1)
|
||||
self.assertEqual(result["vehicle_urls"], [ENCAR_DETAIL_URL_TEMPLATE.format(vehicle_id="1")])
|
||||
|
||||
def test_scrape_vehicle_detail_raises_without_id(self):
|
||||
scraper = EncarScraper()
|
||||
with self.assertRaises(ValueError):
|
||||
scraper.scrape_vehicle_detail("https://www.encar.com/invalid")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user