Commit 84fed748 authored by Kirill Smelkov's avatar Kirill Smelkov

go/internal/xtesting += ZTestData & friends

ZTestData is Go counterpart of ztestdata on zodbtools/py side from
https://lab.nexedi.com/nexedi/zodbtools/-/blob/f9d36ba7/zodbtools/test/conftest.py#L31-65
that was recently added in nexedi/zodbtools@bf772ce0.

On Go side each ZTestData describes testdata files on the filesystem +
carry arbitrary extra information. For example FileStorage tests will
use it to place there "what is expected to load" information
corresponding to on-filesystem database.
parent 08134c07
// Copyright (C) 2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
package xtesting
// ZTestData + friends
import (
"fmt"
"os"
"path/filepath"
"testing"
"lab.nexedi.com/kirr/neo/go/internal/xmaps"
)
// ZTestData[T] represents one ZODB testdata case.
//
// Test files are located at .Prefix on the filesystem and were generated with
// specified zkind (python major and pickle protocol versions). T represents
// additional information about test data, for example which values to expect
// when loading data from ZODB files.
type ZTestData[T any] struct {
Kind string // e.g. py2_pickle3
Prefix string // e.g. testdata/py2_pickle3/
Misc *T // better embed, but https://github.com/golang/go/issues/49030
}
// ZTestDataRegistry maintains registry of available ZTestData.
//
// Each testdata case needs to be registered via .Register method.
type ZTestDataRegistry[T any] struct {
dir map[string/*zkind*/]*ZTestData[T]
}
// Register adds ZODB testdata case to the registry.
//
// ZODB test files are of specified zkind are located under prefix on the filesystem.
// Additional information about testdata goes in misc.
//
// It is invalid to register the same zkind twice.
func (r *ZTestDataRegistry[T]) Register(zkind, prefix string, misc *T) {
if r.dir == nil {
r.dir = make(map[string]*ZTestData[T])
}
_, already := r.dir[zkind]
if already {
panic(fmt.Sprintf("zkind %q already registered, zkind", zkind))
}
r.dir[zkind] = &ZTestData[T]{Kind: zkind, Prefix: prefix, Misc: misc}
}
// RunWithEach tests f with each registered ztestdata separately.
func (r *ZTestDataRegistry[T]) RunWithEach(t *testing.T, f func(t *testing.T, ztestdata *ZTestData[T])) {
for _, zkind := range xmaps.SortedKeys(r.dir) {
ztestdata := r.dir[zkind]
t.Run(zkind, func(t *testing.T) {
f(t, ztestdata)
})
}
}
// BenchWithEach benchmarks f with each registered ztestdata separately.
func (r *ZTestDataRegistry[T]) BenchWithEach(b *testing.B, f func(b *testing.B, ztestdata *ZTestData[T])) {
for _, zkind := range xmaps.SortedKeys(r.dir) {
ztestdata := r.dir[zkind]
b.Run(zkind, func(b *testing.B) {
f(b, ztestdata)
})
}
}
// Path returns filesystem path for a file located as path under ztestdata prefix.
func (z *ZTestData[T]) Path(path string) string {
return fmt.Sprintf("%s/%s", z.Prefix, path)
}
// LoadZTestData loads ZODB testdata cases from the filesystem.
//
// Every subdirectory under dir counts as one testdata case.
func LoadZTestData(dir string) ZTestDataRegistry[struct{}] {
ztestdataReg := ZTestDataRegistry[struct{}]{}
zprefixv, err := filepath.Glob(dir + "/[^.]*")
if err != nil {
panic(err) // the only possible error is ErrBadPattern
}
for _, zprefix := range zprefixv {
i, err := os.Stat(zprefix)
if err != nil {
continue // same behaviour as glob
}
if !i.IsDir() {
continue
}
zkind := filepath.Base(zprefix)
ztestdataReg.Register(zkind, zprefix, nil)
}
return ztestdataReg
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment