Exemplo n.º 1
0
class TestPlatform(unittest.TestCase):
    def setUp(self):
        self.slug = 'na'
        self.platform = Platform(self.slug)

    def test_count_and_has_url(self):
        self.assertEqual(self.platform.count, 0)
        self.assertFalse(self.platform.has_url())

        self.platform.add_data({'url': match_url_template.format(matchid=1)})
        self.assertEqual(self.platform.count, 1)
        self.assertTrue(self.platform.has_url())

        self.platform.add_data(
            {'url': summoner_url_template.format(name=fake_name)})
        self.assertEqual(self.platform.count, 2)
        self.assertTrue(self.platform.has_url())

        self.platform.add_data({'url': static_champions_url})
        self.assertEqual(self.platform.count, 3)
        self.assertTrue(self.platform.has_url())

        self.platform.get()
        self.assertEqual(self.platform.count, 2)
        self.assertTrue(self.platform.has_url())

        self.platform.get()
        self.assertEqual(self.platform.count, 1)
        self.assertTrue(self.platform.has_url())

        self.platform.get()
        self.assertEqual(self.platform.count, 0)
        self.assertFalse(self.platform.has_url())

    def test_rate_limit_ok(self):
        url = match_url_template.format(matchid=1)
        self.assertTrue(self.platform.rate_limit_ok())
        self.platform.add_data({'url': match_url_template.format(matchid=1)})
        self.platform.handle_response_headers(url, headers)
        self.assertTrue(self.platform.rate_limit_ok())
        new_headers = copy.copy(headers)
        new_headers['X-App-Rate-Limit'] = "1:0.01"
        new_headers['X-App-Rate-Limit-Count'] = "1:0.01"
        new_headers['X-Method-Rate-Limit'] = "5:1"
        new_headers['X-Method-Rate-Limit-Count'] = "1:1"
        self.platform.handle_response_headers(url, new_headers)
        self.assertFalse(self.platform.rate_limit_ok())
        time.sleep(0.01)
        self.assertTrue(self.platform.rate_limit_ok())
        self.platform.get()
        self.assertFalse(self.platform.rate_limit_ok())

    def test_handle_response_headers(self):
        url = match_url_template.format(matchid=1)
        endpoint_str = Endpoint.identify_endpoint(url)
        self.assertRaises(Exception, self.platform.handle_response_headers,
                          (url, headers))
        self.platform.add_data({'url': url})
        self.assertEqual(self.platform.platform_limits, {})
        self.platform.handle_response_headers(url, headers)
        self.assertEqual(len(self.platform.platform_limits), 2)
        self.assertEqual(self.platform.platform_limits["1"].cap, 20)
        self.assertEqual(self.platform.platform_limits["1"].seconds, 1)
        self.assertEqual(self.platform.platform_limits["1"].used, 1)
        self.assertEqual(self.platform.platform_limits["120"].cap, 100)
        self.assertEqual(self.platform.platform_limits["120"].seconds, 120)
        self.assertEqual(self.platform.platform_limits["120"].used, 1)
        self.assertEqual(len(self.platform.limited_endpoints), 1)
        self.assertEqual(
            self.platform.limited_endpoints[endpoint_str].limits["60"].cap,
            270)
        self.assertEqual(
            self.platform.limited_endpoints[endpoint_str].limits["60"].seconds,
            60)
        self.assertEqual(
            self.platform.limited_endpoints[endpoint_str].limits["60"].used, 1)
        new_headers = copy.copy(headers)
        new_headers['X-App-Rate-Limit'] = "1:1"
        new_headers['X-App-Rate-Limit-Count'] = "1:1"
        new_headers['X-Method-Rate-Limit'] = "5:1"
        new_headers['X-Method-Rate-Limit-Count'] = "1:1"
        self.platform.handle_response_headers(url, new_headers)
        self.assertEqual(len(self.platform.platform_limits), 1)
        self.assertEqual(self.platform.platform_limits["1"].cap, 1)
        self.assertEqual(self.platform.platform_limits["1"].seconds, 1)
        self.assertEqual(self.platform.platform_limits["1"].used, 1)
        self.assertEqual(len(self.platform.limited_endpoints), 1)
        self.assertEqual(
            self.platform.limited_endpoints[endpoint_str].limits["1"].cap, 5)
        self.assertEqual(
            self.platform.limited_endpoints[endpoint_str].limits["1"].seconds,
            1)
        self.assertEqual(
            self.platform.limited_endpoints[endpoint_str].limits["1"].used, 1)

    def test_add_data_static_random(self):
        s1 = {'url': static_champions_url}
        s2 = {'url': static_summoner_spells_url}
        s3 = {'url': static_items_url}
        s = [s1, s2, s3]
        self.platform.add_data(s1)
        self.platform.add_data(s2)
        self.platform.add_data(s3)
        self.assertEqual(self.platform.static_count, 3)
        self.assertTrue(self.platform.get() in s)
        self.assertTrue(self.platform.get() in s)
        self.assertTrue(self.platform.get() in s)
        self.assertRaises(Exception, self.platform.get)

    def test_add_data_static_sorted(self):
        c1 = {'url': static_champion_url.format(id=1)}
        c2 = {'url': static_champion_url.format(id=2)}
        c3 = {'url': static_champion_url.format(id=3)}
        self.platform.add_data(c1)
        self.platform.add_data(c2)
        self.platform.add_data(c3)
        self.assertEqual(self.platform.static_count, 3)
        self.assertEqual(self.platform.get(), c1)
        self.assertEqual(self.platform.get(), c2)
        self.assertEqual(self.platform.get(), c3)
        self.assertRaises(Exception, self.platform.get)

    def test_add_data_static_sorted_atFront(self):
        c1 = {'url': static_champion_url.format(id=1)}
        c2 = {'url': static_champion_url.format(id=2)}
        c3 = {'url': static_champion_url.format(id=3)}
        self.platform.add_data(c1, front=True)
        self.platform.add_data(c2, front=True)
        self.platform.add_data(c3, front=True)
        self.assertEqual(self.platform.static_count, 3)
        self.assertEqual(self.platform.get(), c3)
        self.assertEqual(self.platform.get(), c2)
        self.assertEqual(self.platform.get(), c1)
        self.assertRaises(Exception, self.platform.get)

    def test_add_data_limited_alternate(self):
        m1 = {'url': match_url_template.format(matchid=1)}
        m2 = {'url': match_url_template.format(matchid=2)}
        m3 = {'url': match_url_template.format(matchid=3)}
        s1 = {'url': summoner_url_template.format(name=1)}
        s2 = {'url': summoner_url_template.format(name=2)}
        s3 = {'url': summoner_url_template.format(name=3)}
        self.platform.add_data(m1)
        self.platform.add_data(m2)
        self.platform.add_data(m3)
        self.platform.add_data(s1)
        self.platform.add_data(s2)
        self.platform.add_data(s3)
        self.assertEqual(self.platform.limited_count, 6)
        self.assertEqual(self.platform.get(), m1)
        self.assertEqual(self.platform.get(), s1)
        self.assertEqual(self.platform.get(), m2)
        self.assertEqual(self.platform.get(), s2)
        self.assertEqual(self.platform.get(), m3)
        self.assertEqual(self.platform.get(), s3)
        self.assertRaises(Exception, self.platform.get)

    def test_add_data_limited_sorted(self):
        m1 = {'url': match_url_template.format(matchid=1)}
        m2 = {'url': match_url_template.format(matchid=2)}
        m3 = {'url': match_url_template.format(matchid=3)}
        self.platform.add_data(m1)
        self.platform.add_data(m2)
        self.platform.add_data(m3)
        self.assertEqual(self.platform.limited_count, 3)
        self.assertEqual(self.platform.get(), m1)
        self.assertEqual(self.platform.get(), m2)
        self.assertEqual(self.platform.get(), m3)
        self.assertRaises(Exception, self.platform.get)

    def test_add_data_limited_sorted_atFront(self):
        m1 = {'url': match_url_template.format(matchid=1)}
        m2 = {'url': match_url_template.format(matchid=2)}
        m3 = {'url': match_url_template.format(matchid=3)}
        self.platform.add_data(m1, front=True)
        self.platform.add_data(m2, front=True)
        self.platform.add_data(m3, front=True)
        self.assertEqual(self.platform.limited_count, 3)
        self.assertEqual(self.platform.get(), m3)
        self.assertEqual(self.platform.get(), m2)
        self.assertEqual(self.platform.get(), m1)
        self.assertRaises(Exception, self.platform.get)

    def test_available(self):
        self.assertFalse(self.platform.available())
        static_data_1 = {'url': static_items_url}
        limited_data_1 = {'url': match_url_template.format(matchid=100)}
        limited_data_2 = {'url': match_url_template.format(matchid=200)}
        self.platform.add_data(static_data_1)
        self.assertTrue(self.platform.available())
        self.platform.get()
        self.assertFalse(self.platform.available())
        self.platform.add_data(limited_data_1)
        self.assertTrue(self.platform.available())
        self.platform.add_data(limited_data_2)
        self.platform.get()
        self.assertTrue(self.platform.available())  # No response headers yet

        new_headers = copy.copy(headers)
        new_headers['X-Method-Rate-Limit'] = '1:0.1'
        new_headers['X-Method-Rate-Limit-Count'] = '1:0.1'
        self.platform.handle_response_headers(
            match_url_template.format(matchid=100), new_headers, 200)
        self.assertFalse(self.platform.available())
        time.sleep(0.1)
        self.assertTrue(self.platform.available())

    def test_handle_delay(self):
        limited_data_1 = {'url': match_url_template.format(matchid=100)}
        limited_data_2 = {'url': match_url_template.format(matchid=200)}
        self.platform.add_data(limited_data_1)
        self.platform.add_data(limited_data_2)
        self.assertTrue(self.platform.available())
        self.platform.get()
        new_headers = get_date_header()
        new_headers['X-Method-Rate-Limit'] = '2:0.1'
        new_headers['X-Method-Rate-Limit-Count'] = '1:0.1'
        self.platform.handle_response_headers(
            match_url_template.format(matchid=100), new_headers, 429)
        self.assertFalse(
            self.platform.available())  # Should have a default delay
        time.sleep(1)
        self.assertTrue(self.platform.available())

    def test_get_usage(self):
        used = {'static': {}, 'limited': {}}
        self.assertEqual(self.platform.get_usage(), used)
        url = match_url_template.format(matchid=100)
        match_endpoint = Endpoint.identify_endpoint(url)
        used['limited'][match_endpoint] = 'No limits defined'
        self.platform.add_data({'url': url})
        self.assertEqual(self.platform.get_usage(), used)
        self.platform.handle_response_headers(url, headers)
        used['limited'][match_endpoint] = '1:270'
        self.assertEqual(self.platform.get_usage(), used)
        self.platform.get()
        used['limited'][match_endpoint] = '2:270'
        self.assertEqual(self.platform.get_usage(), used)

        # Static tests
        static_endpoint = Endpoint.identify_endpoint(static_champions_url)
        self.platform.add_data({'url': static_champions_url})
        used['static'][static_endpoint] = 'No limits defined'
        self.assertEqual(self.platform.get_usage(), used)
        self.platform.get()
        new_headers = copy.copy(headers)
        new_headers['X-Method-Rate-Limit-Count'] = '1:60,2:120'
        new_headers['X-Method-Rate-Limit'] = '7:60,10:120'
        used['static'][static_endpoint] = '1:7,2:10'
        self.platform.handle_response_headers(static_champions_url,
                                              new_headers)
Exemplo n.º 2
0
def testPlatform():
    platform = Platform()
    assert (platform.rate_limit_ok())
    assert (platform.has_url() == False)
    assert (platform.available() == False)  # no URLS
    assert (platform.time_next_available() == None)
    assert (platform.count == 0)
    assert (platform.get() == (None, True, False))

    summoner_url = 'https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/SomeSummonerName'
    platform.addURL(summoner_url)
    assert (platform.count == 1)
    assert (platform.static_count == 0)
    assert (platform.limited_count == 1)
    assert (platform.time_next_available() < time.time())
    assert (platform.available())

    static_url = 'https://na1.api.riotgames.com/lol/static-data/v3/champions?locale=en_US&dataById=false'
    platform.addURL(static_url)
    assert (platform.count == 2)
    assert (platform.static_count == 1)
    assert (platform.limited_count == 1)
    assert (platform.time_next_available() < time.time())
    assert (platform.available())

    # When two are present, static should be pulled first
    assert (platform.get() == (static_url, True, True))
    assert (platform.count == 1)
    assert (platform.static_count == 0)
    assert (platform.get() == (summoner_url, True, True))
    assert (platform.count == 0)

    platform = Platform()
    champ_mastery_url = 'https://na1.api.riotgames.com/lol/champion-mastery/v3/champion-masteries/by-summoner/28341307'
    platform.addURL(summoner_url)
    platform.addURL(summoner_url)
    platform.addURL(champ_mastery_url)
    platform.addURL(champ_mastery_url)
    assert (platform.count == 4)
    assert (platform.last_limited_endpoint == '')
    # summoner_url was added first, so that endpoint gets pulled first
    # it then rotates to champ_mastery_url, the next endpoint added
    assert (platform.get() == (summoner_url, True, True))
    assert (platform.get() == (champ_mastery_url, True, True))
    assert (platform.get() == (summoner_url, True, True))
    assert (platform.get() == (champ_mastery_url, True, True))

    headers = {
        'X-Method-Rate-Limit': '1:0.1,10:1',
        'X-Method-Rate-Limit-Count': '0:0.1,9:1',
        'X-App-Rate-Limit': '5:0.1,40:1',
        'X-App-Rate-Limit-Count': '0:0.1,39:1'
    }
    platform.setLimit(headers)
    platform.setCount(headers)
    assert (platform.rate_limit_ok())
    platform.addURL(summoner_url)
    platform.addURL(summoner_url)
    assert (platform.get() == (summoner_url, False, True))
    assert (platform.rate_limit_ok() == False)
    assert (platform.get() == (None, False, False))

    platform = Platform()
    platform.addURL(summoner_url)
    platform.addURL(summoner_url)
    platform.setLimitAndCount(headers)
    assert (platform.rate_limit_ok())
    assert (platform.get() == (summoner_url, False, True))
    assert (platform.rate_limit_ok() == False)
    platform.addURL(summoner_url)
    assert (platform.get() == (None, False, False))

    platform = Platform()
    platform.addURL(summoner_url)
    platform.addURL(summoner_url)
    platform.setEndpointLimit(summoner_url, headers)
    platform.setEndpointCount(summoner_url, headers)
    assert (platform.get() == (summoner_url, True, False))
    assert (platform.get() == (None, True, False))

    platform = Platform()
    platform.addURL(summoner_url)
    platform.setEndpointLimitAndCount(summoner_url, headers)
    assert (platform.get() == (summoner_url, True, False))

    platform = Platform()
    platform.addURL(summoner_url)
    platform.setLimitAndCount(headers)
    platform.setEndpointLimitAndCount(summoner_url, headers)
    assert (platform.get() == (summoner_url, False, False))

    print('Platform tests pass')