package main import ( "context" "errors" "io" "testing" "time" ) func TestDriverRAM(t *testing.T) { testDriver(t, NewRAM()) } func TestFillTestdata(t *testing.T) { ctx, can := context.WithTimeout(context.Background(), time.Second*15) defer can() ram := NewRAM() if err := FillWithTestdata(ctx, ram); err != nil { t.Fatal(err) } n := 0 if err := ram.ForEach(context.Background(), "m", func(_ string, _ []byte) error { n += 1 return nil }); err != nil { t.Fatal(err) } t.Log(n) } func TestDriverBBolt(t *testing.T) { testDriver(t, NewTestDBIn(t.TempDir())) } func testDriver(t *testing.T, d Driver) { ctx, can := context.WithTimeout(context.Background(), time.Second*15) defer can() defer d.Close() if b, err := d.Get(ctx, "m", "id"); err != nil { t.Error("cannot get from empty:", err) } else if b != nil { t.Error("got fake from empty") } if err := d.ForEach(ctx, "m", func(string, []byte) error { return errors.New("should have no hits") }); err != nil { t.Error("failed to forEach empty:", err) } if err := d.Set(ctx, "m", "id", []byte(`"hello world"`)); err != nil { t.Error("cannot set from empty:", err) } if b, err := d.Get(ctx, "m", "id"); err != nil { t.Error("cannot get from full:", err) } else if string(b) != `"hello world"` { t.Error("got fake from full") } if err := d.ForEach(ctx, "m", func(id string, v []byte) error { if id != "id" { t.Error("for each id weird:", id) } if string(v) != `"hello world"` { t.Error("for each value weird:", string(v)) } return io.EOF }); err != io.EOF { t.Error("failed to forEach full:", err) } if err := d.Set(ctx, "m", "id", nil); err != nil { t.Error("cannot set from full:", err) } if err := d.ForEach(ctx, "m", func(string, []byte) error { return errors.New("should have no hits") }); err != nil { t.Error("failed to forEach empty:", err) } if b, err := d.Get(ctx, "m", "id"); err != nil { t.Error("cannot get from deleted:", err) } else if b != nil { t.Error("got fake from deleted") } }