MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestSaveAndLoadGraph

Function TestSaveAndLoadGraph

examples/server/memory/kb_test.go:190–245  ·  view source on GitHub ↗

TestSaveAndLoadGraph ensures data persists correctly across save/load cycles.

(t *testing.T)

Source from the content-addressed store, hash-verified

188
189// TestSaveAndLoadGraph ensures data persists correctly across save/load cycles.
190func TestSaveAndLoadGraph(t *testing.T) {
191 for name, newStore := range stores() {
192 t.Run(name, func(t *testing.T) {
193 s := newStore(t)
194 kb := knowledgeBase{s: s}
195
196 // Setup test data
197 testGraph := KnowledgeGraph{
198 Entities: []Entity{
199 {
200 Name: "Charlie",
201 EntityType: "Person",
202 Observations: []string{"Likes hiking"},
203 },
204 },
205 Relations: []Relation{
206 {
207 From: "Charlie",
208 To: "Mountains",
209 RelationType: "enjoys",
210 },
211 },
212 }
213
214 // Persist to storage
215 err := kb.saveGraph(testGraph)
216 if err != nil {
217 t.Fatalf("failed to save graph: %v", err)
218 }
219
220 // Reload from storage
221 loadedGraph, err := kb.loadGraph()
222 if err != nil {
223 t.Fatalf("failed to load graph: %v", err)
224 }
225
226 // Verify data integrity
227 if !reflect.DeepEqual(testGraph, loadedGraph) {
228 t.Errorf("loaded graph does not match saved graph.\nExpected: %+v\nGot: %+v", testGraph, loadedGraph)
229 }
230
231 // Test malformed data handling
232 if fs, ok := s.(*fileStore); ok {
233 err := os.WriteFile(fs.path, []byte("invalid json"), 0o600)
234 if err != nil {
235 t.Fatalf("failed to write invalid json: %v", err)
236 }
237
238 _, err = kb.loadGraph()
239 if err == nil {
240 t.Errorf("expected error when loading invalid JSON, got nil")
241 }
242 }
243 })
244 }
245}
246
247// TestDuplicateEntitiesAndRelations verifies duplicate prevention logic.

Callers

nothing calls this directly

Calls 4

saveGraphMethod · 0.95
loadGraphMethod · 0.95
storesFunction · 0.85
RunMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…