Iterate over SQLA Result / Query instead of .all()

This commit is contained in:
Xavier Bustamante Talavera 2018-09-11 22:51:13 +02:00
parent 39c79aef04
commit aa52367f03
6 changed files with 14 additions and 13 deletions

View file

@ -28,5 +28,4 @@ class DeviceView(View):
def find(self, args: dict): def find(self, args: dict):
"""Gets many devices.""" """Gets many devices."""
devices = Device.query.all() return self.schema.jsonify(Device.query, many=True)
return self.schema.jsonify(devices, many=True)

View file

@ -76,7 +76,7 @@ class Lot(Thing):
@classmethod @classmethod
def roots(cls): def roots(cls):
"""Gets the lots that are not under any other lot.""" """Gets the lots that are not under any other lot."""
return set(cls.query.join(cls.paths).filter(db.func.nlevel(Path.path) == 1).all()) return cls.query.join(cls.paths).filter(db.func.nlevel(Path.path) == 1)
def __repr__(self) -> str: def __repr__(self) -> str:
return '<Lot {0.name} devices={0.devices!r}>'.format(self) return '<Lot {0.name} devices={0.devices!r}>'.format(self)

View file

@ -1,15 +1,17 @@
import uuid import uuid
from datetime import datetime from datetime import datetime
from typing import Set, Union from typing import Iterable, Set, Union
from uuid import UUID from uuid import UUID
from sqlalchemy import Column from sqlalchemy import Column
from sqlalchemy.orm import relationship from sqlalchemy.orm import Query, relationship
from sqlalchemy_utils import Ltree from sqlalchemy_utils import Ltree
from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.resources.models import Thing from ereuse_devicehub.resources.models import Thing
LotQuery = Union[Query, Iterable['Lot']]
class Lot(Thing): class Lot(Thing):
id = ... # type: Column id = ... # type: Column
@ -26,18 +28,18 @@ class Lot(Thing):
self.devices = ... # type: Set[Device] self.devices = ... # type: Set[Device]
self.paths = ... # type: Set[Path] self.paths = ... # type: Set[Path]
def add_child(self, child: Union['Lot', uuid.UUID]): def add_child(self, child: Union[Lot, uuid.UUID]):
pass pass
def remove_child(self, child: 'Lot'): def remove_child(self, child: Lot):
pass pass
@classmethod @classmethod
def roots(cls): def roots(cls) -> LotQuery:
pass pass
@property @property
def children(self) -> Set['Lot']: def children(self) -> LotQuery:
pass pass

View file

@ -44,7 +44,7 @@ setup(
'psycopg2-binary', 'psycopg2-binary',
'python-stdnum', 'python-stdnum',
'PyYAML', 'PyYAML',
'teal>=0.2.0a13', 'teal>=0.2.0a14',
'requests', 'requests',
'requests-toolbelt', 'requests-toolbelt',
'sqlalchemy-utils[password, color, phone]', 'sqlalchemy-utils[password, color, phone]',

View file

@ -179,9 +179,9 @@ def test_lot_roots():
db.session.add_all(lots) db.session.add_all(lots)
db.session.flush() db.session.flush()
assert Lot.roots() == {l1, l2, l3} assert set(Lot.roots()) == {l1, l2, l3}
l1.add_child(l2) l1.add_child(l2)
assert Lot.roots() == {l1, l3} assert set(Lot.roots()) == {l1, l3}
@pytest.mark.usefixtures(conftest.auth_app_context.__name__) @pytest.mark.usefixtures(conftest.auth_app_context.__name__)

View file

@ -239,7 +239,7 @@ def test_snapshot_tag_inner_tag(tag_id: str, user: UserClient, app: Devicehub):
snapshot_and_check(user, b, snapshot_and_check(user, b,
event_types=(WorkbenchRate.t, AggregateRate.t, BenchmarkProcessor.t)) event_types=(WorkbenchRate.t, AggregateRate.t, BenchmarkProcessor.t))
with app.app_context(): with app.app_context():
tag, *_ = Tag.query.all() # type: Tag tag = Tag.query.one() # type: Tag
assert tag.device_id == 1, 'Tag should be linked to the first device' assert tag.device_id == 1, 'Tag should be linked to the first device'