|
| 1 | +""" |
| 2 | +Using "pytest-crate" with CrateDB and pytest |
| 3 | +
|
| 4 | +Build test harnesses around CrateDB using the `crate` pytest fixture |
| 5 | +exported by `pytest-crate`. In turn, this is using `CrateNode` |
| 6 | +exported by `cr8`. |
| 7 | +
|
| 8 | +https://pypi.org/project/pytest-crate/ |
| 9 | +https://pypi.org/project/cr8/ |
| 10 | +""" |
| 11 | +import subprocess |
| 12 | + |
| 13 | +SQL_STATEMENT = "SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3;" |
| 14 | + |
| 15 | + |
| 16 | +def test_crash(crate): |
| 17 | + """ |
| 18 | + After provisioning a test instance of CrateDB, invoke `crash`. |
| 19 | + """ |
| 20 | + http_url = crate.dsn() |
| 21 | + command = f"time crash --hosts '{http_url}' --command 'SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3;'" |
| 22 | + subprocess.check_call(command, shell=True) |
| 23 | + |
| 24 | + |
| 25 | +def test_crate(crate): |
| 26 | + assert crate.dsn().startswith("http://127.0.0.1:42") |
| 27 | + assert "http" in crate.addresses |
| 28 | + assert crate.addresses["http"].host == "127.0.0.1" |
| 29 | + assert 4300 > crate.addresses["http"].port >= 4200 |
| 30 | + assert "psql" in crate.addresses |
| 31 | + assert crate.addresses["psql"].host == "127.0.0.1" |
| 32 | + assert 5500 > crate.addresses["psql"].port >= 5432 |
| 33 | + assert "transport" in crate.addresses |
| 34 | + assert crate.addresses["transport"].host == "127.0.0.1" |
| 35 | + assert 4400 > crate.addresses["transport"].port >= 4300 |
| 36 | + |
| 37 | + |
| 38 | +def test_cursor(crate_cursor): |
| 39 | + crate_cursor.execute("SELECT 1") |
| 40 | + assert crate_cursor.fetchone() == [1] |
| 41 | + |
| 42 | + |
| 43 | +def test_execute(crate_execute, crate_cursor): |
| 44 | + for stmt in [ |
| 45 | + "CREATE TABLE pytest (name STRING, version INT)", |
| 46 | + "INSERT INTO pytest (name, version) VALUES ('test_execute', 1)", |
| 47 | + "REFRESH TABLE pytest", |
| 48 | + ]: |
| 49 | + crate_execute(stmt) |
| 50 | + crate_cursor.execute("SELECT name, version FROM pytest") |
| 51 | + assert crate_cursor.fetchall() == [["test_execute", 1]] |
0 commit comments