|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (c) Alibaba, Inc. and its affiliates. |
| 3 | +"""Regression tests for issue #207. |
| 4 | +
|
| 5 | +The deprecated Assistants API must not emit a DeprecationWarning on a plain |
| 6 | +``import dashscope``; it should warn only when a deprecated name is actually |
| 7 | +accessed. These run in subprocesses so each starts from a clean import state. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import textwrap |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 17 | + |
| 18 | + |
| 19 | +def _run(snippet, *, error_on_deprecation): |
| 20 | + env = dict(os.environ) |
| 21 | + existing = env.get("PYTHONPATH") |
| 22 | + env["PYTHONPATH"] = str(REPO_ROOT) + ( |
| 23 | + os.pathsep + existing if existing else "" |
| 24 | + ) |
| 25 | + args = [sys.executable] |
| 26 | + if error_on_deprecation: |
| 27 | + args += ["-W", "error::DeprecationWarning"] |
| 28 | + args += ["-c", textwrap.dedent(snippet)] |
| 29 | + return subprocess.run( |
| 30 | + args, |
| 31 | + capture_output=True, |
| 32 | + text=True, |
| 33 | + cwd=str(REPO_ROOT), |
| 34 | + env=env, |
| 35 | + check=False, |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def test_plain_import_emits_no_deprecation_warning(): |
| 40 | + result = _run("import dashscope", error_on_deprecation=True) |
| 41 | + assert result.returncode == 0, result.stderr |
| 42 | + |
| 43 | + |
| 44 | +def test_non_assistants_usage_emits_no_deprecation_warning(): |
| 45 | + result = _run( |
| 46 | + "import dashscope; dashscope.Generation; dashscope.TextEmbedding", |
| 47 | + error_on_deprecation=True, |
| 48 | + ) |
| 49 | + assert result.returncode == 0, result.stderr |
| 50 | + |
| 51 | + |
| 52 | +def test_accessing_assistants_emits_deprecation_warning(): |
| 53 | + result = _run( |
| 54 | + "import dashscope; dashscope.Assistants", |
| 55 | + error_on_deprecation=True, |
| 56 | + ) |
| 57 | + assert result.returncode != 0 |
| 58 | + assert "DeprecationWarning" in result.stderr |
| 59 | + assert "Assistants API" in result.stderr |
| 60 | + |
| 61 | + |
| 62 | +def test_from_import_assistants_emits_deprecation_warning(): |
| 63 | + result = _run( |
| 64 | + "from dashscope import Assistants", |
| 65 | + error_on_deprecation=True, |
| 66 | + ) |
| 67 | + assert result.returncode != 0 |
| 68 | + assert "DeprecationWarning" in result.stderr |
| 69 | + |
| 70 | + |
| 71 | +def test_accessing_threads_emits_deprecation_warning(): |
| 72 | + result = _run( |
| 73 | + "import dashscope; dashscope.Threads", |
| 74 | + error_on_deprecation=True, |
| 75 | + ) |
| 76 | + assert result.returncode != 0 |
| 77 | + assert "DeprecationWarning" in result.stderr |
| 78 | + |
| 79 | + |
| 80 | +def test_direct_assistants_import_emits_deprecation_warning(): |
| 81 | + result = _run("import dashscope.assistants", error_on_deprecation=True) |
| 82 | + assert result.returncode != 0 |
| 83 | + assert "DeprecationWarning" in result.stderr |
| 84 | + |
| 85 | + |
| 86 | +def test_lazy_names_resolve_to_real_objects(): |
| 87 | + import dashscope |
| 88 | + from dashscope.assistants import Assistant, Assistants |
| 89 | + from dashscope.assistants.assistant_types import ( |
| 90 | + AssistantFile, |
| 91 | + DeleteResponse, |
| 92 | + ) |
| 93 | + from dashscope.threads import RunStepList, Threads |
| 94 | + |
| 95 | + assert dashscope.Assistants is Assistants |
| 96 | + assert dashscope.Assistant is Assistant |
| 97 | + assert dashscope.AssistantFile is AssistantFile |
| 98 | + assert dashscope.DeleteResponse is DeleteResponse |
| 99 | + assert dashscope.Threads is Threads |
| 100 | + assert dashscope.RunStepList is RunStepList |
| 101 | + |
| 102 | + |
| 103 | +def test_dir_includes_lazy_names(): |
| 104 | + import dashscope |
| 105 | + |
| 106 | + listing = dir(dashscope) |
| 107 | + for name in ("Assistants", "Threads", "Runs", "Messages"): |
| 108 | + assert name in listing |
| 109 | + |
| 110 | + |
| 111 | +def test_unknown_attribute_raises_attribute_error(): |
| 112 | + import dashscope |
| 113 | + |
| 114 | + try: |
| 115 | + dashscope.ThisNameDoesNotExist |
| 116 | + except AttributeError: |
| 117 | + pass |
| 118 | + else: # pragma: no cover |
| 119 | + raise AssertionError("expected AttributeError") |
0 commit comments