def test_scan_dir(self, mocker): scandir = mocker.patch("os.scandir") scandir.return_value = [ FakeOsDirEntry( name="file.txt", path="/home/costantine/file.txt", is_dir=lambda: False, is_file=lambda: True, ), FakeOsDirEntry( name=".ssh", path="/home/costantine/.ssh", is_dir=lambda: True, is_file=lambda: False, ), ] expected_values = [ DirEntry(url=URL("file:///home/costantine/file.txt"), is_dir=False, is_file=True), DirEntry(url=URL("file:///home/costantine/.ssh"), is_dir=True, is_file=False), ] entries = LocalFSClient("file://home/costantine").scandir() for entry, expected in zip(entries, expected_values): assert entry.url == expected.url assert entry.is_dir == expected.is_dir assert entry.is_file == expected.is_file
def test_simple_authenticate(with_creds, raw, expected): injector = injection.CredentialsInjector() with_creds_url = URL(with_creds) raw_url = URL(raw) expected_url = URL(expected) injector.register_credentials(with_creds_url) result = injector.inject(raw_url) assert expected_url == result
def test_scandir_folder(self, mocked_ftp_conn): fake_entries = [("another_dir", {"type": "dir"})] client = ftp_client.FTPClient("ftp://localhost:9999/mydir") with client: client.conn.mlsd.return_value = fake_entries entries = list(client.scandir()) assert entries[0].url == URL("ftp://localhost:9999/mydir/another_dir") assert entries[0].is_dir
def test_scandir_file(self, mocked_ftp_conn): fake_entries = [("my_file.txt", {"type": "file"})] client = ftp_client.FTPClient("ftp://localhost:9999/mydir") with client: client.conn.mlsd.return_value = fake_entries entries = list(client.scandir()) assert entries[0].url == URL("ftp://localhost:9999/mydir/my_file.txt") assert entries[0].is_file
def test_scandir_folder(self, mocked_sftp_conn): FakeAttr = collections.namedtuple("FakeAttr", ["filename", "st_mode"]) client = ftp_client.SFTPClient("sftp://localhost:9999/mydir") with client: client.conn.listdir_attr.return_value = [FakeAttr("other_folder", stat.S_IFDIR)] entries = list(client.scandir()) print("entries", entries) assert entries[0].url == URL("sftp://localhost:9999/mydir/other_folder") assert entries[0].is_dir
def test_create_client_correct_url(csv_data, csv_dumper, mocker): mock = mocker.patch("tentaclio.clients.postgres_client.PostgresClient") recorder = csv_dumper mock.return_value = recorder with api.open("postgresql://localhost/database::my_table", mode="w") as writer: writer.write(csv_data.read()) mock.assert_called_with(URL("postgresql://localhost/database"))
def test_scandir_dir_folder(self, mocked_ftp_conn): fake_entry = "drwxrwxrwx 1 owner group 0 Feb 17 17:54 nested" client = ftp_client.FTPClient("ftp://localhost:9999/mydir") with client: client.conn.mlsd.side_effect = ftplib.error_perm("501 'MLST type;'") # mock ftplib.dir behaviour client.conn.dir = lambda url, parser: parser(fake_entry) entries = list(client.scandir()) assert entries[0].url == URL("ftp://localhost:9999/mydir/nested") assert entries[0].is_dir
class FakeScannerWithDepth(object): dirs = { "/": [ DirEntry(url=URL("scantest+depth://mytest/file0.txt"), is_dir=False, is_file=True), DirEntry(url=URL("scantest+depth://mytest/file1.txt"), is_dir=False, is_file=True), DirEntry(url=URL("scantest+depth://mytest/folder1"), is_dir=True, is_file=False), DirEntry(url=URL("scantest+depth://mytest/folder2"), is_dir=True, is_file=False), ], "/folder1": [ DirEntry( url=URL("scantest+depth://mytest/folder1/folder1_file1.txt"), is_dir=False, is_file=True, ), DirEntry( url=URL("scantest+depth://mytest/folder1/folder1_file2.txt"), is_dir=False, is_file=True, ), ], "/folder2": [], } def scandir(self, url: URL) -> Iterable[DirEntry]: self.scanned_url = url return self.dirs[url.path]
def test_file_scheme(): url = URL("file:///path/to/my/file") # would crash if no hander assert url.path == "/path/to/my/file"
def test_filter_by_hostname(): matches = injection._filter_by_hostname( URL("scheme://google.com/"), [URL("scheme://google.com/path"), URL("scheme://yahoo.com/path")], ) assert matches == [URL("scheme://google.com/path")]
def test_hostname_is_wildcard(): matches = injection._filter_by_hostname( URL("scheme://hostname/"), [URL("scheme://google.com/path")] ) assert matches == [URL("scheme://google.com/path")]
def test_client_scanner(): scanner = ClientDirScanner(FakeClient) entries = scanner.scandir(URL("file:///home/")) assert entries[0].url == URL("file:///home/constantine")
def scandir(self) -> Iterable[DirEntry]: assert self.entered # Connection should be open before scanning return (DirEntry(url=URL("file:///home/constantine"), is_dir=True, is_file=False),)
def test_build_folder_entry(): url = URL("scantest://myurl") entry = build_folder_entry(url) assert entry.url == url assert entry.is_dir assert not entry.is_file
def test_scandir(fake_registry): entries = list(api.scandir("scantest://mytest/")) assert len(entries) == 2 assert entries[0].url == URL("scantest://mytest/file.txt") assert entries[1].url == URL("scantest://mytest/myfolder")
def scandir(self, url: URL) -> Iterable[DirEntry]: self.scanned_url = url return [ DirEntry(url=URL("scantest://mytest/file.txt"), is_dir=False, is_file=True), DirEntry(url=URL("scantest://mytest/myfolder"), is_dir=False, is_file=True), ]