From 0c5014aaef854bba096bdaab7d6807fcfd45d7d0 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Fri, 14 Feb 2020 11:02:22 +0100 Subject: Cleanup: Deduplicate some code in new blenfile io/linking tests. --- tests/python/CMakeLists.txt | 1 + tests/python/bl_blendfile_io.py | 45 ++++++++---------------------------- tests/python/bl_blendfile_liblink.py | 37 +++++------------------------ tests/python/bl_blendfile_utils.py | 35 ++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 66 deletions(-) create mode 100644 tests/python/bl_blendfile_utils.py (limited to 'tests/python') diff --git a/tests/python/CMakeLists.txt b/tests/python/CMakeLists.txt index cf4438a8544..5a3eeb6d14e 100644 --- a/tests/python/CMakeLists.txt +++ b/tests/python/CMakeLists.txt @@ -28,6 +28,7 @@ set(TEST_OUT_DIR ${CMAKE_BINARY_DIR}/tests) # ugh, any better way to do this on testing only? file(MAKE_DIRECTORY ${TEST_OUT_DIR}) file(MAKE_DIRECTORY ${TEST_OUT_DIR}/io_tests) +file(MAKE_DIRECTORY ${TEST_OUT_DIR}/blendfile_io) # if(NOT IS_DIRECTORY ${TEST_SRC_DIR}) # message(FATAL_ERROR "CMake test directory not found!") diff --git a/tests/python/bl_blendfile_io.py b/tests/python/bl_blendfile_io.py index 493b16b4ab3..0b055b9d46a 100644 --- a/tests/python/bl_blendfile_io.py +++ b/tests/python/bl_blendfile_io.py @@ -3,35 +3,10 @@ # ./blender.bin --background -noaudio --python tests/python/bl_blendfile_io.py import bpy import os -import pprint +import sys - -class TestHelper: - - @staticmethod - def id_to_uid(id_data): - return (type(id_data).__name__, - id_data.name, - id_data.users, - id_data.library.filepath if id_data.library else None) - - @classmethod - def blender_data_to_tuple(cls, bdata): - return sorted(tuple((cls.id_to_uid(k), sorted(tuple(cls.id_to_uid(vv) for vv in v))) - for k, v in bdata.user_map().items())) - - @staticmethod - def ensure_path(path): - if not os.path.exists(path): - os.makedirs(path) - - def run_all_tests(self): - for inst_attr_id in dir(self): - if not inst_attr_id.startswith("test_"): - continue - inst_attr = getattr(self, inst_attr_id) - if callable(inst_attr): - inst_attr() +sys.path.append(os.path.dirname(os.path.realpath(__file__))) +from bl_blendfile_utils import TestHelper class TestBlendFileSaveLoadBasic(TestHelper): @@ -47,25 +22,25 @@ class TestBlendFileSaveLoadBasic(TestHelper): self.ensure_path(output_dir) output_path = os.path.join(output_dir, "blendfile.blend") - orig_data = self.blender_data_to_tuple(bpy.data) + orig_data = self.blender_data_to_tuple(bpy.data, "orig_data 1") bpy.ops.wm.save_as_mainfile(filepath=output_path, check_existing=False, compress=False) bpy.ops.wm.open_mainfile(filepath=output_path, load_ui=False) - read_data = self.blender_data_to_tuple(bpy.data) - + read_data = self.blender_data_to_tuple(bpy.data, "read_data 1") + # We have orphaned data, which should be removed by file reading, so there should not be equality here. assert(orig_data != read_data) bpy.data.orphans_purge() - - orig_data = self.blender_data_to_tuple(bpy.data) + + orig_data = self.blender_data_to_tuple(bpy.data, "orig_data 2") bpy.ops.wm.save_as_mainfile(filepath=output_path, check_existing=False, compress=False) bpy.ops.wm.open_mainfile(filepath=output_path, load_ui=False) - read_data = self.blender_data_to_tuple(bpy.data) - + read_data = self.blender_data_to_tuple(bpy.data, "read_data 2") + assert(orig_data == read_data) diff --git a/tests/python/bl_blendfile_liblink.py b/tests/python/bl_blendfile_liblink.py index 51dd989ece1..7d93d7c8455 100644 --- a/tests/python/bl_blendfile_liblink.py +++ b/tests/python/bl_blendfile_liblink.py @@ -3,35 +3,10 @@ # ./blender.bin --background -noaudio --python tests/python/bl_blendfile_liblink.py import bpy import os -import pprint +import sys - -class TestHelper: - - @staticmethod - def id_to_uid(id_data): - return (type(id_data).__name__, - id_data.name, - id_data.users, - id_data.library.filepath if id_data.library else None) - - @classmethod - def blender_data_to_tuple(cls, bdata): - return sorted(tuple((cls.id_to_uid(k), sorted(tuple(cls.id_to_uid(vv) for vv in v))) - for k, v in bdata.user_map().items())) - - @staticmethod - def ensure_path(path): - if not os.path.exists(path): - os.makedirs(path) - - def run_all_tests(self): - for inst_attr_id in dir(self): - if not inst_attr_id.startswith("test_"): - continue - inst_attr = getattr(self, inst_attr_id) - if callable(inst_attr): - inst_attr() +sys.path.append(os.path.dirname(os.path.realpath(__file__))) +from bl_blendfile_utils import TestHelper class TestBlendLibLinkSaveLoadBasic(TestHelper): @@ -56,14 +31,14 @@ class TestBlendLibLinkSaveLoadBasic(TestHelper): link_dir = os.path.join(output_path, "Mesh") bpy.ops.wm.link(directory=link_dir, filename="LibMesh") - orig_data = self.blender_data_to_tuple(bpy.data) + orig_data = self.blender_data_to_tuple(bpy.data, "orig_data") output_path = os.path.join(output_dir, "blendfile.blend") bpy.ops.wm.save_as_mainfile(filepath=output_path, check_existing=False, compress=False) bpy.ops.wm.open_mainfile(filepath=output_path, load_ui=False) - read_data = self.blender_data_to_tuple(bpy.data) - + read_data = self.blender_data_to_tuple(bpy.data, "read_data") + assert(orig_data == read_data) diff --git a/tests/python/bl_blendfile_utils.py b/tests/python/bl_blendfile_utils.py new file mode 100644 index 00000000000..48e24cd0684 --- /dev/null +++ b/tests/python/bl_blendfile_utils.py @@ -0,0 +1,35 @@ +# Apache License, Version 2.0 + +import os +import pprint + + +class TestHelper: + + @staticmethod + def id_to_uid(id_data): + return (type(id_data).__name__, + id_data.name_full, + id_data.users) + + @classmethod + def blender_data_to_tuple(cls, bdata, pprint_name=None): + ret = sorted(tuple((cls.id_to_uid(k), sorted(tuple(cls.id_to_uid(vv) for vv in v))) + for k, v in bdata.user_map().items())) + if pprint_name is not None: + print("\n%s:" % pprint_name) + pprint.pprint(ret) + return ret + + @staticmethod + def ensure_path(path): + if not os.path.exists(path): + os.makedirs(path) + + def run_all_tests(self): + for inst_attr_id in dir(self): + if not inst_attr_id.startswith("test_"): + continue + inst_attr = getattr(self, inst_attr_id) + if callable(inst_attr): + inst_attr() -- cgit v1.2.3