-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π IMPROVE: constructor of base data types (#5165)
* π IMPROVE: constructor of base data types Adapt the constructor of the `Dict` and `List` data types so the value no longer needs to be provided as a keyword argument, but can simply be passed as the first input. Remove the `*args` from the `BaseType` constructor, instead specifying the `value` input argument. Otherwise users could just pass multiple positional arguments and it would only use the first without raising an error. Also fixes two issues with the `List` class: * The `remove` method was not working as prescribed. Instead of removing the first element in the list with the specified value, it simply deleted the element with index `value`. * The `set_list()` method didn't make a copy of the `list` input, so any modification done to the `List` instance would also affect the original `list`. If the same list is used to initialise several `List` instances, adapting one would affect the other. Finally, add tests for the `List` class. * Refactor data tests
- Loading branch information
Showing
27 changed files
with
459 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
# pylint: disable=invalid-name | ||
"""Tests for :class:`aiida.orm.nodes.data.base.BaseType` classes.""" | ||
|
||
import operator | ||
|
||
import pytest | ||
|
||
from aiida.orm import Bool, Float, Int, NumericType, Str, load_node | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
@pytest.mark.parametrize( | ||
'node_type, default, value', [ | ||
(Bool, False, True), | ||
(Int, 0, 5), | ||
(Float, 0.0, 5.5), | ||
(Str, '', 'a'), | ||
] | ||
) | ||
def test_create(node_type, default, value): | ||
"""Test the creation of the ``BaseType`` nodes.""" | ||
|
||
node = node_type() | ||
assert node.value == default | ||
|
||
node = node_type(value) | ||
assert node.value == value | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
@pytest.mark.parametrize('node_type', [Bool, Float, Int, Str]) | ||
def test_store_load(node_type): | ||
"""Test ``BaseType`` node storing and loading.""" | ||
node = node_type() | ||
node.store() | ||
loaded = load_node(node.pk) | ||
assert node.value == loaded.value | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
def test_modulo(): | ||
"""Test ``Int`` modulus operation.""" | ||
term_a = Int(12) | ||
term_b = Int(10) | ||
|
||
assert term_a % term_b == 2 | ||
assert isinstance(term_a % term_b, NumericType) | ||
assert term_a % 10 == 2 | ||
assert isinstance(term_a % 10, NumericType) | ||
assert 12 % term_b == 2 | ||
assert isinstance(12 % term_b, NumericType) | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
@pytest.mark.parametrize('node_type, a, b', [ | ||
(Int, 3, 5), | ||
(Float, 1.2, 5.5), | ||
]) | ||
def test_add(node_type, a, b): | ||
"""Test addition for ``Int`` and ``Float`` nodes.""" | ||
node_a = node_type(a) | ||
node_b = node_type(b) | ||
|
||
result = node_a + node_b | ||
assert isinstance(result, node_type) | ||
assert result.value == a + b | ||
|
||
# Node and native (both ways) | ||
result = node_a + b | ||
assert isinstance(result, node_type) | ||
assert result.value == a + b | ||
|
||
result = a + node_b | ||
assert isinstance(result, node_type) | ||
assert result.value == a + b | ||
|
||
# Inplace | ||
result = node_type(a) | ||
result += node_b | ||
assert isinstance(result, node_type) | ||
assert result.value == a + b | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
@pytest.mark.parametrize('node_type, a, b', [ | ||
(Int, 3, 5), | ||
(Float, 1.2, 5.5), | ||
]) | ||
def test_multiplication(node_type, a, b): | ||
"""Test floats multiplication.""" | ||
node_a = node_type(a) | ||
node_b = node_type(b) | ||
|
||
# Check multiplication | ||
result = node_a * node_b | ||
assert isinstance(result, node_type) | ||
assert result.value == a * b | ||
|
||
# Check multiplication Node and native (both ways) | ||
result = node_a * b | ||
assert isinstance(result, node_type) | ||
assert result.value == a * b | ||
|
||
result = a * node_b | ||
assert isinstance(result, node_type) | ||
assert result.value == a * b | ||
|
||
# Inplace | ||
result = node_type(a) | ||
result *= node_b | ||
assert isinstance(result, node_type) | ||
assert result.value == a * b | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
@pytest.mark.parametrize('node_type, a, b', [ | ||
(Int, 3, 5), | ||
(Float, 1.2, 5.5), | ||
]) | ||
@pytest.mark.usefixtures('clear_database_before_test') | ||
def test_division(node_type, a, b): | ||
"""Test the ``BaseType`` normal division operator.""" | ||
node_a = node_type(a) | ||
node_b = node_type(b) | ||
|
||
result = node_a / node_b | ||
assert result == a / b | ||
assert isinstance(result, Float) # Should be a `Float` for both node types | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
@pytest.mark.parametrize('node_type, a, b', [ | ||
(Int, 3, 5), | ||
(Float, 1.2, 5.5), | ||
]) | ||
@pytest.mark.usefixtures('clear_database_before_test') | ||
def test_division_integer(node_type, a, b): | ||
"""Test the ``Int`` integer division operator.""" | ||
node_a = node_type(a) | ||
node_b = node_type(b) | ||
|
||
result = node_a // node_b | ||
assert result == a // b | ||
assert isinstance(result, node_type) | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
@pytest.mark.parametrize('node_type, base, power', [ | ||
(Int, 5, 2), | ||
(Float, 3.5, 3), | ||
]) | ||
def test_power(node_type, base, power): | ||
"""Test power operator.""" | ||
node_base = node_type(base) | ||
node_power = node_type(power) | ||
|
||
result = node_base**node_power | ||
assert result == base**power | ||
assert isinstance(result, node_type) | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
@pytest.mark.parametrize('node_type, a, b', [ | ||
(Int, 5, 2), | ||
(Float, 3.5, 3), | ||
]) | ||
def test_modulus(node_type, a, b): | ||
"""Test modulus operator.""" | ||
node_a = node_type(a) | ||
node_b = node_type(b) | ||
|
||
assert node_a % node_b == a % b | ||
assert isinstance(node_a % node_b, node_type) | ||
|
||
assert node_a % b == a % b | ||
assert isinstance(node_a % b, node_type) | ||
|
||
assert a % node_b == a % b | ||
assert isinstance(a % node_b, node_type) | ||
|
||
|
||
@pytest.mark.usefixtures('clear_database_before_test') | ||
@pytest.mark.parametrize( | ||
'opera', [ | ||
operator.add, operator.mul, operator.pow, operator.lt, operator.le, operator.gt, operator.ge, operator.iadd, | ||
operator.imul | ||
] | ||
) | ||
def test_operator(opera): | ||
"""Test operations between Int and Float objects.""" | ||
node_a = Float(2.2) | ||
node_b = Int(3) | ||
|
||
for node_x, node_y in [(node_a, node_b), (node_b, node_a)]: | ||
res = opera(node_x, node_y) | ||
c_val = opera(node_x.value, node_y.value) | ||
assert res._type == type(c_val) # pylint: disable=protected-access | ||
assert res == opera(node_x.value, node_y.value) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.