Skip to content

Commit

Permalink
test: rs-sdk tests refactoring (#1566)
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Nov 14, 2023
1 parent 19080b5 commit d9e5f27
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 144 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 0 additions & 17 deletions packages/rs-sdk/.env.example

This file was deleted.

32 changes: 24 additions & 8 deletions packages/rs-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ dpp = { path = "../rs-dpp", features = [
"identity-value-conversion",
"state-transition-signing",
] }
dapi-grpc = { path = "../dapi-grpc", features = ["client", "mocks"] }
rs-dapi-client = { path = "../rs-dapi-client" }
dapi-grpc = { path = "../dapi-grpc", features = ["client"] }
rs-dapi-client = { path = "../rs-dapi-client", default-features = false }
drive = { path = "../rs-drive", default-features = false, features = [
"verify",
] }
Expand Down Expand Up @@ -41,14 +41,15 @@ dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore-rpc", branch =
bip37-bloom-filter = { git = "https://github.com/dashpay/rs-bip37-bloom-filter", branch = "develop" }

[dev-dependencies]
rs-dapi-client = { path = "../rs-dapi-client", features = ["mocks", "dump"] }
rs-dapi-client = { path = "../rs-dapi-client", features = ["mocks"] }
base64 = { version = "0.21.0" }
tracing-subscriber = { version = "0.3.16" }
dpp = { path = "../rs-dpp", features = [
"client",
"validation",
"random-documents",
] }
data-contracts = { path = "../data-contracts" }


[features]
Expand All @@ -67,10 +68,25 @@ mocks = [
"dep:envy",
]

# Disable this feature to run integration tests against a local Dash Platform
# and dump all traffic to generate test vectors.
# Run integration tests using test vectors from `tests/vectors/` instead of connecting to live Dash Platform.
#
# This feature is enabled by default because disabling it requires a local Dash Platform
# node to be running. Configuration of the node is done via environment variables
# and/or `${CARGO_MAINFEST_DIR}/.env` file; see also `.env.example`.
# This feature is enabled by default to allow testing without connecting to the Dash Platform as
# part of CI/CD workflows.
#
# `offline-testing` and `network-testing` are mutually exclusive.
offline-testing = ["mocks"]

# Run integration tests using a live Dash Platform network.
#
# Requires configuration of Dash Platform connectivity.
# See [README.md] for more details.
#
# `offline-testing` and `network-testing` are mutually exclusive.
network-testing = ["mocks"]

# Generate test vectors for offline tests.
#
# This will run tests in `network-testing` mode and
# dump all requests and responses to `tests/vectors/`,
# so that they can be used later for `offline-testing`.
generate-test-vectors = ["network-testing"]
63 changes: 19 additions & 44 deletions packages/rs-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,40 @@ You can also inspect tests in `tests/` folder for more detailed examples.

## Tests

This section provides instructions on how to test the RS-SDK for Dash Platform. The tests can be run in two modes: **offline** (without connectivity to the Dash Platform) and **online** (with connectivity to the Dash Platform). **Offline** mode is the default one.
This section provides instructions on how to test the RS-SDK for Dash Platform. The tests can be run in two modes: **offline** (without connectivity to the Dash Platform) and **network** (with connectivity to the Dash Platform). **Offline** mode is the default one.

## Online Testing
## Network Testing

Online testing requires connectivity to the Dash Platform and Dash Core. This mode generates new test vectors that can be used in offline mode.
Network testing requires connectivity to the Dash Platform and Dash Core.

Follow these steps to conduct online testing:
Follow these steps to conduct network testing:

1. Configure the environment variables in `packages/dash-platform-sdk/.env`. Refer to the "Test Configuration" section below.
2. Optionally, you can remove existing test vectors.
3. Run the test without default features, but with `mocks` feature enabled.

Use the following commands for the above steps:
1. Configure platform address and credentials in `packages/rs-sdk/tests/.env`.
Note that the `.env` file might already be configured during project setup (`yarn setup`).
2. Run the test without default features, but with `network-testing` feature enabled.

```bash
cd packages/dash-platform-sdk
rm tests/vectors/*
cargo test -p dash-platform-sdk --no-default-features --features mocks
cd packages/rs-sdk
cargo test -p rs-sdk --no-default-features --features network-testing
```

## Offline Testing

Offline testing uses the vectors generated in online mode. These vectors must be saved in `packages/dash-platform-sdk/tests/vectors`.

Run the offline test using the following command:

```bash
cargo test -p dash-platform-sdk
```

## Test Configuration
Offline testing uses the vectors generated using `packages/rs-sdk/scripts/generate_test_vectors.sh` script.
These vectors must be saved in `packages/rs-sdk/tests/vectors`.

For the `offline-testing` feature, you need to set the configuration in the environment variables or in `packages/dash-platform-sdk/.env` file. You can refer to `packages/dash-platform-sdk/.env.example` for the format.
### Generating test vectors

The identifiers are generated with the platform test suite. To display them, apply the following diff:
To generate test vectors for offline testing:

```diff
diff --git a/packages/platform-test-suite/test/functional/platform/Document.spec.js b/packages/platform-test-suite/test/functional/platform/Document.spec.js
index 29dca311b..fba0aefc2 100644
--- a/packages/platform-test-suite/test/functional/platform/Document.spec.js
+++ b/packages/platform-test-suite/test/functional/platform/Document.spec.js
@@ -180,6 +180,9 @@ describe('Platform', () => {

// Additional wait time to mitigate testnet latency
await waitForSTPropagated();
+ console.log("Owner ID: " + document.getOwnerId().toString("base58"));
+ console.log("Data Contract: " + document.getDataContractId().toString("base58"));
+ console.log("Document: " + document.getId().toString("base58"));
});

it('should fetch created document', async () => {
1. Configure platform address and credentials in `packages/rs-sdk/tests/.env`.
Note that the `.env` file might already be configured during project setup (`yarn setup`).
2. Run `packages/rs-sdk/scripts/generate_test_vectors.sh` script.

```
### Running tests in offline mode

To run the document test, use the following commands:
Run the offline test using the following command:

```bash
cd packages/platform-test-suite/
yarn mocha -b test/functional/platform/Document.spec.js
cargo test -p dash-platform-sdk
```

Find the values in the output and copy them to `packages/dash-platform-sdk/.env`.
21 changes: 21 additions & 0 deletions packages/rs-sdk/scripts/generate_test_vectors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#! /bin/bash -ex

# This script generates test vectors for offline testing of the SDK.
# Test vectors will be generated in the `tests/vectors` directory.
#
# Existing test vectors are removed before generating new ones.
#
# Generation of test vectors is done by running the SDK tests with the
# `generate-test-vectors` feature enabled.

CARGO_DIR="$(realpath "$(dirname "$0")/..")"

pushd "$CARGO_DIR"

rm -f "${CARGO_DIR}"/tests/vectors/*

cargo test -p rs-sdk \
--no-default-features \
--features generate-test-vectors

popd
3 changes: 1 addition & 2 deletions packages/rs-sdk/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
//! ```
//!
//! See tests/mock_*.rs for more detailed examples.
#[cfg(feature = "mocks")]
pub mod config;
#[cfg(feature = "mocks")]
mod requests;
#[cfg(feature = "mocks")]
Expand Down
8 changes: 8 additions & 0 deletions packages/rs-sdk/tests/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Configuration of tests and examples

RS_SDK_PLATFORM_HOST="127.0.0.1"
RS_SDK_PLATFORM_PORT=2443

RS_SDK_CORE_PORT=20002
RS_SDK_CORE_USER="someuser"
RS_SDK_CORE_PASSWORD="verysecretpassword"
21 changes: 0 additions & 21 deletions packages/rs-sdk/tests/fetch/common.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
use dpp::{data_contract::DataContractFactory, prelude::Identifier};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct TestConfig {
// IDs of some objects generated by the testnet
/// ID of existing identity.
///
/// Format: Base58
pub existing_identity_id: Identifier,
/// ID of existing data contract.
///
/// Format: Base58
pub existing_data_contract_id: Identifier,
/// Name of document type defined for [`existing_data_contract_id`](Config::existing_data_contract_id).
pub existing_document_type_name: String,
/// ID of document of the type [`existing_document_type_name`](Config::existing_document_type_name)
/// in [`existing_data_contract_id`](Config::existing_data_contract_id).
pub existing_document_id: Identifier,
}

pub type Config = dash_platform_sdk::mock::config::Config<TestConfig>;

/// Create a mock document type for testing of mock API
pub fn mock_document_type() -> dpp::data_contract::document_type::DocumentType {
Expand Down
Loading

0 comments on commit d9e5f27

Please sign in to comment.