File: test_datastorage.py

package info (click to toggle)
pynpoint 0.11.1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,352 kB
  • sloc: python: 15,971; makefile: 73
file content (76 lines) | stat: -rw-r--r-- 1,970 bytes parent folder | download
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
import os

import pytest
import h5py
import numpy as np

from pynpoint.core.dataio import DataStorage


class TestDataStorage:

    def setup_class(self) -> None:

        self.limit = 1e-10
        self.test_data = os.path.dirname(__file__) + "/PynPoint_database.hdf5"

    def test_create_storage_with_existing_database(self) -> None:

        np.random.seed(1)
        images = np.random.normal(loc=0, scale=2e-4, size=(10, 100, 100))

        with h5py.File(self.test_data, "w") as hdf_file:
            hdf_file.create_dataset("images", data=images)

        storage = DataStorage(self.test_data)
        storage.open_connection()
        data = storage.m_data_bank["images"]

        assert data[0, 0, 0] == pytest.approx(
            0.00032486907273264834, rel=self.limit, abs=0.0
        )
        assert np.mean(data) == pytest.approx(
            1.0506056979365338e-06, rel=self.limit, abs=0.0
        )

        os.remove(self.test_data)

    def test_create_storage_without_existing_database(self) -> None:

        storage = DataStorage(self.test_data)
        storage.open_connection()
        storage.m_data_bank["data"] = [0, 1, 2, 5, 7]

        assert storage.m_data_bank["data"][2] == 2
        assert list(storage.m_data_bank.keys()) == [
            "data",
        ]

        storage.close_connection()

        os.remove(self.test_data)

    def test_create_storage_with_wrong_location(self) -> None:

        file_in = "/test/test.hdf5"

        with pytest.raises(AssertionError):
            DataStorage(file_in)

    def test_open_close_connection(self) -> None:

        storage = DataStorage(self.test_data)

        storage.open_connection()
        assert storage.m_open is True

        storage.open_connection()
        assert storage.m_open is True

        storage.close_connection()
        assert storage.m_open is False

        storage.close_connection()
        assert storage.m_open is False

        os.remove(self.test_data)