forked from orest-d/p4vasp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create wrapper function for common class functionality (orest-d#15)
* Implement wrapper functions for plot and read * Implement wrapper for conversion routines
- Loading branch information
1 parent
25a4f40
commit 394caff
Showing
3 changed files
with
95 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from py4vasp.data import plot, read, to_dict | ||
from py4vasp.exceptions import NotImplementException | ||
from unittest.mock import MagicMock | ||
from contextlib import contextmanager | ||
import pytest | ||
|
||
|
||
def test_plot(): | ||
plotable = MagicMock() | ||
context = plotable.from_file.return_value | ||
obj = context.__enter__.return_value | ||
# without arguments | ||
res = plot(plotable) | ||
plotable.from_file.assert_called_once() | ||
context.__enter__.assert_called_once() | ||
obj.plot.assert_called_once() | ||
assert res == obj.plot.return_value | ||
# with arguments | ||
res = plot(plotable, "arguments") | ||
obj.plot.assert_called_with("arguments") | ||
|
||
|
||
def test_plot(): | ||
readable = MagicMock() | ||
context = readable.from_file.return_value | ||
obj = context.__enter__.return_value | ||
# without arguments | ||
res = read(readable) | ||
readable.from_file.assert_called_once() | ||
context.__enter__.assert_called_once() | ||
obj.read.assert_called_once() | ||
assert res == obj.read.return_value | ||
# with arguments | ||
res = read(readable, "arguments") | ||
obj.read.assert_called_with("arguments") | ||
|
||
|
||
def test_conversion(): | ||
convertible = MagicMock() | ||
context = convertible.from_file.return_value | ||
obj = context.__enter__.return_value | ||
# without arguments | ||
res = to_dict(convertible) # we test to_dict as generic for conversion | ||
convertible.from_file.assert_called_once() | ||
context.__enter__.assert_called_once() | ||
obj.to_dict.assert_called_once() | ||
assert res == obj.to_dict.return_value | ||
# with arguments | ||
res = to_dict(convertible, "arguments") | ||
obj.to_dict.assert_called_with("arguments") | ||
|
||
|
||
def test_exception(): | ||
class NoReadDefined: | ||
@contextmanager | ||
def from_file(): | ||
yield NoReadDefined() | ||
|
||
with pytest.raises(NotImplementException): | ||
read(NoReadDefined) |