Skip to content

Commit f453a52

Browse files
authored
add initial project structure with simple CI integration test (#1)
1 parent 15a4477 commit f453a52

File tree

5 files changed

+155
-2
lines changed

5 files changed

+155
-2
lines changed

.github/workflows/test.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: LocalStack TypeDB Extension Tests
2+
3+
on:
4+
pull_request:
5+
workflow_dispatch:
6+
push:
7+
branches:
8+
- main
9+
10+
env:
11+
LOCALSTACK_DISABLE_EVENTS: "1"
12+
LOCALSTACK_AUTH_TOKEN: ${{ secrets.TEST_LOCALSTACK_AUTH_TOKEN }}
13+
14+
jobs:
15+
integration-tests:
16+
name: Run Integration Tests
17+
runs-on: ubuntu-latest
18+
timeout-minutes: 10
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Setup LocalStack and extension
24+
run: |
25+
docker pull localstack/localstack-pro &
26+
docker pull typedb/typedb &
27+
28+
pip install localstack
29+
localstack extensions install "git+https://github.com/whummer/localstack-utils.git#egg=localstack-typedb&subdirectory=localstack-typedb"
30+
31+
DEBUG=1 localstack start -d
32+
localstack wait
33+
34+
- name: Run integration tests
35+
run: |
36+
make install
37+
make test
38+
39+
- name: Print logs
40+
if: always()
41+
run: |
42+
localstack logs
43+
localstack stop

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
.venv/
3+
*.pyc

Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
VENV_BIN = python3 -m venv
2+
VENV_DIR ?= .venv
3+
VENV_ACTIVATE = $(VENV_DIR)/bin/activate
4+
VENV_RUN = . $(VENV_ACTIVATE)
5+
6+
usage: ## Shows usage for this Makefile
7+
@cat Makefile | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'
8+
9+
venv: $(VENV_ACTIVATE)
10+
11+
$(VENV_ACTIVATE):
12+
test -d .venv || $(VENV_BIN) .venv
13+
14+
clean:
15+
rm -rf .venv/
16+
17+
install: venv ## Install dependencies
18+
$(VENV_RUN); pip install --upgrade localstack pytest requests ruff typedb-driver
19+
20+
format: ## Run ruff to format the whole codebase
21+
$(VENV_RUN); python -m ruff format .; python -m ruff check --output-format=full --fix .
22+
23+
test: ## Run integration tests (requires LocalStack running with the Extension installed)
24+
$(VENV_RUN); pytest tests
25+
26+
.PHONY: clean install usage venv format test

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
# typedb-localstack-demo
2-
Example of using TypeDB + Localstack
1+
# TypeDB LocalStack Demo
2+
3+
Sample app that demonstrates how to use TypeDB + LocalStack, to develop and test cloud applications locally.
4+
5+
## Prerequisites
6+
7+
* Docker
8+
* LocalStack Pro (free trial available [here](https://app.localstack.cloud))
9+
* `localstack` CLI
10+
11+
## License
12+
13+
The code in this repo is available under the Apache 2.0 license.
14+

tests/test_extension.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)