Skip to content

Commit

Permalink
Improve missing key message
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Sep 27, 2023
1 parent 5ebce9b commit 28ee209
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
28 changes: 28 additions & 0 deletions web/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,31 @@ func TestHTTPBadRecord(t *testing.T) {
log.Println("bad record injection returns", err)
}
}

func TestHTTPPostMissingKeys(t *testing.T) {
initMetaDataService()

// initialize FilesDB connection
var err error
FilesDB, err = InitFilesDB()
defer FilesDB.Close()
if err != nil {
log.Printf("FilesDB error: %v\n", err)
}

// HTTP POST request
schema := "test"
inputRecord := `{"StringKey": "test","StrKeyMultipleValues": "3A, 3B","ListKey": ["3A", "3B"],"FloatKey": 1.1}`
form := url.Values{}
form.Add("record", string(inputRecord))
form.Add("SchemaName", schema)
reader := strings.NewReader(form.Encode())
_, err = respRecorder("POST", "/api", reader, APIHandler)
if err == nil {
msg := fmt.Sprintf("fail to catch missing key error for record %v in schema %s", inputRecord, schema)
fmt.Println(msg)
t.Error(errors.New(msg))
} else {
fmt.Printf("INFO: expected error %v", err)
}
}
8 changes: 7 additions & 1 deletion web/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,13 @@ func (s *Schema) Validate(rec Record) error {
}
if len(mkeys) != len(smkeys) {
sort.Sort(StringList(mkeys))
msg := fmt.Sprintf("Unable to collect all mandatory keys %v, found %v", smkeys, mkeys)
var missing []string
for _, k := range smkeys {
if !InList(k, mkeys) {
missing = append(missing, k)
}
}
msg := fmt.Sprintf("Schema %s, mandatory keys %v, record keys %v, missing keys %v", s.FileName, smkeys, mkeys, missing)
log.Printf("ERROR: %s", msg)
return errors.New(msg)
}
Expand Down

0 comments on commit 28ee209

Please sign in to comment.