Skip to content

Commit

Permalink
Add item_count parameter functionality to search
Browse files Browse the repository at this point in the history
  • Loading branch information
apmnt committed Mar 4, 2024
1 parent aa4879c commit 39e1c5e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
10 changes: 6 additions & 4 deletions postalservice/frilservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
class FrilService(BaseService):

async def fetch_data_async(self, params: dict) -> httpx.Response:
self.item_count = params.get("item_count", 36)
url = self.get_search_params(params)
res = await fetch_async(url)
return res

def fetch_data(self, params: dict) -> httpx.Response:
url = self.get_search_params(params)
res = httpx.get(url)
return res
self.item_count = params.get("item_count", 36)
url = self.get_search_params(params)
res = httpx.get(url)
return res

async def parse_response_async(self, response: httpx.Response) -> str:
soup = bs4.BeautifulSoup(response.text, "lxml")
Expand All @@ -50,7 +52,7 @@ def parse_response(self, response: httpx.Response) -> str:

def get_base_details(self, results) -> list:
cleaned_items_list = []
for item in results[:10]:
for item in results[:self.item_count]:
id = item.select_one(".link_search_image")["href"].split("/")[-1]
temp = {}
temp["id"] = id
Expand Down
4 changes: 2 additions & 2 deletions postalservice/mercariservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ async def fetch_data_async(self, params: dict) -> httpx.Response:
raise TypeError('params must be a dict')

keyword = params.get('keyword')

size = params.get('size')
item_count = params.get('item_count')

if size is not None: mapped_size = SIZE_MAP.get(size)
else: mapped_size = None

item_count = params.get('item_count')
url = "https://api.mercari.jp/v2/entities:search"
searchSessionId = ''.join(random.choice(CHARACTERS) for i in range(32))
payload = {
Expand Down
36 changes: 32 additions & 4 deletions tests/baseservicetestclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ def setUpClass(cls):
cls.logger = logger

def test_fetch_code_200(self):

sparams = SearchParams("comme des garcons")
res = self.service.fetch_data(sparams.get_dict())
self.logger.info("Fetched data: %s", res)

# Assert that the status code is 200
self.assertEqual(res.status_code, 200)

def test_parse_results(self):
def test_fetch_code_200_async(self):
sparams = SearchParams("comme des garcons")
res = asyncio.run(self.service.fetch_data_async(sparams.get_dict()))
self.logger.info("Fetched data: %s", res)

# Assert that the status code is 200
self.assertEqual(res.status_code, 200)

def test_parse_results(self):
sparams = SearchParams("comme des garcons")
res: httpx.Response = self.service.fetch_data(sparams.get_dict())
print(res)
Expand All @@ -40,8 +46,31 @@ def test_parse_results(self):
# Assert that the count is greater than 0
self.assertTrue(searchresults.count() > 0)

def test_search_by_size(self):
def test_parse_results_async(self):
sparams = SearchParams("comme des garcons")
res = asyncio.run(self.service.fetch_data_async(sparams.get_dict()))
items = asyncio.run(self.service.parse_response_async(res))
searchresults = SearchResults(items)
self.logger.info(searchresults)

# Assert that the count is greater than 0
self.assertTrue(searchresults.count() > 0)

def test_get_5_results(self):
sparams = SearchParams("comme des garcons", item_count=5)
searchresults = self.service.get_search_results(sparams.get_dict())

# Assert that the count is 5
self.assertEqual(searchresults.count(), 5)

def test_get_5_results_async(self):
sparams = SearchParams("comme des garcons", item_count=5)
searchresults = asyncio.run(self.service.get_search_results_async(sparams.get_dict()))

# Assert that the count is 5
self.assertEqual(searchresults.count(), 5)

def test_search_by_size(self):
size_to_search = "XL"
sparams = SearchParams("comme des garcons", size=size_to_search)
searchresults = self.service.get_search_results(sparams.get_dict())
Expand All @@ -52,7 +81,6 @@ def test_search_by_size(self):
self.assertTrue(size_to_search in searchresults.get(i)["size"])

def test_search_by_size_async(self):

size_to_search = "XL"
sparams = SearchParams("comme des garcons", size=size_to_search)
searchresults = asyncio.run(self.service.get_search_results_async(sparams.get_dict()))
Expand Down

0 comments on commit 39e1c5e

Please sign in to comment.