Coverage for mlos_bench/mlos_bench/tests/util_nullable_test.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-22 01:18 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Unit tests for `nullable` utility function.""" 

6import pytest 

7 

8from mlos_bench.util import nullable 

9 

10 

11def test_nullable_str() -> None: 

12 """Check that the `nullable` function works properly for `str`.""" 

13 assert nullable(str, None) is None 

14 assert nullable(str, "") is not None 

15 assert nullable(str, "") == "" 

16 assert nullable(str, "test") == "test" 

17 assert nullable(str, 10) == "10" 

18 

19 

20def test_nullable_int() -> None: 

21 """Check that the `nullable` function works properly for `int`.""" 

22 assert nullable(int, None) is None 

23 assert nullable(int, 10) is not None 

24 assert nullable(int, 10) == 10 

25 assert nullable(int, 36.6) == 36 

26 

27 

28def test_nullable_func() -> None: 

29 """Check that the `nullable` function works properly with `list.pop()` function.""" 

30 assert nullable(list.pop, None) is None 

31 assert nullable(list.pop, [1, 2, 3]) == 3 

32 

33 with pytest.raises(IndexError): 

34 assert nullable(list.pop, [])