-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy path__init__.py
More file actions
48 lines (35 loc) · 1.49 KB
/
__init__.py
File metadata and controls
48 lines (35 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Testing framework for maas.client.viscera"""
__all__ = ["bind"]
from collections.abc import Mapping
from itertools import chain
from unittest.mock import Mock
from .. import OriginBase
from ...testing import AsyncCallableMock
def bind(*objects, session=None):
"""Bind `objects` into a new `Origin` derived from `session`.
The origin is constructed using `maas.client.viscera.OriginBase` hence
the only other objects that one object may refer to are those given in
`objects`. It's exactly like a minimally populated `Origin`.
If provided, `session` is used when constructing the `OriginBase`
instance, otherwise a mock is substituted. This mock has an empty
`handlers` mapping.
:param objects: Any number of `Object` classes, or mappings of `Object`
classes from the names with which they should be bound in the origin
that's created and returned.
:param session: A `bones.SessionAPI` instance.
:return: An `OriginBase` instance.
"""
def _flatten_to_items(thing):
if isinstance(thing, Mapping):
yield from thing.items()
else:
yield thing.__name__, thing
objects = map(_flatten_to_items, objects)
objects = chain.from_iterable(objects)
objects = dict(objects)
if session is None:
session = Mock(name="session")
session.handlers = {
name: AsyncCallableMock(name="handler(%s)" % name) for name in objects
}
return OriginBase(session, objects=objects)