Coverage for tests/test_time_axis.py: 26.00%

44 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-07-24 10:14 +1000

1from datetime import datetime 

2import pytest 

3import pandas as pd 

4import xarray as xr 

5import numpy as np 

6from efts_io._ncdf_stf2 import _create_cf_time_axis 

7from efts_io.conventions import convert_to_datetime64_utc 

8 

9def test_create_cf_time_axis_valid_input(): 

10 # Create a sample DataArray with a time dimension 

11 dates = pd.date_range(start="2023-01-01", periods=5, freq="D") 

12 data = xr.DataArray(np.random.rand(5), dims=["time"], coords={"time": dates}) 

13 

14 # Test with a valid time step 

15 result, units, calendar = _create_cf_time_axis(data, "days") 

16 

17 # Check if the result is a numpy array 

18 assert isinstance(result, np.ndarray) 

19 assert len(result) == 5 

20 assert units == "days since 2023-01-01 00:00:00+00:00" 

21 assert calendar == 'proleptic_gregorian' 

22 

23def test_create_cf_time_axis_empty_data(): 

24 # Create an empty DataArray 

25 data = xr.DataArray([], dims=["time"]) 

26 

27 # Test with an empty DataArray 

28 with pytest.raises(ValueError, match="Cannot create CF time axis from empty data array."): 

29 _create_cf_time_axis(data, "days") 

30 

31def test_create_cf_time_axis_invalid_time_type(): 

32 # Create a DataArray with invalid time type 

33 data = xr.DataArray([1, 2, 3], dims=["time"], coords={"time": [1, 2, 3]}) 

34 

35 # Test with invalid time type 

36 with pytest.raises(TypeError, match="Expected data\\[TIME_DIMNAME\\] to be of a type convertible to pd.Timestamp, got <class 'numpy.int64'> instead."): 

37 _create_cf_time_axis(data, "days") 

38 

39 

40 

41# Unit tests 

42def test_convert_to_datetime64_utc(): 

43 # Test with a timezone-naive pd.Timestamp 

44 naive_timestamp = pd.Timestamp("2023-10-01 12:00:00") 

45 assert convert_to_datetime64_utc(naive_timestamp) == np.datetime64("2023-10-01T12:00:00.000000000") 

46 

47 # Test with a timezone-aware pd.Timestamp 

48 aware_timestamp = pd.Timestamp("2023-10-01 12:00:00", tz="America/New_York") 

49 assert convert_to_datetime64_utc(aware_timestamp) == np.datetime64("2023-10-01T16:00:00.000000000") 

50 

51 # Test with a timezone-naive datetime 

52 naive_datetime = datetime(2023, 10, 1, 12, 0, 0) 

53 assert convert_to_datetime64_utc(naive_datetime) == np.datetime64("2023-10-01T12:00:00.000000000") 

54 

55 # Test with a timezone-aware datetime 

56 from zoneinfo import ZoneInfo 

57 utc_tz = ZoneInfo("UTC") 

58 aware_datetime = datetime(2023, 10, 1, 12, 0, 0, tzinfo=utc_tz) 

59 assert convert_to_datetime64_utc(aware_datetime) == np.datetime64("2023-10-01T12:00:00.000000000") 

60 

61 # Test with a string representation 

62 naive_string = "2023-10-01 12:00:00" 

63 assert convert_to_datetime64_utc(naive_string) == np.datetime64("2023-10-01T12:00:00.000000000") 

64 

65 # Test with a timezone-aware string representation 

66 aware_string = "2023-10-01 12:00:00-04:00" 

67 assert convert_to_datetime64_utc(aware_string) == np.datetime64("2023-10-01T16:00:00.000000000") 

68 

69 # Test with an np.datetime64 

70 naive_np_datetime = np.datetime64("2023-10-01T12:00:00.000000000") 

71 assert convert_to_datetime64_utc(naive_np_datetime) == np.datetime64("2023-10-01T12:00:00.000000000") 

72 

73 

74if __name__ == "__main__": 74 ↛ 75line 74 didn't jump to line 75 because the condition on line 74 was never true

75 test_create_cf_time_axis_invalid_time_type() 

76 test_create_cf_time_axis_empty_data() 

77 test_create_cf_time_axis_valid_input()