|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +# |
| 4 | +# Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +import io |
| 8 | +import json |
| 9 | +import os |
| 10 | +import pickle |
| 11 | +import random |
| 12 | +import sys |
| 13 | +import tempfile |
| 14 | +import unittest |
| 15 | +import uuid |
| 16 | +import warnings |
| 17 | +from pathlib import Path |
| 18 | +from unittest.mock import DEFAULT, MagicMock, patch |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import pandas as pd |
| 22 | +import pytest |
| 23 | + |
| 24 | +import sasctl.pzmm as pzmm |
| 25 | +from sasctl.core import RestObj |
| 26 | +from sasctl.pzmm import GitIntegrate as GI |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture() |
| 30 | +def get_zipped_model_mocks(): |
| 31 | + with patch.multiple( |
| 32 | + "sasctl._services.model_repository.ModelRepository", |
| 33 | + get=DEFAULT, |
| 34 | + get_model=DEFAULT, |
| 35 | + get_project=DEFAULT, |
| 36 | + list_models=DEFAULT, |
| 37 | + ) as mocks: |
| 38 | + yield mocks |
| 39 | + |
| 40 | + |
| 41 | +def test_check_git_status(): |
| 42 | + """ |
| 43 | + Test Cases: |
| 44 | + - GitPython not installed |
| 45 | + """ |
| 46 | + from sasctl.pzmm.git_integration import check_git_status |
| 47 | + |
| 48 | + with patch("sasctl.pzmm.git_integration.git", None): |
| 49 | + with pytest.raises(RuntimeError): |
| 50 | + check_git_status() |
| 51 | + |
| 52 | + |
| 53 | +def test_get_zipped_model(tmp_path_factory, get_zipped_model_mocks): |
| 54 | + """ |
| 55 | + Test cases: |
| 56 | + - model |
| 57 | + - RestObj |
| 58 | + - UUID |
| 59 | + - name |
| 60 | + - without project |
| 61 | + - with project |
| 62 | + - git project |
| 63 | + - exists |
| 64 | + - model exists |
| 65 | + - model DNE |
| 66 | + - DNE |
| 67 | + """ |
| 68 | + from sasctl.pzmm.git_integration import get_zipped_model |
| 69 | + |
| 70 | + get_zipped_model_mocks["get_model"].return_value = RestObj( |
| 71 | + {"name": "mtest", "id": "123abc", "projectName": "ptest"} |
| 72 | + ) |
| 73 | + get_zipped_model_mocks["get_project"].return_value = RestObj({"name": "ptest"}) |
| 74 | + get_zipped_model_mocks["get"].return_value = bytes(b"789xyz") |
| 75 | + get_zipped_model_mocks["list_models"].return_value = [ |
| 76 | + RestObj({"name": "mtest", "id": "123abc", "projectName": "ptest"}) |
| 77 | + ] |
| 78 | + |
| 79 | + # RestObj model + git repo with project/model |
| 80 | + tmp_dir = Path(tmp_path_factory.mktemp("test1")) |
| 81 | + (tmp_dir / "ptest").mkdir() |
| 82 | + (tmp_dir / "ptest" / "mtest").mkdir() |
| 83 | + model, project = get_zipped_model( |
| 84 | + RestObj({"name": "mtest", "id": "123abc", "projectName": "ptest"}), tmp_dir |
| 85 | + ) |
| 86 | + assert ("mtest", "ptest") == (model, project) |
| 87 | + assert Path(tmp_dir / project / model / (model + ".zip")).exists() |
| 88 | + |
| 89 | + # UUID model + git repo with project |
| 90 | + tmp_dir = Path(tmp_path_factory.mktemp("test2")) |
| 91 | + (tmp_dir / "ptest").mkdir() |
| 92 | + model, project = get_zipped_model(str(uuid.uuid4()), tmp_dir) |
| 93 | + assert ("mtest", "ptest") == (model, project) |
| 94 | + assert Path(tmp_dir / project / model / (model + ".zip")).exists() |
| 95 | + |
| 96 | + # string model + no project |
| 97 | + with pytest.raises(ValueError): |
| 98 | + get_zipped_model("mtest", None) |
| 99 | + |
| 100 | + # string model + project + no git repo |
| 101 | + tmp_dir = Path(tmp_path_factory.mktemp("test3")) |
| 102 | + model, project = get_zipped_model("mtest", tmp_dir, "ptest") |
| 103 | + assert ("mtest", "ptest") == (model, project) |
| 104 | + assert Path(tmp_dir / project / model / (model + ".zip")).exists() |
0 commit comments