1
# Copyright 2014 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""ORM-related test helpers."""
6
from __future__ import (
20
from maasserver.utils.orm import get_one
23
def reload_object(model_object):
24
"""Reload `obj` from the database.
26
Use this when a test needs to inspect changes to model objects made by
29
If the object has been deleted, this will return None.
31
:param model_object: Model object to reload.
32
:type model_object: Concrete `Model` subtype.
33
:return: Freshly-loaded instance of `model_object`, or None.
34
:rtype: Same as `model_object`.
36
model_class = model_object.__class__
37
return get_one(model_class.objects.filter(id=model_object.id))
40
def reload_objects(model_class, model_objects):
41
"""Reload `model_objects` of type `model_class` from the database.
43
Use this when a test needs to inspect changes to model objects made by
46
If any of the objects have been deleted, they will not be included in
49
:param model_class: `Model` class to reload from.
50
:type model_class: Class.
51
:param model_objects: Objects to reload from the database.
52
:type model_objects: Sequence of `model_class` objects.
53
:return: Reloaded objects, in no particular order.
54
:rtype: Sequence of `model_class` objects.
56
assert all(isinstance(obj, model_class) for obj in model_objects)
57
return model_class.objects.filter(
58
id__in=[obj.id for obj in model_objects])