# This file is part of the ISIS IBEX application.
# Copyright (C) 2012-2016 Science & Technology Facilities Council.
# All rights reserved.
#
# This program is distributed in the hope that it will be useful.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v1.0 which accompanies this distribution.
# EXCEPT AS EXPRESSLY SET FORTH IN THE ECLIPSE PUBLIC LICENSE V1.0, THE PROGRAM
# AND ACCOMPANYING MATERIALS ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND.  See the Eclipse Public License v1.0 for more details.
#
# You should have received a copy of the Eclipse Public License v1.0
# along with this program; if not, you can obtain a copy from
# https://www.eclipse.org/org/documents/epl-v10.php or
# http://opensource.org/licenses/eclipse-1.0.php

import unittest
from datetime import datetime, timedelta

from hamcrest import *
from mock import Mock

from ArchiverAccess.archive_data_file_creator import DataFileCreationError
from ArchiverAccess.archive_time_period import ArchiveTimePeriod
from ArchiverAccess.archiver_data_source import ArchiverDataValue
from ArchiverAccess.archive_access_configuration import ArchiveAccessConfigBuilder
from ArchiverAccess.log_file_initiator import LogFileInitiatorOnPVChange, SAMPLING_BEHIND_REAL_TIME
from ArchiverAccess.test_modules.stubs import ArchiverDataStub


class TestLogFileInitiatorForContinousLogging(unittest.TestCase):
    def test_GIVEN_config_with_pv_WHEN_logging_pv_has_changed_0_to_1_THEN_log_file_created(self):
            # logging pv changes to 1 then back to 0
            expected_logging_start = datetime(2017, 1, 1, 1, 1, 2)

            data_changes = [[(expected_logging_start, 0, 1)]]
            archive_data_source = DataSourceMother.set_up_data_source(initial_pv_values=[0], data_changes=data_changes,
                                                                      logging_start_times=[datetime(2017, 1, 1, 1, 1, 1)])
            write_file_header_mock = Mock()
            log_file_initiator, self.log_file_creators = DataSourceMother.create_log_file_intiator(archive_data_source, write_file_header_mock=write_file_header_mock)

            log_file_initiator.check_initiated()

            write_file_header_mock.assert_called_once_with(expected_logging_start)

    def test_GIVEN_config_with_pv_WHEN_logging_pv_has_swicthed_off_in_changes_THEN_log_file_body_written_and_file_made_readonly(self):
            log_period_in_second = 1
            expected_period = timedelta(seconds=log_period_in_second)
            # logging pv changes to 1 then back to 0
            expected_logging_start = datetime(2017, 1, 1, 1, 1, 2)
            expected_logging_stop = datetime(2017, 1, 1, 1, 2, 2)
            data_changes = [[(expected_logging_start, 0, 1),
                             (expected_logging_stop, 0, 0)]]
            archive_data_source = DataSourceMother.set_up_data_source(initial_pv_values=[0], data_changes=data_changes,
                                                                      logging_start_times=[datetime(2017, 1, 1, 1, 1, 1)])
            write_data_lines_mock = Mock()
            finished_log_file_mock = Mock()