diff --git a/abcd/frontends/commandline/commands.py b/abcd/frontends/commandline/commands.py index 485b7548..05e50f0e 100644 --- a/abcd/frontends/commandline/commands.py +++ b/abcd/frontends/commandline/commands.py @@ -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!") diff --git a/abcd/frontends/commandline/decorators.py b/abcd/frontends/commandline/decorators.py index 201f6000..ce509004 100644 --- a/abcd/frontends/commandline/decorators.py +++ b/abcd/frontends/commandline/decorators.py @@ -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 diff --git a/tests/__init__.py b/tests/__init__.py index c4c62ed9..7aac0c4e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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__": diff --git a/tests/cli.py b/tests/cli.py index 7f8be710..4149ecc8 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -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}") diff --git a/tests/mongo_mock.py b/tests/mongo_mock.py new file mode 100644 index 00000000..5d583fdc --- /dev/null +++ b/tests/mongo_mock.py @@ -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) diff --git a/tests/opensearch.py b/tests/opensearch.py index 542a7dfd..e7abc41f 100644 --- a/tests/opensearch.py +++ b/tests/opensearch.py @@ -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): """ @@ -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, @@ -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): """ @@ -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): """ diff --git a/tests/database.py b/tests/opensearch_mock.py similarity index 74% rename from tests/database.py rename to tests/opensearch_mock.py index d4a0c33b..6f0959c6 100644 --- a/tests/database.py +++ b/tests/opensearch_mock.py @@ -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): @@ -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 @@ -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): """ @@ -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( @@ -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( @@ -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( diff --git a/tests/parsers.py b/tests/parsers.py index a484f605..83705115 100644 --- a/tests/parsers.py +++ b/tests/parsers.py @@ -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) @@ -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) @@ -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) diff --git a/tests/properties.py b/tests/properties.py index 1afab1b7..76177cf9 100644 --- a/tests/properties.py +++ b/tests/properties.py @@ -1,4 +1,8 @@ +import os import unittest + +from pandas import DataFrame + from abcd.backends.atoms_properties import Properties @@ -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