Skip to content

Commit

Permalink
Converted AuthInfo and QueryBuilder to new backend interface
Browse files Browse the repository at this point in the history
  • Loading branch information
muhrin authored and sphuber committed Nov 12, 2018
1 parent 8c200ca commit 7ea30a7
Show file tree
Hide file tree
Showing 102 changed files with 1,688 additions and 1,864 deletions.
15 changes: 9 additions & 6 deletions .ci/test_plugin_testcase.py
Expand Up @@ -57,25 +57,28 @@ def get_computer(cls, temp_dir):
"""
Create and store a new computer, and return it
"""
computer = cls.backend.computers.create(
from aiida import orm

computer = orm.Computer(
name='localhost',
hostname='localhost',
description='my computer',
transport_type='local',
scheduler_type='direct',
workdir=temp_dir,
enabled_state=True)
computer.store()
enabled_state=True,
backend=cls.backend).store()
return computer

def test_data_loaded(self):
"""
Check that the data is indeed in the DB when calling load_node
"""
from aiida.orm.utils import load_node
from aiida import orm

self.assertTrue(is_dbenv_loaded())
self.assertEqual(load_node(self.data_pk).uuid, self.data.uuid)
self.assertEqual(self.backend.computers.get(name='localhost').uuid, self.computer.uuid)
self.assertEqual(orm.load_node(self.data_pk).uuid, self.data.uuid)
self.assertEqual(orm.Computer.objects(self.backend).get(name='localhost').uuid, self.computer.uuid)

def test_tear_down(self):
"""
Expand Down
6 changes: 1 addition & 5 deletions .pre-commit-config.yaml
Expand Up @@ -42,14 +42,11 @@
aiida/backends/djsite/__init__.py|
aiida/backends/djsite/manage.py|
aiida/backends/djsite/queries.py|
aiida/backends/djsite/querybuilder_django/dummy_model.py|
aiida/backends/djsite/querybuilder_django/querybuilder_django.py|
aiida/backends/djsite/settings/__init__.py|
aiida/backends/djsite/settings/settings_profile.py|
aiida/backends/djsite/settings/settings.py|
aiida/backends/djsite/settings/wsgi.py|
aiida/backends/general/abstractqueries.py|
aiida/backends/general/querybuilder_interface.py|
aiida/backends/__init__.py|
aiida/backends/profile.py|
aiida/backends/settings.py|
Expand Down Expand Up @@ -83,7 +80,6 @@
aiida/backends/sqlalchemy/models/utils.py|
aiida/backends/sqlalchemy/models/workflow.py|
aiida/backends/sqlalchemy/queries.py|
aiida/backends/sqlalchemy/querybuilder_sqla.py|
aiida/backends/sqlalchemy/tests/generic.py|
aiida/backends/sqlalchemy/tests/__init__.py|
aiida/backends/sqlalchemy/tests/migrations.py|
Expand Down Expand Up @@ -207,6 +203,7 @@
aiida/orm/authinfo.py|
aiida/orm/autogroup.py|
aiida/orm/backend.py|
aiida/orm/querybuilder.py|
aiida/orm/calculation/function.py|
aiida/orm/calculation/__init__.py|
aiida/orm/calculation/inline.py|
Expand Down Expand Up @@ -289,7 +286,6 @@
aiida/orm/log.py|
aiida/orm/mixins.py|
aiida/orm/node.py|
aiida/orm/querybuilder.py|
aiida/orm/utils/__init__.py|
aiida/orm/utils/loaders.py|
aiida/orm/utils/remote.py|
Expand Down
6 changes: 6 additions & 0 deletions aiida/backends/djsite/db/models.py
Expand Up @@ -1450,6 +1450,12 @@ def __str__(self):
else:
return "DB authorization info for {} on {} [DISABLED]".format(self.aiidauser.email, self.dbcomputer.name)

def get_aiida_class(self):
from aiida.orm.implementation.django.authinfo import DjangoAuthInfo
from aiida.orm.backends import construct_backend
return DjangoAuthInfo.from_dbmodel(self, construct_backend())



@python_2_unicode_compatible
class DbComment(m.Model):
Expand Down
12 changes: 7 additions & 5 deletions aiida/backends/djsite/db/subtests/generic.py
Expand Up @@ -17,6 +17,7 @@
from aiida.backends.testbase import AiidaTestCase
from aiida.common import exceptions
from aiida.orm.node import Node
from aiida import orm


class TestComputer(AiidaTestCase):
Expand All @@ -28,9 +29,9 @@ def test_deletion(self):
from aiida.orm import JobCalculation
from aiida.common.exceptions import InvalidOperation

newcomputer = self.backend.computers.create(name="testdeletioncomputer", hostname='localhost',
transport_type='local', scheduler_type='pbspro',
workdir='/tmp/aiida').store()
newcomputer = orm.Computer(name="testdeletioncomputer", hostname='localhost',
transport_type='local', scheduler_type='pbspro',
workdir='/tmp/aiida').store()

# # This should be possible, because nothing is using this computer
self.backend.computers.delete(newcomputer.id)
Expand Down Expand Up @@ -75,7 +76,7 @@ def test_query(self):
g1.add_nodes([n1, n2])
g2.add_nodes([n1, n3])

newuser = self.backend.users.create(email='test@email.xx')
newuser = orm.User(email='test@email.xx')
g3 = Group(name='testquery3', user=newuser).store()

# I should find it
Expand Down Expand Up @@ -109,7 +110,8 @@ def test_query(self):
res = Group.query(user=newuser.email)
self.assertEquals(set(_.pk for _ in res), set(_.pk for _ in [g3]))

res = Group.query(user=self.backend.users.get_default())
default_user = orm.User.objects(self.backend).get_default()
res = Group.query(user=default_user.backend_entity)
self.assertEquals(set(_.pk for _ in res), set(_.pk for _ in [g1, g2]))

def test_rename_existing(self):
Expand Down
4 changes: 2 additions & 2 deletions aiida/backends/djsite/db/subtests/query.py
Expand Up @@ -19,13 +19,13 @@ def test_clsf_django(self):
"""
This tests the classifications of the QueryBuilder u. the django backend.
"""
from aiida.backends.djsite.querybuilder_django.dummy_model import (
from aiida.orm.implementation.django.dummy_model import (
DbNode, DbUser, DbComputer,
DbGroup,
)
from aiida.orm.querybuilder import QueryBuilder
from aiida.orm.data.structure import StructureData
from aiida.orm import Group, Node, Computer, Data, Calculation
from aiida.orm import Group, Node, Computer, Data
from aiida.common.exceptions import InputValidationError
qb = QueryBuilder()

Expand Down
2 changes: 1 addition & 1 deletion aiida/backends/djsite/db/testbase.py
Expand Up @@ -52,7 +52,7 @@ def insert_data(self):
"""
Insert default data into the DB.
"""
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ObjectDoesNotExist # pylint: disable=import-error, no-name-in-module

from aiida.backends.djsite.db.models import DbUser
from aiida.common.utils import get_configured_user_email
Expand Down
8 changes: 4 additions & 4 deletions aiida/backends/djsite/queries.py
Expand Up @@ -129,7 +129,7 @@ def get_creation_statistics(
an integer with the number of nodes created that day.
"""
import sqlalchemy as sa
from aiida.backends.djsite.querybuilder_django import dummy_model
from aiida.orm.implementation.django import dummy_model

# Get the session (uses internally aldjemy - so, sqlalchemy) also for the Djsite backend
s = dummy_model.get_aldjemy_session()
Expand Down Expand Up @@ -201,16 +201,16 @@ def get_bands_and_parents_structure(self, args):
from django.db.models import Q
from aiida.common.utils import grouper
from aiida.backends.djsite.db import models
from aiida.orm.backends import construct_backend
from aiida.orm.data.structure import (get_formula, get_symbols_string)
from aiida.orm.data.array.bands import BandsData
from aiida import orm

backend = construct_backend()
user = orm.User.objects.get_default()

query_group_size = 100
q_object = None
if args.all_users is False:
q_object = Q(user__id=backend.users.get_default().id)
q_object = Q(user__id=user.id)
else:
q_object = Q()

Expand Down
9 changes: 0 additions & 9 deletions aiida/backends/djsite/querybuilder_django/__init__.py

This file was deleted.

2 changes: 2 additions & 0 deletions aiida/backends/djsite/utils.py
Expand Up @@ -19,6 +19,8 @@
import django
from aiida.common.log import get_dblogger_extra

# pylint: disable=no-name-in-module, no-member, import-error


def load_dbenv(profile=None):
"""
Expand Down
13 changes: 5 additions & 8 deletions aiida/backends/general/abstractqueries.py
Expand Up @@ -81,7 +81,7 @@ def query_jobcalculations_by_computer_user_state(
"""
# I assume that calc_states are strings. If this changes in the future,
# update the filter below from dbattributes__tval to the correct field.
from aiida.orm.computer import Computer
from aiida.orm.computers import Computer
from aiida.orm.calculation.job import JobCalculation
from aiida.orm.querybuilder import QueryBuilder
from aiida.common.exceptions import InputValidationError
Expand Down Expand Up @@ -234,17 +234,14 @@ def get_bands_and_parents_structure(self, args):
from aiida.orm.data.structure import (get_formula, get_symbols_string)
from aiida.orm.data.array.bands import BandsData
from aiida.orm.data.structure import StructureData
from aiida.orm.users import User
from aiida.orm.backends import construct_backend

backend = construct_backend()
from aiida import orm

qb = QueryBuilder()
if args.all_users is False:
user = backend.users.get_default()
qb.append(User, tag="creator", filters={"email": user.email})
user = orm.User.objects.get_default()
qb.append(orm.User, tag="creator", filters={"email": user.email})
else:
qb.append(User, tag="creator")
qb.append(orm.User, tag="creator")

bdata_filters = {}
if args.past_days is not None:
Expand Down
4 changes: 4 additions & 0 deletions aiida/backends/sqlalchemy/models/authinfo.py
Expand Up @@ -52,3 +52,7 @@ def __str__(self):
return "DB authorization info for {} on {}".format(self.aiidauser.email, self.dbcomputer.name)
else:
return "DB authorization info for {} on {} [DISABLED]".format(self.aiidauser.email, self.dbcomputer.name)

def get_aiida_class(self):
from aiida.orm.implementation.sqlalchemy.authinfo import SqlaAuthInfo
return SqlaAuthInfo.from_dbmodel(dbmodel=self)
2 changes: 1 addition & 1 deletion aiida/backends/sqlalchemy/models/computer.py
Expand Up @@ -61,7 +61,7 @@ def get_dbcomputer(cls, computer):
Return a DbComputer from its name (or from another Computer or DbComputer instance)
"""

from aiida.orm.computer import Computer
from aiida.orm.computers import Computer
if isinstance(computer, six.string_types):
try:
dbcomputer = cls.session.query(cls).filter(cls.name == computer).one()
Expand Down
44 changes: 22 additions & 22 deletions aiida/backends/sqlalchemy/tests/generic.py
Expand Up @@ -13,11 +13,11 @@
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import unittest

from aiida.backends.testbase import AiidaTestCase
from aiida.orm.node import Node
from aiida.common import exceptions
from aiida import orm


class TestComputer(AiidaTestCase):
Expand All @@ -30,9 +30,11 @@ def test_deletion(self):
from aiida.common.exceptions import InvalidOperation
import aiida.backends.sqlalchemy

newcomputer = self.backend.computers.create(name="testdeletioncomputer", hostname='localhost',
transport_type='local', scheduler_type='pbspro',
workdir='/tmp/aiida').store()
newcomputer = self.backend.computers.create(
name="testdeletioncomputer",
hostname='localhost',
transport_type='local',
scheduler_type='pbspro').store()

# # This should be possible, because nothing is using this computer
self.backend.computers.delete(newcomputer.id)
Expand Down Expand Up @@ -66,16 +68,15 @@ def test_query(self):
"""
Test if queries are working
"""
from aiida.orm.group import Group
from aiida.common.exceptions import NotExistent, MultipleObjectsError
from aiida.backends.sqlalchemy.models.user import DbUser
from aiida.orm.backends import construct_backend

backend = construct_backend()

g1 = Group(name='testquery1').store()
g1 = orm.Group(name='testquery1').store()
self.addCleanup(g1.delete)
g2 = Group(name='testquery2').store()
g2 = orm.Group(name='testquery2').store()
self.addCleanup(g2.delete)

n1 = Node().store()
Expand All @@ -87,40 +88,40 @@ def test_query(self):
g2.add_nodes([n1, n3])

newuser = DbUser(email='test@email.xx', password='').get_aiida_class()
g3 = Group(name='testquery3', user=newuser).store()
g3 = orm.Group(name='testquery3', user=newuser).store()

# I should find it
g1copy = Group.get(uuid=g1.uuid)
g1copy = orm.Group.get(uuid=g1.uuid)
self.assertEquals(g1.pk, g1copy.pk)

# Try queries
res = Group.query(nodes=n4)
res = orm.Group.query(nodes=n4)
self.assertEquals([_.pk for _ in res], [])

res = Group.query(nodes=n1)
res = orm.Group.query(nodes=n1)
self.assertEquals([_.pk for _ in res], [_.pk for _ in [g1, g2]])

res = Group.query(nodes=n2)
res = orm.Group.query(nodes=n2)
self.assertEquals([_.pk for _ in res], [_.pk for _ in [g1]])

# I try to use 'get' with zero or multiple results
with self.assertRaises(NotExistent):
Group.get(nodes=n4)
orm.Group.get(nodes=n4)
with self.assertRaises(MultipleObjectsError):
Group.get(nodes=n1)
orm.Group.get(nodes=n1)

self.assertEquals(Group.get(nodes=n2).pk, g1.pk)
self.assertEquals(orm.Group.get(nodes=n2).pk, g1.pk)

# Query by user
res = Group.query(user=newuser)
res = orm.Group.query(user=newuser)
self.assertEquals(set(_.pk for _ in res), set(_.pk for _ in [g3]))

# Same query, but using a string (the username=email) instead of
# a DbUser object
res = Group.query(user=newuser.email)
res = orm.Group.query(user=newuser)
self.assertEquals(set(_.pk for _ in res), set(_.pk for _ in [g3]))

res = Group.query(user=backend.users.get_default())
res = orm.Group.query(user=orm.User.objects(backend).get_default())

self.assertEquals(set(_.pk for _ in res), set(_.pk for _ in [g1, g2]))

Expand All @@ -129,18 +130,17 @@ def test_rename_existing(self):
Test that renaming to an already existing name is not permitted
"""
from aiida.backends.sqlalchemy import get_scoped_session
from aiida.orm.group import Group

name_group_a = 'group_a'
name_group_b = 'group_b'
name_group_c = 'group_c'

group_a = Group(name=name_group_a, description='I am the Original G')
group_a = orm.Group(name=name_group_a, description='I am the Original G')
group_a.store()

# Before storing everything should be fine
group_b = Group(name=name_group_a, description='They will try to rename me')
group_c = Group(name=name_group_c, description='They will try to rename me')
group_b = orm.Group(name=name_group_a, description='They will try to rename me')
group_c = orm.Group(name=name_group_c, description='They will try to rename me')

session = get_scoped_session()

Expand Down
5 changes: 3 additions & 2 deletions aiida/backends/sqlalchemy/tests/nodes.py
Expand Up @@ -16,6 +16,7 @@
from __future__ import absolute_import
from aiida.backends.testbase import AiidaTestCase
from aiida.orm.node import Node
from aiida import orm


class TestTransitiveClosureDeletionSQLA(AiidaTestCase):
Expand Down Expand Up @@ -310,7 +311,7 @@ def test_multiple_node_creation(self):
import aiida.backends.sqlalchemy

# Get the automatic user
dbuser = self.backend.users.get_default().dbuser
dbuser = orm.User.objects(self.backend).get_default().backend_entity.dbuser
# Create a new node but don't add it to the session
node_uuid = get_new_uuid()
DbNode(user=dbuser, uuid=node_uuid, type=None)
Expand All @@ -333,7 +334,7 @@ def test_multiple_node_creation(self):
"UUID in the session/DB.")

# Get the automatic user
dbuser = self.backend.users.get_default().dbuser
dbuser = orm.User.objects(self.backend).get_default().backend_entity.dbuser
# Create a new node but now add it to the session
node_uuid = get_new_uuid()
node = DbNode(user=dbuser, uuid=node_uuid, type=None)
Expand Down

0 comments on commit 7ea30a7

Please sign in to comment.