"""
@package tests.test_project
unit tests for pmsco.project.

the purpose of these tests is to help debugging the code.

to run the tests, change to the directory which contains the tests directory, and execute =nosetests=.

@pre nose and mock must be installed.

@author Matthias Muntwiler, matthias.muntwiler@psi.ch

@copyright (c) 2015-18 by Paul Scherrer Institut @n
Licensed under the Apache License, Version 2.0 (the "License"); @n
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import unittest
import mock
import numpy as np
import pmsco.data as data
import pmsco.dispatch as dispatch
import pmsco.project as project


class TestProject(unittest.TestCase):
    def setUp(self):
        # before each test method
        self.project = project.Project()

    def tearDown(self):
        # after each test method
        pass

    @classmethod
    def setup_class(cls):
        # before any methods in this class
        pass

    @classmethod
    def teardown_class(cls):
        # teardown_class() after any methods in this class
        pass

    @mock.patch('pmsco.data.load_data')
    @mock.patch('pmsco.data.save_data')
    def test_combine_symmetries(self, save_data_mock, load_data_mock):
        self.project.scans.append(project.Scan())

        parent_task = dispatch.CalculationTask()
        parent_task.change_id(model=0, scan=0)
        parent_task.model['wsym1'] = 0.5

        child_tasks = [parent_task.copy()] * 2
        for idx, task in enumerate(child_tasks):
            task.change_id(sym=idx)

        data1 = data.create_data(5, datatype='EI')
        data1['e'] = np.arange(5)
        data1['i'] = 10.
        data2 = data1.copy()
        data2['i'] = 10.
        load_data_mock.side_effect = [data1, data2]
        data3 = data1.copy()
        data3['i'] = (10. + 0.5 * 10.) / 1.5

        self.project.combine_symmetries(parent_task, child_tasks)

        save_data_mock.assert_called()
        args, kwargs = save_data_mock.call_args
        result_data = args[1]
        np.testing.assert_array_almost_equal(result_data['e'], data3['e'])
        np.testing.assert_array_almost_equal(result_data['i'], data3['i'])


if __name__ == '__main__':
    unittest.main()
