def test_call_route(self): app = Goblet(function_name="goblet_example") mock = Mock() mock_param = Mock() app.route("/test")(mock_dummy_function(mock)) @app.route("/test/{param}", methods=["POST"]) def mock_function2(param): mock_param(param) return True mock_event1 = Mock() mock_event1.path = "/test" mock_event1.method = "GET" mock_event1.headers = {} mock_event1.json = {} app(mock_event1, None) mock_event2 = Mock() mock_event2.path = "/test/param" mock_event2.method = "POST" mock_event2.headers = {} mock_event2.json = {} app(mock_event2, None) assert mock.call_count == 1 mock_param.assert_called_once_with("param")
def test_call_storage(self, monkeypatch): app = Goblet(function_name="goblet_example") mock = Mock() app.storage("test", "finalize")(mock_dummy_function(mock)) mock_context = Mock() mock_context.event_type = "providers/cloud.storage/eventTypes/bucket.finalize" event = {"bucket": "test"} app(event, mock_context) assert mock.call_count == 1
def test_call_route(self): app = Goblet(function_name="goblet_example") mock = Mock() app.http()(mock_dummy_function(mock)) mock_request = Mock() mock_request.path = "/" mock_request.headers = {} mock_request.json = {} app(mock_request, None) assert mock.call_count == 1
def test_call_eventarc_topic_no_response(self): app = Goblet(function_name="goblet_example") app.eventarc(topic="test")(mock_dummy_function(dummy_function)) request = Mock() request.path = "/x-goblet-eventarc-triggers/goblet-example-dummy-function" request.headers = { "Ce-Type": "google.cloud.pubsub.topic.v1.messagePublished", "Ce-Source": "//pubsub.googleapis.com/projects/goblet/topics/test", } resp = app(request, None) assert resp.status_code == 200
def test_call_subscription(self): app = Goblet(function_name="goblet_example") mock = Mock() app.topic("test")(mock_dummy_function(mock)) mock_request = Mock() mock_request.headers = {} event = {"data": base64.b64encode("test".encode())} mock_request.json = { "message": event, "subscription": "projects/PROJECT/subscriptions/goblet_example-test", } # assert dummy_function is run app(mock_request, None) assert mock.call_count == 1
def test_call_scheduler(self, monkeypatch): app = Goblet(function_name="goblet_example") monkeypatch.setenv("GOOGLE_PROJECT", "TEST_PROJECT") monkeypatch.setenv("GOOGLE_LOCATION", "us-central1") mock = Mock() app.schedule("* * * * *", description="test")(mock_dummy_function(mock)) headers = { "X-Goblet-Name": "dummy_function", "X-Goblet-Type": "schedule", "X-Cloudscheduler": True, } mock_event = Mock() mock_event.headers = headers app(mock_event, None) assert mock.call_count == 1
def test_call_subscription_attributes(self): app = Goblet(function_name="goblet_example") mock = Mock() app.topic("test", attributes={"t": 1})(mock_dummy_function(mock)) mock_request = Mock() mock_request.headers = {} event = { "data": base64.b64encode("test".encode()), "attributes": { "t": 1 } } mock_request.json = { "message": event, "subscription": "projects/PROJECT/subscriptions/goblet_example-test", } mock_request.path = None app(mock_request, None) event2 = { "data": base64.b64encode("test".encode()), "attributes": { "t": 2 } } mock_request.json = { "message": event2, "subscription": "projects/PROJECT/subscriptions/goblet_example-test", } app(mock_request, None) assert mock.call_count == 1