コード例 #1
0
    def fill_repo(self):
        """Utility function to create repository nodes"""
        from aiida.orm import CalcJobNode, Data, Dict

        extra_name = self.__class__.__name__ + '/test_with_subclasses'
        resources = {'num_machines': 1, 'num_mpiprocs_per_machine': 1}

        a1 = CalcJobNode(computer=self.computer)
        a1.set_option('resources', resources)
        a1.store()
        # To query only these nodes later
        a1.set_extra(extra_name, True)
        a3 = Data().store()
        a3.set_extra(extra_name, True)
        a4 = Dict(dict={'a': 'b'}).store()
        a4.set_extra(extra_name, True)
        a5 = Data().store()
        a5.set_extra(extra_name, True)
        # I don't set the extras, just to be sure that the filtering works
        # The filtering is needed because other tests will put stuff int he DB
        a6 = CalcJobNode(computer=self.computer)
        a6.set_option('resources', resources)
        a6.store()
        a7 = Data()
        a7.store()
コード例 #2
0
ファイル: backup_script.py プロジェクト: zooks97/aiida_core
    def fill_repo(self):
        from aiida.orm import JobCalculation, CalculationFactory, Data, DataFactory

        extra_name = self.__class__.__name__ + "/test_with_subclasses"
        calc_params = {
            'computer': self.computer,
            'resources': {
                'num_machines': 1,
                'num_mpiprocs_per_machine': 1
            }
        }

        TemplateReplacerCalc = CalculationFactory(
            'simpleplugins.templatereplacer')
        ParameterData = DataFactory('parameter')

        a1 = JobCalculation(**calc_params).store()
        # To query only these nodes later
        a1.set_extra(extra_name, True)
        a2 = TemplateReplacerCalc(**calc_params).store()
        # To query only these nodes later
        a2.set_extra(extra_name, True)
        a3 = Data().store()
        a3.set_extra(extra_name, True)
        a4 = ParameterData(dict={'a': 'b'}).store()
        a4.set_extra(extra_name, True)
        a5 = Node().store()
        a5.set_extra(extra_name, True)
        # I don't set the extras, just to be sure that the filtering works
        # The filtering is needed because other tests will put stuff int he DB
        a6 = JobCalculation(**calc_params)
        a6.store()
        a7 = Node()
        a7.store()
コード例 #3
0
ファイル: nodes.py プロジェクト: santiama/aiida_core
    def test_with_subclasses(self, computer):

        extra_name = self.__class__.__name__ + "/test_with_subclasses"
        calc_params = {
            'computer': computer,
            'resources': {
                'num_machines': 1,
                'num_mpiprocs_per_machine': 1
            }
        }

        TemplateReplacerCalc = CalculationFactory(
            'simpleplugins.templatereplacer')
        ParameterData = DataFactory('parameter')

        a1 = JobCalculation(**calc_params).store()
        # To query only these nodes later
        a1.set_extra(extra_name, True)
        a2 = TemplateReplacerCalc(**calc_params).store()
        # To query only these nodes later
        a2.set_extra(extra_name, True)
        a3 = Data().store()
        a3.set_extra(extra_name, True)
        a4 = ParameterData(dict={'a': 'b'}).store()
        a4.set_extra(extra_name, True)
        a5 = Node().store()
        a5.set_extra(extra_name, True)
        # I don't set the extras, just to be sure that the filtering works
        # The filtering is needed because other tests will put stuff int he DB
        a6 = JobCalculation(**calc_params)
        a6.store()
        a7 = Node()
        a7.store()

        # Query by calculation
        results = list(JobCalculation.query(dbextras__key=extra_name))
        # a3, a4, a5 should not be found because they are not JobCalculations.
        # a6, a7 should not be found because they have not the attribute set.
        self.assertEquals(set([i.pk for i in results]), set([a1.pk, a2.pk]))

        # Same query, but by the generic Node class
        results = list(Node.query(dbextras__key=extra_name))
        self.assertEquals(set([i.pk for i in results]),
                          set([a1.pk, a2.pk, a3.pk, a4.pk, a5.pk]))

        # Same query, but by the Data class
        results = list(Data.query(dbextras__key=extra_name))
        self.assertEquals(set([i.pk for i in results]), set([a3.pk, a4.pk]))

        # Same query, but by the ParameterData subclass
        results = list(ParameterData.query(dbextras__key=extra_name))
        self.assertEquals(set([i.pk for i in results]), set([a4.pk]))

        # Same query, but by the TemplateReplacerCalc subclass
        results = list(TemplateReplacerCalc.query(dbextras__key=extra_name))
        self.assertEquals(set([i.pk for i in results]), set([a2.pk]))
コード例 #4
0
class TestNodeAttributesExtras(AiidaTestCase):
    """Test for node attributes and extras."""

    def setUp(self):
        super(TestNodeAttributesExtras, self).setUp()
        self.node = Data()

    def test_attributes(self):
        """Test the `Node.attributes` property."""
        original_attribute = {'nested': {'a': 1}}

        self.node.set_attribute('key', original_attribute)
        node_attributes = self.node.attributes
        self.assertEqual(node_attributes['key'], original_attribute)
        node_attributes['key']['nested']['a'] = 2

        self.assertEqual(original_attribute['nested']['a'], 2)

        # Now store the node and verify that `attributes` then returns a deep copy
        self.node.store()
        node_attributes = self.node.attributes

        # We change the returned node attributes but the original attribute should remain unchanged
        node_attributes['key']['nested']['a'] = 3
        self.assertEqual(original_attribute['nested']['a'], 2)

    def test_get_attribute(self):
        """Test the `Node.get_attribute` method."""
        original_attribute = {'nested': {'a': 1}}

        self.node.set_attribute('key', original_attribute)
        node_attribute = self.node.get_attribute('key')
        self.assertEqual(node_attribute, original_attribute)
        node_attribute['nested']['a'] = 2

        self.assertEqual(original_attribute['nested']['a'], 2)

        default = 'default'
        self.assertEqual(self.node.get_attribute('not_existing', default=default), default)
        with self.assertRaises(AttributeError):
            self.node.get_attribute('not_existing')

        # Now store the node and verify that `get_attribute` then returns a deep copy
        self.node.store()
        node_attribute = self.node.get_attribute('key')

        # We change the returned node attributes but the original attribute should remain unchanged
        node_attribute['nested']['a'] = 3
        self.assertEqual(original_attribute['nested']['a'], 2)

        default = 'default'
        self.assertEqual(self.node.get_attribute('not_existing', default=default), default)
        with self.assertRaises(AttributeError):
            self.node.get_attribute('not_existing')

    def test_get_attribute_many(self):
        """Test the `Node.get_attribute_many` method."""
        original_attribute = {'nested': {'a': 1}}

        self.node.set_attribute('key', original_attribute)
        node_attribute = self.node.get_attribute_many(['key'])[0]
        self.assertEqual(node_attribute, original_attribute)
        node_attribute['nested']['a'] = 2

        self.assertEqual(original_attribute['nested']['a'], 2)

        # Now store the node and verify that `get_attribute` then returns a deep copy
        self.node.store()
        node_attribute = self.node.get_attribute_many(['key'])[0]

        # We change the returned node attributes but the original attribute should remain unchanged
        node_attribute['nested']['a'] = 3
        self.assertEqual(original_attribute['nested']['a'], 2)

    def test_set_attribute(self):
        """Test the `Node.set_attribute` method."""
        with self.assertRaises(exceptions.ValidationError):
            self.node.set_attribute('illegal.key', 'value')

        self.node.set_attribute('valid_key', 'value')
        self.node.store()

        with self.assertRaises(exceptions.ModificationNotAllowed):
            self.node.set_attribute('valid_key', 'value')

    def test_set_attribute_many(self):
        """Test the `Node.set_attribute` method."""
        with self.assertRaises(exceptions.ValidationError):
            self.node.set_attribute_many({'illegal.key': 'value', 'valid_key': 'value'})

        self.node.set_attribute_many({'valid_key': 'value'})
        self.node.store()

        with self.assertRaises(exceptions.ModificationNotAllowed):
            self.node.set_attribute_many({'valid_key': 'value'})

    def test_reset_attribute(self):
        """Test the `Node.reset_attribute` method."""
        attributes_before = {'attribute_one': 'value', 'attribute_two': 'value'}
        attributes_after = {'attribute_three': 'value', 'attribute_four': 'value'}
        attributes_illegal = {'attribute.illegal': 'value', 'attribute_four': 'value'}

        self.node.set_attribute_many(attributes_before)
        self.assertEqual(self.node.attributes, attributes_before)
        self.node.reset_attributes(attributes_after)
        self.assertEqual(self.node.attributes, attributes_after)

        with self.assertRaises(exceptions.ValidationError):
            self.node.reset_attributes(attributes_illegal)

        self.node.store()

        with self.assertRaises(exceptions.ModificationNotAllowed):
            self.node.reset_attributes(attributes_after)

    def test_delete_attribute(self):
        """Test the `Node.delete_attribute` method."""
        self.node.set_attribute('valid_key', 'value')
        self.assertEqual(self.node.get_attribute('valid_key'), 'value')
        self.node.delete_attribute('valid_key')

        with self.assertRaises(AttributeError):
            self.node.delete_attribute('valid_key')

        # Repeat with stored node
        self.node.set_attribute('valid_key', 'value')
        self.node.store()

        with self.assertRaises(exceptions.ModificationNotAllowed):
            self.node.delete_attribute('valid_key')

    def test_delete_attribute_many(self):
        """Test the `Node.delete_attribute_many` method."""

    def test_clear_attributes(self):
        """Test the `Node.clear_attributes` method."""
        attributes = {'attribute_one': 'value', 'attribute_two': 'value'}
        self.node.set_attribute_many(attributes)
        self.assertEqual(self.node.attributes, attributes)

        self.node.clear_attributes()
        self.assertEqual(self.node.attributes, {})

        # Repeat for stored node
        self.node.store()

        with self.assertRaises(exceptions.ModificationNotAllowed):
            self.node.clear_attributes()

    def test_attributes_items(self):
        """Test the `Node.attributes_items` generator."""
        attributes = {'attribute_one': 'value', 'attribute_two': 'value'}
        self.node.set_attribute_many(attributes)
        self.assertEqual(dict(self.node.attributes_items()), attributes)

    def test_attributes_keys(self):
        """Test the `Node.attributes_keys` generator."""
        attributes = {'attribute_one': 'value', 'attribute_two': 'value'}
        self.node.set_attribute_many(attributes)
        self.assertEqual(set(self.node.attributes_keys()), set(attributes))

    def test_extras(self):
        """Test the `Node.extras` property."""
        original_extra = {'nested': {'a': 1}}

        self.node.set_extra('key', original_extra)
        node_extras = self.node.extras
        self.assertEqual(node_extras['key'], original_extra)
        node_extras['key']['nested']['a'] = 2

        self.assertEqual(original_extra['nested']['a'], 2)

        # Now store the node and verify that `extras` then returns a deep copy
        self.node.store()
        node_extras = self.node.extras

        # We change the returned node extras but the original extra should remain unchanged
        node_extras['key']['nested']['a'] = 3
        self.assertEqual(original_extra['nested']['a'], 2)

    def test_get_extra(self):
        """Test the `Node.get_extra` method."""
        original_extra = {'nested': {'a': 1}}

        self.node.set_extra('key', original_extra)
        node_extra = self.node.get_extra('key')
        self.assertEqual(node_extra, original_extra)
        node_extra['nested']['a'] = 2

        self.assertEqual(original_extra['nested']['a'], 2)

        default = 'default'
        self.assertEqual(self.node.get_extra('not_existing', default=default), default)
        with self.assertRaises(AttributeError):
            self.node.get_extra('not_existing')

        # Now store the node and verify that `get_extra` then returns a deep copy
        self.node.store()
        node_extra = self.node.get_extra('key')

        # We change the returned node extras but the original extra should remain unchanged
        node_extra['nested']['a'] = 3
        self.assertEqual(original_extra['nested']['a'], 2)

        default = 'default'
        self.assertEqual(self.node.get_extra('not_existing', default=default), default)
        with self.assertRaises(AttributeError):
            self.node.get_extra('not_existing')

    def test_get_extra_many(self):
        """Test the `Node.get_extra_many` method."""
        original_extra = {'nested': {'a': 1}}

        self.node.set_extra('key', original_extra)
        node_extra = self.node.get_extra_many(['key'])[0]
        self.assertEqual(node_extra, original_extra)
        node_extra['nested']['a'] = 2

        self.assertEqual(original_extra['nested']['a'], 2)

        # Now store the node and verify that `get_extra` then returns a deep copy
        self.node.store()
        node_extra = self.node.get_extra_many(['key'])[0]

        # We change the returned node extras but the original extra should remain unchanged
        node_extra['nested']['a'] = 3
        self.assertEqual(original_extra['nested']['a'], 2)

    def test_set_extra(self):
        """Test the `Node.set_extra` method."""
        with self.assertRaises(exceptions.ValidationError):
            self.node.set_extra('illegal.key', 'value')

        self.node.set_extra('valid_key', 'value')
        self.node.store()

        self.node.set_extra('valid_key', 'changed')
        self.assertEqual(load_node(self.node.pk).get_extra('valid_key'), 'changed')

    def test_set_extra_many(self):
        """Test the `Node.set_extra` method."""
        with self.assertRaises(exceptions.ValidationError):
            self.node.set_extra_many({'illegal.key': 'value', 'valid_key': 'value'})

        self.node.set_extra_many({'valid_key': 'value'})
        self.node.store()

        self.node.set_extra_many({'valid_key': 'changed'})
        self.assertEqual(load_node(self.node.pk).get_extra('valid_key'), 'changed')

    def test_reset_extra(self):
        """Test the `Node.reset_extra` method."""
        extras_before = {'extra_one': 'value', 'extra_two': 'value'}
        extras_after = {'extra_three': 'value', 'extra_four': 'value'}
        extras_illegal = {'extra.illegal': 'value', 'extra_four': 'value'}

        self.node.set_extra_many(extras_before)
        self.assertEqual(self.node.extras, extras_before)
        self.node.reset_extras(extras_after)
        self.assertEqual(self.node.extras, extras_after)

        with self.assertRaises(exceptions.ValidationError):
            self.node.reset_extras(extras_illegal)

        self.node.store()

        self.node.reset_extras(extras_after)
        self.assertEqual(load_node(self.node.pk).extras, extras_after)

    def test_delete_extra(self):
        """Test the `Node.delete_extra` method."""
        self.node.set_extra('valid_key', 'value')
        self.assertEqual(self.node.get_extra('valid_key'), 'value')
        self.node.delete_extra('valid_key')

        with self.assertRaises(AttributeError):
            self.node.delete_extra('valid_key')

        # Repeat with stored node
        self.node.set_extra('valid_key', 'value')
        self.node.store()

        self.node.delete_extra('valid_key')
        with self.assertRaises(AttributeError):
            load_node(self.node.pk).get_extra('valid_key')

    def test_delete_extra_many(self):
        """Test the `Node.delete_extra_many` method."""

    def test_clear_extras(self):
        """Test the `Node.clear_extras` method."""
        extras = {'extra_one': 'value', 'extra_two': 'value'}
        self.node.set_extra_many(extras)
        self.assertEqual(self.node.extras, extras)

        self.node.clear_extras()
        self.assertEqual(self.node.extras, {})

        # Repeat for stored node
        self.node.store()

        self.node.clear_extras()
        self.assertEqual(load_node(self.node.pk).extras, {})

    def test_extras_items(self):
        """Test the `Node.extras_items` generator."""
        extras = {'extra_one': 'value', 'extra_two': 'value'}
        self.node.set_extra_many(extras)
        self.assertEqual(dict(self.node.extras_items()), extras)

    def test_extras_keys(self):
        """Test the `Node.extras_keys` generator."""
        extras = {'extra_one': 'value', 'extra_two': 'value'}
        self.node.set_extra_many(extras)
        self.assertEqual(set(self.node.extras_keys()), set(extras))