1515from datetime import datetime
1616import json
1717
18- import uuid
19-
2018import main
2119
2220
@@ -25,61 +23,87 @@ class Context(object):
2523
2624
2725def test_rtdb (capsys ):
28- data_id = str (uuid .uuid4 ())
29- resource_id = str (uuid .uuid4 ())
30-
3126 data = {
3227 'admin' : True ,
33- 'delta' : {'id' : data_id }
28+ 'delta' : {'id' : 'my-data' }
3429 }
3530
3631 context = Context ()
37- context .resource = resource_id
32+ context .resource = 'my-resource'
3833
3934 main .hello_rtdb (data , context )
4035
4136 out , _ = capsys .readouterr ()
4237
43- assert ( 'Function triggered by change to: %s' % resource_id ) in out
38+ assert 'Function triggered by change to: my-resource' in out
4439 assert 'Admin?: True' in out
45- assert data_id in out
40+ assert 'my-data' in out
4641
4742
4843def test_firestore (capsys ):
49- resource_id = str (uuid .uuid4 ())
50-
5144 context = Context ()
52- context .resource = resource_id
45+ context .resource = 'my-resource'
5346
5447 data = {
55- 'oldValue' : {'uuid ' : str ( uuid . uuid4 ()) },
56- 'value' : {'uuid ' : str ( uuid . uuid4 ()) }
48+ 'oldValue' : {'a ' : 1 },
49+ 'value' : {'b ' : 2 }
5750 }
5851
5952 main .hello_firestore (data , context )
6053
6154 out , _ = capsys .readouterr ()
6255
63- assert ( 'Function triggered by change to: %s' % resource_id ) in out
56+ assert 'Function triggered by change to: my-resource' in out
6457 assert json .dumps (data ['oldValue' ]) in out
6558 assert json .dumps (data ['value' ]) in out
6659
6760
6861def test_auth (capsys ):
69- user_id = str (uuid .uuid4 ())
7062 date_string = datetime .now ().isoformat ()
71- email_string = '%s@%s.com' % (uuid .uuid4 (), uuid .uuid4 ())
7263
7364 data = {
74- 'uid' : user_id ,
65+ 'uid' : 'my-user' ,
7566 'metadata' : {'createdAt' : date_string },
76- 'email' : email_string
67+ 'email' : 'me@example.com'
7768 }
7869
7970 main .hello_auth (data , None )
8071
8172 out , _ = capsys .readouterr ()
8273
83- assert user_id in out
74+ assert 'Function triggered by creation/deletion of user: my-user' in out
8475 assert date_string in out
85- assert email_string in out
76+ assert 'Email: me@example.com' 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