Coder Social home page Coder Social logo

dsemu's Introduction

dsemu

Description

dsemu is a simple library to help with testing GCP Datastore code written in Python. The provided Emulator class wraps the Datastore emulator and provides basic functionality such as starting/stopping and reseting the emulator instance from inside the test runner.

Requirements

You must have the gcloud tool installed and available in PATH.

Using existing instance of the emulator

If you're running tests that require datastore access frequently it might be better to keep an instance of the emulator running at all time instead of letting the wrapper start and stop it for the duration of the test run. If an instance of the emulator is running and the required environment variables are correctly set the Emulator wrapper will use the running instance instead of starting a new one and will not tear it down at the end of the test run.

Example usage with pytest

# conftest.py
import pytest
from dsemu import Emulator
from google.cloud import datastore


@pytest.fixture(scope="session")
def emulator():
    with Emulator() as emulator:
        yield emulator


@pytest.fixture(scope="session")
def session_client():
    client = datastore.Client(project="test")
    yield client


@pytest.fixture()
def client(emulator: Emulator, session_client: datastore.Client):
    emulator.reset()
    yield session_client
# datastore_test.py
from google.cloud import datastore


def test_datastore_put_and_get(client: datastore.Client):
    kind = "Task"
    name = "sampletask1"
    task_key = client.key(kind, name)
    task = datastore.Entity(key=task_key)
    task["description"] = "Buy milk"
    client.put(task)

    res = client.get(task_key)
    assert res == task

dsemu's People

Contributors

fwojciec avatar joshink 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.