|
| 1 | +import requests |
| 2 | +from localstack.utils.strings import short_uid |
| 3 | +from typedb.driver import TypeDB, Credentials, DriverOptions, TransactionType |
| 4 | + |
| 5 | + |
| 6 | +def test_connect_to_db_via_http_api(): |
| 7 | + host = "typedb.localhost.localstack.cloud:4566" |
| 8 | + |
| 9 | + # get auth token |
| 10 | + response = requests.post( |
| 11 | + f"http://{host}/v1/signin", json={"username": "admin", "password": "password"} |
| 12 | + ) |
| 13 | + assert response.ok |
| 14 | + token = response.json()["token"] |
| 15 | + |
| 16 | + # create database |
| 17 | + db_name = f"db{short_uid()}" |
| 18 | + response = requests.post( |
| 19 | + f"http://{host}/v1/databases/{db_name}", |
| 20 | + json={}, |
| 21 | + headers={"Authorization": f"bearer {token}"}, |
| 22 | + ) |
| 23 | + assert response.ok |
| 24 | + |
| 25 | + # list databases |
| 26 | + response = requests.get( |
| 27 | + f"http://{host}/v1/databases", headers={"Authorization": f"bearer {token}"} |
| 28 | + ) |
| 29 | + assert response.ok |
| 30 | + databases = [db["name"] for db in response.json()["databases"]] |
| 31 | + assert db_name in databases |
| 32 | + |
| 33 | + # clean up |
| 34 | + response = requests.delete( |
| 35 | + f"http://{host}/v1/databases/{db_name}", |
| 36 | + headers={"Authorization": f"bearer {token}"}, |
| 37 | + ) |
| 38 | + assert response.ok |
| 39 | + |
| 40 | + |
| 41 | +def test_connect_to_db_via_grpc_endpoint(): |
| 42 | + db_name = "access-management-db" |
| 43 | + server_host = "typedb.localhost.localstack.cloud:4566" |
| 44 | + |
| 45 | + driver_cfg = TypeDB.driver( |
| 46 | + server_host, |
| 47 | + Credentials("admin", "password"), |
| 48 | + DriverOptions(is_tls_enabled=False), |
| 49 | + ) |
| 50 | + with driver_cfg as driver: |
| 51 | + if driver.databases.contains(db_name): |
| 52 | + driver.databases.get(db_name).delete() |
| 53 | + driver.databases.create(db_name) |
| 54 | + |
| 55 | + with driver.transaction(db_name, TransactionType.SCHEMA) as tx: |
| 56 | + tx.query("define entity person;").resolve() |
| 57 | + tx.query("define attribute name, value string; person owns name;").resolve() |
| 58 | + tx.commit() |
| 59 | + |
| 60 | + with driver.transaction(db_name, TransactionType.WRITE) as tx: |
| 61 | + tx.query("insert $p isa person, has name 'Alice';").resolve() |
| 62 | + tx.query("insert $p isa person, has name 'Bob';").resolve() |
| 63 | + tx.commit() |
| 64 | + with driver.transaction(db_name, TransactionType.READ) as tx: |
| 65 | + results = tx.query( |
| 66 | + 'match $p isa person; fetch {"name": $p.name};' |
| 67 | + ).resolve() |
| 68 | + for json in results: |
| 69 | + print(json) |
0 commit comments