Skip to content

Commit

Permalink
Run redis server and worker from pytest fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
sgarciahelguera committed Jul 19, 2023
1 parent 07a5d38 commit 6533efa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Install dependencies
run: make install
- name: Test the API
run: redis-server & make test
run: make test
env:
POLICYENGINE_DB_PASSWORD: ${{ secrets.POLICYENGINE_DB_PASSWORD }}
POLICYENGINE_GITHUB_MICRODATA_AUTH_TOKEN: ${{ secrets.POLICYENGINE_GITHUB_MICRODATA_AUTH_TOKEN }}
Expand Down
38 changes: 33 additions & 5 deletions tests/python/test_economy_1.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
"""
Test basics for /economy endpoints.
"""
import pytest
import json
import subprocess
from subprocess import Popen, TimeoutExpired
import redis
import pytest
from policyengine_api.api import app


@pytest.fixture(name="rest_client")
@pytest.fixture(name="rest_client", scope="session")
def client():
"""run the app for the tests to run against"""
app.config["TESTING"] = True
with app.test_client() as test_client:
yield test_client

with Popen(["redis-server"]) as redis_server:
redis_client= redis.Redis()
redis_client.ping()
with Popen(["python", "policyengine_api/worker.py"], stdout = subprocess.PIPE) as worker:
#output, err = worker.communicate(timeout=2)
#pytest.logging.info(f"err={err}, output={output}")
with app.test_client() as test_client:
yield test_client
worker.kill()
try:
worker.wait(10)
except TimeoutExpired:
worker.terminate()
redis_server.kill()
try:
redis_server.wait(10)
except TimeoutExpired:
redis_server.terminate()

def test_economy_1(rest_client):
"""Add a simple policy and get /economy for that over 2."""
Expand Down Expand Up @@ -52,4 +70,14 @@ def test_economy_1(rest_client):
"r",
encoding="utf-8",
) as file:
assert (
economy_response.json["status"] == "computing"
), 'Expected first answer status to be "computing" but it is ' + str(
economy_response.json["status"]
)
while economy_response.json["status"] == "computing":
assert (
economy_response.json["average_time"] is not None
), 'Expected computing answer to have "average_time"'
economy_response = rest_client.get(query)
assert economy_response.json == json.load(file)

0 comments on commit 6533efa

Please sign in to comment.