Skip to content

Commit

Permalink
if update _user for field, use new time for _time
Browse files Browse the repository at this point in the history
  • Loading branch information
DocSavage committed Jan 27, 2024
1 parent ab92fb0 commit 1aba7be
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
6 changes: 4 additions & 2 deletions datatype/neuronjson/neuronjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ func updateJSON(origData, newData NeuronJSON, user string, conditionals []string

// determine if any fields are being set for the first time or modified
newlySet := make(map[string]struct{}, len(newData)) // fields that aren't same as old data
newFields := make(map[string]struct{}, len(newData)) // fields that are not _user or _time
newFields := make(map[string]struct{}, len(newData)) // fields that aren't _user or _time
if origData == nil {
for field := range newData {
newlySet[field] = struct{}{}
Expand All @@ -1264,6 +1264,7 @@ func updateJSON(origData, newData NeuronJSON, user string, conditionals []string
newFields[field] = struct{}{}
}
}
fmt.Printf("newlySet: %v\n", newlySet)

// carry forward any fields not being modified if replace option is not set
if !replace {
Expand Down Expand Up @@ -1301,7 +1302,8 @@ func updateJSON(origData, newData NeuronJSON, user string, conditionals []string
if _, foundUser := newlySet[field+"_user"]; !foundUser && user != "" {
newData[field+"_user"] = user
}
if _, foundTime := newlySet[field+"_time"]; !foundTime {
_, foundUser := newlySet[field+"_user"]
if _, foundTime := newlySet[field+"_time"]; foundUser || !foundTime {
newData[field+"_time"] = timeStr
}
}
Expand Down
80 changes: 80 additions & 0 deletions datatype/neuronjson/neuronjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"sync"
"testing"
"time"

pb "google.golang.org/protobuf/proto"

Expand Down Expand Up @@ -1022,6 +1023,85 @@ func TestBodyidUserTime(t *testing.T) {
server.TestBadHTTP(t, "POST", keyreq, strings.NewReader(neuron))
}

// TestUserTime tests that user and time are properly recorded.
func TestUserTime(t *testing.T) {
if err := server.OpenTest(); err != nil {
t.Fatalf("can't open test server: %v\n", err)
}
defer server.CloseTest()

uuid, _ := initTestRepo()

payload := bytes.NewBufferString(`{"typename": "neuronjson", "dataname": "neurons"}`)
apiStr := fmt.Sprintf("%srepo/%s/instance", server.WebAPIPath, uuid)
server.TestHTTP(t, "POST", apiStr, payload)

// Test JSON to be posted.
neuron1 := `{"bodyid": 1, "number": 1234, "string": "foo"}`
neuron2 := `{"bodyid": 2, "number": 3456, "hash": "0x1234567890abcdef"}`

// POST the annotations
keyreq := fmt.Sprintf("%snode/%s/neurons/key/1?u=user1", server.WebAPIPath, uuid)
server.TestHTTP(t, "POST", keyreq, strings.NewReader(neuron1))
keyreq = fmt.Sprintf("%snode/%s/neurons/key/2?u=user2", server.WebAPIPath, uuid)
server.TestHTTP(t, "POST", keyreq, strings.NewReader(neuron2))

// Get all neuronjson
allreq := fmt.Sprintf("%snode/%s/neurons/all?show=all", server.WebAPIPath, uuid)
returnValue := server.TestHTTP(t, "GET", allreq, nil)
var neurons ListNeuronJSON
if err := json.Unmarshal(returnValue, &neurons); err != nil {
t.Fatalf("Unable to parse return from /all request: %s\nError: %v\n", string(returnValue), err)
}
sort.Sort(&neurons)
fmt.Printf("Got neurons: %v\n", string(returnValue))

// Test that user and time were recorded correctly
if len(neurons) != 2 {
t.Fatalf("Expected 2 neuron, got %d\n", len(neurons))
}
if value, found := neurons[0]["number_user"]; !found || value != "user1" {
t.Errorf("Expected 'user1', got %v\n", value)
}
ival, found := neurons[0]["number_time"]
if !found {
t.Errorf("Expected number_time field but got none\n")
}
neuron1_number_time, ok := ival.(string)
if !ok {
t.Errorf("Expected string for number_time, got %v\n", reflect.TypeOf(ival))
}
if value, found := neurons[1]["number_user"]; !found || value != "user2" {
t.Errorf("Expected 'user2', got %v\n", value)
}
if _, found := neurons[1]["number_time"]; !found {
t.Errorf("Expected number_time field but got none\n")
}

// Sleep for a second so we know the timestamps will be different.
time.Sleep(1 * time.Second)

// Test update of neuron1
neuron1a := `{"bodyid": 1, "number_user": "user1a", "string": "moo"}`
keyreq = fmt.Sprintf("%snode/%s/neurons/key/1?u=user3", server.WebAPIPath, uuid)
server.TestHTTP(t, "POST", keyreq, strings.NewReader(neuron1a))
keyreq = fmt.Sprintf("%snode/%s/neurons/key/1?show=all", server.WebAPIPath, uuid)
returnValue = server.TestHTTP(t, "GET", keyreq, nil)
var neuron NeuronJSON
if err := json.Unmarshal(returnValue, &neuron); err != nil {
t.Fatalf("Unable to parse return: %s\nError: %v\n", string(returnValue), err)
}
if value, found := neuron["number_user"]; !found || value != "user1a" {
t.Errorf("Expected 'user1a', got %v\n", value)
}
if value, found := neuron["number_time"]; !found || value == neuron1_number_time {
t.Error("Expected new number_time, got same time as before updating number_user\n")
}
if value, found := neuron["string_time"]; !found || value == neuron1_number_time {
t.Error("Expected new string_time, got same time as before updating string field\n")
}
}

func TestAll(t *testing.T) {
if err := server.OpenTest(); err != nil {
t.Fatalf("can't open test server: %v\n", err)
Expand Down

0 comments on commit 1aba7be

Please sign in to comment.