|
| 1 | +""" |
| 2 | +Pub/Sub trigger tests for v2 |
| 3 | +Test Run ID: {{testRunId}} |
| 4 | +""" |
| 5 | + |
| 6 | +import json |
| 7 | +from firebase_admin import firestore |
| 8 | +from firebase_functions import pubsub_fn |
| 9 | + |
| 10 | +REGION = "{{region}}" |
| 11 | + |
| 12 | +{{#each functions}} |
| 13 | +@pubsub_fn.{{decorator}}( |
| 14 | + topic="{{topic}}", |
| 15 | + region=REGION, |
| 16 | + timeout_sec={{timeout}} |
| 17 | +) |
| 18 | +def {{name}}_{{../testRunId}}(event: pubsub_fn.CloudEvent[pubsub_fn.MessagePublishedData[dict]]) -> None: |
| 19 | + """ |
| 20 | + Test function: {{name}} |
| 21 | + Trigger: {{trigger}} |
| 22 | + Topic: {{topic}} |
| 23 | + """ |
| 24 | + # Extract test_id from message JSON data |
| 25 | + message = event.data.message |
| 26 | + message_json = message.json |
| 27 | + test_id = message_json.get('testId') if message_json else None |
| 28 | + |
| 29 | + if not test_id: |
| 30 | + print(f"Warning: No testId found in message") |
| 31 | + return |
| 32 | + |
| 33 | + # Prepare context data for storage |
| 34 | + context_data = { |
| 35 | + "id": event.id, |
| 36 | + "time": event.time, |
| 37 | + "type": f"google.cloud.pubsub.topic.v1.{{eventType}}", |
| 38 | + "source": event.source, |
| 39 | + # Stringify the message object for storage (matching JS behavior) |
| 40 | + "message": json.dumps({ |
| 41 | + "data": message.data, |
| 42 | + "messageId": message.message_id, |
| 43 | + "publishTime": message.publish_time, |
| 44 | + "attributes": message.attributes, |
| 45 | + "orderingKey": message.ordering_key, |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + # Store context in Firestore for verification |
| 50 | + db = firestore.client() |
| 51 | + collection_name = "{{#if collection}}{{collection}}{{else}}{{name}}{{/if}}" |
| 52 | + db.collection(collection_name).document(test_id).set(context_data) |
| 53 | + |
| 54 | +{{/each}} |
0 commit comments