diff --git a/kratos/tests/test_KratosCore.py b/kratos/tests/test_KratosCore.py index db29ce25a651..0c44c29e8c78 100644 --- a/kratos/tests/test_KratosCore.py +++ b/kratos/tests/test_KratosCore.py @@ -7,6 +7,7 @@ import KratosMultiphysics.KratosUnittest as KratosUnittest # Import the tests o test_classes to create the suites +from test_kratos_parameters import TestKratosParameters as TParameters from test_model_part_io import TestModelPartIO as TModelPartIO from test_model_part import TestModelPart as TModelPart @@ -42,7 +43,8 @@ def AssambleTestSuites(): allSuite.addTests( KratosUnittest.TestLoader().loadTestsFromTestCases([ TModelPartIO, - TModelPart + TModelPart, + TParameters ]) ) diff --git a/kratos/tests/test_kratos_parameters.py b/kratos/tests/test_kratos_parameters.py index f687045df3bc..d775375f6f8a 100644 --- a/kratos/tests/test_kratos_parameters.py +++ b/kratos/tests/test_kratos_parameters.py @@ -1,11 +1,14 @@ -import unittest +from __future__ import print_function, absolute_import, division + from KratosMultiphysics import * +import KratosMultiphysics.KratosUnittest as KratosUnittest + -##input string with ugly formatting +# input string with ugly formatting json_string = """ -{ +{ "int_value" : 10, "double_value": 2.0, "bool_value" : true, "string_value" : "hello", - "level1": + "level1": { "list_value":[ 3, "hi", false], "tmp" : 5.0 @@ -28,7 +31,7 @@ } }""" -pretty_out_after_change ="""{ +pretty_out_after_change = """{ "int_value": 10, "double_value": 2.0, "bool_value": true, @@ -44,39 +47,66 @@ }""" -class TestKratosParameters(unittest.TestCase): +class TestKratosParameters(KratosUnittest.TestCase): + + def setUp(self): + self.kp = KratosParameters(json_string) + self.compact_expected_output = """{ + "int_value":10, + "double_value":2.0, + "bool_value":true, + "string_value":"hello", + "level1":{ + "list_value":[3,"hi",false], + "tmp":5.0 + } + }""" def test_kratos_parameters(self): - - kp = KratosParameters(json_string) - - compact_expected_output = """{"int_value":10,"double_value":2.0,"bool_value":true,"string_value":"hello","level1":{"list_value":[3,"hi",false],"tmp":5.0}}""" - self.assertEqual(kp.WriteJsonString(), compact_expected_output) - - self.assertTrue(kp.Has("int_value")) - self.assertFalse(kp.Has("unextisting_value")) - - self.assertEqual(kp.GetValue("int_value").GetInt(),10) - self.assertEqual(kp.GetValue("double_value").GetDouble(),2.0) - self.assertEqual(kp.GetValue("bool_value").GetBool(),True) - self.assertEqual(kp.GetValue("string_value").GetString(),"hello") - - self.assertEqual(kp.PrettyPrintJsonString(), pretty_out) - - #now change one item in the sublist - subparams = kp.GetValue("level1") + self.assertEqual( + self.kp.WriteJsonString(), + self.compact_expected_output + ) + + self.assertTrue(self.kp.Has("int_value")) + self.assertFalse(self.kp.Has("unextisting_value")) + + self.assertEqual(self.kp.GetValue("int_value").GetInt(), 10) + self.assertEqual(self.kp.GetValue("double_value").GetDouble(), 2.0) + self.assertEqual(self.kp.GetValue("bool_value").GetBool(), True) + self.assertEqual(self.kp.GetValue("string_value").GetString(), "hello") + + self.assertEqual(self.kp.PrettyPrintJsonString(), pretty_out) + + def test_kratos_change_parameters(self): + # now change one item in the sublist + subparams = self.kp.GetValue("level1") + my_list = subparams.GetValue("list_value") my_list.GetArrayItem(0).SetString("changed") - self.assertEqual(kp.PrettyPrintJsonString(), pretty_out_after_change) - - #try to make a copy - other_copy = KratosParameters(kp) - self.assertEqual(other_copy.PrettyPrintJsonString(), pretty_out_after_change) + + self.assertEqual( + self.kp.PrettyPrintJsonString(), + pretty_out_after_change + ) + + def test_kratos_copy_parameters(self): + # try to make a copy + other_copy = KratosParameters(self.kp) + + self.assertEqual( + other_copy.PrettyPrintJsonString(), + pretty_out_after_change + ) + other_copy.GetValue("int_value").SetInt(-1) - self.assertEqual(kp.GetValue("int_value").GetInt(),10) - #self.assertEqual(other_copy.GetValue("int_value").GetString(),-1) - - #should check which errors are thrown!! + self.assertEqual(self.kp.GetValue("int_value").GetInt(), 10) + # self.assertEqual(other_copy.GetValue("int_value").GetString(),-1) + + def test_kratos_wrong_parameters(self): + # should check which errors are thrown!! + with self.assertRaisesRegex(RuntimeError, "kratos"): + self.kp.GetValue("no_value").GetInt() if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()