diff --git a/tests/graphql_test.py b/tests/graphql_test.py index 642e578..09a95e0 100644 --- a/tests/graphql_test.py +++ b/tests/graphql_test.py @@ -69,8 +69,13 @@ def test_check_balance(fx_session, fx_test_client): assert result["data"]["checkBalance"] == expected +@pytest.mark.parametrize("size", [100, 50]) def test_generate_ranking_rewards_csv( - fx_test_client, celery_session_worker, httpx_mock: HTTPXMock, fx_ranking_rewards + fx_test_client, + celery_session_worker, + httpx_mock: HTTPXMock, + fx_ranking_rewards, + size: int, ): requested_rewards = [ { @@ -101,7 +106,7 @@ def test_generate_ranking_rewards_csv( }, ) - query = f'mutation {{ generateRankingRewardsCsv(seasonId: 1, totalUsers: 1, startNonce: 1, password: "{config.graphql_password}") }}' + query = f'mutation {{ generateRankingRewardsCsv(seasonId: 1, totalUsers: 1, startNonce: 1, password: "{config.graphql_password}", size: {size}) }}' with patch("world_boss.app.tasks.client.files_upload_v2") as m: req = fx_test_client.post("/graphql", json={"query": query}) assert req.status_code == 200 @@ -114,8 +119,8 @@ def test_generate_ranking_rewards_csv( kwargs = m.call_args.kwargs assert kwargs["file"] assert kwargs["channels"] == config.slack_channel_id - assert kwargs["title"] == f"world_boss_1_1_1_result" - assert kwargs["filename"] == f"world_boss_1_1_1_result.csv" + assert kwargs["title"] == f"world_boss_1_1_1_{size}_result" + assert kwargs["filename"] == f"world_boss_1_1_1_{size}_result.csv" @pytest.mark.parametrize("has_header", [True, False]) diff --git a/tests/raid_test.py b/tests/raid_test.py index 16a4650..7b6cea1 100644 --- a/tests/raid_test.py +++ b/tests/raid_test.py @@ -71,13 +71,17 @@ def test_update_agent_address( @pytest.mark.parametrize("raid_id", [1, 2]) -@pytest.mark.parametrize("start_nonce, bottom, last_nonce", [(1, 100, 4), (2, 4, 2)]) +@pytest.mark.parametrize( + "start_nonce, bottom, size, last_nonce", + [(1, 100, 100, 4), (1, 100, 50, 8), (2, 4, 100, 2), (2, 4, 1, 17)], +) def test_write_ranking_rewards_csv( tmp_path, fx_ranking_rewards, raid_id: int, start_nonce: int, bottom: int, + size: int, last_nonce: int, ): file_name = tmp_path / "test.csv" @@ -94,9 +98,11 @@ def test_write_ranking_rewards_csv( } for i in range(0, bottom) ] - write_ranking_rewards_csv(file_name, reward_list, raid_id, start_nonce) + write_ranking_rewards_csv(file_name, reward_list, raid_id, start_nonce, size) with open(file_name, "r") as f: rows = f.readlines() + # header + fx_ranking_rewards * bottom + assert len(rows) == 1 + (bottom * 4) # check header assert ( rows[0] diff --git a/tests/tasks_test.py b/tests/tasks_test.py index 1bf6412..57686a3 100644 --- a/tests/tasks_test.py +++ b/tests/tasks_test.py @@ -44,17 +44,15 @@ def test_count_users(celery_session_worker, httpx_mock: HTTPXMock): ) +@pytest.mark.parametrize("size", [100, 50]) def test_get_ranking_rewards( - redisdb, - celery_session_worker, - httpx_mock: HTTPXMock, - fx_ranking_rewards, + redisdb, celery_session_worker, httpx_mock: HTTPXMock, fx_ranking_rewards, size: int ): raid_id = 1 network_type = NetworkType.MAIN offset = 0 - rewards_cache_key = f"world_boss_{raid_id}_{network_type}_{offset}_100" - addresses_cache_key = f"world_boss_agents_{raid_id}_{network_type}_{offset}_100" + rewards_cache_key = f"world_boss_{raid_id}_{network_type}_{offset}_{size}" + addresses_cache_key = f"world_boss_agents_{raid_id}_{network_type}_{offset}_{size}" # get from cache key cached_rewards: List[RankingRewardDictionary] = [ @@ -65,7 +63,7 @@ def test_get_ranking_rewards( }, "rewards": fx_ranking_rewards, } - for i in range(0, 100) + for i in range(0, size) ] # get from service query @@ -73,7 +71,7 @@ def test_get_ranking_rewards( { "raider": { "address": "01A0b412721b00bFb5D619378F8ab4E4a97646Ca", - "ranking": 101, + "ranking": size + 1, }, "rewards": fx_ranking_rewards, }, @@ -88,7 +86,7 @@ def test_get_ranking_rewards( }, "rewards": fx_ranking_rewards, } - for i in range(0, 100) + for i in range(0, size) ] redisdb.set(rewards_cache_key, json.dumps(cached_rewards)) httpx_mock.add_response( @@ -112,18 +110,22 @@ def test_get_ranking_rewards( ) with unittest.mock.patch("world_boss.app.tasks.client.files_upload_v2") as m: - get_ranking_rewards.delay("channel_id", raid_id, 101, 1).get(timeout=10) + get_ranking_rewards.delay("channel_id", raid_id, size + 1, 1, size).get( + timeout=10 + ) m.assert_called_once() # skip check file. because file is temp file. kwargs = m.call_args.kwargs assert kwargs["file"] assert kwargs["channels"] == "channel_id" - assert kwargs["title"] == f"world_boss_{raid_id}_101_1_result" - assert kwargs["filename"] == f"world_boss_{raid_id}_101_1_result.csv" + assert kwargs["title"] == f"world_boss_{raid_id}_{size + 1}_1_{size}_result" + assert ( + kwargs["filename"] == f"world_boss_{raid_id}_{size + 1}_1_{size}_result.csv" + ) assert redisdb.exists(rewards_cache_key) assert redisdb.exists(addresses_cache_key) - assert redisdb.exists(f"world_boss_{raid_id}_{network_type}_100_1") - assert redisdb.exists(f"world_boss_agents_{raid_id}_{network_type}_100_1") + assert redisdb.exists(f"world_boss_{raid_id}_{network_type}_{size}_1") + assert redisdb.exists(f"world_boss_agents_{raid_id}_{network_type}_{size}_1") def test_get_ranking_rewards_error( @@ -148,7 +150,7 @@ def test_get_ranking_rewards_error( with unittest.mock.patch( "world_boss.app.tasks.client.chat_postMessage" ) as m, pytest.raises(Exception): - get_ranking_rewards.delay("channel_id", raid_id, 101, 1).get(timeout=10) + get_ranking_rewards.delay("channel_id", raid_id, 101, 1, 100).get(timeout=10) m.assert_called_once() kwargs = m.call_args.kwargs assert ( diff --git a/world_boss/app/api.py b/world_boss/app/api.py index 890347f..c5d16d9 100644 --- a/world_boss/app/api.py +++ b/world_boss/app/api.py @@ -87,7 +87,7 @@ async def generate_ranking_rewards_csv( ): values = text.split() raid_id, total_users, nonce = [int(v) for v in values] - task = get_ranking_rewards.delay(channel_id, raid_id, total_users, nonce) + task = get_ranking_rewards.delay(channel_id, raid_id, total_users, nonce, 100) return JSONResponse(task.id) diff --git a/world_boss/app/graphql.py b/world_boss/app/graphql.py index 21f015e..32ec1d0 100644 --- a/world_boss/app/graphql.py +++ b/world_boss/app/graphql.py @@ -78,10 +78,11 @@ def generate_ranking_rewards_csv( season_id: int, total_users: int, start_nonce: int, + size: int, password: str, ) -> str: task = get_ranking_rewards.delay( - config.slack_channel_id, season_id, total_users, start_nonce + config.slack_channel_id, season_id, total_users, start_nonce, size ) return task.id diff --git a/world_boss/app/raid.py b/world_boss/app/raid.py index f9636fa..8cff0a9 100644 --- a/world_boss/app/raid.py +++ b/world_boss/app/raid.py @@ -119,6 +119,7 @@ def write_ranking_rewards_csv( reward_list: List[RankingRewardWithAgentDictionary], raid_id: int, start_nonce: int, + size: int, ): with open(file_name, "w") as f: writer = csv.writer(f) @@ -152,7 +153,7 @@ def write_ranking_rewards_csv( amount, currency["ticker"], currency["decimalPlaces"], - start_nonce + int(i / 100), + start_nonce + int(i / size), ] ) i += 1 diff --git a/world_boss/app/tasks.py b/world_boss/app/tasks.py index 6061a93..14a3916 100644 --- a/world_boss/app/tasks.py +++ b/world_boss/app/tasks.py @@ -47,15 +47,15 @@ def count_users(channel_id: str, raid_id: int): @celery.task() def get_ranking_rewards( - channel_id: str, raid_id: int, total_count: int, start_nonce: int + channel_id: str, raid_id: int, total_count: int, start_nonce: int, size: int ): offset = 0 - size = 100 results: List[RankingRewardWithAgentDictionary] = [] + payload_size = size while len(results) < total_count: try: result = data_provider_client.get_ranking_rewards( - raid_id, NetworkType.MAIN, offset, size + raid_id, NetworkType.MAIN, offset, payload_size ) except Exception as e: client.chat_postMessage( @@ -63,14 +63,18 @@ def get_ranking_rewards( text=f"failed to get rewards from {config.data_provider_url} exc: {e}", ) raise e - rewards = update_agent_address(result, raid_id, NetworkType.MAIN, offset, size) + rewards = update_agent_address( + result, raid_id, NetworkType.MAIN, offset, payload_size + ) results.extend(reward for reward in rewards if reward not in results) offset = len(results) - size = min(size, total_count - offset) + payload_size = min(payload_size, total_count - offset) with NamedTemporaryFile(suffix=".csv") as temp_file: file_name = temp_file.name - write_ranking_rewards_csv(file_name, results, raid_id, start_nonce) - result_format = f"world_boss_{raid_id}_{total_count}_{start_nonce}_result" + write_ranking_rewards_csv(file_name, results, raid_id, start_nonce, size) + result_format = ( + f"world_boss_{raid_id}_{total_count}_{start_nonce}_{size}_result" + ) client.files_upload_v2( channels=channel_id, title=result_format,