def test_register(self): ctx = NewContext() ctx.register(Restaurant, id="rest", refs={"manager": IdRef('manager-staff')}) ctx.register(Kitchen, refs={'staff': IdRef('kitchen-staff')}) ctx.register(Cashier, refs={'staff': IdRef('manager-staff')}) ctx.register(EatingRoom, refs={'staff': IdRef('eatingroom-staff')}) ctx.register(Seat) ctx.register(Staff, id='kitchen-staff', consts={'name': 'kitchen-staff'}) ctx.register(Staff, id='eatingroom-staff', consts={'name': 'eatingroom-staff'}) ctx.register(Staff, id='manager-staff', consts={'name': 'manager-staff'}) rest: Restaurant = ctx.instance_by_id("rest") self.assertEqual(rest.manager.name, 'manager-staff') self.assertEqual(rest.kitchen.staff.name, 'kitchen-staff') self.assertEqual(rest.kitchen.number, 1) self.assertEqual(rest.cashier.staff, rest.manager) self.assertEqual(rest.eatingroom.staff.name, 'eatingroom-staff') self.assertEqual(rest.eatingroom.seat.id, 'default-seat') self.assertEqual(rest, ctx.instance_by_cls(Restaurant))
def test_register_to_xml(self): ctx = NewContext() ctx.register(Restaurant, id="rest", refs={"manager": IdRef('manager-staff')}) ctx.register(Kitchen, refs={'staff': IdRef('kitchen-staff')}) ctx.register(Cashier, refs={'staff': IdRef('manager-staff')}) ctx.register(EatingRoom, refs={'staff': IdRef('eatingroom-staff')}) ctx.register(Seat) ctx.register(Staff, id='kitchen-staff', consts={'name': 'kitchen-staff'}) ctx.register(Staff, id='eatingroom-staff', consts={'name': 'eatingroom-staff'}) ctx.register(Staff, id='manager-staff', consts={'name': 'manager-staff'}) self.assertEqual(self.xml_file.read_text(), ctx.format_beans_xml(indent=" "))
from di.decorator import bean from di.ref import IdRef from tests.app.cashier import Cashier from tests.app.eatingroom import EatingRoom from tests.app.kitchen import Kitchen from tests.app.staff import Staff ''' @bean 装饰器仅用于测试register_group. 对于register及register_file无影响 ''' @bean(id="rest", refs={"manager": IdRef("manager-staff")}) class Restaurant: def __init__(self, manager: Staff, kitchen: Kitchen, eatingroom: EatingRoom, cashier: Cashier): self.manager = manager self.kitchen = kitchen self.eatingroom = eatingroom self.cashier = cashier
from dataclasses import dataclass from di.decorator import bean from di.ref import IdRef from tests.app_dc.cashier import Cashier from tests.app_dc.eatingroom import EatingRoom from tests.app_dc.kitchen import Kitchen from tests.app_dc.staff import Staff ''' @bean 装饰器仅用于测试register_group. 对于register及register_file无影响 ''' @bean(id="rest", refs={"manager": IdRef("manager-staff")}, group="dc") @dataclass class Restaurant: manager: Staff kitchen: Kitchen eatingroom: EatingRoom cashier: Cashier
def instance_by_id(self, id: str): return self.instance(IdRef(id))
from di.decorator import bean from di.ref import IdRef from tests.app.seat import Seat from tests.app.staff import Staff @bean(refs={'staff': IdRef('eatingroom-staff')}) class EatingRoom: def __init__(self, staff: Staff, seat: Seat): # @TODO properties中的list好了后,可以将staff及seat修改为 List self.staff = staff self.seat = seat
from dataclasses import dataclass from di.decorator import bean from di.ref import IdRef from tests.app_dc.staff import Staff @bean(refs={'staff': IdRef('kitchen-staff')}, group="dc") @dataclass class Kitchen: staff: Staff number: int = 1
from di.decorator import bean from di.ref import IdRef from tests.app.staff import Staff @bean(refs={'staff': IdRef('manager-staff')}) class Cashier: def __init__(self, staff: Staff): self.staff = staff
from dataclasses import dataclass from di.decorator import bean from di.ref import IdRef from tests.app_dc.staff import Staff @bean(refs={'staff': IdRef('manager-staff')}, group="dc") @dataclass class Cashier: staff: Staff
from dataclasses import dataclass from di.decorator import bean from di.ref import IdRef from tests.app_dc.seat import Seat from tests.app_dc.staff import Staff @bean(refs={'staff': IdRef('eatingroom-staff')}, group="dc") @dataclass class EatingRoom: staff: Staff seat: Seat # @TODO properties中的list好了后,可以将staff及seat修改为 List
from di.decorator import bean from di.ref import IdRef from tests.app.staff import Staff @bean(refs={'staff': IdRef('kitchen-staff')}) class Kitchen: def __init__(self, staff: Staff, number: int = 1): self.staff = staff self.number = number