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 1 commit
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 @@ -20,6 +20,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 @@ -69,3 +73,25 @@ def hello_auth(data, context):
if 'email' in data:
print('Email: %s' % data["email"])
# [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]
41 changes: 40 additions & 1 deletion functions/firebase/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
# 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 @@ -83,3 +85,40 @@ def test_auth(capsys):
assert user_id in out
assert date_string in out
assert email_string 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'})
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