1use rand::rngs::StdRng;
2use rand::SeedableRng;
3
4use platform_value::{platform_value, Value};
5
6use crate::data_contract::accessors::v0::DataContractV0Getters;
7use crate::data_contract::document_type::methods::DocumentTypeV0Methods;
8use crate::document::document_factory::DocumentFactory;
9use crate::document::Document;
10use crate::version::PlatformVersion;
11use crate::{prelude::*, tests::utils::generate_random_identifier_struct as gen_owner_id};
12
13#[cfg(feature = "extended-document")]
14pub fn get_extended_documents_fixture_with_owner_id_from_contract(
15 data_contract: &DataContract,
16 protocol_version: u32,
17) -> Result<Vec<ExtendedDocument>, ProtocolError> {
18 let owner_id = data_contract.owner_id();
19 let factory = DocumentFactory::new(protocol_version)?;
20
21 get_extended_documents(factory, data_contract, owner_id)
22}
23
24pub fn get_documents_fixture_with_owner_id_from_contract(
25 data_contract: &DataContract,
26 protocol_version: u32,
27) -> Result<Vec<Document>, ProtocolError> {
28 let owner_id = data_contract.owner_id();
29 let factory = DocumentFactory::new(protocol_version)?;
30
31 get_documents(factory, data_contract, owner_id)
32}
33
34pub fn get_documents_fixture(
35 data_contract: &DataContract,
36 protocol_version: u32,
37) -> Result<Vec<Document>, ProtocolError> {
38 let factory = DocumentFactory::new(protocol_version)?;
39 let owner_id = gen_owner_id();
40
41 get_documents(factory, data_contract, owner_id)
42}
43
44#[cfg(feature = "extended-document")]
45pub fn get_extended_documents_fixture(
46 data_contract: &DataContract,
47 protocol_version: u32,
48) -> Result<Vec<ExtendedDocument>, ProtocolError> {
49 let factory = DocumentFactory::new(protocol_version)?;
50 let owner_id = gen_owner_id();
51
52 get_extended_documents(factory, data_contract, owner_id)
53}
54
55fn get_documents(
56 factory: DocumentFactory,
57 data_contract: &DataContract,
58 owner_id: Identifier,
59) -> Result<Vec<Document>, ProtocolError> {
60 let documents = vec![
61 factory.create_document(
62 data_contract,
63 owner_id,
64 "niceDocument".to_string(),
65 platform_value!({ "name": "Cutie" }),
66 )?,
67 factory.create_document(
68 data_contract,
69 owner_id,
70 "prettyDocument".to_string(),
71 platform_value!({ "lastName": "Shiny" }),
72 )?,
73 factory.create_document(
74 data_contract,
75 owner_id,
76 "prettyDocument".to_string(),
77 platform_value!({ "lastName": "Sweety" }),
78 )?,
79 factory.create_document(
80 data_contract,
81 owner_id,
82 "indexedDocument".to_string(),
83 platform_value!( { "firstName": "William", "lastName": "Birkin" }),
84 )?,
85 factory.create_document(
86 data_contract,
87 owner_id,
88 "indexedDocument".to_string(),
89 platform_value!( { "firstName": "Leon", "lastName": "Kennedy" }),
90 )?,
91 factory.create_document(
92 data_contract,
93 owner_id,
94 "noTimeDocument".to_string(),
95 platform_value!({ "name": "ImOutOfTime" }),
96 )?,
97 factory.create_document(
98 data_contract,
99 owner_id,
100 "uniqueDates".to_string(),
101 platform_value!({ "firstName": "John" }),
102 )?,
103 factory.create_document(
104 data_contract,
105 owner_id,
106 "indexedDocument".to_string(),
107 platform_value!( { "firstName": "Bill", "lastName": "Gates" }),
108 )?,
109 factory.create_document(data_contract, owner_id, "withByteArrays".to_string(), platform_value!( { "byteArrayField": get_random_10_bytes(), "identifierField": gen_owner_id().to_buffer() }))?,
110 factory.create_document(
111 data_contract,
112 owner_id,
113 "optionalUniqueIndexedDocument".to_string(),
114 platform_value!({ "firstName": "Jacques-Yves", "lastName": "Cousteau" })
115 )?,
116 ];
117
118 Ok(documents)
119}
120
121#[cfg(feature = "extended-document")]
122fn get_extended_documents(
123 factory: DocumentFactory,
124 data_contract: &DataContract,
125 owner_id: Identifier,
126) -> Result<Vec<ExtendedDocument>, ProtocolError> {
127 let documents = vec![
128 factory.create_extended_document(
129 data_contract,
130 owner_id,
131 "niceDocument".to_string(),
132 platform_value!({ "name": "Cutie" }),
133 )?,
134 factory.create_extended_document(
135 data_contract,
136 owner_id,
137 "prettyDocument".to_string(),
138 platform_value!({ "lastName": "Shiny" }),
139 )?,
140 factory.create_extended_document(
141 data_contract,
142 owner_id,
143 "prettyDocument".to_string(),
144 platform_value!({ "lastName": "Sweety" }),
145 )?,
146 factory.create_extended_document(
147 data_contract,
148 owner_id,
149 "indexedDocument".to_string(),
150 platform_value!( { "firstName": "William", "lastName": "Birkin" }),
151 )?,
152 factory.create_extended_document(
153 data_contract,
154 owner_id,
155 "indexedDocument".to_string(),
156 platform_value!( { "firstName": "Leon", "lastName": "Kennedy" }),
157 )?,
158 factory.create_extended_document(
159 data_contract,
160 owner_id,
161 "noTimeDocument".to_string(),
162 platform_value!({ "name": "ImOutOfTime" }),
163 )?,
164 factory.create_extended_document(
165 data_contract,
166 owner_id,
167 "uniqueDates".to_string(),
168 platform_value!({ "firstName": "John" }),
169 )?,
170 factory.create_extended_document(
171 data_contract,
172 owner_id,
173 "indexedDocument".to_string(),
174 platform_value!( { "firstName": "Bill", "lastName": "Gates" }),
175 )?,
176 factory.create_extended_document(data_contract, owner_id, "withByteArrays".to_string(), platform_value!( { "byteArrayField": get_random_10_bytes(), "identifierField": gen_owner_id().to_buffer() }))?,
177 factory.create_extended_document(
178 data_contract,
179 owner_id,
180 "optionalUniqueIndexedDocument".to_string(),
181 platform_value!({ "firstName": "Jacques-Yves", "lastName": "Cousteau" })
182 )?,
183 ];
184
185 Ok(documents)
186}
187
188pub fn get_withdrawal_document_fixture(
189 data_contract: &DataContract,
190 owner_id: Identifier,
191 data: Value,
192 seed: Option<u64>,
193 protocol_version: u32,
194) -> Result<Document, ProtocolError> {
195 let mut rng = match seed {
196 None => StdRng::from_entropy(),
197 Some(seed_value) => StdRng::seed_from_u64(seed_value),
198 };
199
200 let document_type = data_contract.document_type_for_name(
201 data_contracts::withdrawals_contract::v1::document_types::withdrawal::NAME,
202 )?;
203
204 let properties = data
205 .into_btree_string_map()
206 .map_err(ProtocolError::ValueError)?;
207
208 let id = Identifier::random_with_rng(&mut rng);
209
210 let platform_version = PlatformVersion::get(protocol_version)?;
211 document_type.create_document_with_prevalidated_properties(
212 id,
213 owner_id,
214 0,
215 0,
216 properties,
217 platform_version,
218 )
219}
220
221fn get_random_10_bytes() -> Vec<u8> {
222 let mut buffer = [0u8; 10];
223 let _ = getrandom::getrandom(&mut buffer);
224 buffer.to_vec()
225}