Skip to content

Commit

Permalink
Merge pull request #940 from go-kivik/putRev
Browse files Browse the repository at this point in the history
Re-calculate revision for Put
  • Loading branch information
flimzy committed Apr 15, 2024
2 parents 31440b7 + a4072dd commit 3d1dd76
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
4 changes: 2 additions & 2 deletions x/sqlite/deleteattachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ func TestDBDeleteAttachment(t *testing.T) {
{
ID: "foo",
Rev: 2,
RevID: r2.id,
RevID: "def",
ParentRev: &[]int{1}[0],
ParentRevID: &r1.id,
},
{
ID: "foo",
Rev: 2,
RevID: "def",
RevID: r2.id,
ParentRev: &[]int{1}[0],
ParentRevID: &r1.id,
},
Expand Down
56 changes: 56 additions & 0 deletions x/sqlite/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,59 @@ func Test_mergeIntoDoc(t *testing.T) {
})
}
}

func mustParseMD5Sum(s string) md5sum {
m, err := parseMD5sum(s)
if err != nil {
panic(err)
}
return m
}

func Test_RevID(t *testing.T) {
tests := []struct {
name string
doc docData
want string
}{
{
name: "empty",
doc: docData{
ID: "foo",
Doc: []byte(`{}`),
},
want: "52a640a54c0880d3e7b04709b18719c5",
},
{
name: "empty, pre-set md5sum",
doc: docData{
ID: "foo",
MD5sum: mustParseMD5Sum("99914b932bd37a50b983c5e7c90ae93b"),
},
want: "52a640a54c0880d3e7b04709b18719c5",
},
{
name: "foo:bar",
doc: docData{
Doc: []byte(`{"foo":"bar"}`),
},
want: "66f46afbe3effef8424aa0e291d21560",
},
{
name: "foo:baz",
doc: docData{
Doc: []byte(`{"foo":"baz"}`),
},
want: "a7ab6687616e7893a660455467033fc9",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.doc.RevID()
if got != tt.want {
t.Errorf("unexpected revID = %v, want %v", got, tt.want)
}
})
}
}
5 changes: 1 addition & 4 deletions x/sqlite/openrevs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func TestDBOpenRevs(t *testing.T) {
revs: []string{rev3},
options: kivik.Param("revs", true),
want: []rowResult{
{ID: docID, Rev: rev3, Doc: `{"_id":"` + docID + `","_rev":"` + rev3 + `","foo":"qux","_revisions":{"start":3,"ids":["` + r2.id + `","` + r.id + `","` + r3.id + `"]}}`},
{ID: docID, Rev: rev3, Doc: `{"_id":"` + docID + `","_rev":"` + rev3 + `","foo":"qux","_revisions":{"start":3,"ids":["` + r3.id + `","` + r2.id + `","` + r.id + `"]}}`},
},
}
})
Expand Down Expand Up @@ -298,9 +298,6 @@ func TestDBOpenRevs(t *testing.T) {
})
/*
TODO:
- rev calculation is broken
- Include attachment info when relevant (https://docs.couchdb.org/en/stable/replication/protocol.html#:~:text=In%20case%20the%20Document%20contains%20attachments%2C%20Source%20MUST%20return%20information%20only%20for%20those%20ones%20that%20had%20been%20changed%20(added%20or%20updated)%20since%20the%20specified%20Revision%20values.%20If%20an%20attachment%20was%20deleted%2C%20the%20Document%20MUST%20NOT%20have%20stub%20information%20for%20it)
- revs=true + attachments
*/

Expand Down
2 changes: 1 addition & 1 deletion x/sqlite/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (d *db) Put(ctx context.Context, docID string, doc interface{}, options dri
}
}

data.MD5sum, err = d.isLeafRev(ctx, tx, docID, curRev.rev, curRev.id)
_, err = d.isLeafRev(ctx, tx, docID, curRev.rev, curRev.id)
switch {
case kivik.HTTPStatus(err) == http.StatusNotFound:
if docRev != "" {
Expand Down
22 changes: 19 additions & 3 deletions x/sqlite/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,15 +970,16 @@ func TestDBPut(t *testing.T) {
{
ID: "foo",
Rev: 3,
RevID: "abc",
ParentRev: &[]int{2}[0],
ParentRevID: &r2.id,
ParentRevID: &[]string{"def"}[0],
},
{
ID: "foo",
Rev: 3,
RevID: "abc",
RevID: "f99110ca1be121ebf5653ee1ec34610c",
ParentRev: &[]int{2}[0],
ParentRevID: &[]string{"def"}[0],
ParentRevID: &r2.id,
},
},
}
Expand Down Expand Up @@ -1185,3 +1186,18 @@ func checkAttachments(t *testing.T, d *sql.DB, want []attachmentRow) {
t.Errorf("Unexpected attachments: %s", d)
}
}

func TestDBPut_updating_a_doc_should_produce_new_rev_id(t *testing.T) {
t.Parallel()

d := newDB(t)

rev := d.tPut("foo", map[string]string{"foo": "bar"})
rev2 := d.tPut("foo", map[string]string{"foo": "baz"}, kivik.Rev(rev))

r, _ := parseRev(rev)
r2, _ := parseRev(rev2)
if r.id == r2.id {
t.Fatalf("rev(%s) and rev2(%s) should have different rev ids", rev, rev2)
}
}
5 changes: 2 additions & 3 deletions x/sqlite/putattachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,17 @@ func TestDBPutAttachment(t *testing.T) {
{
ID: "foo",
Rev: 2,
RevID: r2.id,
RevID: "def",
ParentRev: &[]int{1}[0],
ParentRevID: &r1.id,
},
{
ID: "foo",
Rev: 2,
RevID: "def",
RevID: r2.id,
ParentRev: &[]int{1}[0],
ParentRevID: &r1.id,
},

{
ID: "foo",
Rev: 3,
Expand Down

0 comments on commit 3d1dd76

Please sign in to comment.