-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_test_lab_fn.py
More file actions
138 lines (122 loc) · 4.82 KB
/
Copy pathtest_test_lab_fn.py
File metadata and controls
138 lines (122 loc) · 4.82 KB
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
# Copyright 2022 Google Inc.
#
# 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.
"""Test Lab function tests."""
import unittest
from unittest.mock import MagicMock, Mock
from cloudevents.http import CloudEvent as _CloudEvent
from firebase_functions import core
from firebase_functions.test_lab_fn import (
ClientInfo,
CloudEvent,
OutcomeSummary,
ResultStorage,
TestMatrixCompletedData,
TestState,
_event_handler,
on_test_matrix_completed,
)
class TestTestLab(unittest.TestCase):
"""
Test Lab function tests.
"""
def test_on_test_matrix_completed_decorator(self):
"""
Tests the on_test_matrix_completed decorator functionality by checking
that the __firebase_endpoint__ attribute is set properly.
"""
func = MagicMock()
func.__name__ = "testfn"
decorated_func = on_test_matrix_completed()(func)
endpoint = decorated_func.__firebase_endpoint__
self.assertIsNotNone(endpoint)
self.assertIsNotNone(endpoint.eventTrigger)
self.assertIsNotNone(endpoint.eventTrigger["eventType"])
def test_event_handler(self):
"""
Tests the _event_handler function, ensuring that it correctly processes
the raw event and calls the user-provided function with a properly
formatted CloudEvent instance.
"""
func = MagicMock()
raw_event = _CloudEvent(
attributes={
"specversion": "1.0",
"type": "com.example.someevent",
"source": "https://example.com/someevent",
"id": "A234-1234-1234",
"time": "2023-03-11T13:25:37.403Z",
},
data={
"createTime": "2023-03-11T13:25:37.403Z",
"state": "FINISHED",
"invalidMatrixDetails": "Some details",
"outcomeSummary": "SUCCESS",
"resultStorage": {
"toolResultsHistory": "projects/123/histories/456",
"resultsUri": "https://example.com/results",
"gcsPath": "gs://bucket/path/to/somewhere",
"toolResultsExecution": "projects/123/histories/456/executions/789",
},
"clientInfo": {
"client": "gcloud",
},
"testMatrixId": "testmatrix-123",
},
)
_event_handler(func, raw_event)
func.assert_called_once()
event_arg = func.call_args.args[0]
self.assertIsInstance(event_arg, CloudEvent)
self.assertIsInstance(event_arg.data, TestMatrixCompletedData)
self.assertIsInstance(event_arg.data.result_storage, ResultStorage)
self.assertIsInstance(event_arg.data.client_info, ClientInfo)
self.assertEqual(event_arg.data.state, TestState.FINISHED)
self.assertEqual(event_arg.data.outcome_summary, OutcomeSummary.SUCCESS)
self.assertEqual(event_arg.data.test_matrix_id, "testmatrix-123")
def test_calls_init(self):
hello = None
@core.init
def init():
nonlocal hello
hello = "world"
func = Mock(__name__="example_func")
raw_event = _CloudEvent(
attributes={
"specversion": "1.0",
"type": "com.example.someevent",
"source": "https://example.com/someevent",
"id": "A234-1234-1234",
"time": "2023-03-11T13:25:37.403Z",
},
data={
"createTime": "2023-03-11T13:25:37.403Z",
"state": "FINISHED",
"invalidMatrixDetails": "Some details",
"outcomeSummary": "SUCCESS",
"resultStorage": {
"toolResultsHistory": "projects/123/histories/456",
"resultsUri": "https://example.com/results",
"gcsPath": "gs://bucket/path/to/somewhere",
"toolResultsExecution": "projects/123/histories/456/executions/789",
},
"clientInfo": {
"client": "gcloud",
},
"testMatrixId": "testmatrix-123",
},
)
decorated_func = on_test_matrix_completed()(func)
decorated_func(raw_event)
func.assert_called_once()
self.assertEqual("world", hello)