Skip to content

Commit 75cefbd

Browse files
committed
Tidy and fix OpenSearch tests
1 parent f2e4bc9 commit 75cefbd

File tree

3 files changed

+120
-136
lines changed

3 files changed

+120
-136
lines changed

tests/test_cli.py

Lines changed: 101 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -8,117 +8,104 @@
88

99
DATA_PATH = Path(__file__).parent / "data"
1010

11-
NOT_GTHUB_ACTIONS = True
12-
if os.getenv("GITHUB_ACTIONS") == "true":
13-
NOT_GTHUB_ACTIONS = False
14-
15-
16-
@pytest.mark.skipif(NOT_GTHUB_ACTIONS, reason="Not running via GitHub Actions")
17-
class TestCli:
18-
"""Testing OpenSearch database CLI integration."""
19-
20-
@pytest.fixture(autouse=True)
21-
def abcd(self):
22-
"""Set up OpenSearch database connection and login with CLI."""
23-
security_enabled = os.getenv("security_enabled") == "true"
24-
port = int(os.environ["port"])
25-
host = "localhost"
26-
if os.environ["opensearch-version"] == "latest":
27-
credential = "admin:myStrongPassword123!"
28-
else:
29-
credential = "admin:admin"
30-
31-
logging.basicConfig(level=logging.INFO)
32-
33-
url = f"opensearch://{credential}@{host}:{port}"
34-
if not security_enabled:
35-
url += " --disable_ssl"
36-
try:
37-
subprocess.run(f"abcd login {url}", shell=True, check=True)
38-
except subprocess.CalledProcessError:
39-
sleep(10)
40-
subprocess.run(f"abcd login {url}", shell=True, check=True)
41-
42-
def test_summary(self, abcd):
43-
"""
44-
Test summary output of uploaded data file.
45-
"""
46-
data_file = DATA_PATH / "example.xyz"
47-
48-
subprocess.run(
49-
f"abcd upload {data_file} -i -e 'test_data'", shell=True, check=True
50-
)
51-
subprocess.run("abcd refresh", shell=True, check=True)
52-
53-
summary = subprocess.run(
54-
"abcd summary", shell=True, check=True, capture_output=True, text=True
55-
)
56-
assert "Total number of configurations" in summary.stdout
57-
subprocess.run("abcd delete -q 'test_data' -y", shell=True)
58-
59-
def test_query(self, abcd):
60-
"""
61-
Test lucene-style query.
62-
"""
63-
data_file_1 = DATA_PATH / "example.xyz"
64-
data_file_2 = DATA_PATH / "example_2.xyz"
65-
66-
subprocess.run(
67-
f"abcd upload {data_file_1} -i -e 'test_data'", shell=True, check=True
68-
)
69-
subprocess.run(
70-
f"abcd upload {data_file_2} -i -e 'test_data'", shell=True, check=True
71-
)
72-
subprocess.run("abcd refresh", shell=True, check=True)
73-
74-
summary = subprocess.run(
75-
"abcd show -p n_atoms -q 'n_atoms : 2'",
76-
shell=True,
77-
check=True,
78-
capture_output=True,
79-
text=True,
80-
)
81-
assert "2" in summary.stdout and "3" not in summary.stdout
82-
summary = subprocess.run(
83-
"abcd show -p n_atoms -q 'n_atoms : 3'",
84-
shell=True,
85-
check=True,
86-
capture_output=True,
87-
text=True,
88-
)
89-
assert "3" in summary.stdout and "2" not in summary.stdout
90-
subprocess.run("abcd delete -q 'test_data' -y", shell=True)
91-
92-
def test_range_query(self, abcd):
93-
"""
94-
Test lucene-style ranged query.
95-
"""
96-
data_file_1 = DATA_PATH / "example.xyz"
97-
data_file_2 = DATA_PATH / "example_2.xyz"
98-
99-
subprocess.run(
100-
f"abcd upload {data_file_1} -i -e 'test_data'", shell=True, check=True
101-
)
102-
subprocess.run(
103-
f"abcd upload {data_file_2} -i -e 'test_data'", shell=True, check=True
104-
)
105-
subprocess.run("abcd refresh", shell=True, check=True)
106-
107-
summary = subprocess.run(
108-
"abcd summary -p energy -q 'energy:[-100 TO -99]'",
109-
shell=True,
110-
check=True,
111-
capture_output=True,
112-
text=True,
113-
)
114-
assert "Total number of configurations: 1" in summary.stdout
115-
116-
summary = subprocess.run(
117-
"abcd summary -p energy -q 'energy:[-102 TO -99]'",
118-
shell=True,
119-
check=True,
120-
capture_output=True,
121-
text=True,
122-
)
123-
assert "Total number of configurations: 2" in summary.stdout
124-
subprocess.run("abcd delete -q 'test_data' -y", shell=True)
11+
PORT = int(os.environ.get("port", 0))
12+
HOST = "localhost"
13+
CREDENTIAL = "admin:myStrongPassword_123"
14+
SECURITY_ENABLED = os.getenv("security_enabled") == "true"
15+
16+
17+
@pytest.fixture(autouse=True)
18+
def skip_if_not_github_actions():
19+
if os.getenv("GITHUB_ACTIONS") == "true":
20+
yield
21+
pytest.skip("Not running via GitHub Actions")
22+
23+
24+
@pytest.fixture(autouse=True)
25+
def abcd():
26+
"""Set up OpenSearch database connection and login with CLI."""
27+
logging.basicConfig(level=logging.INFO)
28+
29+
url = f"opensearch://{CREDENTIAL}@{HOST}:{PORT}"
30+
if not SECURITY_ENABLED:
31+
url += " --disable_ssl"
32+
try:
33+
subprocess.run(f"abcd login {url}", shell=True, check=True)
34+
except subprocess.CalledProcessError:
35+
sleep(10)
36+
subprocess.run(f"abcd login {url}", shell=True, check=True)
37+
38+
39+
def test_summary():
40+
"""Test summary output of uploaded data file."""
41+
data_file = DATA_PATH / "example.xyz"
42+
43+
subprocess.run(f"abcd upload {data_file} -i -e 'test_data'", shell=True, check=True)
44+
subprocess.run("abcd refresh", shell=True, check=True)
45+
46+
summary = subprocess.run(
47+
"abcd summary", shell=True, check=True, capture_output=True, text=True
48+
)
49+
assert "Total number of configurations" in summary.stdout
50+
subprocess.run("abcd delete -q 'test_data' -y", shell=True)
51+
52+
53+
def test_query():
54+
"""Test lucene-style query."""
55+
data_file_1 = DATA_PATH / "example.xyz"
56+
data_file_2 = DATA_PATH / "example_2.xyz"
57+
58+
subprocess.run(
59+
f"abcd upload {data_file_1} -i -e 'test_data'", shell=True, check=True
60+
)
61+
subprocess.run(
62+
f"abcd upload {data_file_2} -i -e 'test_data'", shell=True, check=True
63+
)
64+
subprocess.run("abcd refresh", shell=True, check=True)
65+
66+
summary = subprocess.run(
67+
"abcd show -p n_atoms -q 'n_atoms : 2'",
68+
shell=True,
69+
check=True,
70+
capture_output=True,
71+
text=True,
72+
)
73+
assert "2" in summary.stdout and "3" not in summary.stdout
74+
summary = subprocess.run(
75+
"abcd show -p n_atoms -q 'n_atoms : 3'",
76+
shell=True,
77+
check=True,
78+
capture_output=True,
79+
text=True,
80+
)
81+
assert "3" in summary.stdout and "2" not in summary.stdout
82+
subprocess.run("abcd delete -q 'test_data' -y", shell=True)
83+
84+
85+
def test_range_query():
86+
"""Test lucene-style ranged query."""
87+
data_file_1 = DATA_PATH / "example.xyz"
88+
data_file_2 = DATA_PATH / "example_2.xyz"
89+
90+
subprocess.run(f"abcd upload {data_file_1} -e 'test_data'", shell=True, check=True)
91+
subprocess.run(f"abcd upload {data_file_2} -e 'test_data'", shell=True, check=True)
92+
subprocess.run("abcd refresh", shell=True, check=True)
93+
94+
summary = subprocess.run(
95+
"abcd summary -p energy -q 'energy:[-100 TO -99]'",
96+
shell=True,
97+
check=True,
98+
capture_output=True,
99+
text=True,
100+
)
101+
assert "Total number of configurations: 1" in summary.stdout
102+
103+
summary = subprocess.run(
104+
"abcd summary -p energy -q 'energy:[-102 TO -99]'",
105+
shell=True,
106+
check=True,
107+
capture_output=True,
108+
text=True,
109+
)
110+
assert "Total number of configurations: 2" in summary.stdout
111+
subprocess.run("abcd delete -q 'test_data' -y", shell=True)

tests/test_opensearch.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
PORT = int(os.environ.get("port", 0))
1515
HOST = "localhost"
16+
CREDENTIAL = "admin:myStrongPassword_123"
17+
SECURITY_ENABLED = os.getenv("security_enabled") == "true"
1618

1719

1820
@pytest.fixture(autouse=True)
@@ -25,24 +27,21 @@ def skip_if_not_github_actions():
2527
@pytest.fixture()
2628
def abcd():
2729
"""Set up OpenSearch database connection."""
28-
security_enabled = os.getenv("security_enabled") == "true"
29-
credential = "admin:myStrongPassword_123"
30-
3130
logging.basicConfig(level=logging.INFO)
3231

33-
url = f"opensearch://{credential}@{HOST}:{PORT}"
32+
url = f"opensearch://{CREDENTIAL}@{HOST}:{PORT}"
3433
try:
3534
abcd_opensearch = ABCD.from_url(
3635
url,
3736
index_name="test_index",
38-
use_ssl=security_enabled,
37+
use_ssl=SECURITY_ENABLED,
3938
)
4039
except (ConnectionError, ConnectionResetError):
4140
sleep(10)
4241
abcd_opensearch = ABCD.from_url(
4342
url,
4443
index_name="test_index",
45-
use_ssl=security_enabled,
44+
use_ssl=SECURITY_ENABLED,
4645
)
4746

4847
assert isinstance(abcd_opensearch, OpenSearchDatabase)
@@ -375,7 +374,7 @@ def test_count_properties(abcd):
375374
assert props == expected_counts
376375

377376

378-
def test_add_property(abcd):
377+
def test_add_property(abcd, push_data):
379378
"""Test adding a property to documents in the database."""
380379
abcd.destroy()
381380
abcd.create()
@@ -388,7 +387,7 @@ def test_add_property(abcd):
388387
assert "TEST_PROPERTY" in data["hits"]["hits"][0]["_source"]["derived"]["info_keys"]
389388

390389

391-
def test_rename_property(abcd):
390+
def test_rename_property(abcd, push_data):
392391
"""Test renaming a property for documents in the database."""
393392
abcd.destroy()
394393
abcd.create()
@@ -424,7 +423,7 @@ def test_delete_property(abcd, push_data):
424423
)
425424

426425

427-
def test_get_items(abcd):
426+
def test_get_items(abcd, push_data):
428427
"""Test getting a dictionary of values from documents in the database."""
429428
abcd.destroy()
430429
abcd.create()

tests/test_opensearch_mock.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,27 @@
1010
from abcd import ABCD
1111
from abcd.backends.atoms_opensearch import AtomsModel, OpenSearchDatabase
1212

13+
PORT = int(os.environ.get("port", 9200))
14+
HOST = "localhost"
15+
CREDENTIAL = "admin:myStrongPassword_123"
16+
if os.getenv("GITHUB_ACTIONS") == "true":
17+
SECURITY_ENABLED = os.getenv("security_enabled") == "true"
18+
else:
19+
# Otherwise assume local OpenSearch is enabled
20+
SECURITY_ENABLED = True
21+
1322

1423
@pytest.fixture(autouse=True)
1524
@openmock
1625
def abcd():
1726
"""Set up database connection."""
18-
port = int(os.environ.get("port", 9200))
19-
host = "localhost"
20-
# If running in CI, enable/disable security
21-
if os.getenv("GITHUB_ACTIONS") == "true":
22-
security_enabled = os.getenv("security_enabled") == "true"
23-
else:
24-
# Otherwise assume local OpenSearch is enabled
25-
security_enabled = True
26-
27-
credential = "admin:myStrongPassword_123"
28-
2927
logging.basicConfig(level=logging.INFO)
3028

31-
url = f"opensearch://{credential}@{host}:{port}"
29+
url = f"opensearch://{CREDENTIAL}@{HOST}:{PORT}"
3230
opensearch_abcd = ABCD.from_url(
3331
url,
3432
index_name="test_index",
35-
use_ssl=security_enabled,
33+
use_ssl=SECURITY_ENABLED,
3634
)
3735
assert isinstance(opensearch_abcd, OpenSearchDatabase)
3836
return opensearch_abcd

0 commit comments

Comments
 (0)