15
15
from datetime import datetime
16
16
import json
17
17
18
- import uuid
19
-
20
18
import main
21
19
22
20
@@ -25,61 +23,87 @@ class Context(object):
25
23
26
24
27
25
def test_rtdb (capsys ):
28
- data_id = str (uuid .uuid4 ())
29
- resource_id = str (uuid .uuid4 ())
30
-
31
26
data = {
32
27
'admin' : True ,
33
- 'delta' : {'id' : data_id }
28
+ 'delta' : {'id' : 'my-data' }
34
29
}
35
30
36
31
context = Context ()
37
- context .resource = resource_id
32
+ context .resource = 'my-resource'
38
33
39
34
main .hello_rtdb (data , context )
40
35
41
36
out , _ = capsys .readouterr ()
42
37
43
- assert ( 'Function triggered by change to: %s' % resource_id ) in out
38
+ assert 'Function triggered by change to: my-resource' in out
44
39
assert 'Admin?: True' in out
45
- assert data_id in out
40
+ assert 'my-data' in out
46
41
47
42
48
43
def test_firestore (capsys ):
49
- resource_id = str (uuid .uuid4 ())
50
-
51
44
context = Context ()
52
- context .resource = resource_id
45
+ context .resource = 'my-resource'
53
46
54
47
data = {
55
- 'oldValue' : {'uuid ' : str ( uuid . uuid4 ()) },
56
- 'value' : {'uuid ' : str ( uuid . uuid4 ()) }
48
+ 'oldValue' : {'a ' : 1 },
49
+ 'value' : {'b ' : 2 }
57
50
}
58
51
59
52
main .hello_firestore (data , context )
60
53
61
54
out , _ = capsys .readouterr ()
62
55
63
- assert ( 'Function triggered by change to: %s' % resource_id ) in out
56
+ assert 'Function triggered by change to: my-resource' in out
64
57
assert json .dumps (data ['oldValue' ]) in out
65
58
assert json .dumps (data ['value' ]) in out
66
59
67
60
68
61
def test_auth (capsys ):
69
- user_id = str (uuid .uuid4 ())
70
62
date_string = datetime .now ().isoformat ()
71
- email_string = '%s@%s.com' % (uuid .uuid4 (), uuid .uuid4 ())
72
63
73
64
data = {
74
- 'uid' : user_id ,
65
+ 'uid' : 'my-user' ,
75
66
'metadata' : {'createdAt' : date_string },
76
- 'email' : email_string
67
+
77
68
}
78
69
79
70
main .hello_auth (data , None )
80
71
81
72
out , _ = capsys .readouterr ()
82
73
83
- assert user_id in out
74
+ assert 'Function triggered by creation/deletion of user: my-user' in out
84
75
assert date_string in out
85
- assert email_string in out
76
+ assert 'Email: [email protected] ' in out
77
+
78
+
79
+ def test_analytics (capsys ):
80
+ timestamp = int (datetime .utcnow ().timestamp ())
81
+
82
+ data = {
83
+ 'eventDim' : [{
84
+ 'name' : 'my-event' ,
85
+ 'timestampMicros' : f'{ str (timestamp )} 000000'
86
+ }],
87
+ 'userDim' : {
88
+ 'deviceInfo' : {
89
+ 'deviceModel' : 'Pixel'
90
+ },
91
+ 'geoInfo' : {
92
+ 'city' : 'London' ,
93
+ 'country' : 'UK'
94
+ }
95
+ }
96
+ }
97
+
98
+ context = Context ()
99
+ context .resource = 'my-resource'
100
+
101
+ main .hello_analytics (data , context )
102
+
103
+ out , _ = capsys .readouterr ()
104
+
105
+ assert 'Function triggered by the following event: my-resource' in out
106
+ assert f'Timestamp: { datetime .utcfromtimestamp (timestamp )} ' in out
107
+ assert 'Name: my-event' in out
108
+ assert 'Device Model: Pixel' in out
109
+ assert 'Location: London, UK' in out
0 commit comments