Skip to content

Commit

Permalink
Make tests more robust against changing revs
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Mar 16, 2024
1 parent 8f37f8e commit 37cf52f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 82 deletions.
9 changes: 5 additions & 4 deletions x/sqlite/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package sqlite
import (
"context"
"net/http"
"regexp"
"testing"

"gitlab.com/flimzy/testy"
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestDBDelete(t *testing.T) {
db: dbc,
id: "foo",
options: kivik.Rev(rev),
wantRev: "2-df2a4fe30cde39c357c8d1105748d1b9",
wantRev: "2-.*",
check: func(t *testing.T, d driver.DB) {
var deleted bool
err := d.(*db).db.QueryRow(`
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestDBDelete(t *testing.T) {
db: db,
id: "foo",
options: kivik.Rev(rev2),
wantRev: "3-df2a4fe30cde39c357c8d1105748d1b9",
wantRev: "3-.*",
}
})
tests.Add("delete without rev", func(t *testing.T) interface{} {
Expand Down Expand Up @@ -147,7 +148,7 @@ func TestDBDelete(t *testing.T) {
db: db,
id: "foo",
options: kivik.Rev("1-aaa"),
wantRev: "2-df2a4fe30cde39c357c8d1105748d1b9",
wantRev: "2-.*",
}
})
tests.Add("invalid rev format", test{
Expand Down Expand Up @@ -181,7 +182,7 @@ func TestDBDelete(t *testing.T) {
if err != nil {
return
}
if rev != tt.wantRev {
if !regexp.MustCompile(tt.wantRev).MatchString(rev) {
t.Errorf("Unexpected rev: %s", rev)
}
if tt.check != nil {
Expand Down
4 changes: 2 additions & 2 deletions x/sqlite/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestDBGet(t *testing.T) {
return test{
db: db,
id: "foo",
options: kivik.Rev("2-df2a4fe30cde39c357c8d1105748d1b9"),
options: kivik.Rev(rev),
wantDoc: map[string]interface{}{
"_id": "foo",
"_rev": rev,
Expand All @@ -202,7 +202,7 @@ func TestDBGet(t *testing.T) {
return test{
db: db,
id: "foo",
options: kivik.Rev("1-6872a0fc474ada5c46ce054b92897063"),
options: kivik.Rev(rev),
wantDoc: map[string]interface{}{
"_id": "foo",
"_rev": rev,
Expand Down
102 changes: 53 additions & 49 deletions x/sqlite/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package sqlite
import (
"context"
"net/http"
"regexp"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -274,12 +275,11 @@ func TestDBPut(t *testing.T) {
"_deleted": true,
"foo": "bar",
},
wantRev: "1-6872a0fc474ada5c46ce054b92897063",
wantRev: "1-.*",
wantRevs: []leaf{
{
ID: "foo",
Rev: 1,
RevID: "6872a0fc474ada5c46ce054b92897063",
ID: "foo",
Rev: 1,
},
},
check: func(t *testing.T, d driver.DB) {
Expand Down Expand Up @@ -775,19 +775,17 @@ func TestDBPut(t *testing.T) {
},
"foo": "bar",
},
wantRev: "1-4b98474b255b67856668474854b0d5f8",
wantRev: "1-.*",
wantRevs: []leaf{
{
ID: "foo",
Rev: 1,
RevID: "4b98474b255b67856668474854b0d5f8",
ID: "foo",
Rev: 1,
},
},
wantAttachments: []attachmentRow{
{
DocID: "foo",
Rev: 1,
RevID: "4b98474b255b67856668474854b0d5f8",
Filename: "foo.txt",
ContentType: "text/plain",
Length: 25,
Expand All @@ -806,19 +804,17 @@ func TestDBPut(t *testing.T) {
},
"foo": "bar",
},
wantRev: "1-1a46dc947908f36db2ac78b7edaecda3",
wantRev: "1-.*",
wantRevs: []leaf{
{
ID: "foo",
Rev: 1,
RevID: "1a46dc947908f36db2ac78b7edaecda3",
ID: "foo",
Rev: 1,
},
},
wantAttachments: []attachmentRow{
{
DocID: "foo",
Rev: 1,
RevID: "1a46dc947908f36db2ac78b7edaecda3",
Filename: "foo.txt",
ContentType: "application/octet-stream",
Length: 25,
Expand All @@ -829,7 +825,7 @@ func TestDBPut(t *testing.T) {
})
tests.Add("update doc with attachments without deleting them", func(t *testing.T) interface{} {
db := newDB(t)
_, err := db.Put(context.Background(), "foo", map[string]interface{}{
rev, err := db.Put(context.Background(), "foo", map[string]interface{}{
"foo": "bar",
"_attachments": map[string]interface{}{
"foo.txt": map[string]interface{}{
Expand All @@ -846,34 +842,30 @@ func TestDBPut(t *testing.T) {
db: db,
docID: "foo",
doc: map[string]interface{}{
"_rev": "1-4b98474b255b67856668474854b0d5f8",
"_rev": rev,
"foo": "baz",
"_attachments": map[string]interface{}{
"foo.txt": map[string]interface{}{
"stub": true,
},
},
},
wantRev: "2-a7cadffe4f950734f8eeae832e15f6c2",
wantRev: "2-.*",
wantRevs: []leaf{
{
ID: "foo",
Rev: 1,
RevID: "4b98474b255b67856668474854b0d5f8",
ID: "foo",
Rev: 1,
},
{
ID: "foo",
Rev: 2,
RevID: "a7cadffe4f950734f8eeae832e15f6c2",
ParentRev: &[]int{1}[0],
ParentRevID: &[]string{"4b98474b255b67856668474854b0d5f8"}[0],
ID: "foo",
Rev: 2,
ParentRev: &[]int{1}[0],
},
},
wantAttachments: []attachmentRow{
{
DocID: "foo",
Rev: 1,
RevID: "4b98474b255b67856668474854b0d5f8",
Filename: "foo.txt",
ContentType: "text/plain",
Length: 25,
Expand All @@ -885,7 +877,7 @@ func TestDBPut(t *testing.T) {
})
tests.Add("update doc with attachments, delete one", func(t *testing.T) interface{} {
db := newDB(t)
_, err := db.Put(context.Background(), "foo", map[string]interface{}{
rev, err := db.Put(context.Background(), "foo", map[string]interface{}{
"foo": "bar",
"_attachments": map[string]interface{}{
"foo.txt": map[string]interface{}{
Expand All @@ -906,46 +898,40 @@ func TestDBPut(t *testing.T) {
db: db,
docID: "foo",
doc: map[string]interface{}{
"_rev": "1-7884bb688778892bd22837c5d8cba96b",
"_rev": rev,
"foo": "baz",
"_attachments": map[string]interface{}{
"foo.txt": map[string]interface{}{
"stub": true,
},
},
},
wantRev: "2-a7cadffe4f950734f8eeae832e15f6c2",
wantRev: "2-.*",
wantRevs: []leaf{
{
ID: "foo",
Rev: 1,
RevID: "7884bb688778892bd22837c5d8cba96b",
ID: "foo",
Rev: 1,
},
{
ID: "foo",
Rev: 2,
RevID: "a7cadffe4f950734f8eeae832e15f6c2",
ParentRev: &[]int{1}[0],
ParentRevID: &[]string{"7884bb688778892bd22837c5d8cba96b"}[0],
ID: "foo",
Rev: 2,
ParentRev: &[]int{1}[0],
},
},
wantAttachments: []attachmentRow{
{
DocID: "foo",
Rev: 1,
RevID: "7884bb688778892bd22837c5d8cba96b",
Filename: "bar.txt",
ContentType: "text/plain",
Length: 25,
Digest: "md5-TmfHxaRgUrE9l3tkAn4s0Q==",
Data: "This is a base64 encoding",
DeletedRev: &[]int{2}[0],
DeletedRevID: &[]string{"a7cadffe4f950734f8eeae832e15f6c2"}[0],
DocID: "foo",
Rev: 1,
Filename: "bar.txt",
ContentType: "text/plain",
Length: 25,
Digest: "md5-TmfHxaRgUrE9l3tkAn4s0Q==",
Data: "This is a base64 encoding",
DeletedRev: &[]int{2}[0],
},
{
DocID: "foo",
Rev: 1,
RevID: "7884bb688778892bd22837c5d8cba96b",
Filename: "foo.txt",
ContentType: "text/plain",
Length: 25,
Expand Down Expand Up @@ -992,13 +978,22 @@ func TestDBPut(t *testing.T) {
if err != nil {
return
}
if rev != tt.wantRev {
if !regexp.MustCompile(tt.wantRev).MatchString(rev) {
t.Errorf("Unexpected rev: %s, want %s", rev, tt.wantRev)
}
if len(tt.wantRevs) == 0 {
t.Errorf("No leaves to check")
}
leaves := readRevisions(t, dbc.(*db).db, tt.docID)
for i, r := range tt.wantRevs {
// allow tests to omit RevID
if r.RevID == "" {
leaves[i].RevID = ""
}
if r.ParentRevID == nil {
leaves[i].ParentRevID = nil
}
}
if d := cmp.Diff(tt.wantRevs, leaves); d != "" {
t.Errorf("Unexpected leaves: %s", d)
}
Expand Down Expand Up @@ -1028,6 +1023,15 @@ func checkAttachments(t *testing.T, d driver.DB, want []attachmentRow) {
if err := rows.Err(); err != nil {
t.Fatal(err)
}
for i, w := range want {
// allow tests to omit RevID
if w.RevID == "" {
got[i].RevID = ""
}
if w.DeletedRevID == nil {
got[i].DeletedRevID = nil
}
}
if d := cmp.Diff(want, got); d != "" {
t.Errorf("Unexpected attachments: %s", d)
}
Expand Down
Loading

0 comments on commit 37cf52f

Please sign in to comment.