Skip to content

Commit 84ac025

Browse files
author
Chris Elder
committed
FAB-2775 Add query unit tests to CouchDB layer
Add query tests to the CouchDB unit tests. Change-Id: Ia98cecf55c1991b7e21529bd5e38f4624c340669 Signed-off-by: Chris Elder <[email protected]>
1 parent f086f8b commit 84ac025

File tree

1 file changed

+301
-0
lines changed

1 file changed

+301
-0
lines changed

core/ledger/util/couchdb/couchdb_test.go

+301
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ func TestDBCreateSaveWithoutRevision(t *testing.T) {
112112
}
113113
}
114114

115+
func TestDBCreateEnsureFullCommit(t *testing.T) {
116+
117+
if ledgerconfig.IsCouchDBEnabled() == true {
118+
119+
database := "testdbensurefullcommit"
120+
err := cleanup(database)
121+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to cleanup Error: %s", err))
122+
defer cleanup(database)
123+
124+
if err == nil {
125+
//create a new instance and database object
126+
couchInstance, err := CreateCouchInstance(connectURL, username, password)
127+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
128+
db := CouchDatabase{couchInstance: *couchInstance, dbName: database}
129+
130+
//create a new database
131+
_, errdb := db.CreateDatabaseIfNotExist()
132+
testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to create database"))
133+
134+
//Save the test document
135+
_, saveerr := db.SaveDoc("2", "", &CouchDoc{JSONValue: assetJSON, Attachments: nil})
136+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
137+
138+
//Ensure a full commit
139+
_, commiterr := db.EnsureFullCommit()
140+
testutil.AssertNoError(t, commiterr, fmt.Sprintf("Error when trying to ensure a full commit"))
141+
142+
}
143+
}
144+
}
115145
func TestDBBadDatabaseName(t *testing.T) {
116146

117147
if ledgerconfig.IsCouchDBEnabled() == true {
@@ -471,3 +501,274 @@ func TestCouchDBVersion(t *testing.T) {
471501
testutil.AssertError(t, err, fmt.Sprintf("Error should have been thrown for invalid version"))
472502

473503
}
504+
505+
func TestRichQuery(t *testing.T) {
506+
507+
if ledgerconfig.IsCouchDBEnabled() == true {
508+
509+
byteJSON01 := []byte(`{"asset_name":"marble01","color":"blue","size":1,"owner":"jerry"}`)
510+
byteJSON02 := []byte(`{"asset_name":"marble02","color":"red","size":2,"owner":"tom"}`)
511+
byteJSON03 := []byte(`{"asset_name":"marble03","color":"green","size":3,"owner":"jerry"}`)
512+
byteJSON04 := []byte(`{"asset_name":"marble04","color":"purple","size":4,"owner":"tom"}`)
513+
byteJSON05 := []byte(`{"asset_name":"marble05","color":"blue","size":5,"owner":"jerry"}`)
514+
byteJSON06 := []byte(`{"asset_name":"marble06","color":"white","size":6,"owner":"tom"}`)
515+
byteJSON07 := []byte(`{"asset_name":"marble07","color":"white","size":7,"owner":"tom"}`)
516+
byteJSON08 := []byte(`{"asset_name":"marble08","color":"white","size":8,"owner":"tom"}`)
517+
byteJSON09 := []byte(`{"asset_name":"marble09","color":"white","size":9,"owner":"tom"}`)
518+
byteJSON10 := []byte(`{"asset_name":"marble10","color":"white","size":10,"owner":"tom"}`)
519+
byteJSON11 := []byte(`{"asset_name":"marble11","color":"green","size":11,"owner":"tom"}`)
520+
byteJSON12 := []byte(`{"asset_name":"marble12","color":"green","size":12,"owner":"frank"}`)
521+
522+
attachment1 := &Attachment{}
523+
attachment1.AttachmentBytes = []byte(`marble01 - test attachment`)
524+
attachment1.ContentType = "application/octet-stream"
525+
attachment1.Name = "data"
526+
attachments1 := []Attachment{}
527+
attachments1 = append(attachments1, *attachment1)
528+
529+
attachment2 := &Attachment{}
530+
attachment2.AttachmentBytes = []byte(`marble02 - test attachment`)
531+
attachment2.ContentType = "application/octet-stream"
532+
attachment2.Name = "data"
533+
attachments2 := []Attachment{}
534+
attachments2 = append(attachments2, *attachment2)
535+
536+
attachment3 := &Attachment{}
537+
attachment3.AttachmentBytes = []byte(`marble03 - test attachment`)
538+
attachment3.ContentType = "application/octet-stream"
539+
attachment3.Name = "data"
540+
attachments3 := []Attachment{}
541+
attachments3 = append(attachments3, *attachment3)
542+
543+
attachment4 := &Attachment{}
544+
attachment4.AttachmentBytes = []byte(`marble04 - test attachment`)
545+
attachment4.ContentType = "application/octet-stream"
546+
attachment4.Name = "data"
547+
attachments4 := []Attachment{}
548+
attachments4 = append(attachments4, *attachment4)
549+
550+
attachment5 := &Attachment{}
551+
attachment5.AttachmentBytes = []byte(`marble05 - test attachment`)
552+
attachment5.ContentType = "application/octet-stream"
553+
attachment5.Name = "data"
554+
attachments5 := []Attachment{}
555+
attachments5 = append(attachments5, *attachment5)
556+
557+
attachment6 := &Attachment{}
558+
attachment6.AttachmentBytes = []byte(`marble06 - test attachment`)
559+
attachment6.ContentType = "application/octet-stream"
560+
attachment6.Name = "data"
561+
attachments6 := []Attachment{}
562+
attachments6 = append(attachments6, *attachment6)
563+
564+
attachment7 := &Attachment{}
565+
attachment7.AttachmentBytes = []byte(`marble07 - test attachment`)
566+
attachment7.ContentType = "application/octet-stream"
567+
attachment7.Name = "data"
568+
attachments7 := []Attachment{}
569+
attachments7 = append(attachments7, *attachment7)
570+
571+
attachment8 := &Attachment{}
572+
attachment8.AttachmentBytes = []byte(`marble08 - test attachment`)
573+
attachment8.ContentType = "application/octet-stream"
574+
attachment7.Name = "data"
575+
attachments8 := []Attachment{}
576+
attachments8 = append(attachments8, *attachment8)
577+
578+
attachment9 := &Attachment{}
579+
attachment9.AttachmentBytes = []byte(`marble09 - test attachment`)
580+
attachment9.ContentType = "application/octet-stream"
581+
attachment9.Name = "data"
582+
attachments9 := []Attachment{}
583+
attachments9 = append(attachments9, *attachment9)
584+
585+
attachment10 := &Attachment{}
586+
attachment10.AttachmentBytes = []byte(`marble10 - test attachment`)
587+
attachment10.ContentType = "application/octet-stream"
588+
attachment10.Name = "data"
589+
attachments10 := []Attachment{}
590+
attachments10 = append(attachments10, *attachment10)
591+
592+
attachment11 := &Attachment{}
593+
attachment11.AttachmentBytes = []byte(`marble11 - test attachment`)
594+
attachment11.ContentType = "application/octet-stream"
595+
attachment11.Name = "data"
596+
attachments11 := []Attachment{}
597+
attachments11 = append(attachments11, *attachment11)
598+
599+
attachment12 := &Attachment{}
600+
attachment12.AttachmentBytes = []byte(`marble12 - test attachment`)
601+
attachment12.ContentType = "application/octet-stream"
602+
attachment12.Name = "data"
603+
attachments12 := []Attachment{}
604+
attachments12 = append(attachments12, *attachment12)
605+
606+
database := "testrichquery"
607+
err := cleanup(database)
608+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to cleanup Error: %s", err))
609+
defer cleanup(database)
610+
611+
if err == nil {
612+
//create a new instance and database object --------------------------------------------------------
613+
couchInstance, err := CreateCouchInstance(connectURL, username, password)
614+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
615+
db := CouchDatabase{couchInstance: *couchInstance, dbName: database}
616+
617+
//create a new database
618+
_, errdb := db.CreateDatabaseIfNotExist()
619+
testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to create database"))
620+
621+
//Save the test document
622+
_, saveerr := db.SaveDoc("marble01", "", &CouchDoc{JSONValue: byteJSON01, Attachments: attachments1})
623+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
624+
625+
//Save the test document
626+
_, saveerr = db.SaveDoc("marble02", "", &CouchDoc{JSONValue: byteJSON02, Attachments: attachments2})
627+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
628+
629+
//Save the test document
630+
_, saveerr = db.SaveDoc("marble03", "", &CouchDoc{JSONValue: byteJSON03, Attachments: attachments3})
631+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
632+
633+
//Save the test document
634+
_, saveerr = db.SaveDoc("marble04", "", &CouchDoc{JSONValue: byteJSON04, Attachments: attachments4})
635+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
636+
637+
//Save the test document
638+
_, saveerr = db.SaveDoc("marble05", "", &CouchDoc{JSONValue: byteJSON05, Attachments: attachments5})
639+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
640+
641+
//Save the test document
642+
_, saveerr = db.SaveDoc("marble06", "", &CouchDoc{JSONValue: byteJSON06, Attachments: attachments6})
643+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
644+
645+
//Save the test document
646+
_, saveerr = db.SaveDoc("marble07", "", &CouchDoc{JSONValue: byteJSON07, Attachments: attachments7})
647+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
648+
649+
//Save the test document
650+
_, saveerr = db.SaveDoc("marble08", "", &CouchDoc{JSONValue: byteJSON08, Attachments: attachments8})
651+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
652+
653+
//Save the test document
654+
_, saveerr = db.SaveDoc("marble09", "", &CouchDoc{JSONValue: byteJSON09, Attachments: attachments9})
655+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
656+
657+
//Save the test document
658+
_, saveerr = db.SaveDoc("marble10", "", &CouchDoc{JSONValue: byteJSON10, Attachments: attachments10})
659+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
660+
661+
//Save the test document
662+
_, saveerr = db.SaveDoc("marble11", "", &CouchDoc{JSONValue: byteJSON11, Attachments: attachments11})
663+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
664+
665+
//Save the test document
666+
_, saveerr = db.SaveDoc("marble12", "", &CouchDoc{JSONValue: byteJSON12, Attachments: attachments12})
667+
testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))
668+
669+
//Test query with invalid JSON -------------------------------------------------------------------
670+
queryString := "{\"selector\":{\"owner\":}}"
671+
672+
_, err = db.QueryDocuments(queryString)
673+
testutil.AssertError(t, err, fmt.Sprintf("Error should have been thrown for bad json"))
674+
675+
//Test query with object -------------------------------------------------------------------
676+
queryString = "{\"selector\":{\"owner\":{\"$eq\":\"jerry\"}}}"
677+
678+
queryResult, err := db.QueryDocuments(queryString)
679+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
680+
681+
//There should be 3 results for owner="jerry"
682+
testutil.AssertEquals(t, len(*queryResult), 3)
683+
684+
//Test query with implicit operator --------------------------------------------------------------
685+
queryString = "{\"selector\":{\"owner\":\"jerry\"}}"
686+
687+
queryResult, err = db.QueryDocuments(queryString)
688+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
689+
690+
//There should be 3 results for owner="jerry"
691+
testutil.AssertEquals(t, len(*queryResult), 3)
692+
693+
//Test query with specified fields -------------------------------------------------------------------
694+
queryString = "{\"selector\":{\"owner\":{\"$eq\":\"jerry\"}},\"fields\": [\"owner\",\"asset_name\",\"color\",\"size\"]}"
695+
696+
queryResult, err = db.QueryDocuments(queryString)
697+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
698+
699+
//There should be 3 results for owner="jerry"
700+
testutil.AssertEquals(t, len(*queryResult), 3)
701+
702+
//Test query with a leading operator -------------------------------------------------------------------
703+
queryString = "{\"selector\":{\"$or\":[{\"owner\":{\"$eq\":\"jerry\"}},{\"owner\": {\"$eq\": \"frank\"}}]}}"
704+
705+
queryResult, err = db.QueryDocuments(queryString)
706+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
707+
708+
//There should be 4 results for owner="jerry" or owner="frank"
709+
testutil.AssertEquals(t, len(*queryResult), 4)
710+
711+
//Test query implicit and explicit operator ------------------------------------------------------------------
712+
queryString = "{\"selector\":{\"color\":\"green\",\"$or\":[{\"owner\":\"tom\"},{\"owner\":\"frank\"}]}}"
713+
714+
queryResult, err = db.QueryDocuments(queryString)
715+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
716+
717+
//There should be 2 results for color="green" and (owner="jerry" or owner="frank")
718+
testutil.AssertEquals(t, len(*queryResult), 2)
719+
720+
//Test query with a leading operator -------------------------------------------------------------------------
721+
queryString = "{\"selector\":{\"$and\":[{\"size\":{\"$gte\":2}},{\"size\":{\"$lte\":5}}]}}"
722+
723+
queryResult, err = db.QueryDocuments(queryString)
724+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
725+
726+
//There should be 4 results for size >= 2 and size <= 5
727+
testutil.AssertEquals(t, len(*queryResult), 4)
728+
729+
//Test query with leading and embedded operator -------------------------------------------------------------
730+
queryString = "{\"selector\":{\"$and\":[{\"size\":{\"$gte\":3}},{\"size\":{\"$lte\":10}},{\"$not\":{\"size\":7}}]}}"
731+
732+
queryResult, err = db.QueryDocuments(queryString)
733+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
734+
735+
//There should be 7 results for size >= 3 and size <= 10 and not 7
736+
testutil.AssertEquals(t, len(*queryResult), 7)
737+
738+
//Test query with leading operator and array of objects ----------------------------------------------------------
739+
queryString = "{\"selector\":{\"$and\":[{\"size\":{\"$gte\":2}},{\"size\":{\"$lte\":10}},{\"$nor\":[{\"size\":3},{\"size\":5},{\"size\":7}]}]}}"
740+
741+
queryResult, err = db.QueryDocuments(queryString)
742+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
743+
744+
//There should be 6 results for size >= 2 and size <= 10 and not 3,5 or 7
745+
testutil.AssertEquals(t, len(*queryResult), 6)
746+
747+
//Test a range query ---------------------------------------------------------------------------------------------
748+
queryResult, err = db.ReadDocRange("marble02", "marble06", 10000, 0)
749+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a range query"))
750+
751+
//There should be 4 results
752+
testutil.AssertEquals(t, len(*queryResult), 4)
753+
754+
//Test query with for tom -------------------------------------------------------------------
755+
queryString = "{\"selector\":{\"owner\":{\"$eq\":\"tom\"}}}"
756+
757+
queryResult, err = db.QueryDocuments(queryString)
758+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
759+
760+
//There should be 8 results for owner="tom"
761+
testutil.AssertEquals(t, len(*queryResult), 8)
762+
763+
//Test query with for tom with limit -------------------------------------------------------------------
764+
queryString = "{\"selector\":{\"owner\":{\"$eq\":\"tom\"}},\"limit\":2}"
765+
766+
queryResult, err = db.QueryDocuments(queryString)
767+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when attempting to execute a query"))
768+
769+
//There should be 2 results for owner="tom" with a limit of 2
770+
testutil.AssertEquals(t, len(*queryResult), 2)
771+
772+
}
773+
}
774+
}

0 commit comments

Comments
 (0)