Test basic operations including CRUD and edge cases
(t *testing.T)
| 124 | |
| 125 | // Test basic operations including CRUD and edge cases |
| 126 | func TestBasicOperations(t *testing.T) { |
| 127 | setupTestEnvironment() |
| 128 | kv := testPlugin.operator |
| 129 | ctx := context.Background() |
| 130 | |
| 131 | t.Run("BasicCRUD", func(t *testing.T) { |
| 132 | // Set/Get |
| 133 | mustSet(t, kv, ctx, "group1", "key1", "value1") |
| 134 | mustGet(t, kv, ctx, "group1", "key1", "value1") |
| 135 | |
| 136 | // Update |
| 137 | mustSet(t, kv, ctx, "group1", "key1", "new_value") |
| 138 | mustGet(t, kv, ctx, "group1", "key1", "new_value") |
| 139 | |
| 140 | // Delete |
| 141 | mustDel(t, kv, ctx, "group1", "key1") |
| 142 | assertNotFound(t, kv, ctx, "group1", "key1") |
| 143 | |
| 144 | // Group operation |
| 145 | mustSet(t, kv, ctx, "group1", "key2", "value2") |
| 146 | mustSet(t, kv, ctx, "group1", "key3", "value3") |
| 147 | groupData, err := kv.GetByGroup(ctx, plugin.KVParams{Group: "group1", Page: 1, PageSize: 10}) |
| 148 | if err != nil { |
| 149 | t.Fatalf("Failed to get group data: %v", err) |
| 150 | } |
| 151 | |
| 152 | // the groupData should only have key2 and key3 because key1 is deleted |
| 153 | if len(groupData) != 2 { |
| 154 | t.Errorf("Expected 2 items, got %d", len(groupData)) |
| 155 | } |
| 156 | if groupData["key2"] != "value2" || groupData["key3"] != "value3" { |
| 157 | t.Errorf("Unexpected group data: %v", groupData) |
| 158 | } |
| 159 | }) |
| 160 | |
| 161 | t.Run("EdgeCases", func(t *testing.T) { |
| 162 | // Empty key |
| 163 | err := kv.Set(ctx, plugin.KVParams{Group: "group", Key: "", Value: "value"}) |
| 164 | assertError(t, err, plugin.ErrKVKeyEmpty, "Empty key test") |
| 165 | |
| 166 | // Empty group query |
| 167 | _, err = kv.GetByGroup(ctx, plugin.KVParams{Group: "", Page: 1, PageSize: 10}) |
| 168 | assertError(t, err, plugin.ErrKVGroupEmpty, "Empty group test") |
| 169 | |
| 170 | // Non-existent key |
| 171 | assertNotFound(t, kv, ctx, "non_exist_group", "non_exist_key") |
| 172 | |
| 173 | // Cache penetration protection |
| 174 | key := fmt.Sprintf("non_exist_key_%d", time.Now().UnixNano()) |
| 175 | assertNotFound(t, kv, ctx, "cache_penetration", key) |
| 176 | }) |
| 177 | |
| 178 | t.Run("CacheConsistency", func(t *testing.T) { |
| 179 | mustSet(t, kv, ctx, "cache_group", "cache_key", "cache_value") |
| 180 | mustGet(t, kv, ctx, "cache_group", "cache_key", "cache_value") |
| 181 | |
| 182 | // Update and verify immediate consistency |
| 183 | mustSet(t, kv, ctx, "cache_group", "cache_key", "updated_value") |
nothing calls this directly
no test coverage detected