File: test_sqlite.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 (64 lines) | stat: -rw-r--r-- 1,616 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
import os
from ase.db import connect

db_name = "test_vacuum.db"


def write_entries_to_db(db, n_entries_db=30):
    for i in range(n_entries_db):
        db.reserve(mykey=f'test_{i}')


def update_keys_in_db(db):
    new_keys = {}
    for i in range(50):   
        new_keys.update({f'mynewkey_{i}': 'test'})
    for row in db.select():
        db.update(row.id, **new_keys)


def check_delete_function(db):
    db_size_full = os.path.getsize(db_name)
    db.delete([row.id for row in db.select()])
    db_size_empty = os.path.getsize(db_name)
    assert db_size_full > db_size_empty


def check_update_function(db):
    db_size_update = os.path.getsize(db_name)
    db.vacuum()  # call vacuum explicitly
    db_size_update_vacuum = os.path.getsize(db_name)
    assert db_size_update > db_size_update_vacuum


def test_delete_vacuum():
    # test to call from within the class using the delete function
    db = connect(db_name)
    write_entries_to_db(db)
    check_delete_function(db)


def test_delete_vacuum_context():
    # try within context
    with connect(db_name) as db:
        write_entries_to_db(db)
    with connect(db_name) as db:
        check_delete_function(db)


def test_update_vacuum():
    # test to call vacuum explicitly
    db = connect(db_name)
    write_entries_to_db(db)
    update_keys_in_db(db)
    check_update_function(db)


def test_update_vacuum_context():
    # within context manager
    with connect(db_name) as db:
        write_entries_to_db(db)
    with connect(db_name) as db:
        update_keys_in_db(db)
    with connect(db_name) as db:
        check_update_function(db)