GithubHelp home page GithubHelp logo

jugi92 / dbutilsmock Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 11 KB

Mock to use for Databricks Utils (dbutils) whenever it is not installed, e.g. on you local machine

License: MIT License

Python 98.49% Makefile 1.51%

dbutilsmock's Introduction

dbutilsMock

Simple Mock for dbutils functions that can be used whenever dbutils is not available, e.g. for unittesting databricks notebooks locally

Use in the following way: Before your test initiate the dbutils Mock:

from dbutilsmock.dbutilsmock import DbutilsMock
dbutils = DbutilsMock(
    widgets_dict={
        "input_path": "/test/asd",
        "out_path": "/out/test"
    },
    secrets_dict={
        "my_scope": {
            "my_key": "the_real_secret"
        }
    }
)

Then in your test code the following code should work:

>>>dbutils.widgets.text(name="widget_name", defaultValue="defaultWidgetValue", label="WidgetLabel")
>>>dbutils.widgets.get("input_path")
'/test/asd'
>>>dbutils.secrets.get("my_scope", "my_key")
'the_real_secret'

In case you want to test the function random_number in a notebook like this:

from random import random

path = dbutils.widgets.get("input_path")

def random_number():
    r = random()
    print(r)
    return r

You can inject dbutils in the test for that notebook by using the following code:

from dbutilsmock.dbutilsmock import DbutilsMock
import unittest

class TestNotebookMethods(unittest.TestCase):
    def setUp(self):
        import builtins
        builtins.dbutils = DbutilsMock(
            widgets_dict={
                "input_path": "/in/test"
            }
        )

    def test_random_number(self):
        from notebook_with_dbutils import random_number
        self.assertIsInstance(random_number(), float)

The previous error NameError: name 'dbutils' is not defined will be solved and you can test your function.

Installation

pip install git+https://github.com/jugi92/dbutilsMock

dbutilsmock's People

Contributors

jugi92 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.