跳转到主要内容

提供分层工厂

项目描述

https://travis-ci.org/palankai/baluster.svg?branch=master

这个包是做什么的

此包提供了一种简单的方法来构建应用程序的后结构。可以用来构建作为资源工厂的复合根,构建用于测试的固定工厂。

特性

  • 延迟初始化

  • 简单的组合和依赖处理

示例 - 组合根

from baluster import Baluster, placeholders
import psycopg2

class ApplicationRoot(Baluster):
    @placeholders.factory
    def db(self, root):
        # Will be called at the first use
        # Will be cached so won't be called again
        return psycopg2.connect("dbname=test user=postgres")

    @db.close
    def _close_db(self, root, db):
        db.close()

    @placeholders.factory
    def cr(self, root):
        return self.db.cursor()

    @cr.close
    def _close_cr(self, root, cr):
        cr.close()


def main():
    approot = ApplicationRoot()
    with approot:
        approot.cr.execute('SELECT * FROM user')

    # at this point the connection and the cursor has already been closed

示例 - 异步组合根

from baluster import Baluster, placeholders

class AsyncApplicationRoot(Baluster):

    @placeholders.factory
    async def resource(self, root):
        # Will be called at the first use
        # Will be cached so won't be called again
        return await some_aync_resource()

    @db.close
    async def _close_resource(self, root, resource):
        await resource.close()


def main():
    approot = AsyncApplicationRoot()
    async with approot:
        conn = await approot.resource
        await conn.operation(...)

    # at this point the resource has already been closed

示例 - 测试固定工厂

from baluster import Baluster, placeholders
import psycopg2

class Fixtures(Baluster):

    @placeholders.factory
    def cr(self, root):
        conn = psycopg2.connect("dbname=test user=postgres")
        return conn.cursor()

    class users(Baluster):

        @placeholders.factory
        def user(self, root):
            root.cr.execute('SELECT * FROM user WHERE id=1')
            return User(root.cr.fetchone())

        @placeholders.factory
        def customer(self, root):
            root.cr.execute('SELECT * FROM customer WHERE id=1')
            return Customer(root.cr.fetchone())

    class orders(Baluster):

        @placeholders.factory
        def amount(self, root):
            return 100

        @placeholders.factory
        def quantity(self, root):
            return 1

        @placeholders.factory
        def order(self, root):
            customer = root.users.customer
            created_by = root.users.user
            amount = self.amount
            # Fictive order object...
            return Order(
                customer=customer, created_by=created_by,
                amount=amount, quantity=quantity
            )

        @placeholders.factory
        def shipped_order(self, root):
            order = self.order
            order.mark_shipped()
            return order


def test_order():
    # Demonstrate a few use fictive usecase

    # Creating order with defaults
    f = Fixtures()
    assert f.order.calculated_total_value == 100
    assert f.order.shipping_address == f.users.customer.address

    # Create new fixtures, but keep some cached data
    f2 = f.copy('cr', 'users')

    # Set some value
    f2.order.amount = 50
    f2.order.quantity = 3
    assert f2.order.calculated_total_value == 150

    # Manage different stage of object life
    f3 = f.copy('cr', 'users')
    order = f3.shipped_order

    with pytest.raises(OrderException):
        order.cancel()
        # as it is shipped

安装

Python目标: >=3.6

$ pip install baluster

依赖项

该包是独立的,仅使用python标准库。

开发

pip install -r requirements-dev.txt

这将在开发模式下安装包(setup.py develop)和测试包。我希望达到近100%的测试覆盖率。

测试

pytest

贡献

我非常欢迎任何评论!如果您克隆我的代码并创建pull请求,我将非常高兴。对于批准的pull请求,flake8必须通过就像所有测试一样。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定该选择哪个,请了解有关安装包的更多信息。

源分布

Baluster-0.2.1.tar.gz (8.9 kB 查看哈希值)

上传于

由以下支持