Skip to content

Commit

Permalink
formated and added kratos_parameter tests to "all" suite
Browse files Browse the repository at this point in the history
  • Loading branch information
roigcarlo committed Feb 22, 2016
1 parent 899f775 commit 2d0655e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 35 deletions.
4 changes: 3 additions & 1 deletion kratos/tests/test_KratosCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -42,7 +43,8 @@ def AssambleTestSuites():
allSuite.addTests(
KratosUnittest.TestLoader().loadTestsFromTestCases([
TModelPartIO,
TModelPart
TModelPart,
TParameters
])
)

Expand Down
98 changes: 64 additions & 34 deletions kratos/tests/test_kratos_parameters.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -28,7 +31,7 @@
}
}"""

pretty_out_after_change ="""{
pretty_out_after_change = """{
"int_value": 10,
"double_value": 2.0,
"bool_value": true,
Expand All @@ -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()
unittest.main()

0 comments on commit 2d0655e

Please sign in to comment.