|
1 | 1 | # pylint: disable=protected-access
|
| 2 | +# pylint: disable=redefined-outer-name |
2 | 3 | """
|
3 | 4 | Test the wrappers for the C API.
|
4 | 5 | """
|
5 | 6 | import os
|
6 | 7 | from contextlib import contextmanager
|
| 8 | +from itertools import product |
7 | 9 |
|
8 | 10 | import numpy as np
|
9 | 11 | import numpy.testing as npt
|
|
23 | 25 | from pygmt.helpers import GMTTempFile
|
24 | 26 |
|
25 | 27 | TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
|
| 28 | +POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt") |
26 | 29 |
|
27 | 30 | with clib.Session() as _lib:
|
28 | 31 | gmt_version = Version(_lib.info["version"])
|
29 | 32 |
|
30 | 33 |
|
| 34 | +@pytest.fixture(scope="module") |
| 35 | +def data(): |
| 36 | + """ |
| 37 | + Load the point data from the test file. |
| 38 | + """ |
| 39 | + return np.loadtxt(POINTS_DATA) |
| 40 | + |
| 41 | + |
31 | 42 | @contextmanager
|
32 | 43 | def mock(session, func, returns=None, mock_func=None):
|
33 | 44 | """
|
@@ -410,6 +421,58 @@ def test_virtual_file_bad_direction():
|
410 | 421 | print("This should have failed")
|
411 | 422 |
|
412 | 423 |
|
| 424 | +def test_virtualfile_from_data_required_z_matrix(): |
| 425 | + """ |
| 426 | + Test that function fails when third z column in a matrix is needed but not |
| 427 | + provided. |
| 428 | + """ |
| 429 | + data = np.ones((5, 2)) |
| 430 | + with clib.Session() as lib: |
| 431 | + with pytest.raises(GMTInvalidInput): |
| 432 | + with lib.virtualfile_from_data(data=data, required_z=True): |
| 433 | + pass |
| 434 | + |
| 435 | + |
| 436 | +def test_virtualfile_from_data_fail_non_valid_data(data): |
| 437 | + """ |
| 438 | + Should raise an exception if too few or too much data is given. |
| 439 | + """ |
| 440 | + # Test all combinations where at least one data variable |
| 441 | + # is not given in the x, y case: |
| 442 | + for variable in product([None, data[:, 0]], repeat=2): |
| 443 | + # Filter one valid configuration: |
| 444 | + if not any(item is None for item in variable): |
| 445 | + continue |
| 446 | + with clib.Session() as lib: |
| 447 | + with pytest.raises(GMTInvalidInput): |
| 448 | + lib.virtualfile_from_data( |
| 449 | + x=variable[0], |
| 450 | + y=variable[1], |
| 451 | + ) |
| 452 | + |
| 453 | + # Test all combinations where at least one data variable |
| 454 | + # is not given in the x, y, z case: |
| 455 | + for variable in product([None, data[:, 0]], repeat=3): |
| 456 | + # Filter one valid configuration: |
| 457 | + if not any(item is None for item in variable): |
| 458 | + continue |
| 459 | + with clib.Session() as lib: |
| 460 | + with pytest.raises(GMTInvalidInput): |
| 461 | + lib.virtualfile_from_data( |
| 462 | + x=variable[0], y=variable[1], z=variable[2], required_z=True |
| 463 | + ) |
| 464 | + |
| 465 | + # Should also fail if given too much data |
| 466 | + with clib.Session() as lib: |
| 467 | + with pytest.raises(GMTInvalidInput): |
| 468 | + lib.virtualfile_from_data( |
| 469 | + x=data[:, 0], |
| 470 | + y=data[:, 1], |
| 471 | + z=data[:, 2], |
| 472 | + data=data, |
| 473 | + ) |
| 474 | + |
| 475 | + |
413 | 476 | def test_virtualfile_from_vectors():
|
414 | 477 | """
|
415 | 478 | Test the automation for transforming vectors to virtual file dataset.
|
|
0 commit comments