|
1 | 1 | import pytest |
2 | 2 | import datetime as dt |
3 | 3 | import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import cftime |
4 | 6 | from forest import db |
| 7 | +from forest.db.control import time_array_equal |
5 | 8 |
|
6 | 9 |
|
7 | 10 | @pytest.mark.parametrize("left,right,expect", [ |
|
35 | 38 | (db.State(), db.State(variables=["a", "b"]), False), |
36 | 39 | (db.State(variables=["a", "b"]), db.State(variables=["a", "b"]), True), |
37 | 40 | (db.State(variables=np.array(["a", "b"])), |
38 | | - db.State(variables=["a", "b"]), True), |
| 41 | + db.State(variables=["a", "b"]), True) |
39 | 42 | ]) |
40 | 43 | def test_equality_and_not_equality(left, right, expect): |
41 | 44 | assert (left == right) == expect |
42 | 45 | assert (left != right) == (not expect) |
| 46 | + |
| 47 | + |
| 48 | +def test_state_equality_valueerror_lengths_must_match(): |
| 49 | + """should return False if lengths do not match""" |
| 50 | + valid_times = ( |
| 51 | + pd.date_range("2020-01-01", periods=2), |
| 52 | + pd.date_range("2020-01-01", periods=3), |
| 53 | + ) |
| 54 | + left = db.State(valid_times=valid_times[0]) |
| 55 | + right = db.State(valid_times=valid_times[1]) |
| 56 | + assert (left == right) == False |
| 57 | + |
| 58 | + |
| 59 | +def test_time_array_equal(): |
| 60 | + left = pd.date_range("2020-01-01", periods=2) |
| 61 | + right = pd.date_range("2020-01-01", periods=3) |
| 62 | + assert time_array_equal(left, right) == False |
| 63 | + |
| 64 | + |
| 65 | +def test_valueerror_lengths_must_match(): |
| 66 | + a = ["2020-01-01T00:00:00Z"] |
| 67 | + b = ["2020-02-01T00:00:00Z", "2020-02-02T00:00:00Z", "2020-02-03T00:00:00Z"] |
| 68 | + with pytest.raises(ValueError): |
| 69 | + pd.to_datetime(a) == pd.to_datetime(b) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize("left,right,expect", [ |
| 73 | + pytest.param([cftime.DatetimeGregorian(2020, 1, 1), |
| 74 | + cftime.DatetimeGregorian(2020, 1, 2), |
| 75 | + cftime.DatetimeGregorian(2020, 1, 3)], |
| 76 | + pd.date_range("2020-01-01", periods=3), |
| 77 | + True, |
| 78 | + id="gregorian/pandas same values"), |
| 79 | + pytest.param([cftime.DatetimeGregorian(2020, 2, 1), |
| 80 | + cftime.DatetimeGregorian(2020, 2, 2), |
| 81 | + cftime.DatetimeGregorian(2020, 2, 3)], |
| 82 | + pd.date_range("2020-01-01", periods=3), |
| 83 | + False, |
| 84 | + id="gregorian/pandas same length different values"), |
| 85 | +]) |
| 86 | +def test_time_array_equal_mixed_types(left, right, expect): |
| 87 | + assert time_array_equal(left, right) == expect |
0 commit comments