2020-09-30 16:02:37 +05:30
|
|
|
import pytest
|
2020-10-27 11:04:05 +05:30
|
|
|
import numpy as np
|
2020-09-30 16:02:37 +05:30
|
|
|
|
2020-09-30 12:19:55 +05:30
|
|
|
from damask import Config
|
2021-03-25 20:00:31 +05:30
|
|
|
from damask import Rotation
|
|
|
|
from damask import Orientation
|
2020-09-30 12:19:55 +05:30
|
|
|
|
|
|
|
class TestConfig:
|
|
|
|
|
2023-02-01 18:44:54 +05:30
|
|
|
def test_init_keyword(self):
|
|
|
|
assert Config(p=4)['p'] == 4
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('config',[{'p':1},'{p: 1}'])
|
|
|
|
def test_init_config(self,config):
|
|
|
|
assert Config(config)['p'] == 1
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('config',[{'p':1},'{p: 1}'])
|
|
|
|
def test_init_both(self,config):
|
|
|
|
assert Config(config,p=2)['p'] == 2
|
|
|
|
|
2020-09-30 16:02:37 +05:30
|
|
|
@pytest.mark.parametrize('flow_style',[None,True,False])
|
|
|
|
def test_load_save_str(self,tmp_path,flow_style):
|
2020-09-30 12:19:55 +05:30
|
|
|
config = Config()
|
|
|
|
config['A'] = 1
|
|
|
|
config['B'] = [2,3]
|
2020-09-30 16:02:37 +05:30
|
|
|
config.save(tmp_path/'config.yaml',default_flow_style=flow_style)
|
2020-09-30 12:19:55 +05:30
|
|
|
assert Config.load(tmp_path/'config.yaml') == config
|
|
|
|
|
|
|
|
def test_load_save_file(self,tmp_path):
|
|
|
|
config = Config()
|
|
|
|
config['A'] = 1
|
|
|
|
config['B'] = [2,3]
|
|
|
|
with open(tmp_path/'config.yaml','w') as f:
|
|
|
|
config.save(f)
|
|
|
|
with open(tmp_path/'config.yaml') as f:
|
|
|
|
assert Config.load(f) == config
|
|
|
|
|
2021-01-03 16:39:21 +05:30
|
|
|
def test_add_remove(self):
|
2021-03-10 00:45:15 +05:30
|
|
|
dummy = {'hello':'world','foo':'bar'}
|
2021-01-03 16:39:21 +05:30
|
|
|
config = Config()
|
2021-03-10 00:45:15 +05:30
|
|
|
config |= dummy
|
|
|
|
assert config == Config() | dummy
|
|
|
|
config = config.delete(dummy)
|
|
|
|
assert config == Config()
|
|
|
|
assert (config | dummy ).delete( 'hello' ) == config | {'foo':'bar'}
|
|
|
|
assert (config | dummy ).delete([ 'hello', 'foo' ]) == config
|
|
|
|
assert (config | Config(dummy)).delete({ 'hello':1,'foo':2 }) == config
|
|
|
|
assert (config | Config(dummy)).delete(Config({'hello':1 })) == config | {'foo':'bar'}
|
|
|
|
|
2020-09-30 12:19:55 +05:30
|
|
|
def test_repr(self,tmp_path):
|
|
|
|
config = Config()
|
|
|
|
config['A'] = 1
|
|
|
|
config['B'] = [2,3]
|
|
|
|
with open(tmp_path/'config.yaml','w') as f:
|
|
|
|
f.write(config.__repr__())
|
|
|
|
assert Config.load(tmp_path/'config.yaml') == config
|
|
|
|
|
2020-10-27 11:04:05 +05:30
|
|
|
def test_numpy(self,tmp_path):
|
2022-12-06 00:59:08 +05:30
|
|
|
assert Config({'A':np.ones(3,'i'), 'B':np.ones(1)[0]}).__repr__() == \
|
|
|
|
Config({'A':[1,1,1], 'B':1.0}).__repr__()
|
2020-09-30 12:19:55 +05:30
|
|
|
|
|
|
|
def test_abstract_is_valid(self):
|
2021-05-29 14:24:34 +05:30
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
Config().is_valid
|
2020-09-30 12:19:55 +05:30
|
|
|
|
|
|
|
def test_abstract_is_complete(self):
|
2021-05-29 14:24:34 +05:30
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
Config().is_complete
|
2021-04-07 02:19:08 +05:30
|
|
|
|
|
|
|
@pytest.mark.parametrize('data',[Rotation.from_random(),Orientation.from_random(lattice='cI')])
|
2021-03-25 20:00:31 +05:30
|
|
|
def test_rotation_orientation(self,data):
|
|
|
|
assert str(Config(a=data)) == str(Config(a=data.as_quaternion()))
|
2021-12-03 04:50:27 +05:30
|
|
|
|
|
|
|
def test_initialize(self):
|
|
|
|
yml = """
|
|
|
|
a:
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
"""
|
|
|
|
assert Config(yml) == Config('{"a":[1,2]}') == Config(a=[1,2]) == Config(dict(a=[1,2]))
|