All Projects → miki725 → alchemy-mock

miki725 / alchemy-mock

Licence: other
SQLAlchemy mock helpers.

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to alchemy-mock

flask-template
Template for creating Flask based projects
Stars: ✭ 60 (-18.92%)
Mutual labels:  sqlalchemy
vue3-element-admin
🎉 基于 vite2 + vue3 + element-plus 的后台管理系统vue3-element-admin;使用vue-cli可以切换webpack分支
Stars: ✭ 79 (+6.76%)
Mutual labels:  mock
miz
🎯 Generate fake data, Just like a person.
Stars: ✭ 24 (-67.57%)
Mutual labels:  mock
Rubicon
Swift parser + mock generator
Stars: ✭ 42 (-43.24%)
Mutual labels:  mock
shillelagh
Making it easy to query APIs via SQL
Stars: ✭ 172 (+132.43%)
Mutual labels:  sqlalchemy
vue-antdesign-admin-template
Ant Design Pro Vue Template
Stars: ✭ 127 (+71.62%)
Mutual labels:  mock
definject
Unobtrusive Dependency Injector for Elixir
Stars: ✭ 46 (-37.84%)
Mutual labels:  mock
umi-dva-typescript-mock
基于umi + dva + typescript + mock + antd的react框架,内置PWA
Stars: ✭ 17 (-77.03%)
Mutual labels:  mock
node-mock-examples
Examples of tests that mock Node system APIs: fs, http, child_process, timers
Stars: ✭ 38 (-48.65%)
Mutual labels:  mock
open-api-mocker
A mock server based in OpenAPI Specification
Stars: ✭ 58 (-21.62%)
Mutual labels:  mock
swagger-to-mock
Mock data generator CLI for Swagger3 (OpenAPI 3)
Stars: ✭ 49 (-33.78%)
Mutual labels:  mock
xmocker-cli
为前端提供mock的后台数据,
Stars: ✭ 34 (-54.05%)
Mutual labels:  mock
favv
Fullstack Web Application Framework With FastAPI + Vite + VueJS. Streamlit for rapid development.
Stars: ✭ 17 (-77.03%)
Mutual labels:  sqlalchemy
jmeter-k8s-starterkit
Ultimate starterkit for JMeter in Kubernetes. WIth live test reporting, JMeter monitoring, kubernetes monitoring and mock as a service
Stars: ✭ 40 (-45.95%)
Mutual labels:  mock
fastapi-saas-base
Fast API SAAS Base App
Stars: ✭ 47 (-36.49%)
Mutual labels:  sqlalchemy
hex-example
Little API to demonstrate various microservice design principles and technologies
Stars: ✭ 131 (+77.03%)
Mutual labels:  mock
swap
A Solver for the Wavelength Assignment Problem (RWA) in WDM networks
Stars: ✭ 27 (-63.51%)
Mutual labels:  sqlalchemy
dubbo-mock
dubbo mock web server
Stars: ✭ 62 (-16.22%)
Mutual labels:  mock
ChefAPI
API using FastAPI and PostgreSQL for sharing or keeping track of awesome food recipes Based on Oauth2 and JWT 💎
Stars: ✭ 16 (-78.38%)
Mutual labels:  sqlalchemy
Qwerkey
Qwerkey is a social media platform for connecting and learning more about mechanical keyboards built on React and Redux in the frontend and Flask in the backend on top of a PostgreSQL database.
Stars: ✭ 22 (-70.27%)
Mutual labels:  sqlalchemy

SQLAlchemy Mock

https://badge.fury.io/py/alchemy-mock.png https://travis-ci.org/miki725/alchemy-mock.png?branch=master https://coveralls.io/repos/miki725/alchemy-mock/badge.png?branch=master

SQLAlchemy mock helpers.

Installing

You can install alchemy-mock using pip:

$ pip install alchemy-mock

Why?

SQLAlchemy is awesome. Unittests are great. Accessing DB during tests - not so much. This library provides easy way to mock SQLAlchemy's session in unittests while preserving ability to do sane asserts. Normally SQLAlchemy's expressions cannot be easily compared as comparison on binary expression produces yet another binary expression:

>>> type((Model.foo == 5) == (Model.bar == 5))
<class 'sqlalchemy.sql.elements.BinaryExpression'>

But they can be compared with this library:

>>> ExpressionMatcher(Model.foo == 5) == (Model.bar == 5)
False

Using

ExpressionMatcher can be directly used:

>>> from alchemy_mock.comparison import ExpressionMatcher
>>> ExpressionMatcher(Model.foo == 5) == (Model.foo == 5)
True

Alternatively AlchemyMagicMock can be used to mock out SQLAlchemy session:

>>> from alchemy_mock.mocking import AlchemyMagicMock
>>> session = AlchemyMagicMock()
>>> session.query(Model).filter(Model.foo == 5).all()

>>> session.query.return_value.filter.assert_called_once_with(Model.foo == 5)

In real world though session can be interacted with multiple times to query some data. In those cases UnifiedAlchemyMagicMock can be used which combines various calls for easier assertions:

>>> from alchemy_mock.mocking import UnifiedAlchemyMagicMock
>>> session = UnifiedAlchemyMagicMock()

>>> m = session.query(Model)
>>> q = m.filter(Model.foo == 5)
>>> if condition:
...     q = q.filter(Model.bar > 10).all()
>>> data1 = q.all()
>>> data2 = m.filter(Model.note == 'hello world').all()

>>> session.filter.assert_has_calls([
...     mock.call(Model.foo == 5, Model.bar > 10),
...     mock.call(Model.note == 'hello world'),
... ])

Also real-data can be stubbed by criteria:

>>> from alchemy_mock.mocking import UnifiedAlchemyMagicMock
>>> session = UnifiedAlchemyMagicMock(data=[
...     (
...         [mock.call.query(Model),
...          mock.call.filter(Model.foo == 5, Model.bar > 10)],
...         [Model(foo=5, bar=11)]
...     ),
...     (
...         [mock.call.query(Model),
...          mock.call.filter(Model.note == 'hello world')],
...         [Model(note='hello world')]
...     ),
...     (
...         [mock.call.query(AnotherModel),
...          mock.call.filter(Model.foo == 5, Model.bar > 10)],
...         [AnotherModel(foo=5, bar=17)]
...     ),
... ])
>>> session.query(Model).filter(Model.foo == 5).filter(Model.bar > 10).all()
[Model(foo=5, bar=11)]
>>> session.query(Model).filter(Model.note == 'hello world').all()
[Model(note='hello world')]
>>> session.query(AnotherModel).filter(Model.foo == 5).filter(Model.bar > 10).all()
[AnotherModel(foo=5, bar=17)]
>>> session.query(AnotherModel).filter(Model.note == 'hello world').all()
[]

Finally UnifiedAlchemyMagicMock can partially fake session mutations such as session.add(instance). For example:

>>> session = UnifiedAlchemyMagicMock()
>>> session.add(Model(pk=1, foo='bar'))
>>> session.add(Model(pk=2, foo='baz'))
>>> session.query(Model).all()
[Model(foo='bar'), Model(foo='baz')]
>>> session.query(Model).get(1)
Model(foo='bar')
>>> session.query(Model).get(2)
Model(foo='baz')

Note that its partially correct since if added models are filtered on, session is unable to actually apply any filters so it returns everything:

>>> session.query(Model).filter(Model.foo == 'bar').all()
[Model(foo='bar'), Model(foo='baz')]
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].