Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Firestore reactive sample #1739

Merged
merged 4 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions functions/firebase/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
# [END functions_firebase_firestore]
# [END functions_firebase_auth]

# [START functions_firebase_reactive]
from google.cloud import firestore
# [END functions_firebase_reactive]


# [START functions_firebase_rtdb]
def hello_rtdb(data, context):
Expand Down Expand Up @@ -75,6 +79,28 @@ def hello_auth(data, context):
# [END functions_firebase_auth]


# [START functions_firebase_reactive]
client = firestore.Client()


# Converts strings added to /messages/{pushId}/original to uppercase
def make_upper_case(data, context):
path_parts = context.resource.split('/documents/')[1].split('/')
collection_path = path_parts[0]
document_path = '/'.join(path_parts[1:])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't suppose there are helpers for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.path handles paths intelligently, is that something we can leverage? If that's not a good fit, can you explain what you're intending to do? I can't seem to process this code in my head right now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a Firebase document path, not an OS one. Will os.path still work here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I am not sure. There may also be useful url/uri manipulation features elsewhere in the standard library if that is better.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything I've found works with the right side of a path, not the left. Only functions that do the latter would be convenient here.

Also, Firestore document names cannot contain the / character (according to the Cloud Console) - so we shouldn't have to worry about character escaping.


affected_doc = client.collection(collection_path).document(document_path)

cur_value = data["value"]["fields"]["original"]["stringValue"]
new_value = cur_value.upper()
print(f'Replacing value: {cur_value} --> {new_value}')

affected_doc.set({
u'original': new_value
})
# [END functions_firebase_reactive]


# [START functions_firebase_analytics]
def hello_analytics(data, context):
print(data)
Expand Down
41 changes: 41 additions & 0 deletions functions/firebase/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
# 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

Expand Down Expand Up @@ -76,6 +80,43 @@ def test_auth(capsys):
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'})


def test_analytics(capsys):
timestamp = int(datetime.utcnow().timestamp())

Expand Down
1 change: 1 addition & 0 deletions functions/firebase/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-firestore==0.29.0