Skip to content

Commit 2986760

Browse files
committed
tests: Test conflict between onchain and offchain
1 parent 76a2aa5 commit 2986760

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

tests/integration-tests/file-data-sources/src/mapping.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ethereum, dataSource, BigInt, Bytes } from '@graphprotocol/graph-ts'
1+
import { ethereum, dataSource, BigInt, Bytes, DataSourceContext } from '@graphprotocol/graph-ts'
22
import { TestEvent } from '../generated/Contract/Contract'
33
import { IpfsFile, IpfsFile1 } from '../generated/schema'
44

@@ -49,6 +49,11 @@ export function handleTestEvent(event: TestEvent): void {
4949
let entity = new IpfsFile(KNOWN_CID)
5050
entity.content = "empty"
5151
entity.save()
52+
} else if (command == "createFile1") {
53+
// Will fail the subgraph with a conflict between two entities created by offchain data sources.
54+
let context = new DataSourceContext();
55+
context.setBytes("hash", event.block.hash);
56+
dataSource.createWithContext("File1", [KNOWN_CID], context)
5257
} else {
5358
assert(false, "Unknown command: " + command);
5459
}

tests/tests/runner.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ async fn file_data_sources() {
213213
let stop_block = test_ptr(5);
214214
let err = ctx.start_and_sync_to_error(stop_block.clone()).await;
215215
let message = "entity type `IpfsFile1` is not on the 'entities' list for data source `File2`. \
216-
Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`.\twasm backtrace:\t 0: 0x3528 - <unknown>!src/mapping/handleFile1\t in handler `handleFile1` at block #5 ()".to_string();
216+
Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`.\twasm backtrace:\t 0: 0x35a8 - <unknown>!src/mapping/handleFile1\t in handler `handleFile1` at block #5 ()".to_string();
217217
let expected_err = SubgraphError {
218218
subgraph_id: ctx.deployment.hash.clone(),
219219
message,
@@ -223,7 +223,7 @@ async fn file_data_sources() {
223223
};
224224
assert_eq!(err, expected_err);
225225

226-
// Unfail the subgraph to test a different error
226+
// Unfail the subgraph to test a conflict between an onchain and offchain entity
227227
{
228228
ctx.rewind(test_ptr(4));
229229

@@ -237,17 +237,43 @@ async fn file_data_sources() {
237237

238238
chain.set_block_stream(blocks);
239239

240+
// Errors in the store pipeline can be observed by using the runner directly.
241+
let runner = ctx.runner(block_5_1_ptr.clone()).await;
242+
let err = runner
243+
.run()
244+
.await
245+
.err()
246+
.unwrap_or_else(|| panic!("subgraph ran successfully but an error was expected"));
247+
248+
let message =
249+
"store error: conflicting key value violates exclusion constraint \"ipfs_file_id_block_range_excl\""
250+
.to_string();
251+
assert_eq!(err.to_string(), message);
252+
}
253+
254+
// Unfail the subgraph to test a conflict between an onchain and offchain entity
255+
{
256+
// Replace block number 5 with one that contains a different event
257+
let mut blocks = blocks.clone();
258+
blocks.pop();
259+
let block_5_2_ptr = test_ptr_reorged(5, 2);
260+
let mut block_5_2 = empty_block(test_ptr(4), block_5_2_ptr.clone());
261+
push_test_log(&mut block_5_2, "createFile1");
262+
blocks.push(block_5_2);
263+
264+
chain.set_block_stream(blocks);
265+
240266
// Errors in the store pipeline can be observed by using the runner directly.
241267
let err = ctx
242-
.runner(block_5_1_ptr.clone())
268+
.runner(block_5_2_ptr.clone())
243269
.await
244270
.run()
245271
.await
246272
.err()
247273
.unwrap_or_else(|| panic!("subgraph ran successfully but an error was expected"));
248274

249275
let message =
250-
"store error: conflicting key value violates exclusion constraint \"ipfs_file_id_block_range_excl\""
276+
"store error: conflicting key value violates exclusion constraint \"ipfs_file_1_id_block_range_excl\""
251277
.to_string();
252278
assert_eq!(err.to_string(), message);
253279
}

0 commit comments

Comments
 (0)