drive/util/test_helpers/
mod.rs

1#[cfg(feature = "server")]
2use std::fs::File;
3#[cfg(feature = "server")]
4use std::io;
5#[cfg(feature = "server")]
6use std::io::BufRead;
7#[cfg(feature = "server")]
8use std::path::Path;
9
10#[cfg(feature = "fixtures-and-mocks")]
11use grovedb::TransactionArg;
12
13#[cfg(feature = "fixtures-and-mocks")]
14use crate::drive::Drive;
15#[cfg(feature = "fixtures-and-mocks")]
16use dpp::data_contract::DataContract;
17
18#[cfg(feature = "fixtures-and-mocks")]
19use dpp::block::block_info::BlockInfo;
20#[cfg(feature = "fixtures-and-mocks")]
21use dpp::prelude::Identifier;
22
23#[cfg(feature = "fixtures-and-mocks")]
24use dpp::tests::json_document::json_document_to_contract_with_ids;
25#[cfg(feature = "fixtures-and-mocks")]
26use dpp::version::PlatformVersion;
27
28#[cfg(test)]
29use ciborium::value::Value;
30
31#[cfg(any(test, feature = "server"))]
32pub mod setup;
33#[cfg(any(test, feature = "fixtures-and-mocks"))]
34/// test utils
35pub mod test_utils;
36
37#[cfg(feature = "fixtures-and-mocks")]
38/// Applies to Drive a JSON contract from the file system.
39pub fn setup_contract(
40    drive: &Drive,
41    path: &str,
42    contract_id: Option<[u8; 32]>,
43    owner_id: Option<[u8; 32]>,
44    contract_modification: Option<impl FnOnce(&mut DataContract)>,
45    transaction: TransactionArg,
46    use_platform_version: Option<&PlatformVersion>,
47) -> DataContract {
48    let platform_version = use_platform_version.unwrap_or(PlatformVersion::latest());
49    let mut contract = json_document_to_contract_with_ids(
50        path,
51        contract_id.map(Identifier::from),
52        owner_id.map(Identifier::from),
53        false, //no need to validate the data contracts in tests for drive
54        platform_version,
55    )
56    .expect("expected to get json based contract");
57
58    if let Some(contract_modification) = contract_modification {
59        contract_modification(&mut contract);
60    }
61
62    drive
63        .apply_contract(
64            &contract,
65            BlockInfo::default(),
66            true,
67            None,
68            transaction,
69            platform_version,
70        )
71        .expect("contract should be applied");
72    contract
73}
74
75#[cfg(test)]
76/// Serializes a hex string to CBOR.
77pub fn cbor_from_hex(hex_string: String) -> Vec<u8> {
78    hex::decode(hex_string).expect("Decoding failed")
79}
80
81#[cfg(feature = "server")]
82/// Takes a file and returns the lines as a list of strings.
83pub fn text_file_strings(path: impl AsRef<Path>) -> Vec<String> {
84    let file = File::open(path).expect("file not found");
85    let reader = io::BufReader::new(file).lines();
86    reader.into_iter().map(|a| a.unwrap()).collect()
87}
88
89#[cfg(test)]
90/// Retrieves the value of a key from a CBOR map.
91pub fn get_key_from_cbor_map<'a>(
92    cbor_map: &'a [(Value, Value)],
93    key: &'a str,
94) -> Option<&'a Value> {
95    for (cbor_key, cbor_value) in cbor_map.iter() {
96        if !cbor_key.is_text() {
97            continue;
98        }
99
100        if cbor_key.as_text().expect("confirmed as text") == key {
101            return Some(cbor_value);
102        }
103    }
104    None
105}
106
107#[cfg(test)]
108/// Retrieves the value of a key from a CBOR map if it's a map itself.
109pub fn cbor_inner_map_value<'a>(
110    document_type: &'a [(Value, Value)],
111    key: &'a str,
112) -> Option<&'a Vec<(Value, Value)>> {
113    let key_value = get_key_from_cbor_map(document_type, key)?;
114    if let Value::Map(map_value) = key_value {
115        return Some(map_value);
116    }
117    None
118}