-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
131 lines (89 loc) · 3.57 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# tests.py
#
# Copyright 2020 QuatroPe
#
# This file is part of ProperImage (https://github.com/quatrope/tinynpydb)
# License: BSD3
# Full Text: https://github.com/quatrope/tinynpydb/blob/master/LICENSE
#
import os
import pytest
import tempfile
import pathlib
import numpy as np
import tinynpydb as tnpdb
# =============================================================================
# CONSTANTS
# =============================================================================
TEMP_DIR = tempfile.mkdtemp(suffix="_tinynpydb")
TEMP_PATH = pathlib.Path(TEMP_DIR)
# =============================================================================
# FIXTURES
# =============================================================================
@pytest.fixture(scope="session")
def singlearray():
return np.random.random(size=(3, 3))
@pytest.fixture(scope="session")
def manyarrays():
return [np.random.random(size=(3, 3)) for i in range(10)]
@pytest.fixture(scope="session")
def staticarray():
return np.array([[1, 2, 3], [-1, -2, -3], [0.1, 0.2, 0.3]])
# =============================================================================
# TESTS
# =============================================================================
def test_store_singlearray(singlearray):
dbname = TEMP_PATH / "test_store_singlearray"
npdb = tnpdb.NumPyDB(dbname, mode="store")
assert os.path.exists(dbname.with_suffix(".dat"))
assert os.path.exists(dbname.with_suffix(".map"))
assert os.path.isfile(dbname.with_suffix(".dat"))
assert os.path.isfile(dbname.with_suffix(".map"))
npdb.dump(singlearray, 0)
assert len(npdb.positions) == 1
def test_staticarray(staticarray):
dbname = TEMP_PATH / "test_staticarray"
npdb = tnpdb.NumPyDB(dbname, mode="store")
npdb.dump(staticarray, 0)
assert len(npdb.positions) == 1
npdb2 = tnpdb.NumPyDB(dbname, mode="load")
loaded_array, loaded_id = npdb2.load(0)
np.testing.assert_array_equal(staticarray, loaded_array)
def test_store_manyarrays(manyarrays):
dbname = TEMP_PATH / "test_store_manyarrays"
npdb = tnpdb.NumPyDB(dbname, mode="store")
for iarray, anarray in enumerate(manyarrays):
npdb.dump(anarray, iarray)
assert len(npdb.positions) == len(manyarrays)
def test_load_manyarrays(manyarrays):
original_arrays = manyarrays
dbname = TEMP_PATH / "test_load_manyarrays"
npdb = tnpdb.NumPyDB(dbname, mode="store")
for iarray, anarray in enumerate(original_arrays):
npdb.dump(anarray, iarray)
npdb2 = tnpdb.NumPyDB(dbname, mode="load")
for iarray, anarray in enumerate(original_arrays):
loaded_array, loaded_id = npdb2.load(iarray)
assert str(iarray) == loaded_id
np.testing.assert_array_equal(original_arrays[iarray], loaded_array)
# =============================================================================
# TESTING EXCEPTIONS
# =============================================================================
def test_opening_nonexisting_db():
dbname = TEMP_PATH / "test_opening_nonexisting_db"
with pytest.raises(IOError):
tnpdb.NumPyDB(dbname, mode="load")
def test_bad_identifier(manyarrays):
dbname = TEMP_PATH / "test_bad_identifier"
npdb = tnpdb.NumPyDB(dbname, mode="store")
for iarray, anarray in enumerate(manyarrays):
npdb.dump(anarray, iarray)
with pytest.raises(LookupError):
npdb.load(42)
def test_bad_mode():
dbname = TEMP_PATH / "test_bad_mode"
with pytest.raises(ValueError):
tnpdb.NumPyDB(dbname, mode="bad_mode")