Ejemplo n.º 1
0
    def test_from_json(self):
        json_obj = [{'source': [1, 2, 3], 'target': [4, 5, 6]},
                    {'source': [1, 2, 3], 'target': [4, 5, 6]}]
        file_path = '/path/to/json_file'
        import json  # noqa: F401
        open_patcher = patch('data.open')
        json_load_patcher = patch('json.load', return_value=json_obj)
        open_mock = open_patcher.start()
        json_load_mock = json_load_patcher.start()

        source = Field()
        target = Field()
        examples = Loader.from_json(file_path, {'source': source, 'target': target})

        open_mock.assert_called_once_with(file_path)
        json_load_mock.assert_called_once_with(open_mock(file_path).__enter__())

        self.assertEqual(len(examples['source']), 2)
        self.assertEqual(len(examples['target']), 2)
        self.assertListEqual(examples['source'].data, [[1, 2, 3], [1, 2, 3]])
        self.assertListEqual(examples['target'].data, [[4, 5, 6], [4, 5, 6]])

        open_patcher.stop()
        json_load_patcher.stop()