Skip to content

Commit

Permalink
fixup: resolutions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 22, 2022
1 parent c2e37a5 commit 87544b6
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 29 deletions.
20 changes: 11 additions & 9 deletions src/genesis/helpers/field_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,12 @@ class AlmanacRegistrations(NamedFields):
signature = 2
sequence = 3
agent_id = 4
record_id = 5
transaction_id = 6
block_id = 7
# event_id = 8
# record_id = 9
contract_id = 5
record_id = 6
transaction_id = 7
block_id = 8
# event_id = 9
# record_id = 10

@classmethod
def get_table(self):
Expand All @@ -337,20 +338,21 @@ def get_table(self):

class AlmanacResolutions(NamedFields):
id = 0
address = 1
record_id = 2
agent_id = 1
contract_id = 2
record_id = 3

@classmethod
def get_table(self):
return "almanac_registrations"
return "almanac_resolutions"


class AlmanacRecords(NamedFields):
id = 0
service = 1
transaction_id = 2
block_id = 3
# event_id = 4
# event_id = 4

@classmethod
def get_table(self):
Expand Down
132 changes: 112 additions & 20 deletions tests/e2e/entities/test_almanac.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@
from cosmpy.aerial.tx_helpers import SubmittedTx
from gql import gql

from src.genesis.helpers.field_enums import Agents, AlmanacRecords, AlmanacRegistrations
from src.genesis.helpers.field_enums import (
Agents,
AlmanacRecords,
AlmanacRegistrations,
AlmanacResolutions,
)
from tests.helpers.contracts import AlmanacContract, DefaultAlmanacContractConfig
from tests.helpers.entity_test import EntityTest
from tests.helpers.graphql import filtered_test_query
from tests.helpers.regexes import block_id_regex, msg_id_regex, tx_id_regex
from uagents.src.nexus.crypto import Identity


def gql_by_endpoint_port(resolution_node: Dict) -> int:
return int(resolution_node["record"]["service"]["endpoints"][0]["url"][-4:])


def gql_by_expiry_height(registration_node: Dict) -> int:
return int(registration_node["expiryHeight"])

Expand All @@ -32,13 +41,14 @@ class Scenario:

class TestAlmanac(EntityTest):
test_registrations_endpoints = [
"127.0.0.1:9999",
"127.0.0.1:8888",
"127.0.0.1:7777",
"127.0.0.1:6666",
"127.0.0.1:7777",
"127.0.0.1:8888",
"127.0.0.1:9999",
]
submitted_txs: List[SubmittedTx] = []
expected_registrations: List[Dict] = []
expected_resolutions: List[Dict] = []
expected_records: List[Dict] = [
{
"service": {
Expand Down Expand Up @@ -92,16 +102,114 @@ def setUpClass(cls):
cls.expected_registrations.append(
{
"agentId": agent_address,
"contractId": str(cls._contract.address),
"expiryHeight": tx.response.height
+ DefaultAlmanacContractConfig.expiry_height,
"sequence": sequence,
"signature": signature,
"record": expected_record,
}
)
cls.expected_resolutions.append(
{
"agentId": agent_address,
"contractId": str(cls._contract.address),
"record": expected_record,
}
)
# NB: wait for the indexer
time.sleep(7)

# NB: test resolutions first as it's sensitive to the current height
def test_resolutions_sql(self):
resolutions = self.db_cursor.execute(
AlmanacResolutions.select_query()
).fetchall()
actual_resolution_count = len(resolutions)

current_height = int(
self.db_cursor.execute(
"""SELECT b.height FROM app.blocks b
ORDER BY b.height
LIMIT 1"""
).fetchone()[0]
)

last_expired_height = (
current_height - DefaultAlmanacContractConfig.expiry_height
)
first_unexpired = next(
tx for tx in self.submitted_txs if tx.response.height >= last_expired_height
)
last_expired_index = self.submitted_txs.index(first_unexpired) - 1
expected_resolutions = resolutions[last_expired_index + 1 :]
expected_resolutions_count = len(expected_resolutions)

self.assertEqual(expected_resolutions_count, actual_resolution_count)
# TODO: more assertions

def test_resolutions_gql(self):
resolutions_query = gql(
"""
query {
almanacResolutions {
nodes {
id
agentId
contractId
record {
id
service
# registrationId
# eventId
transactionId
blockId
}
}
}
}
"""
)

current_height = int(
self.db_cursor.execute(
"""SELECT b.height FROM app.blocks b
ORDER BY b.height DESC
LIMIT 1"""
).fetchone()[0]
)
last_expired_height = (
current_height - DefaultAlmanacContractConfig.expiry_height
)
first_unexpired = next(
r for r in self.submitted_txs if r.response.height > last_expired_height
)
first_unexpired_index = self.submitted_txs.index(first_unexpired)
expected_resolutions = self.expected_resolutions[first_unexpired_index:]

gql_result = self.gql_client.execute(resolutions_query)
resolutions = gql_result["almanacResolutions"]["nodes"]
self.assertEqual(len(expected_resolutions), len(resolutions))

# TODO: use respective gql order by when available
# NB: sort by expiry height so that indexes match
# their respective expected_resolutions index
list.sort(resolutions, key=gql_by_endpoint_port)
self.assertEqual(len(expected_resolutions), len(resolutions))

for (i, resolution) in enumerate(resolutions):
self.assertRegex(resolution["id"], msg_id_regex)
self.assertEqual(expected_resolutions[i]["agentId"], resolution["agentId"])
self.assertEqual(str(self._contract.address), resolution["contractId"])
record = resolution["record"]
self.assertEqual(
record["service"],
resolution["record"]["service"],
)
self.assertRegex(record["transactionId"], tx_id_regex)
self.assertRegex(record["blockId"], block_id_regex)
# TODO: assert record equality

def test_registrations_sql(self):
registrations = self.db_cursor.execute(
AlmanacRegistrations.select_query()
Expand Down Expand Up @@ -254,22 +362,6 @@ def test_registrations_gql(self):
self.assertRegex(registration["blockId"], block_id_regex)
# TODO: assert record equality

# GQL query
# - historical registrations: where expired
# - resolutions success
# - resolutions expired (empty result)
# assertions

def test_resolutions_sql(self):
pass
# SQL query
# assertions

def test_resolutions_gql(self):
pass
# GQL query
# assertions


if __name__ == "__main__":
unittest.main()

0 comments on commit 87544b6

Please sign in to comment.