diff --git a/test/json_io/products/test_json_product.py b/test/json_io/products/test_json_product.py index 35694e1..ef880d1 100644 --- a/test/json_io/products/test_json_product.py +++ b/test/json_io/products/test_json_product.py @@ -30,6 +30,7 @@ import FreeCAD import FreeCADGui from json_io.products.json_product import AJsonProduct +from test.json_io.test_json_data import TEST_JSON_PRODUCT_WITHOUT_CHILDREN App = FreeCAD @@ -47,21 +48,7 @@ def tearDown(self): super().tearDown() def test_parse(self): - json_data = """{ - "name": "BasePlateBottom", - "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate", - "posX": 2.0, - "posY": 3.0, - "posZ": 4.0, - "rotX": 0.1, - "rotY": 0.2, - "rotZ": 0.3, - "children": [ - ] - } - """ + json_data = TEST_JSON_PRODUCT_WITHOUT_CHILDREN json_object = json.loads(json_data) json_product = AJsonProduct().parse_from_json(json_object) @@ -72,32 +59,18 @@ def test_parse(self): self.assertEqual(json_product.part_name, "BasePlate", "Property is correctly set") self.assertEqual(json_product.part_uuid, "3d3708fd_5c6c_4af9_b710_d68778466084", "Property is correctly set") - self.assertEqual(json_product.pos_x, 2000, "Property is correctly set") - self.assertEqual(json_product.pos_y, 3000, "Property is correctly set") - self.assertEqual(json_product.pos_z, 4000, "Property is correctly set") + self.assertEqual(json_product.pos_x, 20, "Property is correctly set") + self.assertEqual(json_product.pos_y, 30, "Property is correctly set") + self.assertEqual(json_product.pos_z, 40, "Property is correctly set") - self.assertEqual(json_product.rot_x, 5.729577951308233, "Property is correctly set") - self.assertEqual(json_product.rot_y, 11.459155902616466, "Property is correctly set") - self.assertEqual(json_product.rot_z, 17.188733853924695, "Property is correctly set") + self.assertAlmostEqual(json_product.rot_x, 20, 5, "Property is correctly set") + self.assertAlmostEqual(json_product.rot_y, 40, 5, "Property is correctly set") + self.assertAlmostEqual(json_product.rot_z, 60, 5, "Property is correctly set") self.assertFalse(json_product.has_children, "The defined product has an empty list of children") def test_get_unique_names(self): - json_data = """{ - "name": "BasePlateBottom", - "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate", - "posX": 2.0, - "posY": 3.0, - "posZ": 4.0, - "rotX": 0.1, - "rotY": 0.2, - "rotZ": 0.3, - "children": [ - ] - } - """ + json_data = TEST_JSON_PRODUCT_WITHOUT_CHILDREN json_object = json.loads(json_data) json_product = AJsonProduct().parse_from_json(json_object) @@ -125,21 +98,8 @@ def test_is_part_reference(self): self.assertFalse(json_product.is_part_reference(), "The current product does not reference a part") - json_data = """{ - "name": "BasePlateBottom", - "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate", - "posX": 2.0, - "posY": 3.0, - "posZ": 4.0, - "rotX": 0.1, - "rotY": 0.2, - "rotZ": 0.3, - "children": [ - ] - } - """ + json_data = TEST_JSON_PRODUCT_WITHOUT_CHILDREN + json_object = json.loads(json_data) json_product = AJsonProduct().parse_from_json(json_object) diff --git a/test/json_io/products/test_json_product_assembly.py b/test/json_io/products/test_json_product_assembly.py index 9f76dfe..4fcee99 100644 --- a/test/json_io/products/test_json_product_assembly.py +++ b/test/json_io/products/test_json_product_assembly.py @@ -31,6 +31,8 @@ import FreeCADGui from json_io.products.json_product_assembly import JsonProductAssembly from freecad.active_document import ActiveDocument +from test.json_io.test_json_data import TEST_JSON_PRODUCT_WITH_CHILDREN,\ + TEST_JSON_PRODUCT_WITHOUT_CHILDREN App = FreeCAD @@ -39,49 +41,7 @@ class TestJsonProductAssembly(AWorkingDirectoryTest): - json_data = """{ - "name": "BasePlateBottom1", - "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate", - "posX": 10.0, - "posY": 15.0, - "posZ": 20.0, - "rotX": 0.349, - "rotY": 0.698, - "rotZ": 1.046, - "children": [ - { - "posX": 0.0, - "posY": 0.0, - "posZ": 0.0, - "rotX": 0.0, - "children": [ - ], - "rotZ": 0.0, - "rotY": 0.0, - "name": "BasePlateBottom2", - "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate" - }, - { - "posX": 0.0, - "posY": 0.0, - "posZ": 0.5, - "rotX": 0.0, - "children": [ - ], - "rotZ": 0.0, - "rotY": 0.0, - "name": "BasePlateTop", - "uuid": "a199e3bd-3bc1-426d-8321-e9bd829339b3", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate" - } - ] - } - """ + json_data = TEST_JSON_PRODUCT_WITH_CHILDREN @classmethod def setUpClass(cls): @@ -120,21 +80,7 @@ def test_parse_with_children(self): self.assertEqual(json_product_child_2.name, "BasePlateTop", "Parsed correct child") def test_parse_with_no_children(self): - json_data = """{ - "name": "BasePlateBottom", - "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate", - "posX": 2.0, - "posY": 3.0, - "posZ": 4.0, - "rotX": 0.1, - "rotY": 0.2, - "rotZ": 0.3, - "children": [ - ] - } - """ + json_data = TEST_JSON_PRODUCT_WITHOUT_CHILDREN json_object = json.loads(json_data) json_product = JsonProductAssembly().parse_from_json(json_object) diff --git a/test/json_io/products/test_json_product_child.py b/test/json_io/products/test_json_product_child.py index 4c0d465..7be5aed 100644 --- a/test/json_io/products/test_json_product_child.py +++ b/test/json_io/products/test_json_product_child.py @@ -32,6 +32,8 @@ from json_io.products.json_product_child import JsonProductChild from freecad.active_document import ActiveDocument from json_io.json_definitions import get_product_name_uuid +from test.json_io.test_json_data import TEST_JSON_PRODUCT_WITHOUT_CHILDREN,\ + TEST_JSON_PRODUCT_WITH_ONE_CHILD App = FreeCAD @@ -40,51 +42,8 @@ class TestJsonProductChild(AWorkingDirectoryTest): - json_data = """{ - "name": "BasePlateBottom", - "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate", - "posX": 0.02, - "posY": 0.03, - "posZ": 0.04, - "rotX": 0.3490659, - "rotY": 0.6981317, - "rotZ": 1.0471976, - "children": [ - ] - } - """ - - json_data_with_child = """{ - "name": "BasePlateBottom", - "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate", - "posX": 0.02, - "posY": 0.03, - "posZ": 0.04, - "rotX": 0.3490659, - "rotY": 0.6981317, - "rotZ": 1.0471976, - "children": [ - { - "posX": 0.0, - "posY": 0.0, - "posZ": 0.0, - "rotX": 0.0, - "children": [ - ], - "rotZ": 0.0, - "rotY": 0.0, - "name": "BasePlateBottom", - "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", - "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", - "partName": "BasePlate" - } - ] - } - """ + json_data = TEST_JSON_PRODUCT_WITHOUT_CHILDREN + json_data_with_child = TEST_JSON_PRODUCT_WITH_ONE_CHILD @classmethod def setUpClass(cls): diff --git a/test/json_io/test_json_data.py b/test/json_io/test_json_data.py index 58c6dc2..22c4375 100644 --- a/test/json_io/test_json_data.py +++ b/test/json_io/test_json_data.py @@ -25,69 +25,159 @@ # TEST_JSON_PART_BOX = """{ - "color": 12632256, - "shape": "BOX", - "name": "Beam", - "lengthX": 0.04, - "lengthY": 0.02, - "lengthZ": 0.01, - "radius": 0.0, - "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022" - }""" + "color": 12632256, + "shape": "BOX", + "name": "Beam", + "lengthX": 0.04, + "lengthY": 0.02, + "lengthZ": 0.01, + "radius": 0.0, + "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022" + }""" TEST_JSON_PART_CONE = """{ - "name": "Beam", - "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022", - "shape": "CONE", - "lengthX": 0.0, - "lengthY": 0.5, - "lengthZ": 0.0, - "radius": 0.2, - "color": 12632256 - }""" + "name": "Beam", + "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022", + "shape": "CONE", + "lengthX": 0.0, + "lengthY": 0.5, + "lengthZ": 0.0, + "radius": 0.2, + "color": 12632256 + }""" TEST_JSON_PART_CYLINDER = """{ - "color": 12632256, - "shape": "CYLINDER", - "name": "Tube", - "lengthX": 0.0, - "lengthY": 0.1, - "lengthZ": 0.0, - "radius": 0.05, - "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022" - }""" + "color": 12632256, + "shape": "CYLINDER", + "name": "Tube", + "lengthX": 0.0, + "lengthY": 0.1, + "lengthZ": 0.0, + "radius": 0.05, + "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022" + }""" TEST_JSON_PART_SPHERE = """{ - "color": 12632256, - "shape": "SPHERE", - "name": "Tube", - "lengthX": 0.0, - "lengthY": 0.0, - "lengthZ": 0.0, - "radius": 0.003, - "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022" - }""" + "color": 12632256, + "shape": "SPHERE", + "name": "Tube", + "lengthX": 0.0, + "lengthY": 0.0, + "lengthZ": 0.0, + "radius": 0.003, + "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022" + }""" TEST_JSON_PART_GEOMETRY = """{ - "color": 66280, - "shape": "GEOMETRY", - "name": "Geometry", - "lengthY": 1.0, - "lengthX": 0.0, - "radius": 1.0, - "lengthZ": 0.0, - "uuid": "38eae3a5-8338-4a51-b1df-5583058f9e77", - "stlPath": "Test.stl" - }""" + "color": 66280, + "shape": "GEOMETRY", + "name": "Geometry", + "lengthY": 1.0, + "lengthX": 0.0, + "radius": 1.0, + "lengthZ": 0.0, + "uuid": "38eae3a5-8338-4a51-b1df-5583058f9e77", + "stlPath": "Test.stl" + }""" TEST_JSON_PART_NONE = """{ - "name": "Beam", - "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022", - "shape": "NONE", - "lengthX": 0.04, - "lengthY": 0.01, - "lengthZ": 0.3, - "radius": 0.0, - "color": 12632256, - "stlPath" : "testfile.stl" - }""" + "name": "Beam", + "uuid": "6201a731-d703-43f8-ab37-6a0581dfe022", + "shape": "NONE", + "lengthX": 0.04, + "lengthY": 0.01, + "lengthZ": 0.3, + "radius": 0.0, + "color": 12632256, + "stlPath" : "testfile.stl" + }""" + +TEST_JSON_PRODUCT_WITH_CHILDREN = """{ + "name": "BasePlateBottom1", + "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", + "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", + "partName": "BasePlate", + "posX": 10.0, + "posY": 15.0, + "posZ": 20.0, + "rotX": 0.349, + "rotY": 0.698, + "rotZ": 1.046, + "children": [ + { + "posX": 0.0, + "posY": 0.0, + "posZ": 0.0, + "rotX": 0.0, + "children": [ + ], + "rotZ": 0.0, + "rotY": 0.0, + "name": "BasePlateBottom2", + "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", + "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", + "partName": "BasePlate" + }, + { + "posX": 0.0, + "posY": 0.0, + "posZ": 0.5, + "rotX": 0.0, + "children": [ + ], + "rotZ": 0.0, + "rotY": 0.0, + "name": "BasePlateTop", + "uuid": "a199e3bd-3bc1-426d-8321-e9bd829339b3", + "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", + "partName": "BasePlate" + } + ] + } + """ + +TEST_JSON_PRODUCT_WITHOUT_CHILDREN = """{ + "name": "BasePlateBottom", + "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", + "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", + "partName": "BasePlate", + "posX": 0.02, + "posY": 0.03, + "posZ": 0.04, + "rotX": 0.3490659, + "rotY": 0.6981317, + "rotZ": 1.0471976, + "children": [ + ] + } + """ + +TEST_JSON_PRODUCT_WITH_ONE_CHILD = """{ + "name": "BasePlateBottom", + "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", + "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", + "partName": "BasePlate", + "posX": 0.02, + "posY": 0.03, + "posZ": 0.04, + "rotX": 0.3490659, + "rotY": 0.6981317, + "rotZ": 1.0471976, + "children": [ + { + "posX": 0.0, + "posY": 0.0, + "posZ": 0.0, + "rotX": 0.0, + "children": [ + ], + "rotZ": 0.0, + "rotY": 0.0, + "name": "BasePlateBottom", + "uuid": "e8794f3d-86ec-44c5-9618-8b7170c45484", + "partUuid": "3d3708fd-5c6c-4af9-b710-d68778466084", + "partName": "BasePlate" + } + ] + } + """