1
- use fake:: { Faker , Fake } ;
1
+ use fake:: { Fake , Faker } ;
2
2
use rusqlite:: NO_PARAMS ;
3
3
use stacks_common:: util:: hash:: Sha512Trunc256Sum ;
4
4
5
- use crate :: vm:: { contracts:: Contract , database:: { clarity_store:: ContractCommitment , ClarityBackingStore , ClarityDatabase , ClaritySerializable , MemoryBackingStore , NULL_BURN_STATE_DB , NULL_HEADER_DB } , fakes:: raw:: EnglishWord , Value } ;
5
+ use crate :: vm:: contracts:: Contract ;
6
+ use crate :: vm:: database:: clarity_store:: ContractCommitment ;
7
+ use crate :: vm:: database:: {
8
+ ClarityBackingStore , ClarityDatabase , ClaritySerializable , MemoryBackingStore ,
9
+ NULL_BURN_STATE_DB , NULL_HEADER_DB ,
10
+ } ;
11
+ use crate :: vm:: fakes:: raw:: EnglishWord ;
12
+ use crate :: vm:: Value ;
6
13
7
14
#[ test]
8
15
fn insert_contract ( ) {
@@ -18,12 +25,12 @@ fn insert_contract() {
18
25
db. insert_contract ( & contract_id, contract)
19
26
. expect ( "failed to insert contract into backing store" ) ;
20
27
21
- let exists = sql_exists (
28
+ let exists = sql_exists (
22
29
& mut store,
23
30
& format ! (
24
- "SELECT * FROM metadata_table WHERE key LIKE '%{}%'" ,
31
+ "SELECT * FROM metadata_table WHERE key LIKE '%{}%'" ,
25
32
contract_id. to_string( )
26
- )
33
+ ) ,
27
34
) ;
28
35
assert ! ( !exists) ;
29
36
}
@@ -43,7 +50,8 @@ fn get_contract() {
43
50
db. insert_contract ( & contract_id, contract. clone ( ) )
44
51
. expect ( "failed to insert contract into backing store" ) ;
45
52
46
- let retrieved_contract = db. get_contract ( & contract_id)
53
+ let retrieved_contract = db
54
+ . get_contract ( & contract_id)
47
55
. expect ( "failed to retrieve contract from backing store" ) ;
48
56
49
57
assert_eq ! ( contract, retrieved_contract) ;
@@ -81,10 +89,14 @@ fn insert_contract_with_commit_should_exist_in_backing_store() {
81
89
db. commit ( ) . expect ( "failed to commit to backing store" ) ;
82
90
83
91
let count = sql_query_u32 (
84
- & mut store,
85
- & format ! ( "SELECT COUNT(*) FROM metadata_table WHERE key LIKE '{}'" ,
86
- format!( "clr-meta::{}::vm-metadata::9::contract" , contract_id. to_string( ) )
87
- )
92
+ & mut store,
93
+ & format ! (
94
+ "SELECT COUNT(*) FROM metadata_table WHERE key LIKE '{}'" ,
95
+ format!(
96
+ "clr-meta::{}::vm-metadata::9::contract" ,
97
+ contract_id. to_string( )
98
+ )
99
+ ) ,
88
100
) ;
89
101
90
102
assert_eq ! ( 1 , count) ;
@@ -99,10 +111,14 @@ fn put_data_no_commit() {
99
111
100
112
db. begin ( ) ;
101
113
102
- db. put ( "hello" , & ContractCommitment {
103
- block_height : 1 ,
104
- hash : Sha512Trunc256Sum :: from_data ( & [ 1 , 2 , 3 , 4 ] )
105
- } ) . expect ( "failed to put data" ) ;
114
+ db. put (
115
+ "hello" ,
116
+ & ContractCommitment {
117
+ block_height : 1 ,
118
+ hash : Sha512Trunc256Sum :: from_data ( & [ 1 , 2 , 3 , 4 ] ) ,
119
+ } ,
120
+ )
121
+ . expect ( "failed to put data" ) ;
106
122
107
123
let count = sql_query_u32 ( & mut store, "SELECT COUNT(*) FROM data_table" ) ;
108
124
assert_eq ! ( 0 , count) ;
@@ -118,16 +134,20 @@ fn put_data_with_commit_should_exist_in_backing_store() {
118
134
db. begin ( ) ;
119
135
120
136
let key = Faker . fake :: < String > ( ) ;
121
- db. put ( & key, & ContractCommitment {
122
- block_height : Faker . fake ( ) ,
123
- hash : Sha512Trunc256Sum :: from_data ( & Faker . fake :: < Vec < u8 > > ( ) )
124
- } ) . expect ( "failed to put data" ) ;
137
+ db. put (
138
+ & key,
139
+ & ContractCommitment {
140
+ block_height : Faker . fake ( ) ,
141
+ hash : Sha512Trunc256Sum :: from_data ( & Faker . fake :: < Vec < u8 > > ( ) ) ,
142
+ } ,
143
+ )
144
+ . expect ( "failed to put data" ) ;
125
145
126
146
db. commit ( ) . expect ( "failed to commit to backing store" ) ;
127
147
128
148
let count = sql_query_u32 (
129
- & mut store,
130
- & format ! ( "SELECT COUNT(*) FROM data_table where key = '{}'" , key)
149
+ & mut store,
150
+ & format ! ( "SELECT COUNT(*) FROM data_table where key = '{}'" , key) ,
131
151
) ;
132
152
assert_eq ! ( 1 , count) ;
133
153
}
@@ -140,38 +160,38 @@ fn put_data_without_begin_fails() {
140
160
let mut db = ClarityDatabase :: new ( & mut store, & NULL_HEADER_DB , & NULL_BURN_STATE_DB ) ;
141
161
142
162
let key = Faker . fake :: < String > ( ) ;
143
- db. put ( & key, & ContractCommitment {
144
- block_height : Faker . fake ( ) ,
145
- hash : Sha512Trunc256Sum :: from_data ( & Faker . fake :: < Vec < u8 > > ( ) )
146
- } ) . expect_err ( "expected not-nested error" ) ;
163
+ db. put (
164
+ & key,
165
+ & ContractCommitment {
166
+ block_height : Faker . fake ( ) ,
167
+ hash : Sha512Trunc256Sum :: from_data ( & Faker . fake :: < Vec < u8 > > ( ) ) ,
168
+ } ,
169
+ )
170
+ . expect_err ( "expected not-nested error" ) ;
147
171
}
148
172
}
149
173
150
174
/// Executes the provided SQL query, which is expected to return a positive
151
175
/// integer, and returns it as a u32. Panics upon SQL failure.
152
176
fn sql_query_u32 < S : ClarityBackingStore > ( store : & mut S , sql : & str ) -> u32 {
153
177
let sqlite = store. get_side_store ( ) ;
154
- sqlite. query_row (
155
- sql,
156
- NO_PARAMS ,
157
- |row| {
178
+ sqlite
179
+ . query_row ( sql, NO_PARAMS , |row| {
158
180
let i: u32 = row. get ( 0 ) ?;
159
181
Ok ( i)
160
- }
161
- ) . expect ( "failed to verify results in sqlite" )
182
+ } )
183
+ . expect ( "failed to verify results in sqlite" )
162
184
}
163
185
164
- /// Executes the provided SQL query as a subquery within a `SELECT EXISTS(...)`
186
+ /// Executes the provided SQL query as a subquery within a `SELECT EXISTS(...)`
165
187
/// statement. Returns true if the statement returns any rows, false otherwise.
166
188
/// Panics upon SQL failure.
167
189
fn sql_exists < S : ClarityBackingStore > ( store : & mut S , sql : & str ) -> bool {
168
190
let sqlite = store. get_side_store ( ) ;
169
- sqlite. query_row (
170
- & format ! ( "SELECT EXISTS({});" , sql) ,
171
- NO_PARAMS ,
172
- |row| {
191
+ sqlite
192
+ . query_row ( & format ! ( "SELECT EXISTS({});" , sql) , NO_PARAMS , |row| {
173
193
let exists: bool = row. get ( 0 ) ?;
174
194
Ok ( exists)
175
- }
176
- ) . expect ( "failed to verify results in sqlite" )
177
- }
195
+ } )
196
+ . expect ( "failed to verify results in sqlite" )
197
+ }
0 commit comments