-
Notifications
You must be signed in to change notification settings - Fork 332
test: add config CRUD unit tests #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MD-Mushfiqur123
wants to merge
2
commits into
Agent-Field:main
Choose a base branch
from
MD-Mushfiqur123:tests/config-crud
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
198 changes: 198 additions & 0 deletions
198
control-plane/internal/storage/config_storage_db_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestConfigStorageSetConfig(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| key string | ||
| value string | ||
| updatedBy string | ||
| }{ | ||
| { | ||
| name: "first insert creates version 1", | ||
| key: "config/agentfield.yaml", | ||
| value: "key: value\nlogging:\n level: debug\n", | ||
| updatedBy: "api", | ||
| }, | ||
| { | ||
| name: "second config entry", | ||
| key: "config/another.yaml", | ||
| value: "setting: true", | ||
| updatedBy: "cli", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ls, ctx := setupLocalStorage(t) | ||
| err := ls.SetConfig(ctx, tt.key, tt.value, tt.updatedBy) | ||
| require.NoError(t, err) | ||
|
|
||
| entry, err := ls.GetConfig(ctx, tt.key) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, entry) | ||
| require.Equal(t, tt.key, entry.Key) | ||
| require.Equal(t, tt.value, entry.Value) | ||
| require.Equal(t, 1, entry.Version) | ||
| require.Equal(t, tt.updatedBy, entry.CreatedBy) | ||
| require.Equal(t, tt.updatedBy, entry.UpdatedBy) | ||
| require.False(t, entry.CreatedAt.IsZero()) | ||
| require.False(t, entry.UpdatedAt.IsZero()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestConfigStorageUpsertIncrementsVersion(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ls, ctx := setupLocalStorage(t) | ||
| key := "config/agentfield.yaml" | ||
|
|
||
| err := ls.SetConfig(ctx, key, "version1", "alice") | ||
| require.NoError(t, err) | ||
|
|
||
| entry, err := ls.GetConfig(ctx, key) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "version1", entry.Value) | ||
| require.Equal(t, 1, entry.Version) | ||
| originalCreatedAt := entry.CreatedAt | ||
| originalCreatedBy := entry.CreatedBy | ||
|
|
||
| err = ls.SetConfig(ctx, key, "version2", "bob") | ||
| require.NoError(t, err) | ||
|
|
||
| entry, err = ls.GetConfig(ctx, key) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "version2", entry.Value) | ||
| require.Equal(t, 2, entry.Version) | ||
| require.Equal(t, originalCreatedBy, entry.CreatedBy) | ||
| require.Equal(t, originalCreatedAt, entry.CreatedAt) | ||
| require.Equal(t, "bob", entry.UpdatedBy) | ||
| require.False(t, entry.UpdatedAt.IsZero()) | ||
| } | ||
|
|
||
| func TestConfigStorageListConfigsLexicalOrder(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ls, ctx := setupLocalStorage(t) | ||
|
|
||
| require.NoError(t, ls.SetConfig(ctx, "c", "3", "user")) | ||
| require.NoError(t, ls.SetConfig(ctx, "a", "1", "user")) | ||
| require.NoError(t, ls.SetConfig(ctx, "b", "2", "user")) | ||
|
|
||
| entries, err := ls.ListConfigs(ctx) | ||
| require.NoError(t, err) | ||
| require.Len(t, entries, 3) | ||
| require.Equal(t, "a", entries[0].Key) | ||
| require.Equal(t, "b", entries[1].Key) | ||
| require.Equal(t, "c", entries[2].Key) | ||
| } | ||
|
|
||
| func TestConfigStorageGetMissingKey(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ls, ctx := setupLocalStorage(t) | ||
|
|
||
| entry, err := ls.GetConfig(ctx, "nonexistent") | ||
| require.NoError(t, err) | ||
| require.Nil(t, entry) | ||
| } | ||
|
|
||
| func TestConfigStorageDeleteConfig(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ls, ctx := setupLocalStorage(t) | ||
|
|
||
| require.NoError(t, ls.SetConfig(ctx, "test-key", "value", "admin")) | ||
|
|
||
| err := ls.DeleteConfig(ctx, "test-key") | ||
| require.NoError(t, err) | ||
|
|
||
| entry, err := ls.GetConfig(ctx, "test-key") | ||
| require.NoError(t, err) | ||
| require.Nil(t, entry) | ||
| } | ||
|
|
||
| func TestConfigStorageDeleteMissingKeyReturnsError(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ls, ctx := setupLocalStorage(t) | ||
|
|
||
| err := ls.DeleteConfig(ctx, "absent-key") | ||
| require.ErrorContains(t, err, `"absent-key"`) | ||
| } | ||
|
|
||
| func TestConfigStorageCancelledContext(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ls, ctx := setupLocalStorage(t) | ||
| cancelled, cancel := context.WithCancel(ctx) | ||
| cancel() | ||
|
|
||
| err := ls.SetConfig(cancelled, "key", "val", "user") | ||
| require.ErrorIs(t, err, context.Canceled) | ||
|
|
||
| _, err = ls.GetConfig(cancelled, "key") | ||
| require.ErrorIs(t, err, context.Canceled) | ||
|
|
||
| _, err = ls.ListConfigs(cancelled) | ||
| require.ErrorIs(t, err, context.Canceled) | ||
|
|
||
| err = ls.DeleteConfig(cancelled, "key") | ||
| require.ErrorIs(t, err, context.Canceled) | ||
| } | ||
|
|
||
| func TestConfigStorageDeleteConfigTwice(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ls, ctx := setupLocalStorage(t) | ||
| require.NoError(t, ls.SetConfig(ctx, "once", "val", "admin")) | ||
| require.NoError(t, ls.DeleteConfig(ctx, "once")) | ||
| err := ls.DeleteConfig(ctx, "once") | ||
| require.ErrorContains(t, err, `"once"`) | ||
| } | ||
|
|
||
| func TestConfigStorageListConfigsEmpty(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ls, ctx := setupLocalStorage(t) | ||
|
|
||
| entries, err := ls.ListConfigs(ctx) | ||
| require.NoError(t, err) | ||
| require.Empty(t, entries) | ||
| } | ||
|
|
||
| func TestConfigStorageMultipleUpsertRoundTrip(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ls, ctx := setupLocalStorage(t) | ||
| key := "roundtrip" | ||
|
|
||
| versions := []struct { | ||
| value string | ||
| updatedBy string | ||
| }{ | ||
| {"v1", "alice"}, | ||
| {"v2", "bob"}, | ||
| {"v3", "charlie"}, | ||
| } | ||
|
|
||
| for _, v := range versions { | ||
| require.NoError(t, ls.SetConfig(ctx, key, v.value, v.updatedBy)) | ||
| } | ||
|
|
||
| entry, err := ls.GetConfig(ctx, key) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "v3", entry.Value) | ||
| require.Equal(t, 3, entry.Version) | ||
| require.Equal(t, "alice", entry.CreatedBy) | ||
| require.Equal(t, "charlie", entry.UpdatedBy) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of this new file overlaps with coverage we already have on
main.TestLocalStorageConfigLifecycleinlocal_vector_config_pubsub_test.goalready exercises the SQLite config CRUD path, andcoverage_storage_clinch_test.gocovers the postgres branch too. Can we fold any truly missing assertions into those existing tests instead of adding a second end-to-end CRUD suite here?