Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.
/ cr8 Public archive

Commit b70a69d

Browse files
committed
Testing: Generalize integration test setup/teardown boilerplate code
1 parent 4825bcb commit b70a69d

File tree

2 files changed

+72
-40
lines changed

2 files changed

+72
-40
lines changed

tests/integration_util.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from cr8.clients import client
2+
from cr8 import aio
3+
from cr8.run_crate import get_crate, CrateNode
4+
5+
6+
crate_dir = get_crate('latest-testing')
7+
node = CrateNode(
8+
crate_dir=crate_dir,
9+
settings={
10+
'cluster.name': 'cr8-tests',
11+
'http.port': '44200-44250',
12+
'cluster.routing.allocation.disk.threshold_enabled': 'false',
13+
})
14+
15+
16+
def setup(*args):
17+
with client(node.http_url) as c:
18+
aio.run(
19+
c.execute,
20+
'create table x.demo (id int, name string, country string) \
21+
with (number_of_replicas = 0)'
22+
)
23+
aio.run(c.execute, 'create table y.demo (name text) with (number_of_replicas = 0)')
24+
aio.run(c.execute, 'create blob table blobtable with (number_of_replicas = 0)')
25+
26+
27+
def teardown(*args):
28+
with client(node.http_url) as c:
29+
aio.run(c.execute, 'drop table x.demo')
30+
aio.run(c.execute, 'drop table y.demo')
31+
aio.run(c.execute, 'drop blob table blobtable')
32+
33+
34+
def translate(s):
35+
"""
36+
Translate canonical database addresses to match the ones provided by the test layer.
37+
"""
38+
s = s.replace('localhost:4200', node.http_url)
39+
s = s.replace(
40+
'asyncpg://localhost:5432',
41+
f'asyncpg://{node.addresses.psql.host}:{node.addresses.psql.port}')
42+
s = s.replace(
43+
'postgresql://crate@localhost:5432/doc',
44+
f'postgresql://crate@{node.addresses.psql.host}:{node.addresses.psql.port}/doc')
45+
return s
46+
47+
48+
def transform(s):
49+
"""
50+
Transform all commands parsed from doctests.
51+
"""
52+
s = translate(s)
53+
return (
54+
r'print(sh("""%s""").stdout.decode("utf-8"))' % s) + '\n'

tests/test_integration.py

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,17 @@
55
import sys
66
import unittest
77

8-
from cr8.run_crate import CrateNode, get_crate
9-
from cr8.clients import client
10-
from cr8 import aio
8+
from cr8.run_crate import get_crate
9+
from tests.integration_util import teardown, node, setup, transform
1110

1211

13-
crate_dir = get_crate('latest-testing')
14-
node = CrateNode(
15-
crate_dir=crate_dir,
16-
settings={
17-
'cluster.name': 'cr8-tests',
18-
'http.port': '44200-44250'
19-
})
20-
21-
22-
def setup(*args):
23-
with client(node.http_url) as c:
24-
aio.run(
25-
c.execute,
26-
'create table x.demo (id int, name string, country string) \
27-
with (number_of_replicas = 0)'
28-
)
29-
aio.run(c.execute, 'create table y.demo (name text) with (number_of_replicas = 0)')
30-
aio.run(c.execute, 'create blob table blobtable with (number_of_replicas = 0)')
31-
32-
33-
def teardown(*args):
12+
def final_teardown(*args):
3413
try:
35-
with client(node.http_url) as c:
36-
aio.run(c.execute, 'drop table x.demo')
37-
aio.run(c.execute, 'drop blob table blobtable')
14+
teardown()
3815
finally:
3916
node.stop()
4017

4118

42-
def transform(s):
43-
s = s.replace('localhost:4200', node.http_url)
44-
s = s.replace(
45-
'asyncpg://localhost:5432',
46-
f'asyncpg://{node.addresses.psql.host}:{node.addresses.psql.port}')
47-
s = s.replace(
48-
'postgresql://crate@localhost:5432/doc',
49-
f'postgresql://crate@{node.addresses.psql.host}:{node.addresses.psql.port}/doc')
50-
return (
51-
r'print(sh("""%s""").stdout.decode("utf-8"))' % s) + '\n'
52-
53-
5419
class Parser(doctest.DocTestParser):
5520

5621
def parse(self, string, name='<string>'):
@@ -69,10 +34,23 @@ def test_build_from_branch(self):
6934

7035

7136
def load_tests(loader, tests, ignore):
37+
"""
38+
Intercept test discovery, in order to add doctests from `README.rst`.
39+
"""
40+
41+
# FIXME: doctests have errors on Windows.
42+
if sys.platform.startswith("win"):
43+
return tests
44+
45+
# Parsing doctests happens early, way before the test suite is invoked.
46+
# However, the doctest translator needs to know about the TCP address
47+
# of CrateDB, so it needs to be started right away.
7248
env = os.environ.copy()
7349
env['CR8_NO_TQDM'] = 'True'
7450
node.start()
7551
assert node.http_host, "http_url must be available"
52+
53+
# Add integration tests defined as doctests in README.rst.
7654
tests.addTests(doctest.DocFileSuite(
7755
os.path.join('..', 'README.rst'),
7856
globs={
@@ -88,7 +66,7 @@ def load_tests(loader, tests, ignore):
8866
},
8967
optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS,
9068
setUp=setup,
91-
tearDown=teardown,
69+
tearDown=final_teardown,
9270
parser=Parser()
9371
))
9472
return tests

0 commit comments

Comments
 (0)