Skip to content

Commit

Permalink
Fix SSL-disabled OpenSearch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliottKasoar committed Jan 22, 2024
1 parent 1a66af0 commit 3c8747b
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 85 deletions.
1 change: 1 addition & 0 deletions abcd/frontends/commandline/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def login(*, config, name, url, disable_ssl=False, **kwargs):
info = db.info()

config["url"] = url
config["use_ssl"] = not disable_ssl
config.save()

print("Successfully connected to the database!")
Expand Down
7 changes: 6 additions & 1 deletion abcd/frontends/commandline/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ def init_db(func):
@functools.wraps(func)
def wrapper(*args, config, **kwargs):
url = config.get("url", None)
use_ssl = config.get("use_ssl", None)

if url is None:
print("Please use abcd login first!")
exit(1)

db = ABCD.from_url(url=url)
if use_ssl is None:
print("use_ssl has not been saved. Please login again")
exit(1)

db = ABCD.from_url(url=url, use_ssl=use_ssl)

# TODO: AST.from_string() ?!
# TODO: better ast optimisation
Expand Down
10 changes: 6 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import unittest
import logging
# from tests.parsers import ParsingQueries, ParsingExtras
# from tests.database import MongoMock, OpenSearchMock
# from tests.properties import PropertiesTests
# from tests.opensearch import OpenSearch

from tests.parsers import ParsingQueries, ParsingExtras
from tests.properties import PropertiesTests
from tests.mongo_mock import MongoMock
from tests.opensearch_mock import OpenSearchMock
from tests.opensearch import OpenSearch
from tests.cli import CLI

if __name__ == "__main__":
Expand Down
6 changes: 5 additions & 1 deletion tests/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ def setUpClass(cls):
cls.security_enabled = os.getenv("security_enabled") == "true"
cls.port = int(os.environ["port"])
cls.host = "localhost"

logging.basicConfig(level=logging.INFO)

url = f"opensearch://admin:admin@{cls.host}:{cls.port}"
if not cls.security_enabled:
url += " --disable_ssl"
try:
os.system(f"abcd login {url}")
except ConnectionError or ConnectionResetError:
except (ConnectionError, ConnectionResetError):
sleep(10)
os.system(f"abcd login {url}")

Expand Down
52 changes: 52 additions & 0 deletions tests/mongo_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from io import StringIO
import logging
import unittest

from ase.io import read
from ase.atoms import Atoms
import mongomock

from abcd import ABCD


class MongoMock(unittest.TestCase):
@classmethod
@mongomock.patch(servers=(("localhost", 27017),))
def setUpClass(cls):
logging.basicConfig(level=logging.INFO)
url = "mongodb://localhost"
abcd = ABCD.from_url(url)
abcd.print_info()

cls.abcd = abcd

@classmethod
def tearDownClass(cls):
cls.abcd.destroy()

def test_thing(self):
print(self.abcd.info())

def test_push(self):
xyz = StringIO(
"""2
Properties=species:S:1:pos:R:3 s="sadf" _vtk_test="t _ e s t" pbc="F F F"
Si 0.00000000 0.00000000 0.00000000
Si 0.00000000 0.00000000 0.00000000
"""
)

atoms = read(xyz, format="extxyz")
assert isinstance(atoms, Atoms)
atoms.set_cell([1, 1, 1])

self.abcd.destroy()
self.abcd.push(atoms)
new = list(self.abcd.get_atoms())[0]

assert atoms == new
self.abcd.destroy()


if __name__ == "__main__":
unittest.main(verbosity=1, exit=False)
20 changes: 10 additions & 10 deletions tests/opensearch.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import unittest
from abcd import ABCD
from io import StringIO
import logging
import os
from time import sleep
from abcd.backends.atoms_opensearch import OpenSearchDatabase
from opensearchpy.exceptions import ConnectionError
from io import StringIO
from ase.io import read
import unittest

from ase.atoms import Atoms
from abcd.backends.atoms_opensearch import AtomsModel
from ase.io import read
from opensearchpy.exceptions import ConnectionError

from abcd import ABCD
from abcd.backends.atoms_opensearch import AtomsModel, OpenSearchDatabase


class OpenSearch(unittest.TestCase):
"""
Expand Down Expand Up @@ -36,7 +38,7 @@ def setUpClass(cls):
analyse_schema=False,
use_ssl=cls.security_enabled,
)
except ConnectionError or ConnectionResetError:
except (ConnectionError, ConnectionResetError):
sleep(10)
abcd = ABCD.from_url(
url,
Expand Down Expand Up @@ -103,7 +105,6 @@ def test_destroy(self):

self.abcd.destroy()
self.assertFalse(self.abcd.client.indices.exists("test_index"))
return

def test_create(self):
"""
Expand Down Expand Up @@ -165,7 +166,6 @@ def test_delete(self):
self.assertTrue(self.abcd.client.indices.exists("test_index"))
self.abcd.refresh()
self.assertEqual(self.abcd.count(), 0)
return

def test_bulk(self):
"""
Expand Down
79 changes: 18 additions & 61 deletions tests/database.py → tests/opensearch_mock.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,16 @@
from importlib import reload
from io import StringIO
import logging
import os
import unittest
import mongomock

from ase.atoms import Atoms
from ase.io import read
from openmock import openmock

from abcd import ABCD
import logging


class MongoMock(unittest.TestCase):
@classmethod
@mongomock.patch(servers=(("localhost", 27017),))
def setUpClass(cls):
logging.basicConfig(level=logging.INFO)
url = "mongodb://localhost"
abcd = ABCD.from_url(url)
abcd.print_info()

cls.abcd = abcd

@classmethod
def tearDownClass(cls):
cls.abcd.destroy()

def test_thing(self):
print(self.abcd.info())

def test_push(self):
from io import StringIO
from ase.io import read
from ase.atoms import Atoms

xyz = StringIO(
"""2
Properties=species:S:1:pos:R:3 s="sadf" _vtk_test="t _ e s t" pbc="F F F"
Si 0.00000000 0.00000000 0.00000000
Si 0.00000000 0.00000000 0.00000000
"""
)

atoms = read(xyz, format="extxyz")
assert isinstance(atoms, Atoms)
atoms.set_cell([1, 1, 1])

self.abcd.destroy()
self.abcd.push(atoms)
new = list(self.abcd.get_atoms())[0]

assert atoms == new
self.abcd.destroy()
from abcd.backends import atoms_opensearch
from abcd.backends.atoms_opensearch import AtomsModel


class OpenSearchMock(unittest.TestCase):
Expand All @@ -60,10 +24,18 @@ def setUpClass(cls):
"""
Set up database connection.
"""
reload(atoms_opensearch)
from abcd.backends.atoms_opensearch import OpenSearchDatabase

if "port" in os.environ:
cls.port = int(os.environ["port"])
else:
cls.port = 9200
cls.host = "localhost"

logging.basicConfig(level=logging.INFO)
url = "opensearch://admin:admin@localhost:9200"

url = f"opensearch://admin:admin@{cls.host}:{cls.port}"
abcd = ABCD.from_url(url, index_name="test_index", analyse_schema=False)
assert isinstance(abcd, OpenSearchDatabase)
cls.abcd = abcd
Expand All @@ -82,7 +54,6 @@ def test_destroy(self):
self.assertTrue(self.abcd.client.indices.exists("test_index"))
self.abcd.destroy()
self.assertFalse(self.abcd.client.indices.exists("test_index"))
return

def test_create(self):
"""
Expand All @@ -97,11 +68,6 @@ def test_push(self):
"""
Test pushing atoms objects to database individually.
"""
from io import StringIO
from ase.io import read
from ase.atoms import Atoms
from abcd.backends.atoms_opensearch import AtomsModel

self.abcd.destroy()
self.abcd.create()
xyz_1 = StringIO(
Expand Down Expand Up @@ -139,11 +105,6 @@ def test_bulk(self):
"""
Test pushing atoms object to database together.
"""
from io import StringIO
from ase.io import read
from ase.atoms import Atoms
from abcd.backends.atoms_opensearch import AtomsModel

self.abcd.destroy()
self.abcd.create()
xyz_1 = StringIO(
Expand Down Expand Up @@ -190,10 +151,6 @@ def test_count(self):
"""
Test counting the number of documents in the database.
"""
from io import StringIO
from ase.io import read
from ase.atoms import Atoms

self.abcd.destroy()
self.abcd.create()
xyz = StringIO(
Expand Down
6 changes: 3 additions & 3 deletions tests/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_operators(self):
('string = "some string"', {}),
('regexp ~ ".*H"', {}),
)
for string, expected in s:
for string, _ in s:
with self.subTest(string=string):
self.parser.parse(string)

Expand All @@ -200,7 +200,7 @@ def test_combination(self):
("aa and bb > 23 and bb > 23 and bb > 23 ", {}),
("aa and bb > 23.54 or 22 in cc and dd", {}),
)
for string, expected in s:
for string, _ in s:
with self.subTest(string=string):
self.parser.parse(string)

Expand All @@ -213,7 +213,7 @@ def test_expressions(self):
("all(aa) > 3", {}),
("any(aa) > 3", {}),
)
for string, expected in s:
for string, _ in s:
with self.subTest(string=string):
self.parser.parse(string)

Expand Down
10 changes: 5 additions & 5 deletions tests/properties.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import os
import unittest

from pandas import DataFrame

from abcd.backends.atoms_properties import Properties


Expand All @@ -10,18 +14,14 @@ def setUpClass(cls):
"""
Load example data file.
"""
import os

class_path = os.path.normpath(os.path.abspath(__file__))
data_file = os.path.dirname(class_path) + "/examples.csv"
data_file = os.path.dirname(class_path) + "/data/examples.csv"
cls.property = Properties(data_file)

def test_dataframe(self):
"""
Test data correctly stored in pandas DataFrame.
"""
from pandas import DataFrame

assert isinstance(self.property.df, DataFrame)
assert len(self.property.df) == 3

Expand Down

0 comments on commit 3c8747b

Please sign in to comment.