isabl-cli 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tests/test_utils.py ADDED
@@ -0,0 +1,154 @@
1
+ """isabl_cli utils tests."""
2
+
3
+ from getpass import getuser
4
+ from os.path import join
5
+ import os
6
+ import tarfile
7
+
8
+ import pytest
9
+
10
+ from isabl_cli import utils
11
+ from isabl_cli.settings import _DEFAULTS
12
+
13
+
14
+ def test_find_user(tmpdir):
15
+ assert getuser() == utils.find_owner(str(tmpdir))
16
+ assert utils.assert_same_owner(str(tmpdir)) is None
17
+
18
+
19
+ def test_force_symlink(tmpdir):
20
+ src = join(str(tmpdir), "src")
21
+ dst = join(str(tmpdir), "dst")
22
+
23
+ with open(src, "w") as f:
24
+ f.write("Not empty.")
25
+
26
+ utils.force_symlink(src, dst)
27
+ assert os.path.islink(dst)
28
+
29
+
30
+ def test_force_symlink_overwrite(tmpdir):
31
+ src = join(str(tmpdir), "src")
32
+ dst = join(str(tmpdir), "dst")
33
+
34
+ with open(src, "w") as f:
35
+ f.write("Correct.")
36
+
37
+ with open(dst, "w") as f:
38
+ f.write("Wrong.")
39
+
40
+ utils.force_symlink(src, dst)
41
+ assert os.path.islink(dst)
42
+
43
+ with open(dst, "r") as f:
44
+ assert "Correct" in f.read()
45
+
46
+
47
+ def test_force_link(tmpdir):
48
+ src = join(str(tmpdir), "src")
49
+ dst = join(str(tmpdir), "dst")
50
+
51
+ with open(src, "w") as f:
52
+ f.write("Not empty.")
53
+
54
+ utils.force_link(src, dst)
55
+ assert os.path.isfile(dst)
56
+ assert not os.path.islink(dst)
57
+
58
+
59
+ def test_force_link_overwrite(tmpdir):
60
+ src = join(str(tmpdir), "src")
61
+ dst = join(str(tmpdir), "dst")
62
+
63
+ with open(src, "w") as f:
64
+ f.write("Correct.")
65
+
66
+ with open(dst, "w") as f:
67
+ f.write("Wrong.")
68
+
69
+ utils.force_link(src, dst)
70
+ assert os.path.isfile(dst)
71
+ assert not os.path.islink(dst)
72
+
73
+ with open(dst, "r") as f:
74
+ assert "Correct" in f.read()
75
+
76
+
77
+ def test_tar_dir(tmpdir):
78
+ dst_dir = join(str(tmpdir), "dst_dir")
79
+ source_dir = join(str(tmpdir), "source_dir")
80
+ output_path = join(str(tmpdir), "source_dir.tar.gz")
81
+
82
+ files = (
83
+ (join(source_dir, "1"), "first file."),
84
+ (join(source_dir, "2"), "second file."),
85
+ (join(source_dir, "3"), "third file."),
86
+ )
87
+
88
+ os.makedirs(source_dir)
89
+ os.makedirs(dst_dir)
90
+
91
+ for i, j in files:
92
+ with open(i, "w") as f:
93
+ f.write(j)
94
+
95
+ utils.tar_dir(output_path=output_path, source_dir=source_dir)
96
+ assert tarfile.is_tarfile(output_path)
97
+
98
+ # Remove files and dir
99
+ for i in files:
100
+ os.unlink(i[0])
101
+
102
+ os.rmdir(source_dir)
103
+ tar = tarfile.open(output_path)
104
+ tar.extractall(path=dst_dir)
105
+ tar.close()
106
+
107
+ for i, j in files:
108
+ i = i.replace(source_dir, join(dst_dir, "source_dir"))
109
+ with open(i, "r") as f:
110
+ assert j in f.read()
111
+
112
+
113
+ def test_get_tree_size(tmpdir):
114
+ tmpdir.mkdir("l1").mkdir("l2").join("test").write("foo")
115
+ tmpdir.mkdir("l3").mkdir("l2").join("test").write("foo")
116
+ sym_false = utils.get_tree_size(tmpdir.join("l1"), follow_symlinks=False)
117
+ os.symlink(tmpdir.join("l3"), tmpdir.join("l1").join("l3"))
118
+ sym_true = utils.get_tree_size(tmpdir.join("l1"), follow_symlinks=True)
119
+
120
+ assert sym_false
121
+ assert sym_true
122
+ assert sym_true > sym_false
123
+
124
+
125
+ def test_check_admin():
126
+ admin = _DEFAULTS["ADMIN_USER"]
127
+ _DEFAULTS["ADMIN_USER"] = "not the admin"
128
+
129
+ with pytest.raises(PermissionError) as error:
130
+ utils.check_admin()
131
+
132
+ assert "not the admin" in str(error.value)
133
+ _DEFAULTS["ADMIN_USER"] = admin
134
+
135
+
136
+ def test_traverse_dict():
137
+ assert utils.traverse_dict({"1": {}}, ["1"], serialize=True) == "{}"
138
+ assert utils.traverse_dict({"1": [{"2": 1}]}, ["1", "2"], serialize=True) == "[1]"
139
+
140
+
141
+ def test_called_from():
142
+ def a_function():
143
+ return utils.called_from(3, verbose=False)
144
+
145
+ assert "a_function" in a_function()
146
+ assert "test_called_from" in a_function()
147
+ assert "pytest_pyfunc_call" in a_function()
148
+
149
+ def test_rsync_version_checking():
150
+ incompatible_version_string = "rsync version 2.6.9 protocol version 29"
151
+ with pytest.raises(ValueError):
152
+ utils.check_rsync_version(incompatible_version_string)
153
+ compatible_version_string = "rsync version 3.2.7 protocol version 31"
154
+ assert utils.check_rsync_version(compatible_version_string) is None
@@ -0,0 +1,128 @@
1
+ """isabl_cli validators tests."""
2
+
3
+ from os.path import join
4
+
5
+ import click
6
+ import pytest
7
+
8
+ from isabl_cli import api
9
+ from isabl_cli import exceptions
10
+ from isabl_cli import factories
11
+ from isabl_cli import validators
12
+
13
+
14
+ def test_validate_patterns_are_files(tmpdir):
15
+ """Create multiple files and test test_validate_patterns_are_files."""
16
+ tmpdir_path = str(tmpdir)
17
+
18
+ for i in range(5):
19
+ with open(join(tmpdir_path, "empty" + str(i)), "w"):
20
+ pass
21
+
22
+ for i in range(11, 15):
23
+ with open(join(tmpdir_path, "not_empty" + str(i)), "w") as f:
24
+ f.write("I'm not empty.")
25
+
26
+ empty = [join(tmpdir_path, "empty*")]
27
+ not_empty = [join(tmpdir_path, "not_empty*")]
28
+ none_existing = ["florentino", "ariza*"]
29
+ none_file = [tmpdir_path]
30
+
31
+ # check empty files exist
32
+ assert validators.validate_patterns_are_files(empty, check_size=False)
33
+
34
+ # check files exist amd are not empty
35
+ assert validators.validate_patterns_are_files(not_empty, check_size=True)
36
+
37
+ # check that empty files raise error with default setting
38
+ with pytest.raises(exceptions.ValidationError):
39
+ validators.validate_patterns_are_files(empty)
40
+
41
+ # check that empty files raise error with flag
42
+ with pytest.raises(exceptions.ValidationError):
43
+ validators.validate_patterns_are_files(empty, check_size=True)
44
+
45
+ # check that pattern is not file
46
+ with pytest.raises(exceptions.ValidationError):
47
+ validators.validate_patterns_are_files(none_file, check_size=True)
48
+
49
+ # check that empty files raise error with flag
50
+ with pytest.raises(exceptions.ValidationError):
51
+ validators.validate_patterns_are_files(empty, check_size=True)
52
+
53
+ # check that invalid patterns raise validationerror error
54
+ with pytest.raises(exceptions.ValidationError):
55
+ validators.validate_patterns_are_files(none_existing, check_size=True)
56
+
57
+
58
+ def test_validate_patterns_are_dirs(tmpdir):
59
+ """test_validate_patterns_are_dirs."""
60
+ tmpdir_path = str(tmpdir)
61
+ file_patterns = [join(tmpdir_path, "a_file")]
62
+ existing_patterns = [tmpdir_path]
63
+ none_existing_patterns = ["florentino", "ariza*"]
64
+
65
+ # touch the file
66
+ with open(file_patterns[0], "w"):
67
+ pass
68
+
69
+ # check empty files exist
70
+ assert validators.validate_patterns_are_dirs(existing_patterns)
71
+
72
+ # check that empty files raise error with default setting
73
+ with pytest.raises(exceptions.ValidationError):
74
+ validators.validate_patterns_are_dirs(none_existing_patterns)
75
+
76
+ # check that empty files raise error with flag
77
+ with pytest.raises(exceptions.ValidationError):
78
+ validators.validate_patterns_are_dirs(file_patterns)
79
+
80
+
81
+ def test_validate_pairs(tmpdir):
82
+ """test_validate_pairs."""
83
+ data = [factories.ExperimentFactory() for i in range(3)]
84
+ instance_a = api.create_instance("experiments", **data[0])
85
+ instance_b = api.create_instance("experiments", **data[1])
86
+ instance_c = api.create_instance("experiments", **data[2])
87
+
88
+ # pairs that exist
89
+ real_pairs = [
90
+ (instance_a["system_id"], instance_b["system_id"]),
91
+ (instance_c["system_id"], instance_b["system_id"]),
92
+ ]
93
+ parsed_pairs = validators.validate_pairs(real_pairs)
94
+ assert parsed_pairs[0] == ([instance_a], [instance_b])
95
+ assert parsed_pairs[1] == ([instance_c], [instance_b])
96
+
97
+ # pairs that dont exist
98
+ fake_pair = [("not_real", 0), (2, "fake")]
99
+ with pytest.raises(exceptions.ValidationError):
100
+ validators.validate_pairs(fake_pair)
101
+
102
+
103
+ def test_validate_pairs_from_file(tmpdir):
104
+ """test_validate_pairs_from_file."""
105
+ tmpdir_path = str(tmpdir)
106
+ data = [factories.ExperimentFactory() for i in range(3)]
107
+ instance_a = api.create_instance("experiments", **data[0])
108
+ instance_b = api.create_instance("experiments", **data[1])
109
+ instance_c = api.create_instance("experiments", **data[2])
110
+
111
+ realpairdf = join(tmpdir_path, "realpairdf.tsv")
112
+ with open(realpairdf, "w") as f:
113
+ f.write("# I am the header\n")
114
+ f.write(instance_a["system_id"] + "\t" + instance_b["system_id"] + "\n")
115
+ f.write(instance_c["system_id"] + "\t" + instance_b["system_id"] + "\n")
116
+
117
+ badpairdf = join(tmpdir_path, "badpairdf.tsv")
118
+ with open(badpairdf, "w") as f:
119
+ f.write("\t".join([instance_a["system_id"]]))
120
+
121
+ # valid pair file
122
+ parsed_pairs = validators.validate_pairs_from_file(None, None, realpairdf)
123
+ assert parsed_pairs[0] == ([instance_a], [instance_b])
124
+ assert parsed_pairs[1] == ([instance_c], [instance_b])
125
+
126
+ # invalid pair file
127
+ with pytest.raises(click.UsageError):
128
+ validators.validate_pairs_from_file(None, None, badpairdf)
tests/test_version.py ADDED
@@ -0,0 +1,8 @@
1
+ """isabl_cli version test."""
2
+
3
+ from isabl_cli import __version__
4
+
5
+
6
+ def test_version():
7
+ """Sample test for the __version__ variable."""
8
+ assert __version__