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
|
# fmt: off
import io
import pytest
from ase import Atoms
from ase.calculators.calculator import compare_atoms
from ase.db import connect
from ase.db.app import DatabaseProject
from ase.db.cli import check_jsmol
from ase.db.web import Session
from ase.io import read
projectname = 'db-web-test-project'
def get_atoms():
atoms = Atoms('H2O',
[(0, 0, 0),
(2, 0, 0),
(1, 1, 0)])
atoms.center(vacuum=5)
atoms.set_pbc(True)
return atoms
@pytest.fixture(scope='module')
def database(tmp_path_factory):
dbtestdir = tmp_path_factory.mktemp('dbtest')
db = connect(dbtestdir / 'test.db', append=False)
x = [0, 1, 2]
t1 = [1, 2, 0]
t2 = [[2, 3], [1, 1], [1, 0]]
atoms = get_atoms()
db.write(atoms,
foo=42.0,
bar='abc',
data={'x': x,
't1': t1,
't2': t2})
db.write(atoms)
return db
@pytest.fixture(scope='module')
def client(database):
pytest.importorskip('flask')
from ase.db.app import DBApp
dbapp = DBApp()
dbapp.add_project(projectname, database)
app = dbapp.flask
app.testing = True
return app.test_client()
def test_add_columns(database):
"""Test that all keys can be added also for row withous keys."""
pytest.importorskip('flask')
session = Session('name')
project = DatabaseProject.dummyproject(
default_columns=['bar'])
session.update('query', '', {'query': 'id=2'}, project)
table = session.create_table(database, 'id', ['foo'])
assert table.columns == ['bar'] # selected row doesn't have a foo key
assert 'foo' in table.addcolumns # ... but we can add it
def test_favicon(client):
# no content or redirect
assert client.get('/favicon.ico').status_code in (204, 308)
assert client.get('/favicon.ico/').status_code == 204 # no content
def test_db_web(client):
c = client
page = c.get('/').data.decode()
sid = Session.next_id - 1
assert 'foo' in page
for url in [f'/update/{sid}/query/bla/?query=id=1',
f'/{projectname}/row/1']:
resp = c.get(url)
assert resp.status_code == 200
for type in ['json', 'xyz', 'cif']:
url = f'atoms/{projectname}/1/{type}'
resp = c.get(url)
assert resp.status_code == 200
txt = resp.data.decode()
print(type)
print(txt)
fmt = type
if fmt == 'xyz':
fmt = 'extxyz'
atoms = read(io.StringIO(txt), format=fmt)
assert (atoms.numbers == [1, 1, 8]).all()
tol = 1e-5 if type == 'cif' else 1e-10
assert not compare_atoms(atoms, get_atoms(), tol), type
def test_paging(database):
"""Test paging."""
pytest.importorskip('flask')
session = Session('name')
project = DatabaseProject.dummyproject(
default_columns=['bar'])
session.update('query', '', {'query': ''}, project)
table = session.create_table(database, 'id', ['foo'])
assert len(table.rows) == 2
assert session.nrows == 2
assert session.nrows_total == 2
session.update('limit', '1', {}, project)
session.update('page', '1', {}, project)
table = session.create_table(database, 'id', ['foo'])
assert len(table.rows) == 1
# We are now on page 2 and select something on page 1:
session.update('query', '', {'query': 'id=1'}, project)
table = session.create_table(database, 'id', ['foo'])
assert len(table.rows) == 1
assert session.nrows == 1
assert session.nrows_total == 2
def test_check_jsmol():
check_jsmol()
|