File size: 1,263 Bytes
c2fb337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import subprocess
import sys
import time
import requests
import os
import pytest


def start_server(port):
    env = os.environ.copy()
    env["PORT"] = str(port)
    # Start server as a subprocess using the same Python interpreter
    proc = subprocess.Popen(
        [sys.executable, "app.py"],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        env=env,
    )
    return proc


def wait_until_up(url, timeout=10.0):
    start = time.time()
    while True:
        try:
            requests.post(url, json={"features": [200, 1, 0, 500]}, timeout=1.0)
            return True
        except Exception:
            if time.time() - start > timeout:
                return False
            time.sleep(0.2)


@pytest.mark.integration
def test_predict_integration():
    port = 5001
    url = f"http://127.0.0.1:{port}/predict"
    proc = start_server(port)
    try:
        assert wait_until_up(url), "Server did not start in time"
        resp = requests.post(url, json={"features": [200, 1, 0, 500]})
        assert resp.status_code == 200
        j = resp.json()
        assert "fraud" in j
        assert "probability" in j
    finally:
        proc.terminate()
        proc.wait(timeout=5)