fabric-testing


Namefabric-testing JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryTesting functionalities for Microsoft Fabric
upload_time2024-10-03 10:04:24
maintainerNone
docs_urlNone
authorLauJohansson
requires_python>=3.11
licenseMIT License Copyright (c) 2024 Lau Johansson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords microsoft fabric testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # fabric-testing
**Author: Lau Johansson**

![](https://debruyn.dev/2023/all-microsoft-fabric-icons-for-diagramming/Fabric_final_x256.png)


This library enables you to deploy python tests to [Microsoft Fabric](https://www.microsoft.com/en-us/microsoft-fabric).<br>

In short you can:
* Tests: Deploy and run custom tests
* Wheel: Deploy custom wheel that you want to test
* Requirements: Deploy custom requirements for the tests

With this CLI, it is possible to test functionalities like ``CREATE TABLE`` or ``MERGE INTO``.<br>
Also, working with Fabric built-in libraries like the ``notebookutils``. <br>

## Why do you need it?
With this CLI, you can run tests like the one below. It will enable you to run on-cluster
testing.

This will especially help two scenarios:
* CICD - automated testing: Submit and fetch tests in your Azure Pipelines / Github Workflows
* In a local development setup (your own computer), where you develop using a IDE - you can now deploy tests directly into Fabric

Lets say, you want to test some logic you have made, that creates a table and insert some data. 

How can you test the following code for Fabric today?  (Without actually have a Fabric open)


```python
import unittest
import uuid
from pyspark.sql import SparkSession


class CreateTable(unittest.TestCase):
    """
    This is an example of interacting with the default lakehouse.
    
    """
    def test_create_table(self):
        spark = SparkSession \
        .builder \
        .appName("fabric-testing example") \
        .config("spark.some.config.option", "some-value") \
        .getOrCreate()
        
        _uuid = _uuid = uuid.uuid4().hex

        table_name = f"test_table_{_uuid}"
        
        # Create table in default lakehouse
        spark.sql(f"CREATE TABLE {table_name} (col1 int)")
        
        # Insert data into table
        spark.sql(f"INSERT INTO {table_name} values (22)")

        # Assert data in the table
        table_with_one_row = spark.sql(f"select * from {table_name}")
        self.assertIsNotNone(table_with_one_row.count(), 1)
```

## Installation

[![PyPI version](https://badge.fury.io/py/fabric-testing.svg)](https://pypi.org/project/fabric-testing/)
[![PyPI](https://img.shields.io/pypi/dm/fabric-testing)](https://pypi.org/project/fabric-testing/)
```powershell
pip install fabric-testing
```

## CLI Usage

Deploy python tests to Fabric. You do not necessarily **need** to add a wheel or requirements, 
maybe you just have some tests you want to run inside Fabric:

```powershell
fabric-testing-submit `
    --tests-path <path> `
    --whl-path <path> ` #(optional)
    --requirements-file <path> ` #(optional)
    --workspace-name <name> `
    --workspace-id <id> `
    --lakehouse-name <name> `
    --lakehouse-id <id>
```

Fetch the notebook status from Fabric:
```powershell
fabric-testing-fetch `
    --tenant-id <your-tenant-id> `
    --url <your-fetch-url>

# or if you want to use logged fetch url
fabric-testing-fetch `
    --tenant-id <your-tenant-id> `
    --fetch-url-log-file-path <path-to-log-file>
```

If you want to follow along more "interactively", you can find the test run in the [Fabric Monitor](https://app.fabric.microsoft.com/monitoringhub?experience=data-engineering):


The interaction with OneLake use default azure login settings. 
Therefore, remember to login to the expected user (or spn):

```powershell
az login --tenant <tenant-id>
```

## How does it work?

The submit CLI does the following:
* Collects wheel, requirement file and python tests
* Uploads to OneLake
* Runs a test notebook as a job inside Fabric

The fetch CLI does the following:
* Load fetch url from submit
* Poll status from the Jobs API (the Fabric Monitor)

## Authentication support

### Fetch
Fetch should be fully supported both User and Service Principal

### Submit
Submit is only fully supported for User.

Currently the Job scheduler API only support ``User`` identity.

Fabric-testing library are expected to work for service principals
when these APIs support the identity. See the documentation:

* https://learn.microsoft.com/en-us/rest/api/fabric/core/job-scheduler/get-item-job-instance?tabs=HTTP
* https://learn.microsoft.com/en-us/rest/api/fabric/core/job-scheduler/get-item-schedule?tabs=HTTP


## Prerequisites
* Python 3.x (align the Fabric version with the version used in your tests)
* A Microsoft Fabric subscription
* Azure CLI (for authentication)
* Required permissions in the Fabric workspace (access to OneLake, ability to create and run notebooks)
  * User or service principal needs at the right level of access to the Fabric workspace
    * Needs access to onelake
    * Needs access to create and run notebooks
    * If your tests require other permissions, of course, give the permission to the SPN


# Limitations and known issues

* Ensure that the lakehouse for the test-upload (OneLake) in the same workspace as where you execute the notebook runs
* Referencing between test-files will cause import errors
* The test notebook that gets uploaded uses ``!pip`` and not ``%pip`` - although Microsoft recommends the last one.
  * This is due to challenges in activating inline-installation when running an on-demand job


# Future improvements
* Automatically extracts workspaceId and lakehouseId, so the user only needs to add names
* If the [create notebook API](https://learn.microsoft.com/en-us/rest/api/fabric/notebook/items/create-notebook?tabs=HTTP) supports uploading a base64 encoded .py file, instead of uploading .ipynb files - that could perhaps be preferable.
* Make it possible distinguish between the lakehouse for uploading the load test-files and the lakehouse that is chosen as default when testing

## Contributing

Contributions are welcome! To contribute:
- Fork the repository
- Create a new branch for your feature or bug fix
- Submit a pull request

Please ensure that your code follows the existing style and includes unit tests for any new features.
See the ``pyproject.toml`` or the ``.github/workflows/pr.yml`` to inspect which ruff format/linting checks are made, and which tests are executed.


## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Changelog

### Version 0.1.0
- Initial release with CLI for submitting and fetching tests from Microsoft Fabric


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fabric-testing",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "Microsoft, Fabric, testing",
    "author": "LauJohansson",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/fc/1c/31981c20c470fbedd10cd71bb0d30fece8fe7c1515edad9b4b4c8d8208cf/fabric_testing-0.1.0.tar.gz",
    "platform": null,
    "description": "# fabric-testing\n**Author: Lau Johansson**\n\n![](https://debruyn.dev/2023/all-microsoft-fabric-icons-for-diagramming/Fabric_final_x256.png)\n\n\nThis library enables you to deploy python tests to [Microsoft Fabric](https://www.microsoft.com/en-us/microsoft-fabric).<br>\n\nIn short you can:\n* Tests: Deploy and run custom tests\n* Wheel: Deploy custom wheel that you want to test\n* Requirements: Deploy custom requirements for the tests\n\nWith this CLI, it is possible to test functionalities like ``CREATE TABLE`` or ``MERGE INTO``.<br>\nAlso, working with Fabric built-in libraries like the ``notebookutils``. <br>\n\n## Why do you need it?\nWith this CLI, you can run tests like the one below. It will enable you to run on-cluster\ntesting.\n\nThis will especially help two scenarios:\n* CICD - automated testing: Submit and fetch tests in your Azure Pipelines / Github Workflows\n* In a local development setup (your own computer), where you develop using a IDE - you can now deploy tests directly into Fabric\n\nLets say, you want to test some logic you have made, that creates a table and insert some data. \n\nHow can you test the following code for Fabric today?  (Without actually have a Fabric open)\n\n\n```python\nimport unittest\nimport uuid\nfrom pyspark.sql import SparkSession\n\n\nclass CreateTable(unittest.TestCase):\n    \"\"\"\n    This is an example of interacting with the default lakehouse.\n    \n    \"\"\"\n    def test_create_table(self):\n        spark = SparkSession \\\n        .builder \\\n        .appName(\"fabric-testing example\") \\\n        .config(\"spark.some.config.option\", \"some-value\") \\\n        .getOrCreate()\n        \n        _uuid = _uuid = uuid.uuid4().hex\n\n        table_name = f\"test_table_{_uuid}\"\n        \n        # Create table in default lakehouse\n        spark.sql(f\"CREATE TABLE {table_name} (col1 int)\")\n        \n        # Insert data into table\n        spark.sql(f\"INSERT INTO {table_name} values (22)\")\n\n        # Assert data in the table\n        table_with_one_row = spark.sql(f\"select * from {table_name}\")\n        self.assertIsNotNone(table_with_one_row.count(), 1)\n```\n\n## Installation\n\n[![PyPI version](https://badge.fury.io/py/fabric-testing.svg)](https://pypi.org/project/fabric-testing/)\n[![PyPI](https://img.shields.io/pypi/dm/fabric-testing)](https://pypi.org/project/fabric-testing/)\n```powershell\npip install fabric-testing\n```\n\n## CLI Usage\n\nDeploy python tests to Fabric. You do not necessarily **need** to add a wheel or requirements, \nmaybe you just have some tests you want to run inside Fabric:\n\n```powershell\nfabric-testing-submit `\n    --tests-path <path> `\n    --whl-path <path> ` #(optional)\n    --requirements-file <path> ` #(optional)\n    --workspace-name <name> `\n    --workspace-id <id> `\n    --lakehouse-name <name> `\n    --lakehouse-id <id>\n```\n\nFetch the notebook status from Fabric:\n```powershell\nfabric-testing-fetch `\n    --tenant-id <your-tenant-id> `\n    --url <your-fetch-url>\n\n# or if you want to use logged fetch url\nfabric-testing-fetch `\n    --tenant-id <your-tenant-id> `\n    --fetch-url-log-file-path <path-to-log-file>\n```\n\nIf you want to follow along more \"interactively\", you can find the test run in the [Fabric Monitor](https://app.fabric.microsoft.com/monitoringhub?experience=data-engineering):\n\n\nThe interaction with OneLake use default azure login settings. \nTherefore, remember to login to the expected user (or spn):\n\n```powershell\naz login --tenant <tenant-id>\n```\n\n## How does it work?\n\nThe submit CLI does the following:\n* Collects wheel, requirement file and python tests\n* Uploads to OneLake\n* Runs a test notebook as a job inside Fabric\n\nThe fetch CLI does the following:\n* Load fetch url from submit\n* Poll status from the Jobs API (the Fabric Monitor)\n\n## Authentication support\n\n### Fetch\nFetch should be fully supported both User and Service Principal\n\n### Submit\nSubmit is only fully supported for User.\n\nCurrently the Job scheduler API only support ``User`` identity.\n\nFabric-testing library are expected to work for service principals\nwhen these APIs support the identity. See the documentation:\n\n* https://learn.microsoft.com/en-us/rest/api/fabric/core/job-scheduler/get-item-job-instance?tabs=HTTP\n* https://learn.microsoft.com/en-us/rest/api/fabric/core/job-scheduler/get-item-schedule?tabs=HTTP\n\n\n## Prerequisites\n* Python 3.x (align the Fabric version with the version used in your tests)\n* A Microsoft Fabric subscription\n* Azure CLI (for authentication)\n* Required permissions in the Fabric workspace (access to OneLake, ability to create and run notebooks)\n  * User or service principal needs at the right level of access to the Fabric workspace\n    * Needs access to onelake\n    * Needs access to create and run notebooks\n    * If your tests require other permissions, of course, give the permission to the SPN\n\n\n# Limitations and known issues\n\n* Ensure that the lakehouse for the test-upload (OneLake) in the same workspace as where you execute the notebook runs\n* Referencing between test-files will cause import errors\n* The test notebook that gets uploaded uses ``!pip`` and not ``%pip`` - although Microsoft recommends the last one.\n  * This is due to challenges in activating inline-installation when running an on-demand job\n\n\n# Future improvements\n* Automatically extracts workspaceId and lakehouseId, so the user only needs to add names\n* If the [create notebook API](https://learn.microsoft.com/en-us/rest/api/fabric/notebook/items/create-notebook?tabs=HTTP) supports uploading a base64 encoded .py file, instead of uploading .ipynb files - that could perhaps be preferable.\n* Make it possible distinguish between the lakehouse for uploading the load test-files and the lakehouse that is chosen as default when testing\n\n## Contributing\n\nContributions are welcome! To contribute:\n- Fork the repository\n- Create a new branch for your feature or bug fix\n- Submit a pull request\n\nPlease ensure that your code follows the existing style and includes unit tests for any new features.\nSee the ``pyproject.toml`` or the ``.github/workflows/pr.yml`` to inspect which ruff format/linting checks are made, and which tests are executed.\n\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Changelog\n\n### Version 0.1.0\n- Initial release with CLI for submitting and fetching tests from Microsoft Fabric\n\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Lau Johansson  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Testing functionalities for Microsoft Fabric",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/LauJohansson/fabric-testing"
    },
    "split_keywords": [
        "microsoft",
        " fabric",
        " testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3dcad20720a8bdb06e8b70e300b8787ca0f0fe976965c729d1730b961d26d33c",
                "md5": "9557e1341084bc02a918da576892cadc",
                "sha256": "f476d508871e6a8729bd100e32cac9cb7d342b307b275f5313be1d3fa9de0afa"
            },
            "downloads": -1,
            "filename": "fabric_testing-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9557e1341084bc02a918da576892cadc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 26307,
            "upload_time": "2024-10-03T10:04:23",
            "upload_time_iso_8601": "2024-10-03T10:04:23.522287Z",
            "url": "https://files.pythonhosted.org/packages/3d/ca/d20720a8bdb06e8b70e300b8787ca0f0fe976965c729d1730b961d26d33c/fabric_testing-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fc1c31981c20c470fbedd10cd71bb0d30fece8fe7c1515edad9b4b4c8d8208cf",
                "md5": "c1b866ef2ec105bc1e6030c33453945d",
                "sha256": "e39975cdcedbd355b7738098afeb6ee7bd3e3999e18b2545d7fca8e1b340b6d0"
            },
            "downloads": -1,
            "filename": "fabric_testing-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c1b866ef2ec105bc1e6030c33453945d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 19139,
            "upload_time": "2024-10-03T10:04:24",
            "upload_time_iso_8601": "2024-10-03T10:04:24.471753Z",
            "url": "https://files.pythonhosted.org/packages/fc/1c/31981c20c470fbedd10cd71bb0d30fece8fe7c1515edad9b4b4c8d8208cf/fabric_testing-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-03 10:04:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "LauJohansson",
    "github_project": "fabric-testing",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "fabric-testing"
}
        
Elapsed time: 1.12803s