from os.path import abspath, dirname, join
import unittest
from unittest.mock import MagicMock, PropertyMock

from nose.tools import eq_
from werkzeug.datastructures import FileStorage

from helix.constants.file_validation_error import FileValidationError\
    , FileValidationMessage
from helix.constants.system_type import SystemType
from helix.validators.file_validator import FileValidator, FileType, \
    InvalidMappingException


class FileValidatorTest(unittest.TestCase):
    def setUp(self):
        self.user_values = MagicMock()
        self.subject = FileValidator(self.user_values)

    def should_have_error(self, result, message, row):
        eq_(result, FileValidationError(message, row))

    def test_csv_files_are_valid(self):
        fake_file = MagicMock()
        type(fake_file).filename = \
            PropertyMock(return_value="input_single_tilt.csv")
        self.user_values.system_type.return_value = SystemType.singleTilt
        with open('test/fixtures/input_single_tilt.csv', 'r',
                  newline='') as file:
            cad_input = file.read()
        eq_(self.subject.validate(cad_input, FileType.Csv, fake_file), None)

    def test_unknown_files_are_invalid(self):
        fake_file = MagicMock()
        type(fake_file).filename = PropertyMock(return_value="Hi")
        result = self.subject.validate("Hi", FileType.Unknown, fake_file)
        self.should_have_error(result,
                               FileValidationMessage.UnknownFileUploaded, None)

    def test_identifies_dxf_files_as_aurora_dxf(self):
        with open('test/fixtures/dxf/input_single_tilt_96_cell.dxf', 'r',
                  newline='') as file:
            dxf_input = file.read()
        eq_(self.subject.identify_file_type(dxf_input), FileType.AuroraDxf)

    def test_identifies_csv_files_as_csv(self):
        fake_file = MagicMock()
        fake_file.filename.return_value = "input_single_tilt.csv"

        with open('test/fixtures/input_single_tilt.csv', 'r',
                  newline='') as file:
            cad_input = file.read()
        eq_(self.subject.identify_file_type(cad_input), FileType.Csv)

    def test_identifies_other_files_as_unknown(self):
        eq_(self.subject.identify_file_type("hello"), FileType.Unknown)

    def test_read_invalid_file_is_captured(self):
        fname = join(abspath(dirname(__file__)),
                     '../fixtures/images/expected_dual_tilt_pseries_image.png')
        fake_file = MagicMock()
        fake_file.read.return_value = open(fname, "rb").read()
        fake_file.filename.return_value = "expected_dual_tilt_pseries_image.png"
        stream = self.subject.obtain_stream(fake_file)
        self.should_have_error(self.subject.validate(stream, FileType.AuroraDxf, fake_file),
                               FileValidationMessage.ExpectedDxfFile,
                               None)

    def test_read_file_passes(self):
        fname = join(abspath(dirname(__file__)),
                     "../fixtures/expected_design_metadata.txt")
        stream = self.subject.obtain_stream(FileStorage(open(fname, "rb")))
        self.assertGreater(len(stream), 0)

    def test_txt_validator_rejects_dxf_file(self):
        file_type = FileType.Csv
        self.assertRaises(InvalidMappingException,
                          file_type.valid_mapping, "dxf")

    def test_dxf_validator_rejects_txt_file(self):
        file_type = FileType.AuroraDxf
        self.assertRaises(InvalidMappingException,
                          file_type.valid_mapping, "txt")
        self.assertRaises(InvalidMappingException,
                          file_type.valid_mapping, "csv")
        self.assertRaises(InvalidMappingException,
                          file_type.valid_mapping, "gif")

    def test_unknown_validator_rejects_any_file(self):
        file_type = FileType.Unknown
        self.assertRaises(InvalidMappingException,
                          file_type.valid_mapping,
                          "exe")

    def test_file_validator_obtain_extension(self):
        fake_file = MagicMock()
        fname = "Feb1717DT-dxf.1487698302339.dxf"
        type(fake_file).filename = PropertyMock(return_value=fname)
        validator = FileValidator(self.user_values)
        result = validator.obtain_extension(fake_file)
        self.assertEqual("dxf", result)
