-
Notifications
You must be signed in to change notification settings - Fork 23
Fix: Implement and test basic key-value storeImplement basic key-value store with persistence and tests #49
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
tendinginfinity24
wants to merge
3
commits into
zhravan:main
Choose a base branch
from
tendinginfinity24:fix/key-value-store
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.
Open
Changes from all commits
Commits
Show all changes
3 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
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
120 changes: 49 additions & 71 deletions
120
internal/exercises/templates/35_basic_key_value_store/key_value_store_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 |
|---|---|---|
| @@ -1,98 +1,76 @@ | ||
| package basic_key_value_store | ||
|
|
||
| import ( | ||
| "os" | ||
| "errors" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNewKeyValueStore(t *testing.T) { | ||
| filepath := "test_kv_store.txt" | ||
| s := NewKeyValueStore(filepath) | ||
| if s == nil { | ||
| t.Errorf("Expected a new KeyValueStore, got nil") | ||
| } | ||
| if s.filepath != filepath { | ||
| t.Errorf("Expected filepath %s, got %s", filepath, s.filepath) | ||
| } | ||
| if s.data == nil { | ||
| t.Errorf("Expected data map to be initialized, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestSetAndGet(t *testing.T) { | ||
| filepath := "test_set_get.txt" | ||
| defer os.Remove(filepath) | ||
| func TestLoadAndSave(t *testing.T) { | ||
| dir := t.TempDir() | ||
| filename := filepath.Join(dir, "test_store.db") | ||
|
|
||
| s := NewKeyValueStore(filepath) | ||
| s.Set("name", "Alice") | ||
| s.Set("age", "30") | ||
| store := NewKeyValueStore(filename) | ||
| store.Set("city", "New York") | ||
| store.Set("country", "USA") | ||
|
|
||
| val, err := s.Get("name") | ||
| if err != nil || val != "Alice" { | ||
| t.Errorf("Expected 'Alice', got %q, error: %v", val, err) | ||
| if err := store.Save(); err != nil { | ||
| t.Fatalf("unexpected save error: %v", err) | ||
| } | ||
|
|
||
| val, err = s.Get("age") | ||
| if err != nil || val != "30" { | ||
| t.Errorf("Expected '30', got %q, error: %v", val, err) | ||
| // load into a new store | ||
| other := NewKeyValueStore(filename) | ||
| if err := other.Load(); err != nil { | ||
| t.Fatalf("unexpected load error: %v", err) | ||
| } | ||
|
|
||
| _, err = s.Get("nonexistent") | ||
| if err == nil { | ||
| t.Errorf("Expected error for non-existent key, got nil") | ||
| if v, err := other.Get("city"); v != "New York" || err != nil { | ||
| t.Errorf("expected New York, got %q, err=%v", v, err) | ||
| } | ||
| if v, err := other.Get("country"); v != "USA" || err != nil { | ||
| t.Errorf("expected USA, got %q, err=%v", v, err) | ||
| } | ||
| } | ||
|
|
||
| func TestDelete(t *testing.T) { | ||
| filepath := "test_delete.txt" | ||
| defer os.Remove(filepath) | ||
| func TestMissingKey(t *testing.T) { | ||
| dir := t.TempDir() | ||
| filename := filepath.Join(dir, "test_store_missing.db") | ||
|
|
||
| s := NewKeyValueStore(filepath) | ||
| s.Set("key1", "value1") | ||
|
|
||
| err := s.Delete("key1") | ||
| if err != nil { | ||
| t.Errorf("Expected no error, got %v", err) | ||
| } | ||
| store := NewKeyValueStore(filename) | ||
| store.Set("name", "Alice") | ||
|
|
||
| _, err = s.Get("key1") | ||
| if err == nil { | ||
| t.Errorf("Expected error for deleted key, got nil") | ||
| } | ||
|
|
||
| err = s.Delete("nonexistent") | ||
| if err == nil { | ||
| t.Errorf("Expected error for deleting non-existent key, got nil") | ||
| _, err := store.Get("notthere") | ||
| if !errors.Is(err, ErrKeyNotFound) { | ||
| t.Errorf("expected ErrKeyNotFound, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestLoadAndSave(t *testing.T) { | ||
| filepath := "test_load_save.txt" | ||
| defer os.Remove(filepath) | ||
| func TestDelete(t *testing.T) { | ||
| t.Parallel() | ||
| dir := t.TempDir() | ||
| filename := filepath.Join(dir, "test_store.db") | ||
|
|
||
| // Create a store and save it | ||
| s1 := NewKeyValueStore(filepath) | ||
| s1.Set("city", "New York") | ||
| s1.Set("country", "USA") | ||
| err := s1.Save() | ||
| if err != nil { | ||
| t.Fatalf("Failed to save store: %v", err) | ||
| } | ||
| store := NewKeyValueStore(filename) | ||
| store.Set("k", "v") | ||
|
|
||
| // Load into a new store | ||
| s2 := NewKeyValueStore(filepath) | ||
| err = s2.Load() | ||
| if err != nil { | ||
| t.Fatalf("Failed to load store: %v", err) | ||
| if err := store.Delete("k"); err != nil { | ||
| t.Fatalf("unexpected delete error: %v", err) | ||
| } | ||
|
|
||
| val, err := s2.Get("city") | ||
| if err != nil || val != "New York" { | ||
| t.Errorf("Expected 'New York', got %q, error: %v", val, err) | ||
| if _, err := store.Get("k"); !errors.Is(err, ErrKeyNotFound) { | ||
| t.Fatalf("expected ErrKeyNotFound after delete, got %v", err) | ||
| } | ||
| if err := store.Delete("k"); !errors.Is(err, ErrKeyNotFound) { | ||
| t.Fatalf("expected ErrKeyNotFound on second delete, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestLoadMissingFileIsNoop(t *testing.T) { | ||
| t.Parallel() | ||
| dir := t.TempDir() | ||
| filename := filepath.Join(dir, "does_not_exist.db") | ||
|
|
||
| val, err = s2.Get("country") | ||
| if err != nil || val != "USA" { | ||
| t.Errorf("Expected 'USA', got %q, error: %v", val, err) | ||
| store := NewKeyValueStore(filename) | ||
| if err := store.Load(); err != nil { | ||
| t.Fatalf("expected nil error on missing file, got %v", err) | ||
| } | ||
| } |
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.
🛠️ Refactor suggestion
Create parent directory before atomic write.
Save fails if s.filepath’s directory doesn’t exist. Ensure dir is created.
📝 Committable suggestion
🤖 Prompt for AI Agents