Commit 11f1a2bd authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodb: Add tests for Map and List

As promised in 6c162438 (go/zodb: Draft support for Map and List) add
test that Map and List implemented on Go side can load PersistentMapping
and PersistentList data generated by Python.

Test that Py side can load Map and List data generated by Go is still pending.

Testdata is still generated with zodbtools f9d36ba7~ i.e. before
nexedi/zodbtools!29 was merged.
parent dc844465
// Copyright (C) 2019-2021 Nexedi SA and Contributors.
// Copyright (C) 2019-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
......@@ -20,6 +20,8 @@
package zodb
import (
"testing"
"lab.nexedi.com/kirr/go123/mem"
)
......@@ -38,3 +40,13 @@ type ZRawObject struct { // keep in sync with xtesting.ZRawObject
func PSerialize(obj IPersistent) *mem.Buf {
return obj.persistent().pSerialize()
}
// OpenTestDB opens zurl via tDB.
func OpenTestDB(t0 *testing.T, zurl string) *tDB {
// reuse tDB repointing its .zurl to requested zurl
// TODO instruct tDB not to create/populate its own temporary database first.
tdb := testdb(t0, /*rawcache=*/false)
tdb.zurl = zurl
tdb.Reopen()
return tdb
}
// Copyright (C) 2018-2021 Nexedi SA and Contributors.
// Copyright (C) 2018-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
......@@ -361,14 +361,11 @@ func (t *tDB) Open(opt *ConnOptions) *tConnection {
}
}
// Get gets oid from t.conn and asserts its type.
// Get gets oid from t.conn and asserts it to be MyObject.
func (t *tConnection) Get(oid Oid) *MyObject {
t.Helper()
xobj, err := t.conn.Get(t.ctx, oid)
if err != nil {
t.Fatal(err)
}
xobj := t.GetAny(oid)
zclass := ClassOf(xobj)
zmy := "t.zodb.MyObject"
if zclass != zmy {
......@@ -378,6 +375,17 @@ func (t *tConnection) Get(oid Oid) *MyObject {
return xobj.(*MyObject)
}
// GetAny gets oid from t.conn.
func (t *tConnection) GetAny(oid Oid) IPersistent {
t.Helper()
xobj, err := t.conn.Get(t.ctx, oid)
if err != nil {
t.Fatal(err)
}
return xobj
}
// PActivate activates obj in t environment.
func (t *tConnection) PActivate(obj IPersistent) {
t.Helper()
......@@ -910,8 +918,6 @@ func TestLiveCache(t0 *testing.T) {
// TODO reclassify tests
}
// TODO Map & List tests.
// TODO PyGetState vs PySetState tests (general - for any type):
//
......
// 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 zodb_test
import (
"testing"
"lab.nexedi.com/kirr/neo/go/zodb"
pickle "github.com/kisielk/og-rek"
assert "github.com/stretchr/testify/require"
)
// Verify that Map and List can load from data saved by py.
// TODO go saves Map/List -> py loads and checks.
func TestPersistentMapListLoad(t0 *testing.T) {
assert := assert.New(t0)
tdb := zodb.OpenTestDB(t0, "testdata/data.fs")
defer tdb.Close()
t := tdb.Open(&zodb.ConnOptions{})
xroot := t.GetAny(0)
root, ok := xroot.(*zodb.Map)
if !ok {
t.Fatalf("root: got %T ; expect Map", xroot)
}
t.PActivate(root)
defer root.PDeactivate()
// see py/pydata-gen-testdata
assert.Equal(len(root.Data), 3)
assert.Equal(root.Data["int1"], int64(1))
xabc := root.Data["strABC"]
abc, ok := xabc.(string)
if !ok {
t.Fatalf("root[strABC]: got %T ; expect str", xabc)
}
assert.Equal(abc, "abc")
xplist := root.Data["plist"]
plist, ok := xplist.(*zodb.List)
if !ok {
t.Fatalf("plist: got %T ; expect List", xplist)
}
t.PActivate(plist)
defer plist.PDeactivate()
assert.Equal(len(plist.Data), 3)
xa := plist.Data[0]
a, ok := xa.(string)
if !ok {
t.Fatalf("plist[0]: got %T ; expect str", xa)
}
assert.Equal(a, "a")
assert.Equal(plist.Data[1], int64(1))
assert.Equal(plist.Data[2], pickle.None{})
}
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Copyright (C) 2017-2021 Nexedi SA and Contributors.
# Copyright (C) 2017-2024 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
......@@ -18,13 +18,41 @@
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
"""generate reference pickle objects encoding for tests"""
"""generate reference database and pickle objects encoding for tests"""
from ZODB import serialize
from ZODB import DB, serialize
from ZODB.utils import z64
from persistent.mapping import PersistentMapping
from persistent.list import PersistentList
import transaction
from zodbtools.test.gen_testdata import run_with_zodb4py2_compat
from golang.gcompat import qq
from os import remove
from os.path import exists
def main2():
# gen_ztestdata generates test databases that ZODB/go will try to load.
def gen_ztestdata():
outfs = "testdata/data.fs"
if exists(outfs):
remove(outfs)
remove(outfs+".index")
db = DB(outfs)
conn = db.open()
root = conn.root()
assert type(root) is PersistentMapping
assert root._p_oid == z64
# NOTE keep in sync with TestPersistentMapListLoad
root['int1'] = 1
root['strABC'] = 'abc'
root['plist'] = PersistentList(["a", 1, None])
transaction.commit()
# gen_test_pydata generates testdata for PyData serialization tests.
def gen_test_pydata():
# import ZODB.tests at runtime after ZODB.X._protocol is patched
from ZODB.tests import testSerialize
......@@ -56,7 +84,8 @@ def main2():
emit("}")
def main():
run_with_zodb4py2_compat(main2)
run_with_zodb4py2_compat(gen_ztestdata)
run_with_zodb4py2_compat(gen_test_pydata)
if __name__ == '__main__':
main()
*.lock
*.tmp
*.tr[0-9]
*.old
K.N.
\ No newline at end of file
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