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_api.py ADDED
@@ -0,0 +1,124 @@
1
+ from click.testing import CliRunner
2
+ import click
3
+ import pytest
4
+ import requests
5
+
6
+ from isabl_cli import api
7
+ from isabl_cli import factories
8
+ from isabl_cli.settings import system_settings
9
+ from isabl_cli.settings import user_settings
10
+
11
+
12
+ def test_get_custom_fields():
13
+ i = api.IsablDict.fromDict(dict(custom_fields={}))
14
+
15
+ # assert set
16
+ i.custom_fields.field = 2
17
+ assert i.field == 2
18
+ i.field = 3
19
+ assert i.custom_fields.field == 3
20
+ i["field"] = 1
21
+
22
+ # check can get
23
+ assert i.field == 1
24
+ assert i.custom_fields.field == 1
25
+ assert i["field"] == 1
26
+ assert i.get("field") == 1
27
+
28
+ # assert contains
29
+ assert "field" in i
30
+ assert "field" in dir(i)
31
+ assert "field" in list(i)
32
+
33
+ # assert can delete
34
+ i.custom_fields.a = 1
35
+ i.custom_fields.b = 1
36
+ i.custom_fields.c = 1
37
+ del i.a
38
+ del i["b"]
39
+ assert i.pop("c") == 1
40
+ assert "a" not in i.custom_fields
41
+
42
+
43
+ def test_api_request():
44
+ assert api.api_request("get", url="auth").ok
45
+ with pytest.raises(requests.HTTPError):
46
+ api.api_request("delete", url="http://google.com")
47
+
48
+
49
+ def test_api_methods():
50
+ endpoint = "diseases"
51
+ diseases = [factories.DiseaseFactory() for _ in range(3)]
52
+ created = [api.create_instance(endpoint, **i) for i in diseases]
53
+ pk = created[0]["pk"]
54
+ pks = [i["pk"] for i in created[:2]]
55
+ patched = api.patch_instance(endpoint, pk, data={"one": 1})
56
+
57
+ assert patched["data"]["one"] == 1
58
+ assert api.get_instance(endpoint, pk)["pk"] == pk
59
+ assert api.get_instances(endpoint, pk=pk)[0]["pk"] == pk
60
+ assert api.get_instances_count(endpoint, pk=pk) == 1
61
+ assert len(api.get_instances(endpoint)) == api.get_instances_count(endpoint)
62
+ assert len(api.get_instances(endpoint, pks)) == 2
63
+ assert len(api.get_instances(endpoint, pks, pk__in=pks)) == 2
64
+ assert len(api.get_instances(endpoint, pks, pk__in=pks[0])) == 1
65
+
66
+ for i in created:
67
+ assert api.delete_instance(endpoint, i["pk"]) is None
68
+
69
+ assert api.get_token_headers()["Authorization"]
70
+
71
+
72
+ def test_patch_analyses_status():
73
+ application = factories.ApplicationFactory()
74
+ analyses = [factories.AnalysisFactory(application=application) for _ in range(2)]
75
+ created = [api.create_instance("analyses", **i) for i in analyses]
76
+ assert all([i["status"] == "CREATED" for i in created])
77
+
78
+ pks = [i["pk"] for i in created]
79
+ api.patch_analyses_status(created, "STAGED")
80
+ retrieved = api.get_instances("analyses", pks)
81
+ assert all([i["status"] == "STAGED" for i in retrieved])
82
+
83
+ for i in created:
84
+ api.delete_instance("analyses", i["pk"])
85
+
86
+ api.delete_instance("applications", created[0]["application"]["pk"])
87
+
88
+
89
+ def test_system_id():
90
+ data_a = factories.ExperimentFactory()
91
+ data_b = factories.ExperimentFactory(sample=data_a["sample"])
92
+ instance_a = api.create_instance("experiments", **data_a)
93
+ instance_b = api.create_instance("experiments", **data_b)
94
+ system_ids = [instance_a["system_id"], instance_b["system_id"]]
95
+ assert instance_a["sample"]["pk"] == instance_b["sample"]["pk"]
96
+ assert api.get_instance("experiments", system_ids[0])["pk"] == instance_a["pk"]
97
+ assert len(api.get_instances("experiments", system_ids)) == 2
98
+
99
+ instance_a["sample"]["data"]["key"] = "value"
100
+ instance_a["sample"]["notes"] = "a note"
101
+ patched = api.patch_instance(
102
+ "experiments", instance_a["pk"], sample=instance_a["sample"]
103
+ )
104
+ assert patched["sample"]["data"]["key"] == "value"
105
+ assert patched["sample"]["notes"] == "a note"
106
+
107
+
108
+ def test_get_instances():
109
+ technique = api.create_instance("techniques", **factories.TechniqueFactory())
110
+ assert api.get_instances("techniques", [technique.name])[0].pk == technique.pk
111
+
112
+ experiment = api.create_instance("experiments", **factories.ExperimentFactory())
113
+ individual = experiment.sample.individual
114
+ project = experiment.projects[0]
115
+ assert api.get_experiments([experiment.pk])[0].pk == experiment.pk
116
+ assert api.get_projects([project.pk])[0].pk == project.pk
117
+ assert api.get_tree(individual.pk).pk == individual.pk
118
+ assert api.get_trees([individual.pk])[0].pk == individual.pk
119
+
120
+
121
+ def test_send_error_email():
122
+ # Test notification for errors
123
+ assert api.send_error_email(["test@test.com"], "test", "test").ok
124
+ assert api.send_error_email(["test1@test.com","test2@test.com"], "test", "test").ok