Skip to content

Commit

Permalink
Use first(flat=True) where applicable and add pylint ignore where i…
Browse files Browse the repository at this point in the history
…t is being a pita
  • Loading branch information
sphuber committed Mar 9, 2022
1 parent fe0d3a9 commit 952ed5a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
3 changes: 1 addition & 2 deletions aiida/orm/nodes/data/upf.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def upload_upf_family(folder, group_label, group_description, stop_if_existing=T
md5sum = md5_file(filename)
builder = orm.QueryBuilder(backend=backend)
builder.append(UpfData, filters={'attributes.md5': {'==': md5sum}})
existing_upf = builder.first()
existing_upf = builder.first(flat=True)

if existing_upf is None:
# return the upfdata instances, not stored
Expand All @@ -133,7 +133,6 @@ def upload_upf_family(folder, group_label, group_description, stop_if_existing=T
else:
if stop_if_existing:
raise ValueError(f'A UPF with identical MD5 to {filename} cannot be added with stop_if_existing')
existing_upf = existing_upf[0]
pseudo_and_created.append((existing_upf, False))

# check whether pseudo are unique per element
Expand Down
4 changes: 2 additions & 2 deletions aiida/restapi/translator/nodes/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def _get_content(self):
return {}

# otherwise ...
node = self.qbobj.first()[0]
node = self.qbobj.first()[0] # pylint: disable=unsubscriptable-object

# content/attributes
if self._content_type == 'attributes':
Expand Down Expand Up @@ -643,7 +643,7 @@ def get_node_description(node):
nodes = []

if qb_obj.count() > 0:
main_node = qb_obj.first()[0]
main_node = qb_obj.first(flat=True)
pk = main_node.pk
uuid = main_node.uuid
nodetype = main_node.node_type
Expand Down
4 changes: 2 additions & 2 deletions tests/orm/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def test_group_uuid_hashing_for_querybuidler(self):
# Search for the UUID of the stored group
builder = orm.QueryBuilder()
builder.append(orm.Group, project=['uuid'], filters={'label': {'==': 'test_group'}})
[uuid] = builder.first()
uuid = builder.first(flat=True)

# Look the node with the previously returned UUID
builder = orm.QueryBuilder()
Expand All @@ -279,7 +279,7 @@ def test_group_uuid_hashing_for_querybuidler(self):

# And that the results are correct
assert builder.count() == 1
assert builder.first()[0] == group.id
assert builder.first(flat=True) == group.id


@pytest.mark.usefixtures('aiida_profile_clean')
Expand Down
11 changes: 7 additions & 4 deletions tests/orm/test_querybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,16 @@ def test_first_multiple_projections(self):
orm.Data().store()
orm.Data().store()

result = orm.QueryBuilder().append(orm.User, tag='user',
project=['email']).append(orm.Data, with_user='user', project=['*']).first()
query = orm.QueryBuilder()
query.append(orm.User, tag='user', project=['email'])
query.append(orm.Data, with_user='user', project=['*'])

result = query.first()

assert isinstance(result, list)
assert len(result) == 2
assert isinstance(result[0], str)
assert isinstance(result[1], orm.Data)
assert isinstance(result[0], str) # pylint: disable=unsubscriptable-object
assert isinstance(result[1], orm.Data) # pylint: disable=unsubscriptable-object


class TestRepresentations:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_node_uuid_hashing_for_querybuidler(self):
# Search for the UUID of the stored node
qb = orm.QueryBuilder()
qb.append(orm.Data, project=['uuid'], filters={'id': {'==': n.id}})
[uuid] = qb.first()
uuid = qb.first(flat=True)

# Look the node with the previously returned UUID
qb = orm.QueryBuilder()
Expand All @@ -99,7 +99,7 @@ def test_node_uuid_hashing_for_querybuidler(self):
qb.all()
# And that the results are correct
assert qb.count() == 1
assert qb.first()[0] == n.id
assert qb.first(flat=True) == n.id

@staticmethod
def create_folderdata_with_empty_file():
Expand Down
20 changes: 10 additions & 10 deletions tests/tools/archive/orm/test_computers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ def test_same_computer_import(tmp_path, aiida_profile_clean, aiida_localhost):
builder = orm.QueryBuilder()
builder.append(orm.CalcJobNode, project=['label'])
assert builder.count() == 1, 'Only one calculation should be found.'
assert str(builder.first()[0]) == calc1_label, 'The calculation label is not correct.'
assert str(builder.first(flat=True)) == calc1_label, 'The calculation label is not correct.'

# Check that the referenced computer is imported correctly.
builder = orm.QueryBuilder()
builder.append(orm.Computer, project=['label', 'uuid', 'id'])
assert builder.count() == 1, 'Only one computer should be found.'
assert str(builder.first()[0]) == comp_name, 'The computer name is not correct.'
assert str(builder.first()[1]) == comp_uuid, 'The computer uuid is not correct.'
assert str(builder.first()[0]) == comp_name, 'The computer name is not correct.' # pylint: disable=unsubscriptable-object
assert str(builder.first()[1]) == comp_uuid, 'The computer uuid is not correct.' # pylint: disable=unsubscriptable-object

# Store the id of the computer
comp_id = builder.first()[2]
comp_id = builder.first()[2] # pylint: disable=unsubscriptable-object

# Import the second calculation
import_archive(filename2)
Expand All @@ -99,9 +99,9 @@ def test_same_computer_import(tmp_path, aiida_profile_clean, aiida_localhost):
builder = orm.QueryBuilder()
builder.append(orm.Computer, project=['label', 'uuid', 'id'])
assert builder.count() == 1, f'Found {builder.count()} computersbut only one computer should be found.'
assert str(builder.first()[0]) == comp_name, 'The computer name is not correct.'
assert str(builder.first()[1]) == comp_uuid, 'The computer uuid is not correct.'
assert builder.first()[2] == comp_id, 'The computer id is not correct.'
assert str(builder.first()[0]) == comp_name, 'The computer name is not correct.' # pylint: disable=unsubscriptable-object
assert str(builder.first()[1]) == comp_uuid, 'The computer uuid is not correct.' # pylint: disable=unsubscriptable-object
assert builder.first()[2] == comp_id, 'The computer id is not correct.' # pylint: disable=unsubscriptable-object

# Check that now you have two calculations attached to the same
# computer.
Expand Down Expand Up @@ -175,13 +175,13 @@ def test_same_computer_different_name_import(tmp_path, aiida_profile_clean, aiid
builder = orm.QueryBuilder()
builder.append(orm.CalcJobNode, project=['label'])
assert builder.count() == 1, 'Only one calculation should be found.'
assert str(builder.first()[0]) == calc1_label, 'The calculation label is not correct.'
assert str(builder.first(flat=True)) == calc1_label, 'The calculation label is not correct.'

# Check that the referenced computer is imported correctly.
builder = orm.QueryBuilder()
builder.append(orm.Computer, project=['label', 'uuid', 'id'])
assert builder.count() == 1, 'Only one computer should be found.'
assert str(builder.first()[0]) == comp1_name, 'The computer name is not correct.'
assert str(builder.first()[0]) == comp1_name, 'The computer name is not correct.' # pylint: disable=unsubscriptable-object

# Import the second calculation
import_archive(filename2)
Expand All @@ -191,7 +191,7 @@ def test_same_computer_different_name_import(tmp_path, aiida_profile_clean, aiid
builder = orm.QueryBuilder()
builder.append(orm.Computer, project=['label'])
assert builder.count() == 1, f'Found {builder.count()} computersbut only one computer should be found.'
assert str(builder.first()[0]) == comp1_name, 'The computer name is not correct.'
assert str(builder.first(flat=True)) == comp1_name, 'The computer name is not correct.'


def test_different_computer_same_name_import(tmp_path, aiida_profile_clean, aiida_localhost_factory):
Expand Down

0 comments on commit 952ed5a

Please sign in to comment.