File: test_mysql.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 (111 lines) | stat: -rw-r--r-- 2,664 bytes parent folder | download | duplicates (2)
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
import os

import pytest

from ase.db import connect
from ase import Atoms
from ase.calculators.emt import EMT
from ase.build import molecule


@pytest.fixture(scope='module')
def url():
    pytest.importorskip('pymysql')

    on_ci_server = 'CI_PROJECT_DIR' in os.environ

    if on_ci_server:
        db_url = 'mysql://root:ase@mysql:3306/testase_mysql'
        # HOST = 'mysql'
        # USER = 'root'
        # PASSWD = 'ase'
        # DB_NAME = 'testase_mysql'
    else:
        db_url = os.environ.get('MYSQL_DB_URL')
        # HOST = os.environ.get('MYSQL_HOST', None)
        # USER = os.environ.get('MYSQL_USER', None)
        # PASSWD = os.environ.get('MYSQL_PASSWD', None)
        # DB_NAME = os.environ.get('MYSQL_DB_NAME', None)

    if db_url is None:
        msg = ('Not on GitLab CI server. To run this test '
               'host, username, password and database name '
               'must be in the environment variables '
               'MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD and '
               'MYSQL_DB_NAME, respectively.')
        pytest.skip(msg)
    return db_url


@pytest.fixture
def db(url):
    return connect(url)


@pytest.fixture
def h2o():
    return molecule('H2O')


def test_connect(db):
    db.delete([row.id for row in db.select()])


def test_write_read(db):
    co = Atoms('CO', positions=[(0, 0, 0), (0, 0, 1.1)])
    uid = db.write(co, tag=1, type='molecule')

    co_db = db.get(id=uid)
    atoms_db = co_db.toatoms()

    assert len(atoms_db) == 2
    assert atoms_db[0].symbol == co[0].symbol
    assert atoms_db[1].symbol == co[1].symbol
    assert co_db.tag == 1
    assert co_db.type == 'molecule'


def test_write_read_with_calculator(db, h2o):
    calc = EMT(dummy_param=2.4)
    h2o.calc = calc

    uid = db.write(h2o)

    h2o_db = db.get(id=uid).toatoms(attach_calculator=True)

    calc_db = h2o_db.calc
    assert calc_db.parameters['dummy_param'] == 2.4

    # Check that get_atoms function works
    db.get_atoms(H=2)


def test_update(db, h2o):
    h2o = molecule('H2O')

    uid = db.write(h2o, type='molecule')
    db.update(id=uid, type='oxide')

    atoms_type = db.get(id=uid).type

    assert atoms_type == 'oxide'


def test_delete(db, h2o):
    h2o = molecule('H2O')
    uid = db.write(h2o, type='molecule')

    # Make sure that we can get the value
    db.get(id=uid)
    db.delete([uid])

    with pytest.raises(KeyError):
        db.get(id=uid)


def test_read_write_bool_key_value_pair(db, h2o):
    # Make sure we can read and write boolean key value pairs
    uid = db.write(h2o, is_water=True, is_solid=False)
    row = db.get(id=uid)
    assert row.is_water
    assert not row.is_solid