-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
👌 IMPROVE: constructor of base data types #5165
Merged
unkcpz
merged 2 commits into
aiidateam:develop
from
mbercx:fix/4495/dict-list-constructor
Dec 6, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just want to make sure this will not break any backwards compatibility, correct? We can still use
d = orm.Dict(dict={})
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I see a test case for this particular. 👍