Commit 66f07e52 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[test][test_format_load] Code cleanup

parent 3c358e74
......@@ -34,8 +34,6 @@
###################################################################################
import os
import six
import numpy
import nose.tools
......@@ -49,31 +47,31 @@ from .utils import cleanup
def test_load_default_format():
df = DataFormat(prefix, data=None)
assert df.valid, "\n * %s" % "\n * ".join(df.errors)
nose.tools.assert_true(df.valid, "\n * %s" % "\n * ".join(df.errors))
def test_load_unknown_format():
df = DataFormat(prefix, "user/unknown/1")
assert df.valid is False
assert df.errors
nose.tools.assert_false(df.valid)
nose.tools.assert_true(df.errors)
nose.tools.eq_(len(df.errors), 1)
assert df.errors[0].find("file not found") != -1
nose.tools.assert_not_equal(df.errors[0].find("file not found"), -1)
def test_load_invalid_format():
df = DataFormat(prefix, "user/invalid/1")
assert df.valid is False
assert df.errors
nose.tools.assert_false(df.valid)
nose.tools.assert_true(df.errors)
nose.tools.eq_(len(df.errors), 1)
assert df.errors[0].find("invalid JSON code") != -1
nose.tools.assert_not_equal(df.errors[0].find("invalid JSON code"), -1)
def test_load_valid_format():
df = DataFormat(prefix, "user/single_integer/1")
assert df.valid, "\n * %s" % "\n * ".join(df.errors)
nose.tools.assert_true(df.valid, "\n * %s" % "\n * ".join(df.errors))
nose.tools.eq_(len(df.errors), 0)
......@@ -81,88 +79,92 @@ def test_load_valid_format():
def test_fail_to_create_data_of_unknown_format():
df = DataFormat(prefix, "user/unknown/1")
assert df.valid is False
assert df.type
nose.tools.assert_false(df.valid)
nose.tools.assert_true(df.type)
def test_fail_to_load_format_with_several_invalid_types():
df = DataFormat(prefix, "user/errors/1")
assert df.valid is False
nose.tools.assert_false(df.valid)
nose.tools.eq_(len(df.errors), 1)
assert df.errors[0].find("is not valid under any of the given schemas") != -1
nose.tools.assert_not_equal(
df.errors[0].find("is not valid under any of the given schemas"), -1
)
def test_load_valid_format_from_JSON_declaration():
df = DataFormat(prefix, dict(value="int32"))
assert df.valid, "\n * %s" % "\n * ".join(df.errors)
assert df.name == "__unnamed_dataformat__"
nose.tools.assert_true(df.valid, "\n * %s" % "\n * ".join(df.errors))
nose.tools.eq_(df.name, "__unnamed_dataformat__")
def test_load_versioned_format():
df1 = DataFormat(prefix, "user/versioned/1")
assert df1.valid, "\n * %s" % "\n * ".join(df1.errors)
nose.tools.assert_true(df1.valid, "\n * %s" % "\n * ".join(df1.errors))
nose.tools.eq_(df1.name, "user/versioned/1")
df2 = DataFormat(prefix, "user/versioned/2")
assert df2.valid, "\n * %s" % "\n * ".join(df2.errors)
nose.tools.assert_true(df2.valid, "\n * %s" % "\n * ".join(df2.errors))
nose.tools.eq_(df2.name, "user/versioned/2")
ftype = df2.type
instance = ftype(value=numpy.float32(32))
assert isinstance(instance.value, numpy.float32)
nose.tools.assert_true(isinstance(instance.value, numpy.float32))
def test_no_description():
df = DataFormat(prefix, "user/versioned/1")
assert df.valid, "\n * %s" % "\n * ".join(df.errors)
assert df.description is None
assert df.documentation is None
nose.tools.assert_true(df.valid, "\n * %s" % "\n * ".join(df.errors))
nose.tools.assert_is_none(df.description)
nose.tools.assert_is_none(df.documentation)
description = "This is my descriptor"
df.description = description
assert isinstance(df.description, six.string_types)
nose.tools.assert_true(isinstance(df.description, six.string_types))
nose.tools.eq_(df.description, description)
def test_with_description():
df = DataFormat(prefix, "user/versioned/2")
assert df.valid, "\n * %s" % "\n * ".join(df.errors)
assert isinstance(df.description, six.string_types)
assert len(df.description) != 0
assert df.documentation is None
nose.tools.assert_true(df.valid, "\n * %s" % "\n * ".join(df.errors))
nose.tools.assert_true(isinstance(df.description, six.string_types))
nose.tools.assert_not_equal(len(df.description), 0)
nose.tools.assert_is_none(df.documentation)
def test_description_does_not_affect_hash():
df2 = DataFormat(prefix, "user/versioned/2")
assert df2.valid, "\n * %s" % "\n * ".join(df2.errors)
nose.tools.assert_true(df2.valid, "\n * %s" % "\n * ".join(df2.errors))
df3 = DataFormat(prefix, "user/versioned/3") # the same, but no description
assert df3.valid, "\n * %s" % "\n * ".join(df3.errors)
assert df2.hash() == df3.hash()
nose.tools.assert_true(df3.valid, "\n * %s" % "\n * ".join(df3.errors))
nose.tools.eq_(df2.hash(), df3.hash())
def test_load_direct_recursion():
df = DataFormat(prefix, "user/direct_recursion/1")
assert df.valid is False
assert df.errors
assert df.errors[0].find("recursion for") != -1
assert df.errors[0].find("user/direct_recursion/1") != -1
nose.tools.assert_false(df.valid)
nose.tools.assert_true(df.errors)
nose.tools.assert_not_equal(df.errors[0].find("recursion for"), -1)
nose.tools.assert_not_equal(df.errors[0].find("user/direct_recursion/1"), -1)
def test_load_indirect_recursion():
df = DataFormat(prefix, "user/indirect_recursion_top/1")
assert df.valid is False
assert df.errors
nose.tools.assert_false(df.valid)
nose.tools.assert_true(df.errors)
nose.tools.eq_(len(df.errors), 1)
assert df.errors[0].find("is invalid") != -1
assert df.errors[0].find("user/indirect_recursion_bottom/1") != -1
nose.tools.assert_not_equal(df.errors[0].find("is invalid"), -1)
nose.tools.assert_not_equal(
df.errors[0].find("user/indirect_recursion_bottom/1"), -1
)
@nose.tools.with_setup(teardown=cleanup)
......@@ -170,10 +172,10 @@ def test_export():
name = "user/composed/1"
obj = DataFormat(prefix, name)
assert obj.valid, "\n * %s" % "\n * ".join(obj.errors)
nose.tools.assert_true(obj.valid, "\n * %s" % "\n * ".join(obj.errors))
obj.export(tmp_prefix)
# load from tmp_prefix and validates
exported = DataFormat(tmp_prefix, name)
assert exported.valid, "\n * %s" % "\n * ".join(exported.errors)
nose.tools.assert_true(exported.valid, "\n * %s" % "\n * ".join(exported.errors))
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