|
1 | 1 | from datetime import datetime
|
| 2 | +from io import StringIO |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +import tempfile |
2 | 6 | from zoneinfo import ZoneInfo
|
3 | 7 |
|
4 | 8 | import numpy as np
|
@@ -252,3 +256,29 @@ def test_djd_to_datetime():
|
252 | 256 |
|
253 | 257 | expected = datetime(1974, 6, 22, 23, 30, 15, tzinfo=ZoneInfo("UTC"))
|
254 | 258 | assert tools.djd_to_datetime(djd) == expected
|
| 259 | + |
| 260 | + |
| 261 | +def test__file_context_manager(): |
| 262 | + with tempfile.TemporaryDirectory() as td: |
| 263 | + # make a test file |
| 264 | + filename = os.path.join(td, 'test.txt') |
| 265 | + with open(filename, 'w') as fh: |
| 266 | + fh.write('test content') |
| 267 | + |
| 268 | + # test with filename as string: |
| 269 | + with tools._file_context_manager(filename) as obj: |
| 270 | + assert obj.read() == "test content" |
| 271 | + |
| 272 | + # test with filename as Path: |
| 273 | + with tools._file_context_manager(Path(filename)) as obj: |
| 274 | + assert obj.read() == "test content" |
| 275 | + |
| 276 | + # test with file object: |
| 277 | + with open(filename, "r") as f: |
| 278 | + with tools._file_context_manager(f) as obj: |
| 279 | + assert obj.read() == "test content" |
| 280 | + |
| 281 | + # test with buffer: |
| 282 | + buffer = StringIO("test content") |
| 283 | + with tools._file_context_manager(buffer) as obj: |
| 284 | + assert obj.read() == "test content" |
0 commit comments