Skip to content

Commit 972ca35

Browse files
Use fake-seam-connect module to run the tests (#67)
* Use fake-seam-connect module to run the tests * Start fake server for every test fn separately * Fix lint * Setup node during test workflow * Use npm registry for fake-seam-connect * Use context manager to fix lint * Run the fake-seam-connect server entrypoint directly without an additional script * Remove start-fake-seam-server.js * Don't pipe subprocess's output and error + minor corrections * Update fake server start script name, pin fake-seam-connect version * Rename find_free_port to get_port * ci: Generate code * Update command that starts fake server in conftest.py --------- Co-authored-by: Seam Bot <devops@getseam.com>
1 parent 199bf56 commit 972ca35

File tree

4 files changed

+172
-9
lines changed

4 files changed

+172
-9
lines changed

.github/workflows/check.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ jobs:
3434
uses: ./.github/actions/setup
3535
with:
3636
python_version: ${{ matrix.python }}
37+
- name: Setup Node.js
38+
uses: ./.github/actions/setup-node
3739
- name: Test
3840
run: make test
3941
lint:

package-lock.json

Lines changed: 126 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
"scripts": {
66
"generate": "node generate-routes.js",
77
"postgenerate": "make format",
8-
"format": "prettier --write --ignore-path .gitignore ."
8+
"format": "prettier --write --ignore-path .gitignore .",
9+
"start": "fake-seam-connect --seed"
910
},
1011
"devDependencies": {
12+
"@seamapi/fake-seam-connect": "1.65.5",
1113
"@seamapi/nextlove-sdk-generator": "1.13.1",
1214
"@seamapi/types": "1.178.0",
1315
"del": "^7.1.0",

test/conftest.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
1+
import socket
12
import pytest
3+
import subprocess
4+
import time
5+
import os
26
from seam import Seam
3-
import random
4-
import string
7+
from contextlib import contextmanager
8+
9+
10+
def get_port():
11+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
12+
s.bind(("", 0))
13+
return s.getsockname()[1]
14+
15+
16+
# Create a custom context manager to ensure the fake server subprocess is terminated correctly
17+
@contextmanager
18+
def subprocess_popen(*args):
19+
process = subprocess.Popen(*args)
20+
try:
21+
yield process
22+
finally:
23+
process.terminate()
24+
try:
25+
process.wait(timeout=10)
26+
except subprocess.TimeoutExpired:
27+
process.kill()
28+
29+
30+
@pytest.fixture(scope="function")
31+
def fake_seam_connect_server():
32+
port = get_port()
33+
os.environ["PORT"] = str(port)
34+
35+
with subprocess_popen(["npm", "run", "start"]):
36+
# Allow some time for the server to start
37+
time.sleep(0.5)
38+
39+
endpoint = f"http://localhost:{port}"
40+
41+
yield endpoint
542

643

744
@pytest.fixture(scope="function")
8-
def seam():
9-
r = "".join(random.choices(string.ascii_uppercase + string.digits, k=10))
10-
seam = Seam(
11-
endpoint=f"https://{r}.fakeseamconnect.seam.vc", api_key="seam_apikey1_token"
12-
)
45+
def seam(fake_seam_connect_server):
46+
seam = Seam(endpoint=fake_seam_connect_server, api_key="seam_apikey1_token")
1347
yield seam

0 commit comments

Comments
 (0)