From 2986760d43d8f5599dd25fe0a71e84c6f5450331 Mon Sep 17 00:00:00 2001 From: Leonardo Yvens Date: Wed, 4 Jan 2023 17:38:52 -0300 Subject: [PATCH] tests: Test conflict between onchain and offchain --- .../file-data-sources/src/mapping.ts | 7 +++- tests/tests/runner.rs | 34 ++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/tests/integration-tests/file-data-sources/src/mapping.ts b/tests/integration-tests/file-data-sources/src/mapping.ts index e62470149b8..8c08a94030a 100644 --- a/tests/integration-tests/file-data-sources/src/mapping.ts +++ b/tests/integration-tests/file-data-sources/src/mapping.ts @@ -1,4 +1,4 @@ -import { ethereum, dataSource, BigInt, Bytes } from '@graphprotocol/graph-ts' +import { ethereum, dataSource, BigInt, Bytes, DataSourceContext } from '@graphprotocol/graph-ts' import { TestEvent } from '../generated/Contract/Contract' import { IpfsFile, IpfsFile1 } from '../generated/schema' @@ -49,6 +49,11 @@ export function handleTestEvent(event: TestEvent): void { let entity = new IpfsFile(KNOWN_CID) entity.content = "empty" entity.save() + } else if (command == "createFile1") { + // Will fail the subgraph with a conflict between two entities created by offchain data sources. + let context = new DataSourceContext(); + context.setBytes("hash", event.block.hash); + dataSource.createWithContext("File1", [KNOWN_CID], context) } else { assert(false, "Unknown command: " + command); } diff --git a/tests/tests/runner.rs b/tests/tests/runner.rs index 7e8925a9f8f..712db1c9d34 100644 --- a/tests/tests/runner.rs +++ b/tests/tests/runner.rs @@ -213,7 +213,7 @@ async fn file_data_sources() { let stop_block = test_ptr(5); let err = ctx.start_and_sync_to_error(stop_block.clone()).await; let message = "entity type `IpfsFile1` is not on the 'entities' list for data source `File2`. \ - Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`.\twasm backtrace:\t 0: 0x3528 - !src/mapping/handleFile1\t in handler `handleFile1` at block #5 ()".to_string(); + Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`.\twasm backtrace:\t 0: 0x35a8 - !src/mapping/handleFile1\t in handler `handleFile1` at block #5 ()".to_string(); let expected_err = SubgraphError { subgraph_id: ctx.deployment.hash.clone(), message, @@ -223,7 +223,7 @@ async fn file_data_sources() { }; assert_eq!(err, expected_err); - // Unfail the subgraph to test a different error + // Unfail the subgraph to test a conflict between an onchain and offchain entity { ctx.rewind(test_ptr(4)); @@ -237,9 +237,35 @@ async fn file_data_sources() { chain.set_block_stream(blocks); + // Errors in the store pipeline can be observed by using the runner directly. + let runner = ctx.runner(block_5_1_ptr.clone()).await; + let err = runner + .run() + .await + .err() + .unwrap_or_else(|| panic!("subgraph ran successfully but an error was expected")); + + let message = + "store error: conflicting key value violates exclusion constraint \"ipfs_file_id_block_range_excl\"" + .to_string(); + assert_eq!(err.to_string(), message); + } + + // Unfail the subgraph to test a conflict between an onchain and offchain entity + { + // Replace block number 5 with one that contains a different event + let mut blocks = blocks.clone(); + blocks.pop(); + let block_5_2_ptr = test_ptr_reorged(5, 2); + let mut block_5_2 = empty_block(test_ptr(4), block_5_2_ptr.clone()); + push_test_log(&mut block_5_2, "createFile1"); + blocks.push(block_5_2); + + chain.set_block_stream(blocks); + // Errors in the store pipeline can be observed by using the runner directly. let err = ctx - .runner(block_5_1_ptr.clone()) + .runner(block_5_2_ptr.clone()) .await .run() .await @@ -247,7 +273,7 @@ async fn file_data_sources() { .unwrap_or_else(|| panic!("subgraph ran successfully but an error was expected")); let message = - "store error: conflicting key value violates exclusion constraint \"ipfs_file_id_block_range_excl\"" + "store error: conflicting key value violates exclusion constraint \"ipfs_file_1_id_block_range_excl\"" .to_string(); assert_eq!(err.to_string(), message); }