Skip to content

Commit f4ccf7e

Browse files
committed
fix FindDocuments bug. was using qap.HeaderCodesEqual
1 parent 4868ba6 commit f4ccf7e

7 files changed

Lines changed: 110 additions & 52 deletions

File tree

‎cmd/boltqap/boltqap.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (q *boltqap) addDoc(doc document) error {
164164
}
165165

166166
func (q *boltqap) DoProjectDocuments(project string, f func(d document) error) error {
167-
return q.db.View(func(tx *bbolt.Tx) error {
167+
err := q.db.View(func(tx *bbolt.Tx) error {
168168
b := tx.Bucket([]byte(project))
169169
if b == nil {
170170
return fmt.Errorf("project %q not found", project)
@@ -175,13 +175,13 @@ func (q *boltqap) DoProjectDocuments(project string, f func(d document) error) e
175175
log.Println("error reading document from database: ", err.Error())
176176
return nil
177177
}
178-
err = f(doc)
179-
if errors.Is(err, ErrEndLookup) {
180-
return nil
181-
}
182-
return err
178+
return f(doc)
183179
})
184180
})
181+
if err == nil || errors.Is(err, ErrEndLookup) {
182+
return nil
183+
}
184+
return err
185185
}
186186

187187
func (q *boltqap) DoDocuments(f func(d document) error) error {
@@ -276,8 +276,8 @@ func (q *boltqap) ImportDocuments(documents []document) (err error) {
276276
return nil
277277
}
278278

279-
// FindMainDocument finds main document ignoring attachment.
280-
func (q *boltqap) FindMainDocument(target qap.Header) (doc document, err error) {
279+
// FindDocument finds main document ignoring attachment.
280+
func (q *boltqap) FindDocument(target qap.Header) (doc document, err error) {
281281
err = target.Validate()
282282
if err != nil {
283283
return document{}, err
@@ -287,7 +287,7 @@ func (q *boltqap) FindMainDocument(target qap.Header) (doc document, err error)
287287
if err != nil {
288288
return fmt.Errorf("document %s has Header error: %s", d, err)
289289
}
290-
if qap.HeaderCodesEqual(h, target) {
290+
if qap.HeadersEqual(h, target) {
291291
doc = d
292292
return ErrEndLookup
293293
}
@@ -301,7 +301,7 @@ func (q *boltqap) AddRevision(target qap.Header, newrev revision) error {
301301
if err != nil {
302302
return err
303303
}
304-
doc, err := q.FindMainDocument(target)
304+
doc, err := q.FindDocument(target)
305305
if err != nil {
306306
return err
307307
}

‎cmd/boltqap/boltqap_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,48 @@ func TestBoltKey(t *testing.T) {
2828
}
2929
}
3030

31+
func TestBoltStore(t *testing.T) {
32+
const testFile = "qap_test.db"
33+
q, err := OpenBoltQAP(testFile, nil)
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
defer os.Remove(testFile)
38+
defer q.Close()
39+
rev, err := qap.ParseRevision("C.3")
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
time1 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
44+
doc1 := document{
45+
Project: "SPS",
46+
Equipment: "A",
47+
DocType: "HP",
48+
SubmittedBy: "pato",
49+
Number: 1,
50+
Location: "/1/",
51+
HumanName: "human name",
52+
FileExtension: "catpart",
53+
Revisions: []revision{{Index: qap.NewRevision(), Description: "first"}, {Index: rev, Description: "second"}},
54+
Created: time1,
55+
Revised: time1,
56+
}
57+
err = q.CreateProject(doc1.Project)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
err = q.addDoc(doc1)
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
hd, err := doc1.Header()
66+
got, err := q.FindDocument(hd)
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
assertDocEqual(t, got, doc1)
71+
}
72+
3173
func TestDoDocumentRange(t *testing.T) {
3274
const testFile = "qap_test.db"
3375
q, err := OpenBoltQAP(testFile, nil)

‎cmd/boltqap/boltqaphandlers.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (q *boltqap) handleGetDocument(rw http.ResponseWriter, r *http.Request) {
2626
return
2727
}
2828
log.Println("get document", hd.String())
29-
doc, err := q.FindMainDocument(hd)
29+
doc, err := q.FindDocument(hd)
3030
if err != nil {
3131
http.Error(rw, err.Error(), http.StatusInternalServerError)
3232
return

‎cmd/boltqap/document.go‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"log"
78
"net/http"
89
"reflect"
910
"strconv"
@@ -82,13 +83,6 @@ func (d document) records() []string {
8283
}
8384
}
8485

85-
func (d document) Version() string {
86-
if len(d.Revisions) == 0 {
87-
return qap.NewRevision().String()
88-
}
89-
return d.Revisions[len(d.Revisions)-1].Index.String()
90-
}
91-
9286
func docFromRecord(record []string, ignoreTime bool) (document, error) {
9387
if len(record) < len(document{}.recordsHeader()) {
9488
return document{}, errors.New("not enough record fields to parse document")
@@ -140,16 +134,6 @@ func (d document) key() []byte {
140134
return boltKey(d.Created)
141135
}
142136

143-
func (d *document) AddRevision(rev revision) error {
144-
for i := range d.Revisions {
145-
if d.Revisions[i].Index == rev.Index {
146-
return errors.New("document revision index already exists")
147-
}
148-
}
149-
d.Revisions = append(d.Revisions, rev)
150-
return nil
151-
}
152-
153137
func (d document) Info() (qap.DocInfo, error) {
154138
hd, err := d.Header()
155139
if err != nil {
@@ -175,6 +159,20 @@ func (d document) Revision() qap.Revision {
175159
return d.Revisions[len(d.Revisions)-1].Index
176160
}
177161

162+
func (d *document) AddRevision(rev revision) error {
163+
for i := range d.Revisions {
164+
if d.Revisions[i].Index == rev.Index {
165+
return errors.New("document revision index already exists")
166+
}
167+
}
168+
d.Revisions = append(d.Revisions, rev)
169+
return nil
170+
}
171+
172+
func (d document) Version() string {
173+
return d.Revision().String()
174+
}
175+
178176
// String returns the Header's document name representation i.e. "SPS-PEC-HP-023
179177
func (d document) String() string {
180178
di, err := d.Info()
@@ -205,7 +203,7 @@ func (d document) Header() (qap.Header, error) {
205203
return qap.ParseHeader(fmt.Sprintf("%s-%s-%s-%d.%02d", d.Project, d.Equipment, d.DocType, d.Number, d.Attachment), false)
206204
}
207205

208-
func (d document) value() []byte {
206+
func (d *document) value() []byte {
209207
b, err := json.Marshal(d)
210208
if err != nil {
211209
panic("unreachable")
@@ -221,20 +219,22 @@ func consolidateMainDocumentVersions(documents []document) ([]document, error) {
221219
return nil, err
222220
}
223221
rev := doc.Revision()
224-
got, ok := mdoc[hd]
222+
original, ok := mdoc[hd]
225223
if !ok {
226224
doc.AddRevision(revision{Index: rev})
227225
mdoc[hd] = doc
228226
continue
229227
}
230228
// we have two documents of identical header
231-
if got.Revision() == doc.Revision() {
229+
if original.Revision() == doc.Revision() {
232230
return nil, fmt.Errorf("conflicting document %s rev %s", doc.String(), doc.Revision())
233231
}
234-
err = doc.AddRevision(revision{Index: rev})
232+
err = original.AddRevision(revision{Index: rev})
235233
if err != nil {
236234
return nil, fmt.Errorf("attempting to merge document %s revision: %s", doc.String(), err)
237235
}
236+
log.Println("revision ", rev, " added to ", original.String())
237+
mdoc[hd] = original
238238
}
239239
var newDocs []document
240240
for _, d := range mdoc {

‎cmd/boltqap/document_test.go‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"testing"
65
"time"
76

@@ -35,51 +34,57 @@ func TestDocumentRecords(t *testing.T) {
3534
if err != nil {
3635
t.Fatal(err)
3736
}
38-
39-
if err := assertDocEqual(d, dpiped); err != nil {
40-
t.Errorf("piped document differs: %s", err)
41-
}
37+
assertDocEqual(t, d, dpiped)
4238
}
4339

44-
func assertDocEqual(a, b document) error {
40+
func assertDocEqual(t *testing.T, a, b document) error {
41+
if len(a.Revisions) == len(b.Revisions) {
42+
for i := range a.Revisions {
43+
if a.Revisions[i] != b.Revisions[i] {
44+
t.Errorf("%dth revision not equal %s,%s", i, a.Revisions[i], b.Revisions[i])
45+
}
46+
}
47+
} else {
48+
t.Error("revisions lengths unequal")
49+
}
4550
if a.Revision() != b.Revision() {
46-
return fmt.Errorf("Revision not equal %q, %q", a.Revision(), b.Revision())
51+
t.Errorf("Revision not equal %q, %q", a.Revision(), b.Revision())
4752
}
4853
if a.Location != b.Location {
49-
return fmt.Errorf("Location not equal %q, %q", a.Location, b.Location)
54+
t.Errorf("Location not equal %q, %q", a.Location, b.Location)
5055
}
5156
if a.SubmittedBy != b.SubmittedBy {
52-
return fmt.Errorf("SubmittedBy not equal %q, %q", a.SubmittedBy, b.SubmittedBy)
57+
t.Errorf("SubmittedBy not equal %q, %q", a.SubmittedBy, b.SubmittedBy)
5358
}
5459
if a.Number != b.Number {
55-
return fmt.Errorf("Number not equal %d, %d", a.Number, b.Number)
60+
t.Errorf("Number not equal %d, %d", a.Number, b.Number)
5661
}
5762
if a.FileExtension != b.FileExtension {
58-
return fmt.Errorf("FileExtension not equal %q, %q", a.FileExtension, b.FileExtension)
63+
t.Errorf("FileExtension not equal %q, %q", a.FileExtension, b.FileExtension)
5964
}
6065
if a.Project != b.Project {
61-
return fmt.Errorf("project not equal %q, %q", a.Project, b.Project)
66+
t.Errorf("project not equal %q, %q", a.Project, b.Project)
6267
}
6368
if a.DocType != b.DocType {
64-
return fmt.Errorf("DocType not equal %q, %q", a.DocType, b.DocType)
69+
t.Errorf("DocType not equal %q, %q", a.DocType, b.DocType)
6570
}
6671
if a.Attachment != b.Attachment {
67-
return fmt.Errorf("Attachment not equal %d, %d", a.Attachment, b.Attachment)
72+
t.Errorf("Attachment not equal %d, %d", a.Attachment, b.Attachment)
6873
}
6974
if a.Created != b.Created {
70-
return fmt.Errorf("Created not equal %q, %q", a.Created, b.Created)
75+
t.Errorf("Created not equal %q, %q", a.Created, b.Created)
7176
}
7277
if a.Revised != b.Revised {
73-
return fmt.Errorf("Revised not equal %q, %q", a.Revised, b.Revised)
78+
t.Errorf("Revised not equal %q, %q", a.Revised, b.Revised)
7479
}
7580
if a.Deleted != b.Deleted {
76-
return fmt.Errorf("deleted not equal %t, %t", a.Deleted, b.Deleted)
81+
t.Errorf("deleted not equal %t, %t", a.Deleted, b.Deleted)
7782
}
7883
if a.HumanName != b.HumanName {
79-
return fmt.Errorf("HumanName not equal %q, %q", a.HumanName, b.HumanName)
84+
t.Errorf("HumanName not equal %q, %q", a.HumanName, b.HumanName)
8085
}
8186
if a.Equipment != b.Equipment {
82-
return fmt.Errorf("Equipment not equal %q, %q", a.Equipment, b.Equipment)
87+
t.Errorf("Equipment not equal %q, %q", a.Equipment, b.Equipment)
8388
}
8489
return nil
8590
}

‎cmd/boltqap/main.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"embed"
5+
"encoding/json"
56
"flag"
67
"fmt"
78
"html/template"
@@ -85,6 +86,16 @@ var funcs = template.FuncMap{
8586
}
8687
return "/qap/doc/" + hd.String()
8788
},
89+
"debug": func(a any) template.HTML {
90+
b, err := json.Marshal(a)
91+
if err != nil {
92+
b, err = json.Marshal(&a)
93+
}
94+
if err != nil {
95+
return template.HTML(err.Error())
96+
}
97+
return template.HTML(b)
98+
},
8899
}
89100

90101
func headerURL(hd qap.Header) string {

‎cmd/boltqap/templates/document.tmpl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<p>{{.Description}}</p>
2727
</div>
2828
{{else}}
29-
<p><strong>rev A.1-draft</strong></p>
29+
<p><strong>rev A.1-draft</strong> (default)</p>
3030
{{end}}
3131

3232
{{if eq .Attachment 0}}

0 commit comments

Comments
 (0)