import unittest
from ..adl import ADL
import six

if six.PY3:
    from unittest.mock import Mock, MagicMock
else:
    from mock import Mock, MagicMock


class ADLTest(unittest.TestCase):
    """
    Tests for `ADL`
    """

    def setUp(self):
        self.ls = Mock(return_value=["foo", "bar", "baz"])
        self.fakeFile = MagicMock()
        self.fakeFile.__iter__.return_value = [b"a", b"b", b"c"]
        self.fakeFile.__enter__.return_value = self.fakeFile
        self.open = Mock(return_value=self.fakeFile)
        self.fakeAdapter = Mock(open=self.open, ls=self.ls)
        self.adl = ADL()
        self.adl._create_adapter = Mock(return_value=self.fakeAdapter)

    def test_split_url_raises_exception_on_invalid_url(self):
        with self.assertRaises(Exception) as context:
            ADL._split_url("this_is_not_a_valid_url")
        self.assertTrue("Invalid ADL url 'this_is_not_a_valid_url'" in str(context.exception))

    def test_split_url_splits_valid_url(self):
        (store_name, path) = ADL._split_url("adl://foo.azuredatalakestore.net/bar/baz")
        self.assertEqual(store_name, "foo")
        self.assertEqual(path, "bar/baz")

    def test_listdir_calls_ls_on_adl_adapter(self):
        self.assertEqual(
            self.adl.listdir("adl://foo_store.azuredatalakestore.net/path/to/file"),
            ["foo", "bar", "baz"],
        )
        self.ls.assert_called_once_with("path/to/file")

    def test_read_opens_and_reads_file(self):
        self.assertEquals(
            self.adl.read("adl://foo_store.azuredatalakestore.net/path/to/file"), ["a", "b", "c"]
        )
        self.fakeFile.__iter__.assert_called_once_with()

    def test_write_opens_file_and_writes_to_it(self):
        self.adl.write("hello world", "adl://foo_store.azuredatalakestore.net/path/to/file")
        self.fakeFile.write.assert_called_once_with(b"hello world")

[((263, 303), 'mock.Mock', 'Mock', ([], {'return_value': "['foo', 'bar', 'baz']"}), "(return_value=['foo', 'bar', 'baz'])\n", (267, 303), False, 'from mock import Mock, MagicMock\n'), ((328, 339), 'mock.MagicMock', 'MagicMock', ([], {}), '()\n', (337, 339), False, 'from mock import Mock, MagicMock\n'), ((486, 518), 'mock.Mock', 'Mock', ([], {'return_value': 'self.fakeFile'}), '(return_value=self.fakeFile)\n', (490, 518), False, 'from mock import Mock, MagicMock\n'), ((546, 578), 'mock.Mock', 'Mock', ([], {'open': 'self.open', 'ls': 'self.ls'}), '(open=self.open, ls=self.ls)\n', (550, 578), False, 'from mock import Mock, MagicMock\n'), ((639, 674), 'mock.Mock', 'Mock', ([], {'return_value': 'self.fakeAdapter'}), '(return_value=self.fakeAdapter)\n', (643, 674), False, 'from mock import Mock, MagicMock\n')]