codecoverage improvement
[dcaegen2/platform.git] / mod / distributorapi / tests / test_registry_client.py
1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2019-2022 AT&T Intellectual Property. All rights reserved.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=========================================================
16 import distributor
17 from distributor import registry_client as rc
18
19
20 def test_add_url_from_link():
21     test = {"link": {"href": "bar"}, "name": "jane", "age": 33, "innerTest": {"link": {"href": "baz"}, "name": "bob"}}
22     result = rc._add_url_from_link("http://foo", test)
23
24     assert result["selfUrl"] == "http://foo/bar"
25     assert result["innerTest"]["selfUrl"] == "http://foo/baz"
26
27
28 def test_get_buckets(monkeypatch):
29     def fake_get_json(url):
30         if url == "http://registry/buckets":
31             return []
32         return None
33
34     monkeypatch.setattr(distributor.registry_client, "_get_json", fake_get_json)
35
36     assert [] == rc.get_buckets("http://registry")
37     assert [] == rc.get_buckets("http://registry/")
38
39
40 def test_find_flow(monkeypatch):
41     def fake_get_buckets(url):
42         return [{"selfUrl": "{0}/buckets/123".format(url)}]
43
44     monkeypatch.setattr(distributor.registry_client, "get_buckets", fake_get_buckets)
45
46     def fake_get_flows(registry_url, url):
47         if url == "http://registry/buckets/123":
48             return [{"identifier": "abc"}]
49         return None
50
51     monkeypatch.setattr(distributor.registry_client, "get_flows", fake_get_flows)
52
53     assert rc.find_flow("http://registry", "abc")["identifier"] == "abc"
54
55
56 def test_flow_versions(monkeypatch):
57     def fake_get_json_many(url):
58         if url == "http://registry/buckets/123/flows/abc/versions":
59             return [{"version": 1}, {"version": 3}, {"version": 2}]
60         print(url)
61         return []
62
63     monkeypatch.setattr(distributor.registry_client, "_get_json", fake_get_json_many)
64
65     assert [3, 2, 1] == rc.get_flow_versions("http://registry/buckets/123/flows/abc/")
66
67
68 def test_get_flow_diff_latest(monkeypatch):
69     def fake_get_flow_versions(url):
70         return ["1"]
71
72     monkeypatch.setattr(distributor.registry_client, "get_flow_versions", fake_get_flow_versions)
73
74     assert None == rc.get_flow_diff_latest("http://registry", "http://registry/buckets/123/flows/abc/")