Skip to content

Commit

Permalink
Add queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Dec 5, 2023
1 parent 4c329ae commit d087c3c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
64 changes: 64 additions & 0 deletions tests/graphql_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
from decimal import Decimal
from unittest.mock import patch

import pytest
from pytest_httpx import HTTPXMock

from world_boss.app.data_provider import DATA_PROVIDER_URLS
from world_boss.app.enums import NetworkType
from world_boss.app.models import Transaction, WorldBossReward, WorldBossRewardAmount


@pytest.fixture()
def non_mocked_hosts() -> list:
return ["testserver"]


def test_next_tx_nonce(fx_session, fx_test_client):
query = "query { nextTxNonce }"
resp = fx_test_client.post("/graphql", json={"query": query})
result = resp.json()
assert result["data"]["nextTxNonce"] == 1


def test_count_total_users(fx_test_client, httpx_mock: HTTPXMock):
httpx_mock.add_response(
method="POST",
url=DATA_PROVIDER_URLS[NetworkType.MAIN],
json={"data": {"worldBossTotalUsers": 100}},
)
query = "query { countTotalUsers(seasonId: 1) }"
req = fx_test_client.post("/graphql", json={"query": query})
assert req.status_code == 200
result = req.json()
assert result["data"]["countTotalUsers"] == 100


def test_check_balance(fx_session, fx_test_client):
reward = WorldBossReward()
reward.avatar_address = "avatar_address"
reward.agent_address = "agent_address"
reward.raid_id = 1
reward.ranking = 1
i = 1
for ticker, decimal_places in [("CRYSTAL", 18), ("RUNE_FENRIR1", 0)]:
transaction = Transaction()
transaction.tx_id = str(i)
transaction.signer = "signer"
transaction.payload = f"10 {ticker}"
transaction.nonce = i
reward_amount = WorldBossRewardAmount()
reward_amount.amount = Decimal("10")
reward_amount.ticker = ticker
reward_amount.decimal_places = decimal_places
reward_amount.reward = reward
reward_amount.transaction = transaction
fx_session.add(reward_amount)
i += 1
fx_session.commit()
expected = ["1 CRYSTAL", "2 RUNE_FENRIR1"]
query = "query { checkBalance }"
with patch(
"world_boss.app.graphql.signer.query_balance",
side_effect=expected,
) as m:
req = fx_test_client.post("/graphql", json={"query": query})
assert req.status_code == 200
result = req.json()
assert m.call_count == 2
assert result["data"]["checkBalance"] == expected
20 changes: 19 additions & 1 deletion world_boss/app/graphql.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import typing

import strawberry
from fastapi import Depends
from strawberry.fastapi import GraphQLRouter
from strawberry.types import Info

from world_boss.app.api import get_db
from world_boss.app.raid import get_next_tx_nonce
from world_boss.app.data_provider import data_provider_client
from world_boss.app.enums import NetworkType
from world_boss.app.kms import MINER_URLS, signer
from world_boss.app.raid import get_currencies, get_next_tx_nonce


async def get_context(db=Depends(get_db)):
Expand All @@ -21,6 +26,19 @@ def next_tx_nonce(self, info: Info) -> int:
print(nonce)
return nonce

@strawberry.field
def count_total_users(self, season_id: int) -> int:
return data_provider_client.get_total_users_count(season_id, NetworkType.MAIN)

@strawberry.field
def check_balance(self, info: Info) -> typing.List[str]:
currencies = get_currencies(info.context["db"])
url = MINER_URLS[NetworkType.MAIN]
result = []
for currency in currencies:
result.append(signer.query_balance(url, currency))
return result


schema = strawberry.Schema(Query)
graphql_app = GraphQLRouter(schema, context_getter=get_context)

0 comments on commit d087c3c

Please sign in to comment.