-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_bindings.py
157 lines (132 loc) · 5 KB
/
test_bindings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2021 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import unittest
from os import path
from bindings import binding, bindings
class TestBindings(unittest.TestCase):
def test__cached(self):
b = bindings.cached([
binding.DictBinding("test-name-1", {}),
binding.DictBinding("test-name-2", {}),
])
for b in b:
self.assertIs(binding.CacheBinding, type(b))
def test__from__missing(self):
self.assertEqual(0, len(bindings.from_path("missing")))
def test__from__file(self):
self.assertEqual(0, len(bindings.from_path("tests/testdata/additional-file")))
def test__from__exists(self):
self.assertEqual(3, len(bindings.from_path("tests/testdata")))
def test__from_service_binding_root__unset(self):
self.assertEqual(0, len(bindings.from_service_binding_root()))
def test__from_service_binding_root__set(self):
old = os.getenv("SERVICE_BINDING_ROOT")
os.environ["SERVICE_BINDING_ROOT"] = path.join("tests", "testdata")
try:
self.assertEqual(3, len(bindings.from_service_binding_root()))
finally:
if old is None:
del os.environ["SERVICE_BINDING_ROOT"]
else:
os.environ["SERVICE_BINDING_ROOT"] = old
def test__find__missing(self):
b = [
binding.DictBinding("test-name-1", {}),
]
self.assertIsNone(bindings.find(b, "test-name-2"))
def test_find_valid(self):
b = [
binding.DictBinding("test-name-1", {}),
binding.DictBinding("test-name-2", {}),
]
self.assertEqual("test-name-1", bindings.find(b, "test-name-1").get_name())
def test_filter_none(self):
b = [
binding.DictBinding("test-name-1", {
"type": b"test-type-1",
"provider": b"test-provider-1",
}),
binding.DictBinding("test-name-2", {
"type": b"test-type-1",
"provider": b"test-provider-2",
}),
binding.DictBinding("test-name-3", {
"type": b"test-type-2",
"provider": b"test-provider-2",
}),
binding.DictBinding("test-name-4", {
"type": b"test-type-2",
}),
]
self.assertEqual(4, len(bindings.filter(b)))
def test_filter_type(self):
b = [
binding.DictBinding("test-name-1", {
"type": b"test-type-1",
"provider": b"test-provider-1",
}),
binding.DictBinding("test-name-2", {
"type": b"test-type-1",
"provider": b"test-provider-2",
}),
binding.DictBinding("test-name-3", {
"type": b"test-type-2",
"provider": b"test-provider-2",
}),
binding.DictBinding("test-name-4", {
"type": b"test-type-2",
}),
]
self.assertEqual(2, len(bindings.filter(b, "test-type-1")))
def test_filter_provider(self):
b = [
binding.DictBinding("test-name-1", {
"type": b"test-type-1",
"provider": b"test-provider-1",
}),
binding.DictBinding("test-name-2", {
"type": b"test-type-1",
"provider": b"test-provider-2",
}),
binding.DictBinding("test-name-3", {
"type": b"test-type-2",
"provider": b"test-provider-2",
}),
binding.DictBinding("test-name-4", {
"type": b"test-type-2",
}),
]
self.assertEqual(2, len(bindings.filter(b, None, "test-provider-2")))
def test_filter_type_and_provider(self):
b = [
binding.DictBinding("test-name-1", {
"type": b"test-type-1",
"provider": b"test-provider-1",
}),
binding.DictBinding("test-name-2", {
"type": b"test-type-1",
"provider": b"test-provider-2",
}),
binding.DictBinding("test-name-3", {
"type": b"test-type-2",
"provider": b"test-provider-2",
}),
binding.DictBinding("test-name-4", {
"type": b"test-type-2",
}),
]
self.assertEqual(1, len(bindings.filter(b, "test-type-1", "test-provider-1")))
if __name__ == "__main__":
unittest.main()