@@ -17,10 +17,13 @@ limitations under the License.
17
17
package couchdbtxmgmt
18
18
19
19
import (
20
+ "encoding/json"
20
21
"fmt"
21
22
"os"
22
23
"testing"
23
24
25
+ "github.com/hyperledger/fabric/core/ledger"
26
+ "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
24
27
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
25
28
"github.com/hyperledger/fabric/core/ledger/testutil"
26
29
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
@@ -139,3 +142,93 @@ func TestSavepoint(t *testing.T) {
139
142
txMgr .Shutdown ()
140
143
}
141
144
}
145
+
146
+ func TestDatabaseQuery (t * testing.T ) {
147
+
148
+ //call a helper method to load the core.yaml
149
+ testutil .SetupCoreYAMLConfig ("./../../../../../peer" )
150
+
151
+ //Only run the tests if CouchDB is explitily enabled in the code,
152
+ //otherwise CouchDB may not be installed and all the tests would fail
153
+ //TODO replace this with external config property rather than config within the code
154
+ if ledgerconfig .IsCouchDBEnabled () == true {
155
+
156
+ env := newTestEnv (t )
157
+ //env.Cleanup() //cleanup at the beginning to ensure the database doesn't exist already
158
+ //defer env.Cleanup() //and cleanup at the end
159
+
160
+ txMgr := NewCouchDBTxMgr (env .conf ,
161
+ env .couchDBAddress , //couchDB Address
162
+ env .couchDatabaseName , //couchDB db name
163
+ env .couchUsername , //enter couchDB id
164
+ env .couchPassword ) //enter couchDB pw
165
+
166
+ type Asset struct {
167
+ ID string `json:"_id"`
168
+ Rev string `json:"_rev"`
169
+ AssetName string `json:"asset_name"`
170
+ Color string `json:"color"`
171
+ Size string `json:"size"`
172
+ Owner string `json:"owner"`
173
+ }
174
+
175
+ s1 , _ := txMgr .NewTxSimulator ()
176
+
177
+ s1 .SetState ("ns1" , "key1" , []byte ("value1" ))
178
+ s1 .SetState ("ns1" , "key2" , []byte ("value2" ))
179
+ s1 .SetState ("ns1" , "key3" , []byte ("value3" ))
180
+ s1 .SetState ("ns1" , "key4" , []byte ("value4" ))
181
+ s1 .SetState ("ns1" , "key5" , []byte ("value5" ))
182
+ s1 .SetState ("ns1" , "key6" , []byte ("value6" ))
183
+ s1 .SetState ("ns1" , "key7" , []byte ("value7" ))
184
+ s1 .SetState ("ns1" , "key8" , []byte ("value8" ))
185
+
186
+ s1 .SetState ("ns1" , "key9" , []byte (`{"asset_name":"marble1","color":"red","size":"25","owner":"jerry"}` ))
187
+ s1 .SetState ("ns1" , "key10" , []byte (`{"asset_name":"marble2","color":"blue","size":"10","owner":"bob"}` ))
188
+ s1 .SetState ("ns1" , "key11" , []byte (`{"asset_name":"marble3","color":"blue","size":"35","owner":"jerry"}` ))
189
+ s1 .SetState ("ns1" , "key12" , []byte (`{"asset_name":"marble4","color":"green","size":"15","owner":"bob"}` ))
190
+ s1 .SetState ("ns1" , "key13" , []byte (`{"asset_name":"marble5","color":"red","size":"35","owner":"jerry"}` ))
191
+ s1 .SetState ("ns1" , "key14" , []byte (`{"asset_name":"marble6","color":"blue","size":"25","owner":"bob"}` ))
192
+
193
+ s1 .Done ()
194
+
195
+ // validate and commit RWset
196
+ txRWSet := s1 .(* CouchDBTxSimulator ).getTxReadWriteSet ()
197
+ isValid , err := txMgr .validateTx (txRWSet )
198
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error in validateTx(): %s" , err ))
199
+ testutil .AssertSame (t , isValid , true )
200
+ txMgr .addWriteSetToBatch (txRWSet , version .NewHeight (1 , 1 ))
201
+ err = txMgr .Commit ()
202
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error while calling commit(): %s" , err ))
203
+
204
+ queryExecuter , _ := txMgr .NewQueryExecutor ()
205
+ queryString := "{\" selector\" :{\" owner\" : {\" $eq\" : \" bob\" }},\" limit\" : 10,\" skip\" : 0}"
206
+
207
+ itr , _ := queryExecuter .ExecuteQuery (queryString )
208
+
209
+ counter := 0
210
+ for {
211
+ queryRecord , _ := itr .Next ()
212
+ if queryRecord == nil {
213
+ break
214
+ }
215
+
216
+ //Unmarshal the document to Asset structure
217
+ assetResp := & Asset {}
218
+ json .Unmarshal (queryRecord .(* ledger.QueryRecord ).Record , & assetResp )
219
+
220
+ //Verify the owner retrieved matches
221
+ testutil .AssertEquals (t , assetResp .Owner , "bob" )
222
+
223
+ counter ++
224
+
225
+ }
226
+
227
+ //Ensure the query returns 3 documents
228
+ testutil .AssertEquals (t , counter , 3 )
229
+
230
+ txMgr .Shutdown ()
231
+
232
+ }
233
+
234
+ }
0 commit comments