source: framspy/framsfiles/tests/writer_tests.py @ 1149

Last change on this file since 1149 was 1104, checked in by Maciej Komosinski, 3 years ago

Added the "framsfiles" module for reading and writing Framsticks-format files (genotypes, settings, scrips, neurons, etc.)

File size: 3.1 KB
Line 
1#!/usr/bin/env python3
2import glob
3import json
4import os
5import unittest
6
7from parameterized import parameterized
8
9from ..reader import loads
10from ..writer import from_collection
11
12
13class SerializedTest(unittest.TestCase):
14    def test_keyword_serialization(self):
15        in_obj = [{'f': '@Serialized:'}]
16        expected = 'f:@Serialized:"@Serialized:"\n'
17        actual = from_collection(in_obj)
18        self.assertEqual(actual, expected)
19
20    def test_list_serialization(self):
21        in_obj = [{'f': [16, None, 'abc']}]
22        expected = 'f:@Serialized:[16,null,"abc"]\n'
23        actual = from_collection(in_obj)
24        self.assertEqual(actual, expected)
25
26    def test_object_serialization(self):
27        in_obj = [{'f': {'a': 123, 'b': [7, 8, 9]}}]
28        expected = 'f:@Serialized:{"a":123,"b":[7,8,9]}\n'
29        actual = from_collection(in_obj)
30        self.assertEqual(actual, expected)
31
32    def test_empty_nested_list_serialization(self):
33        in_obj = [{'f': [[[]]]}]
34        expected = 'f:@Serialized:[[[]]]\n'
35        actual = from_collection(in_obj)
36        self.assertEqual(actual, expected)
37
38    def test_list_with_newlines_serialization(self):
39        in_obj = [{'f': ['\n\n', '']}]
40        expected = 'f:@Serialized:["\n\n",""]\n'
41        actual = from_collection(in_obj)
42        self.assertEqual(actual, expected)
43
44
45class ClassnameTest(unittest.TestCase):
46    def test_classname_detection(self):
47        in_obj = [{'_classname': 'stats', 'a': 'b'}]
48        expected = 'stats:\na:b\n'
49        actual = from_collection(in_obj)
50        self.assertEqual(actual, expected)
51
52    def test_multiple_classname_detection(self):
53        in_obj = [{'_classname': 'stats', 'a': 'b'}, {'_classname': 'org', 'c': 'd'}]
54        expected = 'stats:\na:b\n\norg:\nc:d\n'
55        actual = from_collection(in_obj)
56        self.assertEqual(actual, expected)
57
58
59class MultilineTest(unittest.TestCase):
60    def test_tilde_wrap(self):
61        in_obj = [{'ml': 'this is\na field\nwith multiple lines'}]
62        expected = 'ml:~\nthis is\na field\nwith multiple lines~\n'
63        actual = from_collection(in_obj)
64        self.assertEqual(actual, expected)
65
66    def test_tilde_escape(self):
67        in_obj = [{'ml': 'this is\na field\nwith multiple lines\n~ is also here'}]
68        expected = 'ml:~\nthis is\na field\nwith multiple lines\n\\~ is also here~\n'
69        actual = from_collection(in_obj)
70        self.assertEqual(actual, expected)
71
72
73json_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_files", "outputs")
74input_files = glob.glob(os.path.join(json_root, '*', '*'))
75
76
77class WriterReaderTest(unittest.TestCase):
78    @parameterized.expand(input_files)
79    def test_write_then_read(self, filename):
80        with self.subTest(i=filename):
81            with open(filename, encoding='UTF-8') as file:
82                json_content = json.load(file)
83                after_write = from_collection(json_content)
84                after_read = loads(after_write)
85                self.assertEqual(len(json_content), len(after_read))
86                for i, o in zip(json_content, after_read):
87                    self.assertDictEqual(i, o)
88
89
90if __name__ == '__main__':
91    unittest.main()
Note: See TracBrowser for help on using the repository browser.