cloud-functions-test


Namecloud-functions-test JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/RobinPicard/cloud-functions-test
SummaryTest locally GCP Cloud Functions
upload_time2023-09-03 22:22:30
maintainer
docs_urlNone
authorRobin Picard
requires_python
licenseapache-2.0
keywords python testing tests gcp cloud functions
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # cloud-functions-test

Test GCP Cloud Functions locally

<br>

## Table of Contents

* [Basic Description](#basic-description)
* [Installation](#installation)
* [Using cloud-functions-test](#using-cloud-functions-test)
    * [General Functioning](#general-functioning)
    * [Http-triggered Functions](#http-triggered-functions)
    * [Wildcards for Expected Content](#wildcards-for-expected-content)
    * [Event-triggered Functions](#event-triggered-functions)
    * [Settings](#settings)
* [Contributing](#contributing)
* [Contact](#contact)

<br>

## Basic Description <a name="basic-description"></a>

The objective of this package is to allow you to easily test your GCP Cloud Functions locally before deploying them. The package relies on Google's functions-framework to launch a local server that receives requests triggering your Cloud Function. Even though the functions are triggered through a request in the package's testing mechanism, event-triggered Cloud Functions are also supported.
The package's interface is intended to look similar to that of popular python unit-testing libraries. You can define different tests as classes in a python file, specifying the input and the expected output. You can then launch the tests from the cli and the results are printed unto your terminal.

Let's say your Cloud Function is defined in the following file `main.py` with an entrypoint function `main`
```python
def main(request):
    value = request.get_json()
    if "a" not in value:
        return ("Error", 400)
    return ({**value, "b": 2}, 200)
```

You can define your tests in a file called `cf_tests.py` in the same directory
```python
class CorrectInput:
    data = {"a": 1}
    headers = {'Content-Type': 'application/json'}
    status_code = 200
    output = {"a": 1, "b": 2}

class IncorrectInput:
    data = {1: 2}
    headers = {'Content-Type': 'application/json'}
    status_code = 400

class CrashInput:
    data = ["a"]
    headers = {'Content-Type': 'application/json'}
    error = True

class OopsUnintendedError:
    data = {"a": "1"}
    headers = {'Content-Type': 'application/json'}
    status_code = 200
    output = {"a": 1, "b": 2}
```

You can then launch the tests by entering in your terminal `cloud-functions-test`
You would see the following output in your terminal (with colors):
```
================================================ Running 4 tests from the cf_tests module... =================================================
test CorrectInput in 0.009216s:  PASSED
test IncorrectInput in 0.002605s:  PASSED
test CrashInput in 0.007339s:  PASSED
test OopsUnintendedError in 0.002422s:  FAILED
*** 3 tests passed and 1 failed ***
=================================================================== FAILED ===================================================================
test OopsUnintendedError
Unexpected output
- expected: {'a': 1, 'b': 2}
- received: {'a': '1', 'b': 2}
```

<br>

## Installation <a name="installation"></a>

To install the latest version of the package:

```bash
pip install cloud-functions-test
```

<br>

## Using cloud-functions-test <a name="using-cloud-functions-test"></a>

### General Functioning <a name="general-functioning"></a>

In your test module, `cf_tests.py` by default, you need to create a basic class for each of your test.

Each class can have a set of attributes that work as parameters for the test. All of those attributes are optional. If none are specified, the test will return "PASSED" if the Cloud Function ran without crashing.

If you do specifiy some of those attributes, you need to make sure their value is of a supported type.

There are 2 possible attributes that are common to both http-triggered and event-triggerd functions:
* `error` (bool): indicates whether the test is expected to raise an Exception. The test will succeed if the function crashes while it will fail if it runs without error
* `display_logs` (bool): indicates whether the logs and the return value should be displayed even in case of success (they are always displayed in case of failure of the test)


### Http-triggered Functions <a name="http-triggered-functions"></a>

This package has primarily been made for http-triggered Cloud Function so your test classes will be considered to be for this kind of function by default.

You can specify 4 additional attributes for those:
* `data` (dict, list): the payload that will be included in the request triggering your function
* `headers` (dict): the headers that will be included in the request triggering your function
* `status_code` (dict, list): the status code your function is expected to return giving the parameters provided
* `output` (dict, list): the output your function is expected to return giving the parameters provided. Details on the specific structure the value of this attribute can take are specified below


### Wildcards for Expected Content <a name="wildcards-for-expected-content"></a>

It's quite common to want to check the validity of the output of an http-triggered Cloud Function without knowing the exact value of the expected output. This happens for instance if you Cloud Function is calling an external service or if your output includes an "updated_at" timestamp. In that case, you may want to test that the structure of the output is correct rather than the exact content.

There are 3 types of wildcards your can use for that purpose:
* Ellipsis
    * If you include this object in a list, the test will only check that the other elements of the list are found in the actual output
    ```
    A:
        output = ["a", Ellipsis]
    ```
    This will match with `actual_output = ["a", "b", "c"]`
    * If you pass this object as a key in a dict, the test will only check that all other key/value pairs are found in the actual output
    ```
    A:
        output = {"a": 1, Ellipsis: Ellipsis}
    ```
    This will match with `actual_output = {"a": 1, "b": 2, "c": 3}`
* Types

    You can include both basic/collection types (int, float, str, list, dict) and types from the typing package (Any, Union, List, Tuple, Dict) in the expected output.
    * Simple case
        ```
        A:
            output = List[int]
        ```
        This will match with `actual_output = [1, 2]`
    * More complex example
        ```
        A:
            output = Tuple[dict, List[int], Union[int, str]]
        ```
        This will match with `actual_output = [{"a": 1}, [1, 2], "b"]`
* Regex

    You can include an object of type re.Pattern in your expected output. If the actual output is a string, it will be matched with the pattern.
    ```
    A:
        output = ["a", re.compile(r'^\d+$')]
    ```
    This will match with `actual_output = ["a", "123a"]`


### Event-triggered Functions <a name="event-triggered-functions"></a>

Testing event-triggered functions is not as straightforward as for http-triggered ones. Since we cannot fully simulate the underlying event for which GCP would trigger the function, we make a request to it as a workaround. In the definition of your test class, you can specify the event and the context you want your function to receive as dictionaries. The package will then pass them on your function.

You can specify 2 additional attributes for those:
* `event` (dict): the event that your function will receive
* `context` (dict): the context that you function will receive


### Settings <a name="settings"></a>

There are 5 options you can modify for running your tests. Those can typically be modified either in your test module or in the cli

* module: defaults to "cf_tests.py", name of the module in which your test classes are defined

   * cli: `cloud-functions-test --module <name_test_module>`


* source: defaults to "main.py", path to the file in which your Cloud Function is defined

   * cli: `cloud-functions-test --source <path_to_file>`
   * in test module:
   ```
   import cloud_function_framework
   cloud_function_framework.source = "<path_to_file>"
   ```

* entrypoint: defaults to "main", name of the Cloud Function entrypoint in the file in which your Cloud Function is defined

   * cli: `cloud-functions-test --entrypoint <entrypoint>`
   * in test module:
   ```
   import cloud_function_framework
   cloud_function_framework.entrypoint = "<entrypoint>"
   ```

* env: defaults to ".env", path to the file in which your environment variables are defined (nothing happens if the file does not exist)

   * cli: `cloud-functions-test --env <env_file_path>`
   * in test module:
   ```
   import cloud_function_framework
   cloud_function_framework.env = "<env_file_path>"
   ```
   This file can either be a bash file organized as such:
   ```bash
   ENV=dev
   FOO=bar
   ```
   Or a Terraform file. In this case, the package will do its best to infer the env variables from the structure of the file. Example of supported structure:
   ```terraform
    module "test_module" {
      env         = var.env
      environment_variables = {
        ENV               = var.env,
        LOCATION_ID       = "europe-west1",
        NAME = "test_${var.env}",
      }
      memory_mb   = 512
    }
   ```

* port: defaults to 8080, port of localhost that will be used by functions-framework to launch the local server receiving the requests that will trigger the Cloud Functions

   * cli: `cloud-functions-test --port <port>`
   * in test module:
   ```
   import cloud_function_framework
   cloud_function_framework.port = <port>

<br>

## Contributing <a name="contributing"></a>

Your contribution is very much welcome, both through the creation of issues and through the opening of pull requests. To open a pull request:
1. Fork the repo
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -m 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

<br>

## Contact <a name="contacts"></a>

You can reach out to me at: robin.picard@sciencespo.fr



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/RobinPicard/cloud-functions-test",
    "name": "cloud-functions-test",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "python,testing,tests,gcp,cloud functions",
    "author": "Robin Picard",
    "author_email": "robin.picard@sciencespo.fr",
    "download_url": "https://files.pythonhosted.org/packages/f6/ab/7817a740b92a5dfc4e031d5cb524e5c8ec71dfd741de2514992fc6c63cce/cloud-functions-test-0.0.2.tar.gz",
    "platform": null,
    "description": "# cloud-functions-test\n\nTest GCP Cloud Functions locally\n\n<br>\n\n## Table of Contents\n\n* [Basic Description](#basic-description)\n* [Installation](#installation)\n* [Using cloud-functions-test](#using-cloud-functions-test)\n    * [General Functioning](#general-functioning)\n    * [Http-triggered Functions](#http-triggered-functions)\n    * [Wildcards for Expected Content](#wildcards-for-expected-content)\n    * [Event-triggered Functions](#event-triggered-functions)\n    * [Settings](#settings)\n* [Contributing](#contributing)\n* [Contact](#contact)\n\n<br>\n\n## Basic Description <a name=\"basic-description\"></a>\n\nThe objective of this package is to allow you to easily test your GCP Cloud Functions locally before deploying them. The package relies on Google's functions-framework to launch a local server that receives requests triggering your Cloud Function. Even though the functions are triggered through a request in the package's testing mechanism, event-triggered Cloud Functions are also supported.\nThe package's interface is intended to look similar to that of popular python unit-testing libraries. You can define different tests as classes in a python file, specifying the input and the expected output. You can then launch the tests from the cli and the results are printed unto your terminal.\n\nLet's say your Cloud Function is defined in the following file `main.py` with an entrypoint function `main`\n```python\ndef main(request):\n    value = request.get_json()\n    if \"a\" not in value:\n        return (\"Error\", 400)\n    return ({**value, \"b\": 2}, 200)\n```\n\nYou can define your tests in a file called `cf_tests.py` in the same directory\n```python\nclass CorrectInput:\n    data = {\"a\": 1}\n    headers = {'Content-Type': 'application/json'}\n    status_code = 200\n    output = {\"a\": 1, \"b\": 2}\n\nclass IncorrectInput:\n    data = {1: 2}\n    headers = {'Content-Type': 'application/json'}\n    status_code = 400\n\nclass CrashInput:\n    data = [\"a\"]\n    headers = {'Content-Type': 'application/json'}\n    error = True\n\nclass OopsUnintendedError:\n    data = {\"a\": \"1\"}\n    headers = {'Content-Type': 'application/json'}\n    status_code = 200\n    output = {\"a\": 1, \"b\": 2}\n```\n\nYou can then launch the tests by entering in your terminal `cloud-functions-test`\nYou would see the following output in your terminal (with colors):\n```\n================================================ Running 4 tests from the cf_tests module... =================================================\ntest CorrectInput in 0.009216s:  PASSED\ntest IncorrectInput in 0.002605s:  PASSED\ntest CrashInput in 0.007339s:  PASSED\ntest OopsUnintendedError in 0.002422s:  FAILED\n*** 3 tests passed and 1 failed ***\n=================================================================== FAILED ===================================================================\ntest OopsUnintendedError\nUnexpected output\n- expected: {'a': 1, 'b': 2}\n- received: {'a': '1', 'b': 2}\n```\n\n<br>\n\n## Installation <a name=\"installation\"></a>\n\nTo install the latest version of the package:\n\n```bash\npip install cloud-functions-test\n```\n\n<br>\n\n## Using cloud-functions-test <a name=\"using-cloud-functions-test\"></a>\n\n### General Functioning <a name=\"general-functioning\"></a>\n\nIn your test module, `cf_tests.py` by default, you need to create a basic class for each of your test.\n\nEach class can have a set of attributes that work as parameters for the test. All of those attributes are optional. If none are specified, the test will return \"PASSED\" if the Cloud Function ran without crashing.\n\nIf you do specifiy some of those attributes, you need to make sure their value is of a supported type.\n\nThere are 2 possible attributes that are common to both http-triggered and event-triggerd functions:\n* `error` (bool): indicates whether the test is expected to raise an Exception. The test will succeed if the function crashes while it will fail if it runs without error\n* `display_logs` (bool): indicates whether the logs and the return value should be displayed even in case of success (they are always displayed in case of failure of the test)\n\n\n### Http-triggered Functions <a name=\"http-triggered-functions\"></a>\n\nThis package has primarily been made for http-triggered Cloud Function so your test classes will be considered to be for this kind of function by default.\n\nYou can specify 4 additional attributes for those:\n* `data` (dict, list): the payload that will be included in the request triggering your function\n* `headers` (dict): the headers that will be included in the request triggering your function\n* `status_code` (dict, list): the status code your function is expected to return giving the parameters provided\n* `output` (dict, list): the output your function is expected to return giving the parameters provided. Details on the specific structure the value of this attribute can take are specified below\n\n\n### Wildcards for Expected Content <a name=\"wildcards-for-expected-content\"></a>\n\nIt's quite common to want to check the validity of the output of an http-triggered Cloud Function without knowing the exact value of the expected output. This happens for instance if you Cloud Function is calling an external service or if your output includes an \"updated_at\" timestamp. In that case, you may want to test that the structure of the output is correct rather than the exact content.\n\nThere are 3 types of wildcards your can use for that purpose:\n* Ellipsis\n    * If you include this object in a list, the test will only check that the other elements of the list are found in the actual output\n    ```\n    A:\n        output = [\"a\", Ellipsis]\n    ```\n    This will match with `actual_output = [\"a\", \"b\", \"c\"]`\n    * If you pass this object as a key in a dict, the test will only check that all other key/value pairs are found in the actual output\n    ```\n    A:\n        output = {\"a\": 1, Ellipsis: Ellipsis}\n    ```\n    This will match with `actual_output = {\"a\": 1, \"b\": 2, \"c\": 3}`\n* Types\n\n    You can include both basic/collection types (int, float, str, list, dict) and types from the typing package (Any, Union, List, Tuple, Dict) in the expected output.\n    * Simple case\n        ```\n        A:\n            output = List[int]\n        ```\n        This will match with `actual_output = [1, 2]`\n    * More complex example\n        ```\n        A:\n            output = Tuple[dict, List[int], Union[int, str]]\n        ```\n        This will match with `actual_output = [{\"a\": 1}, [1, 2], \"b\"]`\n* Regex\n\n    You can include an object of type re.Pattern in your expected output. If the actual output is a string, it will be matched with the pattern.\n    ```\n    A:\n        output = [\"a\", re.compile(r'^\\d+$')]\n    ```\n    This will match with `actual_output = [\"a\", \"123a\"]`\n\n\n### Event-triggered Functions <a name=\"event-triggered-functions\"></a>\n\nTesting event-triggered functions is not as straightforward as for http-triggered ones. Since we cannot fully simulate the underlying event for which GCP would trigger the function, we make a request to it as a workaround. In the definition of your test class, you can specify the event and the context you want your function to receive as dictionaries. The package will then pass them on your function.\n\nYou can specify 2 additional attributes for those:\n* `event` (dict): the event that your function will receive\n* `context` (dict): the context that you function will receive\n\n\n### Settings <a name=\"settings\"></a>\n\nThere are 5 options you can modify for running your tests. Those can typically be modified either in your test module or in the cli\n\n* module: defaults to \"cf_tests.py\", name of the module in which your test classes are defined\n\n   * cli: `cloud-functions-test --module <name_test_module>`\n\n\n* source: defaults to \"main.py\", path to the file in which your Cloud Function is defined\n\n   * cli: `cloud-functions-test --source <path_to_file>`\n   * in test module:\n   ```\n   import cloud_function_framework\n   cloud_function_framework.source = \"<path_to_file>\"\n   ```\n\n* entrypoint: defaults to \"main\", name of the Cloud Function entrypoint in the file in which your Cloud Function is defined\n\n   * cli: `cloud-functions-test --entrypoint <entrypoint>`\n   * in test module:\n   ```\n   import cloud_function_framework\n   cloud_function_framework.entrypoint = \"<entrypoint>\"\n   ```\n\n* env: defaults to \".env\", path to the file in which your environment variables are defined (nothing happens if the file does not exist)\n\n   * cli: `cloud-functions-test --env <env_file_path>`\n   * in test module:\n   ```\n   import cloud_function_framework\n   cloud_function_framework.env = \"<env_file_path>\"\n   ```\n   This file can either be a bash file organized as such:\n   ```bash\n   ENV=dev\n   FOO=bar\n   ```\n   Or a Terraform file. In this case, the package will do its best to infer the env variables from the structure of the file. Example of supported structure:\n   ```terraform\n    module \"test_module\" {\n      env         = var.env\n      environment_variables = {\n        ENV               = var.env,\n        LOCATION_ID       = \"europe-west1\",\n        NAME = \"test_${var.env}\",\n      }\n      memory_mb   = 512\n    }\n   ```\n\n* port: defaults to 8080, port of localhost that will be used by functions-framework to launch the local server receiving the requests that will trigger the Cloud Functions\n\n   * cli: `cloud-functions-test --port <port>`\n   * in test module:\n   ```\n   import cloud_function_framework\n   cloud_function_framework.port = <port>\n\n<br>\n\n## Contributing <a name=\"contributing\"></a>\n\nYour contribution is very much welcome, both through the creation of issues and through the opening of pull requests. To open a pull request:\n1. Fork the repo\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -m 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n<br>\n\n## Contact <a name=\"contacts\"></a>\n\nYou can reach out to me at: robin.picard@sciencespo.fr\n\n\n",
    "bugtrack_url": null,
    "license": "apache-2.0",
    "summary": "Test locally GCP Cloud Functions",
    "version": "0.0.2",
    "project_urls": {
        "Download": "https://github.com/RobinPicard/cloud-functions-test/releases/download/v0.0.1/cloud-functions-test-0.0.2.tar.gz",
        "Homepage": "https://github.com/RobinPicard/cloud-functions-test"
    },
    "split_keywords": [
        "python",
        "testing",
        "tests",
        "gcp",
        "cloud functions"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f6ab7817a740b92a5dfc4e031d5cb524e5c8ec71dfd741de2514992fc6c63cce",
                "md5": "8306442b090c4aadb1dda6ed13190085",
                "sha256": "72ac90ecf86e78ecdda3331f90d9be0fb069abe1652741e1a3fa4ee9a6c77261"
            },
            "downloads": -1,
            "filename": "cloud-functions-test-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "8306442b090c4aadb1dda6ed13190085",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 19732,
            "upload_time": "2023-09-03T22:22:30",
            "upload_time_iso_8601": "2023-09-03T22:22:30.822975Z",
            "url": "https://files.pythonhosted.org/packages/f6/ab/7817a740b92a5dfc4e031d5cb524e5c8ec71dfd741de2514992fc6c63cce/cloud-functions-test-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-03 22:22:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "RobinPicard",
    "github_project": "cloud-functions-test",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "cloud-functions-test"
}
        
Elapsed time: 2.28031s