|
| 1 | +import pytest |
| 2 | + |
| 3 | +sm = pytest.importorskip("statsmodels.api", reason="statsmodels library not installed") |
| 4 | + |
| 5 | +statsmodels = pytest.importorskip( |
| 6 | + "statsmodels", reason="statsmodels library not installed" |
| 7 | +) |
| 8 | + |
| 9 | +import numpy as np # noqa |
| 10 | +import pandas as pd # noqa |
| 11 | +from fastapi.testclient import TestClient # noqa |
| 12 | + |
| 13 | +import vetiver # noqa |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def build_sm(): |
| 18 | + |
| 19 | + X, y = vetiver.get_mock_data() |
| 20 | + glm = sm.GLM(y, X).fit() |
| 21 | + |
| 22 | + v = vetiver.VetiverModel(glm, "glm", X) |
| 23 | + return v |
| 24 | + |
| 25 | + |
| 26 | +def test_vetiver_build(build_sm): |
| 27 | + api = vetiver.VetiverAPI(build_sm) |
| 28 | + client = TestClient(api.app) |
| 29 | + data = [{"B": 0, "C": 0, "D": 0}] |
| 30 | + |
| 31 | + response = vetiver.predict(endpoint=client, data=data) |
| 32 | + |
| 33 | + assert response.iloc[0, 0] == 0.0 |
| 34 | + assert len(response) == 1 |
| 35 | + |
| 36 | + |
| 37 | +def test_batch(build_sm): |
| 38 | + api = vetiver.VetiverAPI(build_sm) |
| 39 | + client = TestClient(api.app) |
| 40 | + data = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list("ABCD")) |
| 41 | + |
| 42 | + response = vetiver.predict(endpoint=client, data=data) |
| 43 | + |
| 44 | + assert len(response) == 100 |
| 45 | + |
| 46 | + |
| 47 | +def test_no_ptype(build_sm): |
| 48 | + api = vetiver.VetiverAPI(build_sm, check_ptype=False) |
| 49 | + client = TestClient(api.app) |
| 50 | + data = [0, 0, 0] |
| 51 | + |
| 52 | + response = vetiver.predict(endpoint=client, data=data) |
| 53 | + |
| 54 | + assert response.iloc[0, 0] == 0.0 |
| 55 | + assert len(response) == 1 |
| 56 | + |
| 57 | + |
| 58 | +def test_serialize(build_sm): |
| 59 | + import pins |
| 60 | + |
| 61 | + board = pins.board_temp(allow_pickle_read=True) |
| 62 | + vetiver.vetiver_pin_write(board=board, model=build_sm) |
| 63 | + assert isinstance( |
| 64 | + board.pin_read("glm"), |
| 65 | + statsmodels.genmod.generalized_linear_model.GLMResultsWrapper, |
| 66 | + ) |
| 67 | + board.pin_delete("glm") |
0 commit comments