from datetime import datetime

import pytest
from unittest.mock import Mock

from channelarchiver import Archiver, codes, utils, exceptions
from channelarchiver.models import ChannelData, ArchiveProperties
from mock_archiver import MockArchiver

utc = utils.UTC()
local_tz = utils.local_tz


@pytest.fixture
def archiver():
    archiver = Archiver("http://fake")
    archiver.archiver = MockArchiver()
    return archiver


def test_scan_archives_all(archiver):
    archiver.scan_archives()
    archives_for_channel = archiver.archives_for_channel
    assert "EXAMPLE:DOUBLE_SCALAR" in archives_for_channel
    assert "EXAMPLE:INT_WAVEFORM" in archives_for_channel
    assert "EXAMPLE:ENUM_SCALAR" in archives_for_channel
    expected_archives = {
        "EXAMPLE:DOUBLE_SCALAR": [
            ArchiveProperties(
                key=1001,
                start_time=datetime(2012, 7, 12, 21, 47, 23, 664000, tzinfo=utc),
                end_time=datetime(2012, 7, 13, 11, 18, 55, 671259, tzinfo=utc),
            )
        ],
        "EXAMPLE:INT_WAVEFORM": [
            ArchiveProperties(
                key=1001,
                start_time=datetime(2012, 7, 12, 23, 14, 19, 129600, tzinfo=utc),
                end_time=datetime(2012, 7, 13, 8, 26, 18, 558211, tzinfo=utc),
            )
        ],
        "EXAMPLE:ENUM_SCALAR": [
            ArchiveProperties(
                key=1008,
                start_time=datetime(2012, 7, 12, 22, 41, 10, 765676, tzinfo=utc),