|
| 1 | +# Copyright 2019 Google LLC All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import base64 |
| 16 | + |
| 17 | +from unittest.mock import Mock |
| 18 | + |
| 19 | +import main |
| 20 | + |
| 21 | +mock_context = Mock() |
| 22 | +mock_context.event_id = '617187464135194' |
| 23 | +mock_context.timestamp = '2020-04-15T22:09:03.761Z' |
| 24 | + |
| 25 | + |
| 26 | +def test_datastore_export(capsys): |
| 27 | + # Test an export without an entity filter |
| 28 | + bucket = 'gs://my-bucket' |
| 29 | + json_string = '{{ "bucket": "{bucket}" }}'.format(bucket=bucket) |
| 30 | + |
| 31 | + # Encode data like Cloud Scheduler |
| 32 | + data = bytes(json_string, 'utf-8') |
| 33 | + data_encoded = base64.b64encode(data) |
| 34 | + event = {"data": data_encoded} |
| 35 | + |
| 36 | + # Mock the Datastore service |
| 37 | + mockDatastore = Mock() |
| 38 | + main.datastore = mockDatastore |
| 39 | + |
| 40 | + # Call tested function |
| 41 | + main.datastore_export(event, mock_context) |
| 42 | + out, err = capsys.readouterr() |
| 43 | + export_args = mockDatastore.projects().export.call_args[1] |
| 44 | + req_body = export_args['body'] |
| 45 | + # Assert request includes test values |
| 46 | + assert req_body['outputUrlPrefix'] == bucket |
| 47 | + |
| 48 | + |
| 49 | +def test_datastore_export_entity_filter(capsys): |
| 50 | + # Test an export with an entity filter |
| 51 | + bucket = 'gs://my-bucket' |
| 52 | + kinds = 'Users,Tasks' |
| 53 | + namespaceIds = 'Customer831,Customer157' |
| 54 | + json_string = '{{ "bucket": "{bucket}", "kinds": "{kinds}", "namespaceIds": "{namespaceIds}" }}'.format( |
| 55 | + bucket=bucket, kinds=kinds, namespaceIds=namespaceIds) |
| 56 | + |
| 57 | + # Encode data like Cloud Scheduler |
| 58 | + data = bytes(json_string, 'utf-8') |
| 59 | + data_encoded = base64.b64encode(data) |
| 60 | + event = {"data": data_encoded} |
| 61 | + |
| 62 | + # Mock the Datastore service |
| 63 | + mockDatastore = Mock() |
| 64 | + main.datastore = mockDatastore |
| 65 | + |
| 66 | + # Call tested function |
| 67 | + main.datastore_export(event, mock_context) |
| 68 | + out, err = capsys.readouterr() |
| 69 | + export_args = mockDatastore.projects().export.call_args[1] |
| 70 | + req_body = export_args['body'] |
| 71 | + # Assert request includes test values |
| 72 | + assert req_body['outputUrlPrefix'] == bucket |
| 73 | + assert req_body['entityFilter']['kinds'] == kinds |
| 74 | + assert req_body['entityFilter']['namespaceIds'] == namespaceIds |
0 commit comments