Skip to content

Commit

Permalink
Added tests for BulkWrite method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritchie6 committed Jan 9, 2025
1 parent 8ada4aa commit b7936d9
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions test/memoria_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,124 @@ func TestMemoriaWriteReadString(t1 *testing.T) {
})
}
}

func TestMemoriaBulkWrite(t *testing.T) {

// temp-dir for tests
tempDir, err := os.MkdirTemp("", "memoria-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)

m := memoria.New(memoria.Options{
Basedir: tempDir,
MaxCacheSize: 1024,
})

tests := []struct {
name string
pairs map[string][]byte
numWorkers int
expectedErr bool
}{
{
name: "Successful bulk write with multiple pairs",
pairs: map[string][]byte{
"key1": []byte("value1"),
"key2": []byte("value2"),
"key3": []byte("value3"),
},
numWorkers: 2,
expectedErr: false,
},
{
name: "Bulk write with an empty key",
pairs: map[string][]byte{
"": []byte("value1"), // Empty key should cause error
},
numWorkers: 1,
expectedErr: true,
},
{
name: "Bulk write with an empty value",
pairs: map[string][]byte{
"key1": []byte{}, // Empty value should be handled correctly
},
numWorkers: 1,
expectedErr: false,
},
{
name: "Bulk write with large values",
pairs: map[string][]byte{
"key1": bytes.Repeat([]byte("a"), 1000), // Large value
"key2": bytes.Repeat([]byte("b"), 1000), // Large value
},
numWorkers: 2,
expectedErr: false,
},
{
name: "Bulk write with mix of valid and invalid keys",
pairs: map[string][]byte{
"validKey1": []byte("value1"),
"": []byte("value2"), // invalid key
},
numWorkers: 2,
expectedErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

results := m.BulkWrite(tt.pairs, tt.numWorkers)

// Check the number of results matches the number of pairs
if len(results) != len(tt.pairs) {
t.Errorf("Expected %d results, but got %d", len(tt.pairs), len(results))
}

var hasError bool
for _, result := range results {
if result.Error != nil {
hasError = true
}
}

// Check if the results contain errors where expected
// for _, result := range results {
// if tt.expectedErr && result.Error == nil {
// t.Errorf("Expected error but got nil for key %s", result.Key)
// }
// if !tt.expectedErr && result.Error != nil {
// t.Errorf("Unexpected error for key %s: %v", result.Key, result.Error)
// }
// }

// If we expect an error, ensure at least one result has an error
if tt.expectedErr && !hasError {
t.Errorf("Expected at least one error but got none")
}

// If no error is expected, ensure all results are successful
if !tt.expectedErr && hasError {
t.Errorf("Unexpected error(s) occurred")
}

// Verify the data was written correctly
for key, expectedValue := range tt.pairs {
if tt.expectedErr {
continue
}
got, err := m.Read(key)
if err != nil {
t.Errorf("Failed to read key %s: %v", key, err)
continue
}
if !bytes.Equal(got, expectedValue) {
t.Errorf("Read value for key %s = %v, want %v", key, got, expectedValue)
}
}
})
}
}

0 comments on commit b7936d9

Please sign in to comment.