Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.88 KB | None | 0 0
  1. import io
  2. import sys
  3. import unittest
  4. import requests_mock
  5. import json
  6. from collections import OrderedDict
  7. from datetime import datetime, timezone
  8. from colorama import Fore, deinit, init
  9.  
  10. import parse
  11. from utils import config
  12. from tests.mocks import *
  13. from tests.sampleItems import items
  14.  
  15. LOOKUP_URL = "https://www.pathofexile.com/api/trade/search/Metamorph"
  16.  
  17. class TestItemLookup(unittest.TestCase):
  18.     def test_lookups(self):
  19.         # Mockups of response data from pathofexile.com/trade
  20.         expected = [
  21.             # (mocked up json response, expected condition)
  22.             (mockResponse(11), lambda v: "[$]" in v),
  23.             (mockResponse(12), lambda v: "[$]" in v),
  24.             (mockResponse(0), lambda v: "[!]" in v),
  25.             (mockResponse(0), lambda v: "[!]" in v),
  26.             (mockResponse(1), lambda v: "[!]" in v),
  27.             (mockResponse(0), lambda v: "[!]" in v),
  28.             (mockResponse(0), lambda v: "[!]" in v),
  29.             (mockResponse(0), lambda v: "[!]" in v),
  30.             (mockResponse(0), lambda v: "[!]" in v),
  31.             (mockResponse(0), lambda v: "[!]" in v),
  32.             (mockResponse(66), lambda v: "[$]" in v),
  33.             (mockResponse(0), lambda v: "[!]" in v),
  34.             (mockResponse(0), lambda v: "[!]" in v),
  35.             (mockResponse(0), lambda v: "[!]" in v),
  36.             (mockResponse(0), lambda v: "[!]" in v),
  37.             (mockResponse(10), lambda v: "[$]" in v),
  38.         ]
  39.  
  40.         # Mocked up prices to return when searching. We only take the first
  41.         # 10 results from any search. We don't have to worry about sorting by
  42.         # price here, as we know that PoE/trade sorts by default.
  43.         prices = [
  44.             [ # List 1
  45.                 (45, "alch"),
  46.                 (45, "alch"),
  47.                 (45, "alch"),
  48.                 (45, "alch"),
  49.                 (45, "alch"),
  50.                 (45, "alch"),
  51.                 (45, "alch"),
  52.                 (45, "alch"),
  53.                 (45, "alch"),
  54.                 (45, "alch"),
  55.             ],
  56.             [ # List 2
  57.                 # 5 x 1 chaos
  58.                 (1, "chaos"),
  59.                 (1, "chaos"),
  60.                 (1, "chaos"),
  61.                 (1, "chaos"),
  62.                 (1, "chaos"),
  63.                 # 1 x 3 chaos
  64.                 (3, "chaos"),
  65.                 # 4 x 2 alch
  66.                 (2, "alch"),
  67.                 (2, "alch"),
  68.                 (2, "alch"),
  69.                 (2, "alch"),
  70.             ],
  71.             [], # List 3
  72.             [], # List 4
  73.             [ # List 5
  74.                 (666, "exa")
  75.             ],
  76.             [], # List 6
  77.             [], # List 7
  78.             [], # List 8
  79.             [], # List 9
  80.             [], # List 10
  81.             [ # List 11
  82.                 (2.5, "mir")
  83.             ] * 10,
  84.             [], # List 12
  85.             [], # List 13
  86.             [], # List 14
  87.             [], # List 15
  88.             [ # List 16
  89.                 (1, "fuse")
  90.             ] * 10,
  91.         ]
  92.  
  93.         for i in range(len(items)):
  94.             with self.subTest(i=i):
  95.                 sortedPrices = sorted(prices[i])
  96.                 priceCount = OrderedDict()
  97.                 for price in sortedPrices:
  98.                     if price in priceCount:
  99.                         priceCount[price] += 1
  100.                     else:
  101.                         priceCount[price] = 1
  102.  
  103.                 # Middleman our stdout, so we can check programmatically
  104.                 out = io.StringIO()
  105.                 sys.stdout = out
  106.  
  107.                 # Mockup response
  108.                 with requests_mock.Mocker() as mock:
  109.                     mock.post(
  110.                         LOOKUP_URL,
  111.                         json=expected[i][0]
  112.                     )
  113.  
  114.                     response = {
  115.                         "result": [
  116.                             {
  117.                                 "id": "result%d" % x,
  118.                                 "listing": {
  119.                                     # Mocked account name of the lister
  120.                                     "account": { "name": "account%d" % x },
  121.  
  122.                                     # Price of this item result; we should mock
  123.                                     # and test against this amount.
  124.                                     "price": {
  125.                                         "type": "~",
  126.                                         "amount": sortedPrices[x][0],
  127.                                         "currency": sortedPrices[x][1]
  128.                                     },
  129.  
  130.                                     # Indexed now.
  131.                                     "indexed": datetime.strftime(
  132.                                         datetime.now(timezone.utc),
  133.                                         "%Y-%m-%dT%H:%M:%SZ"
  134.                                     ),
  135.                                 }
  136.                             } for x in range(min(expected[i][0]["total"], 10))
  137.                         ]
  138.                     }
  139.                     # If we have at least MIN_RESULTS items, we should mock
  140.                     # the GET request for fetching the first 10 items.
  141.                     # See search_item's pathing in parse.py
  142.                     if len(expected[i][0]["result"]) >= config.MIN_RESULTS:
  143.                         fetch_url = makeFetchURL(expected[i][0])
  144.                         mock.get(fetch_url, json=response)
  145.  
  146.                     # Callout to API and price the item
  147.                     parse.price_item(items[i])
  148.  
  149.                 # Get the expected condition
  150.                 currentExpected = expected[i][1]
  151.  
  152.                 # Restore stdout and assert that the expected condition is true
  153.                 sys.stdout = sys.__stdout__
  154.  
  155.                 # Expect that our truthy condition is true
  156.                 self.assertTrue(currentExpected(out.getvalue()))
  157.  
  158.                 if len(expected[i][0]["result"]) >= config.MIN_RESULTS:
  159.                     # Expect that the currency is output properly, including
  160.                     # the color(s) expected.
  161.                     priceList = []
  162.                     for k, v in priceCount.items():
  163.                         # Normalize any non-integer decimal number. That is,
  164.                         # if k[0] == int(k[0]), output it as a pure integer.
  165.                         # Otherwise, use the string formatter, so 2.750
  166.                         # becomes 2.75 but retains it's floating pointness.
  167.                         fmt = "%i" if int(k[0]) == k[0] else "%s"
  168.                         priceList.append(
  169.                             ("%d x %s" + fmt + " %s") % (
  170.                                 v, Fore.YELLOW, k[0], k[1]
  171.                             )
  172.                         )
  173.                     expectedStr = ("%s, " % Fore.WHITE).join(priceList)
  174.                     self.assertTrue(expectedStr in out.getvalue())
  175.  
  176. if __name__ == "__main__":
  177.     init(autoreset=True) # Colorama
  178.     unittest.main(failfast=True)
  179.     deinit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement