File: test_sql_db_ext_tables.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (201 lines) | stat: -rw-r--r-- 6,596 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os

import pytest

from ase.db import connect
from ase import Atoms
import numpy as np


def test_sql_db_ext_tables(psycopg2):
    DB_NAMES = ["test_ext_tables.db", "postgresql", "mysql", "mariadb"]


    def get_db_name(name):
        if name == 'postgresql':
            if os.environ.get('POSTGRES_DB'):  # gitlab-ci
                name = 'postgresql://ase:ase@postgres:5432/testase'
            else:
                name = os.environ.get('ASE_TEST_POSTGRES_URL')
        elif name == "mysql":
            if os.environ.get('CI_PROJECT_DIR'):  # gitlab-ci
                name = 'mysql://root:ase@mysql:3306/testase_mysql'
            else:
                name = os.environ.get('MYSQL_DB_URL')
        elif name == 'mariadb':
            if os.environ.get('CI_PROJECT_DIR'):  # gitlab-ci
                name = 'mariadb://root:ase@mariadb:3306/testase_mysql'
            else:
                name = os.environ.get('MYSQL_DB_URL')
        return name


    def test_create_and_delete_ext_tab(db_name):
        ext_tab = ["tab1", "tab2", "tab3"]
        atoms = Atoms()
        db = connect(name)
        db.write(atoms)

        for tab in ext_tab:
            db._create_table_if_not_exists(tab, "INTEGER")
        current_ext_tables = db._get_external_table_names()
        for tab in ext_tab:
            assert tab in current_ext_tables

        db.delete_external_table("tab1")
        assert "tab1" not in db._get_external_table_names()


    def test_insert_in_external_tables(db_name):
        atoms = Atoms()
        db = connect(db_name)

        # Now a table called insert_tab with schema datatype REAL should
        # be created
        uid = db.write(
            atoms,
            external_tables={
                "insert_tab": {
                    "rate": 1.0,
                    "rate1": -
                    2.0}})

        db.delete([uid])

        # Hack: retrieve the connection
        con = db._connect()
        cur = con.cursor()

        sql = "SELECT * FROM insert_tab WHERE ID=?"
        cur.execute(sql, (uid,))

        entries = [x for x in cur.fetchall()]
        if db.connection is None:
            con.close()
        assert not entries

        # Make sure that there are now entries in the
        # external table with current uid

        # Try to insert something that should not pass
        # i.e. string value into the same table
        with pytest.raises(ValueError):
            db.write(atoms, external_tables={"insert_tab": {"rate": "something"}})

        # Try to insert Numpy floats
        db.write(atoms, external_tables={"insert_tab": {"rate": np.float32(1.0)}})
        db.write(atoms, external_tables={"insert_tab": {"rate": np.float64(1.0)}})

        # Make sure that we cannot insert a Numpy integer types into
        # a float array
        with pytest.raises(ValueError):
            db.write(
                atoms, external_tables={
                    "insert_tab": {
                        "rate": np.int32(1.0)}})

        with pytest.raises(ValueError):
            db.write(
                atoms, external_tables={
                    "insert_tab": {
                        "rate": np.int64(1.0)}})

        # Create a new table should have INTEGER types
        db.write(atoms, external_tables={"integer_tab": {"rate": 1}})

        # Make sure we can insert Numpy integers
        db.write(atoms, external_tables={"integer_tab": {"rate": np.int32(1)}})
        db.write(atoms, external_tables={"integer_tab": {"rate": np.int64(1)}})

        # Make sure that we cannot insert float
        with pytest.raises(ValueError):
            db.write(
                atoms, external_tables={
                    "integer_tab": {
                        "rate": np.float32(1)}})

        with pytest.raises(ValueError):
            db.write(
                atoms, external_tables={
                    "integer_tab": {
                        "rate": np.float64(1)}})

        # Make sure that ValueError is raised with mixed datatypes
        with pytest.raises(ValueError):
            db.write(
                atoms,
                external_tables={
                    "integer_tab": {
                        "rate": 1,
                        "rate2": 2.0}})

        # Test that we cannot insert anything into a reserved table name
        from ase.db.sqlite import all_tables
        for tab_name in all_tables:
            with pytest.raises(ValueError):
                db.write(atoms, external_tables={tab_name: {"value": 1}})


    def test_extract_from_table(db_name):
        atoms = Atoms()
        db = connect(db_name)
        uid = db.write(
            atoms,
            external_tables={
                "insert_tab": {
                    "rate": 12.0,
                    "rate1": -
                    10.0}})

        row = db.get(id=uid)
        assert abs(row["insert_tab"]["rate"] - 12.0) < 1E-8
        assert abs(row["insert_tab"]["rate1"] + 10.0) < 1E-8


    def test_write_atoms_row(db_name):
        atoms = Atoms()
        db = connect(db_name)
        uid = db.write(
            atoms, external_tables={"insert_tab": {"rate": 12.0, "rate1": -10.0},
                                    "another_tab": {"somevalue": 1.0}})
        row = db.get(id=uid)

        # Hack: Just change the unique ID
        row["unique_id"] = "uniqueIDTest"
        db.write(row)


    def test_external_table_upon_update(db_name):
        db = connect(db_name)
        no_features = 500
        ext_table = dict((i, i) for i in range(no_features))
        atoms = Atoms('Pb', positions=[[0, 0, 0]])
        uid = db.write(atoms)
        db.update(uid, external_tables={'sys': ext_table})


    def test_external_table_upon_update_with_float(db_name):
        db = connect(db_name)
        ext_table = {'value1': 1.0, 'value2': 2.0}
        atoms = Atoms('Pb', positions=[[0, 0, 0]])
        uid = db.write(atoms)
        db.update(uid, external_tables={'float_table': ext_table})

    for db_name in DB_NAMES:
        name = get_db_name(db_name)

        if name is None:
            continue

        if db_name in ["postgresql", "mysql", "mariadb"]:
            c = connect(name)
            c.delete([row.id for row in c.select()])
        test_create_and_delete_ext_tab(name)
        test_insert_in_external_tables(name)
        test_extract_from_table(name)
        test_write_atoms_row(name)
        test_external_table_upon_update(name)
        test_external_table_upon_update_with_float(name)

        if db_name not in ["postgresql", "mysql", "mariadb"]:
            os.remove(name)