Skip to content

Commit f0a6a10

Browse files
fixup: resolutions tests
1 parent ff6ddaa commit f0a6a10

File tree

2 files changed

+123
-29
lines changed

2 files changed

+123
-29
lines changed

src/genesis/helpers/field_enums.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,12 @@ class AlmanacRegistrations(NamedFields):
324324
signature = 2
325325
sequence = 3
326326
agent_id = 4
327-
record_id = 5
328-
transaction_id = 6
329-
block_id = 7
330-
# event_id = 8
331-
# record_id = 9
327+
contract_id = 5
328+
record_id = 6
329+
transaction_id = 7
330+
block_id = 8
331+
# event_id = 9
332+
# record_id = 10
332333

333334
@classmethod
334335
def get_table(self):
@@ -337,20 +338,21 @@ def get_table(self):
337338

338339
class AlmanacResolutions(NamedFields):
339340
id = 0
340-
address = 1
341-
record_id = 2
341+
agent_id = 1
342+
contract_id = 2
343+
record_id = 3
342344

343345
@classmethod
344346
def get_table(self):
345-
return "almanac_registrations"
347+
return "almanac_resolutions"
346348

347349

348350
class AlmanacRecords(NamedFields):
349351
id = 0
350352
service = 1
351353
transaction_id = 2
352354
block_id = 3
353-
# event_id = 4
355+
# event_id = 4
354356

355357
@classmethod
356358
def get_table(self):

tests/e2e/entities/test_almanac.py

+112-20
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@
77
from cosmpy.aerial.tx_helpers import SubmittedTx
88
from gql import gql
99

10-
from src.genesis.helpers.field_enums import Agents, AlmanacRecords, AlmanacRegistrations
10+
from src.genesis.helpers.field_enums import (
11+
Agents,
12+
AlmanacRecords,
13+
AlmanacRegistrations,
14+
AlmanacResolutions,
15+
)
1116
from tests.helpers.contracts import AlmanacContract, DefaultAlmanacContractConfig
1217
from tests.helpers.entity_test import EntityTest
1318
from tests.helpers.graphql import filtered_test_query
1419
from tests.helpers.regexes import block_id_regex, msg_id_regex, tx_id_regex
1520
from uagents.src.nexus.crypto import Identity
1621

1722

23+
def gql_by_endpoint_port(resolution_node: Dict) -> int:
24+
return int(resolution_node["record"]["service"]["endpoints"][0]["url"][-4:])
25+
26+
1827
def gql_by_expiry_height(registration_node: Dict) -> int:
1928
return int(registration_node["expiryHeight"])
2029

@@ -32,13 +41,14 @@ class Scenario:
3241

3342
class TestAlmanac(EntityTest):
3443
test_registrations_endpoints = [
35-
"127.0.0.1:9999",
36-
"127.0.0.1:8888",
37-
"127.0.0.1:7777",
3844
"127.0.0.1:6666",
45+
"127.0.0.1:7777",
46+
"127.0.0.1:8888",
47+
"127.0.0.1:9999",
3948
]
4049
submitted_txs: List[SubmittedTx] = []
4150
expected_registrations: List[Dict] = []
51+
expected_resolutions: List[Dict] = []
4252
expected_records: List[Dict] = [
4353
{
4454
"service": {
@@ -92,16 +102,114 @@ def setUpClass(cls):
92102
cls.expected_registrations.append(
93103
{
94104
"agentId": agent_address,
105+
"contractId": str(cls._contract.address),
95106
"expiryHeight": tx.response.height
96107
+ DefaultAlmanacContractConfig.expiry_height,
97108
"sequence": sequence,
98109
"signature": signature,
99110
"record": expected_record,
100111
}
101112
)
113+
cls.expected_resolutions.append(
114+
{
115+
"agentId": agent_address,
116+
"contractId": str(cls._contract.address),
117+
"record": expected_record,
118+
}
119+
)
102120
# NB: wait for the indexer
103121
time.sleep(7)
104122

123+
# NB: test resolutions first as it's sensitive to the current height
124+
def test_resolutions_sql(self):
125+
resolutions = self.db_cursor.execute(
126+
AlmanacResolutions.select_query()
127+
).fetchall()
128+
actual_resolution_count = len(resolutions)
129+
130+
current_height = int(
131+
self.db_cursor.execute(
132+
"""SELECT b.height FROM app.blocks b
133+
ORDER BY b.height
134+
LIMIT 1"""
135+
).fetchone()[0]
136+
)
137+
138+
last_expired_height = (
139+
current_height - DefaultAlmanacContractConfig.expiry_height
140+
)
141+
first_unexpired = next(
142+
tx for tx in self.submitted_txs if tx.response.height >= last_expired_height
143+
)
144+
last_expired_index = self.submitted_txs.index(first_unexpired) - 1
145+
expected_resolutions = resolutions[last_expired_index + 1 :]
146+
expected_resolutions_count = len(expected_resolutions)
147+
148+
self.assertEqual(expected_resolutions_count, actual_resolution_count)
149+
# TODO: more assertions
150+
151+
def test_resolutions_gql(self):
152+
resolutions_query = gql(
153+
"""
154+
query {
155+
almanacResolutions {
156+
nodes {
157+
id
158+
agentId
159+
contractId
160+
record {
161+
id
162+
service
163+
# registrationId
164+
# eventId
165+
transactionId
166+
blockId
167+
}
168+
}
169+
}
170+
}
171+
"""
172+
)
173+
174+
current_height = int(
175+
self.db_cursor.execute(
176+
"""SELECT b.height FROM app.blocks b
177+
ORDER BY b.height DESC
178+
LIMIT 1"""
179+
).fetchone()[0]
180+
)
181+
last_expired_height = (
182+
current_height - DefaultAlmanacContractConfig.expiry_height
183+
)
184+
first_unexpired = next(
185+
r for r in self.submitted_txs if r.response.height > last_expired_height
186+
)
187+
first_unexpired_index = self.submitted_txs.index(first_unexpired)
188+
expected_resolutions = self.expected_resolutions[first_unexpired_index:]
189+
190+
gql_result = self.gql_client.execute(resolutions_query)
191+
resolutions = gql_result["almanacResolutions"]["nodes"]
192+
self.assertEqual(len(expected_resolutions), len(resolutions))
193+
194+
# TODO: use respective gql order by when available
195+
# NB: sort by expiry height so that indexes match
196+
# their respective expected_resolutions index
197+
list.sort(resolutions, key=gql_by_endpoint_port)
198+
self.assertEqual(len(expected_resolutions), len(resolutions))
199+
200+
for (i, resolution) in enumerate(resolutions):
201+
self.assertRegex(resolution["id"], msg_id_regex)
202+
self.assertEqual(expected_resolutions[i]["agentId"], resolution["agentId"])
203+
self.assertEqual(str(self._contract.address), resolution["contractId"])
204+
record = resolution["record"]
205+
self.assertEqual(
206+
record["service"],
207+
resolution["record"]["service"],
208+
)
209+
self.assertRegex(record["transactionId"], tx_id_regex)
210+
self.assertRegex(record["blockId"], block_id_regex)
211+
# TODO: assert record equality
212+
105213
def test_registrations_sql(self):
106214
registrations = self.db_cursor.execute(
107215
AlmanacRegistrations.select_query()
@@ -254,22 +362,6 @@ def test_registrations_gql(self):
254362
self.assertRegex(registration["blockId"], block_id_regex)
255363
# TODO: assert record equality
256364

257-
# GQL query
258-
# - historical registrations: where expired
259-
# - resolutions success
260-
# - resolutions expired (empty result)
261-
# assertions
262-
263-
def test_resolutions_sql(self):
264-
pass
265-
# SQL query
266-
# assertions
267-
268-
def test_resolutions_gql(self):
269-
pass
270-
# GQL query
271-
# assertions
272-
273365

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

0 commit comments

Comments
 (0)