def test_to_from_json(self):
     household_model, _ = self._mock_household_collection()
     household_string = household_model.to_json()
     household_model_new = BayesianNetworkModel.from_json(household_string)
     self.assertSequenceEqual(household_model.fields,
                              household_model_new.fields)
     self._check_household_generate(household_model_new)
    def test_read_write(self):
        household_model, _ = self._mock_household_collection()

        def _check_network(household_model_new):
            self.assertSequenceEqual(household_model.fields,
                                     household_model_new.fields)
            self._check_household_generate(household_model_new)

        builtin_module_name = 'builtins' if sys.version_info.major == 3 else '__builtin__'
        with patch('{}.open'.format(builtin_module_name),
                   new_callable=mock_open()) as open_mock:
            household_model.write('file')
            open_mock.assert_called_once_with('file', 'w')
            # Check that correct json is written
            json_net = open_mock.return_value.__enter__.return_value.write.call_args[
                0][0]
            household_model_new = BayesianNetworkModel.from_json(
                json_net, self._household_segmenter())
            _check_network(household_model_new)

            # Check that json is correctly read
            open_mock.return_value.__enter__.return_value.read.return_value = json_net
            household_model_new = BayesianNetworkModel.from_file(
                'file', self._household_segmenter())
            _check_network(household_model_new)
    def test_read_write(self):
        household_model, _ = self._mock_household_collection()

        def _check_network(household_model_new):
            self.assertSequenceEqual(household_model.fields,
                                     household_model_new.fields)
            self._check_household_generate(household_model_new)

        with patch('__builtin__.open') as open_mock:
            household_model.write('file')
            open_mock.assert_called_once_with('file', 'w')
            # Check that correct json is written
            json_net = open_mock.return_value.__enter__.return_value.write.call_args[
                0][0]
            household_model_new = BayesianNetworkModel.from_json(
                json_net, self._household_segmenter())
            _check_network(household_model_new)

            # Check that json is correctly read
            open_mock.return_value.__enter__.return_value.read.return_value = json_net
            household_model_new = BayesianNetworkModel.from_file(
                'file', self._household_segmenter())
            _check_network(household_model_new)