Skip to content

Commit

Permalink
add updated_at to update statements; unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalef committed Sep 18, 2023
1 parent 680e2e2 commit fa1f697
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions pkg/models/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Message struct {
UUID uuid.UUID `json:"uuid"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Role string `json:"role"`
Content string `json:"content"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/store/postgres/documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestCollectionUpdate(t *testing.T) {
err = collection.GetByName(ctx)
assert.NoError(t, err)
assert.Equal(t, expectedDimensions, collection.EmbeddingDimensions)
assert.Less(t, collection.CreatedAt, collection.UpdatedAt)
}

func TestCollectionGetByName(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/store/postgres/memorystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ func verifyMessagesInDB(
resultMessages[i].Metadata,
"Expected Metadata to be equal",
)
assert.Less(
t,
resultMessages[i].CreatedAt,
resultMessages[i].UpdatedAt,
"CreatedAt should be less than UpdatedAt",
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/store/postgres/message_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func putMessageMetadataTx(
retrievedMessage.UUID = message.UUID
_, err = tx.NewUpdate().
Model(&retrievedMessage).
Column("metadata").
Column("metadata", "updated_at").
Where("session_id = ? AND uuid = ?", sessionID, message.UUID).
Returning("*").
Exec(ctx)
Expand Down
3 changes: 2 additions & 1 deletion pkg/store/postgres/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func putMessages(
UUID: msg.UUID,
SessionID: sessionID,
CreatedAt: msg.CreatedAt,
UpdatedAt: msg.UpdatedAt,
Role: msg.Role,
Content: msg.Content,
TokenCount: msg.TokenCount,
Expand All @@ -68,7 +69,7 @@ func putMessages(
// Insert messages
_, err = db.NewInsert().
Model(&pgMessages).
Column("id", "created_at", "uuid", "session_id", "role", "content", "token_count").
Column("id", "created_at", "uuid", "session_id", "role", "content", "token_count", "updated_at").
On("CONFLICT (uuid) DO UPDATE").
Exec(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/postgres/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (dao *SessionDAO) updateSession(
Metadata: session.Metadata,
DeletedAt: time.Time{}, // Intentionally overwrite soft-delete with zero value
}
var columns = []string{"deleted_at"}
var columns = []string{"deleted_at", "updated_at"}
if session.Metadata != nil {
columns = append(columns, "metadata")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/store/postgres/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func TestSessionDAO_Update(t *testing.T) {
assert.Equal(t, createdSession.SessionID, updatedSession.SessionID)
assert.Equal(t, createdSession.UserID, updatedSession.UserID)
assert.Equal(t, updateSession.Metadata, updatedSession.Metadata)
assert.Less(t, createdSession.UpdatedAt, updatedSession.UpdatedAt)
}

func TestSessionDAO_UpdateWithNilMetadata(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/postgres/userstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (dao *UserStoreDAO) updateUser(
}
r, err := dao.db.NewUpdate().
Model(&userDB).
Column("email", "first_name", "last_name", "metadata").
Column("email", "first_name", "last_name", "metadata", "updated_at").
OmitZero().
Where("user_id = ?", user.UserID).
Exec(ctx)
Expand Down
7 changes: 4 additions & 3 deletions pkg/store/postgres/userstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestUserStoreDAO(t *testing.T) {
},
Email: "email",
}
_, err := userStore.Create(ctx, user)
createdUser, err := userStore.Create(ctx, user)
assert.NoError(t, err)

// Update the user with zero values
Expand All @@ -77,10 +77,11 @@ func TestUserStoreDAO(t *testing.T) {
assert.NoError(t, err)

// Check that the updated user still has the original non-zero values
assert.Equal(t, user.Metadata, updatedUser.Metadata)
assert.Equal(t, user.Email, updatedUser.Email)
assert.Equal(t, createdUser.Metadata, updatedUser.Metadata)
assert.Equal(t, createdUser.Email, updatedUser.Email)
// Bob should be the new first name
assert.Equal(t, "bob", updatedUser.FirstName)
assert.Less(t, createdUser.UpdatedAt, updatedUser.UpdatedAt)
})

t.Run("Update Non-Existant User should result in NotFoundError", func(t *testing.T) {
Expand Down

0 comments on commit fa1f697

Please sign in to comment.