Commit 3529459c authored by Kirill Smelkov's avatar Kirill Smelkov

X PyData.ClassName() tested

parent b242e07f
......@@ -20,15 +20,50 @@
# See https://www.nexedi.com/licensing for rationale and options.
"""generate reference pickle objects encoding for tests"""
from ZODB.tests.testSerialize import SerializerTestCase as t
from ZODB.tests import testSerialize
from ZODB import serialize
# escape string into valid "..." string
# XXX dup in fs1/py/pyserialize-gen-testdata
def escapeqq(s):
outv = []
# we don't want ' to be escaped
for _ in s.split("'"):
# this escape almost everything except " character
# NOTE string_escape does not do smartquotes and always uses ' for quoting
# (repr(str) is the same except it does smartquoting picking ' or " automatically)
q = _.encode("string_escape")
q = q.replace('"', r'\"')
outv.append(q)
return '"' + "'".join(outv) + '"'
def main():
# dump to go what to expect
with open("ztestdata_pyserialize.go", "w") as f:
with open("ztestdata_pyserialize_test.go", "w") as f:
def emit(v):
print >>f, v
emit("// Code generated by %s; DO NOT EDIT." % __file__)
emit("%r" % ft.old_style_without_newargs)
emit("package zodb")
# [] of pickle
t = testSerialize.SerializerTestCase
testv = [
t.old_style_without_newargs,
t.old_style_with_newargs,
t.new_style_without_newargs,
t.new_style_with_newargs,
]
r = serialize.ObjectReader(factory=testSerialize._factory)
emit("\nvar _PyData_ClassName_Testv = [...]_PyDataClassName_TestEntry{")
for test in testv:
emit("\t{")
emit("\t\t%s," % escapeqq(test))
emit("\t\t%s," % escapeqq(r.getClassName(test)))
emit("\t},")
emit('\t{"aaa", "?.?"},') # invalid
emit("}")
if __name__ == '__main__':
main()
......@@ -37,11 +37,10 @@ import (
// for format description.
type PyData []byte
// ClassString returns fully-qualified python class name used for object type.
// ClassName returns fully-qualified python class name used for object type.
// The format is "module.class".
// If pickle decoding fails "?.?" is returned.
// TODO tests
func (d PyData) ClassString() string {
func (d PyData) ClassName() string {
// see ObjectReader.getClassName & get_pickle_metadata in zodb/py
p := pickle.NewDecoder(bytes.NewReader([]byte(d)))
xklass, err := p.Decode()
......
......@@ -25,5 +25,17 @@ import (
"testing"
)
func TestPyClassString(t *testing.T) {
type _PyDataClassName_TestEntry struct {
pydata string
className string
}
func TestPyClassName(t *testing.T) {
for _, tt := range _PyData_ClassName_Testv {
className := PyData(tt.pydata).ClassName()
if className != tt.className {
t.Errorf("class name for %q:\nhave: %q\nwant: %q",
tt.pydata, className, tt.className)
}
}
}
......@@ -188,7 +188,7 @@ func (d *DumperFsDump) DumpTxn(buf *xfmt.Buffer, it *fs1.Iter) error {
if data == nil {
buf .S(" class=undo or abort of object creation")
} else {
fullclass := zodb.PyData(data).ClassString()
fullclass := zodb.PyData(data).ClassName()
buf .S(" size=") .D64(d.dhLoading.DataLen)
buf .S(" class=") .S(fullclass)
......
......@@ -45,6 +45,7 @@ def hex64(packed):
return '0x%016x' % unpack64(packed)
# escape string into valid "..." string
# XXX dup in zodb/py/pyserialize-gen-testdata
def escapeqq(s):
outv = []
# we don't want ' to be escaped
......@@ -197,7 +198,7 @@ def main():
with open("ztestdata_expect_test.go", "w") as f:
def emit(v):
print >>f, v
emit("// Code generated by py/gen-testdata; DO NOT EDIT.")
emit("// Code generated by %s; DO NOT EDIT." % __name__)
emit("package fs1\n")
emit("import \"lab.nexedi.com/kirr/neo/go/zodb\"\n")
......
// Code generated by ./py/pyserialize-gen-testdata; DO NOT EDIT.
package zodb
var _PyData_ClassName_Testv = [...]_PyDataClassName_TestEntry{
{
"((U\x18ZODB.tests.testSerializeq\x01U\x13ClassWithoutNewargsq\x02tNtq\x03.",
"ZODB.tests.testSerialize.ClassWithoutNewargs",
},
{
"((U\x18ZODB.tests.testSerializeq\x01U\x10ClassWithNewargsq\x02t(K\x01tq\x03tq\x04.",
"ZODB.tests.testSerialize.ClassWithNewargs",
},
{
"cZODB.tests.testSerialize\nClassWithoutNewargs\nq\x01.",
"ZODB.tests.testSerialize.ClassWithoutNewargs",
},
{
"(cZODB.tests.testSerialize\nClassWithNewargs\nq\x01(K\x01tq\x02tq\x03.",
"ZODB.tests.testSerialize.ClassWithNewargs",
},
{"aaa", "?.?"},
}
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