|
| 1 | +package contextds_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ipfs/go-datastore" |
| 8 | + contextds "github.com/ipfs/go-datastore/context" |
| 9 | + "github.com/ipfs/go-datastore/query" |
| 10 | +) |
| 11 | + |
| 12 | +// AI generated tests and mocks |
| 13 | + |
| 14 | +type mockWrite struct { |
| 15 | + putCalled bool |
| 16 | + putKey datastore.Key |
| 17 | + putValue []byte |
| 18 | + putErr error |
| 19 | + deleteCalled bool |
| 20 | + deleteKey datastore.Key |
| 21 | + deleteErr error |
| 22 | +} |
| 23 | + |
| 24 | +func (m *mockWrite) Put(ctx context.Context, key datastore.Key, value []byte) error { |
| 25 | + m.putCalled = true |
| 26 | + m.putKey = key |
| 27 | + m.putValue = value |
| 28 | + return m.putErr |
| 29 | +} |
| 30 | + |
| 31 | +func (m *mockWrite) Delete(ctx context.Context, key datastore.Key) error { |
| 32 | + m.deleteCalled = true |
| 33 | + m.deleteKey = key |
| 34 | + return m.deleteErr |
| 35 | +} |
| 36 | + |
| 37 | +type mockRead struct { |
| 38 | + getCalled bool |
| 39 | + getKey datastore.Key |
| 40 | + getValue []byte |
| 41 | + getErr error |
| 42 | + hasCalled bool |
| 43 | + hasKey datastore.Key |
| 44 | + hasValue bool |
| 45 | + hasErr error |
| 46 | + |
| 47 | + getSizeCalled bool |
| 48 | + getSizeKey datastore.Key |
| 49 | + getSizeValue int |
| 50 | + getSizeErr error |
| 51 | + |
| 52 | + queryCalled bool |
| 53 | + queryQ query.Query |
| 54 | + queryResults query.Results |
| 55 | + queryErr error |
| 56 | +} |
| 57 | + |
| 58 | +func (m *mockRead) Get(ctx context.Context, key datastore.Key) ([]byte, error) { |
| 59 | + m.getCalled = true |
| 60 | + m.getKey = key |
| 61 | + return m.getValue, m.getErr |
| 62 | +} |
| 63 | + |
| 64 | +func (m *mockRead) Has(ctx context.Context, key datastore.Key) (bool, error) { |
| 65 | + m.hasCalled = true |
| 66 | + m.hasKey = key |
| 67 | + return m.hasValue, m.hasErr |
| 68 | +} |
| 69 | + |
| 70 | +func (m *mockRead) GetSize(ctx context.Context, key datastore.Key) (int, error) { |
| 71 | + m.getSizeCalled = true |
| 72 | + m.getSizeKey = key |
| 73 | + return m.getSizeValue, m.getSizeErr |
| 74 | +} |
| 75 | + |
| 76 | +func (m *mockRead) Query(ctx context.Context, q query.Query) (query.Results, error) { |
| 77 | + m.queryCalled = true |
| 78 | + m.queryQ = q |
| 79 | + return m.queryResults, m.queryErr |
| 80 | +} |
| 81 | + |
| 82 | +func TestDatastore_WithWriteContext(t *testing.T) { |
| 83 | + inner := datastore.NewMapDatastore() |
| 84 | + mock := &mockWrite{} |
| 85 | + ds := contextds.WrapDatastore(inner) |
| 86 | + ctx := contextds.WithWrite(context.Background(), mock) |
| 87 | + |
| 88 | + key := datastore.NewKey("foo") |
| 89 | + value := []byte("bar") |
| 90 | + |
| 91 | + err := ds.Put(ctx, key, value) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("Put failed: %v", err) |
| 94 | + } |
| 95 | + if !mock.putCalled || mock.putKey != key || string(mock.putValue) != string(value) { |
| 96 | + t.Errorf("Put did not delegate to mockWrite correctly") |
| 97 | + } |
| 98 | + |
| 99 | + err = ds.Delete(ctx, key) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("Delete failed: %v", err) |
| 102 | + } |
| 103 | + if !mock.deleteCalled || mock.deleteKey != key { |
| 104 | + t.Errorf("Delete did not delegate to mockWrite correctly") |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestDatastore_WithReadContext(t *testing.T) { |
| 109 | + inner := datastore.NewMapDatastore() |
| 110 | + mock := &mockRead{ |
| 111 | + getValue: []byte("baz"), |
| 112 | + hasValue: true, |
| 113 | + getSizeValue: 3, |
| 114 | + } |
| 115 | + ds := contextds.WrapDatastore(inner) |
| 116 | + ctx := contextds.WithRead(context.Background(), mock) |
| 117 | + |
| 118 | + key := datastore.NewKey("foo") |
| 119 | + |
| 120 | + val, err := ds.Get(ctx, key) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("Get failed: %v", err) |
| 123 | + } |
| 124 | + if !mock.getCalled || mock.getKey != key || string(val) != "baz" { |
| 125 | + t.Errorf("Get did not delegate to mockRead correctly") |
| 126 | + } |
| 127 | + |
| 128 | + has, err := ds.Has(ctx, key) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("Has failed: %v", err) |
| 131 | + } |
| 132 | + if !mock.hasCalled || mock.hasKey != key || !has { |
| 133 | + t.Errorf("Has did not delegate to mockRead correctly") |
| 134 | + } |
| 135 | + |
| 136 | + sz, err := ds.GetSize(ctx, key) |
| 137 | + if err != nil { |
| 138 | + t.Fatalf("GetSize failed: %v", err) |
| 139 | + } |
| 140 | + if !mock.getSizeCalled || mock.getSizeKey != key || sz != 3 { |
| 141 | + t.Errorf("GetSize did not delegate to mockRead correctly") |
| 142 | + } |
| 143 | + |
| 144 | + q := query.Query{} |
| 145 | + _, err = ds.Query(ctx, q) |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("Query failed: %v", err) |
| 148 | + } |
| 149 | + if !mock.queryCalled { |
| 150 | + t.Errorf("Query did not delegate to mockRead correctly") |
| 151 | + } |
| 152 | +} |
0 commit comments