|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2023 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +from unittest import mock |
| 16 | + |
| 17 | +from absl.testing import absltest |
| 18 | + |
| 19 | +import google.ai.generativelanguage as glm |
| 20 | +from google.ai.generativelanguage_v1beta2.types import model |
| 21 | + |
| 22 | +from google.generativeai import models |
| 23 | + |
| 24 | +_FAKE_MODEL = model.Model( |
| 25 | + name="models/fake-model-001", |
| 26 | + base_model_id="", |
| 27 | + version="001", |
| 28 | + display_name="Fake Model", |
| 29 | + description="A fake model", |
| 30 | + input_token_limit=123, |
| 31 | + output_token_limit=234, |
| 32 | + supported_generation_methods=[], |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +class UnitTests(absltest.TestCase): |
| 37 | + def test_model_prefix(self): |
| 38 | + """Test `models/` prefix applies to get_model calls when necessary.""" |
| 39 | + # The SUT needs a concrete return type from `get_model`, so set up a real-enough client. |
| 40 | + fake_client = mock.Mock(spec=glm.ModelServiceClient) |
| 41 | + fake_client.get_model.return_value = _FAKE_MODEL |
| 42 | + |
| 43 | + # Ensure that we don't mess with correctly structure args. |
| 44 | + models.get_model(name="models/text-bison-001", client=fake_client) |
| 45 | + fake_client.get_model.assert_called_with(name="models/text-bison-001") |
| 46 | + |
| 47 | + # Ensure that we do correct bare models. |
| 48 | + models.get_model(name="text-bison-001", client=fake_client) |
| 49 | + fake_client.get_model.assert_called_with(name="models/text-bison-001") |
| 50 | + |
| 51 | + # And unknown structure is not touched. |
| 52 | + models.get_model(name="unknown_string", client=fake_client) |
| 53 | + fake_client.get_model.assert_called_with(name="unknown_string") |
0 commit comments