Skip to main content

The package for working with SQLAlchemy in unit tests

Project description

sqlalchemy-mock

The package for working with SQLAlchemy in unit tests, it mocks requests to database, and provides necessary functionality within unit tests

It doesn't work with existing databases and doesn't create any tests databases, thanks to this it works enouth fast

Installing

Exists a few aproaches to install package:

  • You can clone this repository and run the next command to install package from local sourse:
    pip install -e /path/to/repository
    
  • You can install this package from github:
    pip install git+https://github.com/ivanostapiuk/sqlalchemy-mock.git
    
  • There is a package also in pypi:
    pip install sqlalchemy-mock
    

How to use

By default primary key is 'uuid', but you can set another field as primary key and function to generate value, for example:

import uuid


db = Session(primary_key="uuid", primary_key_generate=lambda: str(uuid.uuid4()))

A simple example how to use it in flask:

import unittest
from app import flask_app
from sqlalchemy_mock import Session
from models import Model


class TestCase(unittest.TestCase):
    @classmethod
    def setUp(cls):
        cls.app = flask_app.test_client()
        cls.db = Session()

    def test(self):
        row = Model(field1="value1", field2="value2", field3="value3")
        self.db.add(row)
        self.db.commit()

        with self.db.mock_session():
            response = self.app.get(f"/get/model/object/{row.uuid}")
            self.assertEqual(response.json["field1"], row.field1)

Runinng unittests

To run unittests use command:

python -m unittest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_mock-1.0.8.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_mock-1.0.8-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_mock-1.0.8.tar.gz.

File metadata

  • Download URL: sqlalchemy_mock-1.0.8.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.6 Darwin/22.5.0

File hashes

Hashes for sqlalchemy_mock-1.0.8.tar.gz
Algorithm Hash digest
SHA256 32c8d6c2fe139f3a53b02455dcaf43c268d34c9732f514e2595bbdbfabd7f6f4
MD5 422e6308ca5bbb8b79dda0d9c65a076e
BLAKE2b-256 98f4c24f819925d25b8b1d478fa32f565531525625ecc5069d02b4a3e5d81f5a

See more details on using hashes here.

File details

Details for the file sqlalchemy_mock-1.0.8-py3-none-any.whl.

File metadata

  • Download URL: sqlalchemy_mock-1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.6 Darwin/22.5.0

File hashes

Hashes for sqlalchemy_mock-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 729abd33d53236be08e247b6f095c582d2cd4fcc71ef9b7b81ae1550303a7201
MD5 90a2766d50df3302dcf2bc347febe716
BLAKE2b-256 ec95ad399084f9fa7ec9a71f935c7e611a6d2b16a0fd8adf7430b7388056d805

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page