forked from smooth80/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.py
202 lines (149 loc) · 5.14 KB
/
main_test.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Copyright 2018 Google LLC
#
# 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.
from collections import UserDict
from datetime import datetime
import json
import uuid
from mock import MagicMock, patch
import main
class Context(object):
pass
def test_rtdb(capsys):
data = {
'admin': True,
'delta': {'id': 'my-data'}
}
context = Context()
context.resource = 'my-resource'
main.hello_rtdb(data, context)
out, _ = capsys.readouterr()
assert 'Function triggered by change to: my-resource' in out
assert 'Admin?: True' in out
assert 'my-data' in out
def test_firestore(capsys):
context = Context()
context.resource = 'my-resource'
data = {
'oldValue': {'a': 1},
'value': {'b': 2}
}
main.hello_firestore(data, context)
out, _ = capsys.readouterr()
assert 'Function triggered by change to: my-resource' in out
assert json.dumps(data['oldValue']) in out
assert json.dumps(data['value']) in out
def test_auth(capsys):
date_string = datetime.now().isoformat()
data = {
'uid': 'my-user',
'metadata': {'createdAt': date_string},
'email': '[email protected]'
}
main.hello_auth(data, None)
out, _ = capsys.readouterr()
assert 'Function triggered by creation/deletion of user: my-user' in out
assert date_string in out
assert 'Email: [email protected]' in out
@patch('main.client')
def test_make_upper_case(firestore_mock, capsys):
firestore_mock.collection = MagicMock(return_value=firestore_mock)
firestore_mock.document = MagicMock(return_value=firestore_mock)
firestore_mock.set = MagicMock(return_value=firestore_mock)
user_id = str(uuid.uuid4())
date_string = datetime.now().isoformat()
email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4())
data = {
'uid': user_id,
'metadata': {'createdAt': date_string},
'email': email_string,
'value': {
'fields': {
'original': {
'stringValue': 'foobar'
}
}
}
}
context = UserDict()
context.resource = '/documents/some_collection/path/some/path'
main.make_upper_case(data, context)
out, _ = capsys.readouterr()
assert 'Replacing value: foobar --> FOOBAR' in out
firestore_mock.collection.assert_called_with('some_collection')
firestore_mock.document.assert_called_with('path/some/path')
firestore_mock.set.assert_called_with({'original': 'FOOBAR'})
@patch('main.client')
def test_make_upper_case_ignores_already_uppercased(firestore_mock, capsys):
firestore_mock.collection = MagicMock(return_value=firestore_mock)
firestore_mock.document = MagicMock(return_value=firestore_mock)
firestore_mock.set = MagicMock(return_value=firestore_mock)
user_id = str(uuid.uuid4())
date_string = datetime.now().isoformat()
email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4())
data = {
'uid': user_id,
'metadata': {'createdAt': date_string},
'email': email_string,
'value': {
'fields': {
'original': {
'stringValue': 'FOOBAR'
}
}
}
}
context = UserDict()
context.resource = '/documents/some_collection/path/some/path'
main.make_upper_case(data, context)
out, _ = capsys.readouterr()
assert 'Value is already upper-case.' in out
firestore_mock.set.assert_not_called()
def test_analytics(capsys):
timestamp = int(datetime.utcnow().timestamp())
data = {
'eventDim': [{
'name': 'my-event',
'timestampMicros': f'{str(timestamp)}000000'
}],
'userDim': {
'deviceInfo': {
'deviceModel': 'Pixel'
},
'geoInfo': {
'city': 'London',
'country': 'UK'
}
}
}
context = Context()
context.resource = 'my-resource'
main.hello_analytics(data, context)
out, _ = capsys.readouterr()
assert 'Function triggered by the following event: my-resource' in out
assert f'Timestamp: {datetime.utcfromtimestamp(timestamp)}' in out
assert 'Name: my-event' in out
assert 'Device Model: Pixel' in out
assert 'Location: London, UK' in out
def test_remote_config(capsys):
data = {
'updateOrigin': 'CONSOLE',
'updateType': 'INCREMENTAL_UPDATE',
'versionNumber': '1'
}
context = Context()
main.hello_remote_config(data, context)
out, _ = capsys.readouterr()
assert 'Update type: INCREMENTAL_UPDATE' in out
assert 'Origin: CONSOLE' in out
assert 'Version: 1' in out