Skip to content

Commit 4c834f2

Browse files
committed
tests
1 parent 36d4354 commit 4c834f2

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

.github/workflows/pytest.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Run pytest
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: [3.9, "3.10", "3.11"]
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
24+
- name: Install Poetry
25+
uses: snok/install-poetry@v1
26+
with:
27+
version: 1.5.1
28+
29+
- name: Install dependencies
30+
run: |
31+
poetry install
32+
33+
- name: Run pytest
34+
run: |
35+
poetry run pytest tests/

tests/test_closure.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import pytest
2+
import math
3+
from typing import Set, Any
4+
import numpy as np
5+
from ell.util.closure import (
6+
lexical_closure,
7+
is_immutable_variable,
8+
should_import,
9+
get_referenced_names,
10+
is_function_called,
11+
)
12+
13+
def test_lexical_closure_simple_function():
14+
def simple_func(x):
15+
return x * 2
16+
17+
result, (source, dsrc), uses = lexical_closure(simple_func)
18+
assert "def simple_func(x):" in result
19+
assert "return x * 2" in result
20+
assert isinstance(uses, Set)
21+
22+
def test_lexical_closure_with_global():
23+
global_var = 10
24+
def func_with_global():
25+
return global_var
26+
27+
result, _, _ = lexical_closure(func_with_global)
28+
assert "global_var = 10" in result
29+
assert "def func_with_global():" in result
30+
31+
def test_lexical_closure_with_nested_function():
32+
def outer():
33+
def inner():
34+
return 42
35+
return inner()
36+
37+
result, _, _ = lexical_closure(outer)
38+
assert "def outer():" in result
39+
assert "def inner():" in result
40+
assert "return 42" in result
41+
42+
def test_lexical_closure_with_default_args():
43+
def func_with_default(x=10):
44+
return x
45+
46+
result, _, _ = lexical_closure(func_with_default)
47+
print(result)
48+
assert "def func_with_default(x=10):" in result
49+
50+
@pytest.mark.parametrize("value, expected", [
51+
(42, True),
52+
("string", True),
53+
((1, 2, 3), True),
54+
([1, 2, 3], False),
55+
({"a": 1}, False),
56+
])
57+
def test_is_immutable_variable(value, expected):
58+
assert is_immutable_variable(value) == expected
59+
60+
def test_should_import():
61+
import os
62+
assert should_import(os)
63+
64+
class DummyModule:
65+
__name__ = "dummy"
66+
dummy = DummyModule()
67+
assert not should_import(dummy)
68+
69+
def test_get_referenced_names():
70+
code = """
71+
import math
72+
result = math.sin(x) + math.cos(y)
73+
"""
74+
referenced = get_referenced_names(code, "math")
75+
print(referenced)
76+
assert "sin" in referenced
77+
assert "cos" in referenced
78+
79+
def test_is_function_called():
80+
code = """
81+
def foo():
82+
pass
83+
84+
def bar():
85+
foo()
86+
87+
x = 1 + 2
88+
"""
89+
assert is_function_called("foo", code)
90+
assert not is_function_called("bar", code)
91+
assert not is_function_called("nonexistent", code)
92+
93+
# Addressing linter errors
94+
def test_lexical_closure_signature():
95+
def dummy_func():
96+
pass
97+
98+
# Test that the function accepts None for these arguments
99+
result, _, _ = lexical_closure(dummy_func, already_closed=None, recursion_stack=None)
100+
assert result # Just check that it doesn't raise an exception
101+
102+
def test_lexical_closure_uses_type():
103+
def dummy_func():
104+
pass
105+
106+
_, _, uses = lexical_closure(dummy_func, initial_call=True)
107+
assert isinstance(uses, Set)
108+
# You might want to add a more specific check for the content of 'uses'

tests/test_lmp_to_prompt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Pytest for the LM function (mocks the openai api so we can pretend to generate completions through the typical approach taken in the decorators (and adapters file.))
33
"""
44

5+
import ell
56
from ell.decorators.lm import lm
67
import pytest
78
from unittest.mock import patch, MagicMock
8-
from ell.util.lm import DEFAULT_SYSTEM_PROMPT
99
from ell.types import Message, LMPParams
1010

1111

@@ -47,7 +47,7 @@ def test_lm_decorator_with_params(client_mock):
4747
client_mock.assert_called_with(
4848
model="gpt-4-turbo",
4949
messages=[
50-
Message(role="system", content=DEFAULT_SYSTEM_PROMPT),
50+
Message(role="system", content=ell.config.default_system_prompt),
5151
Message(role="user", content="Test user prompt"),
5252
],
5353
temperature=0.5,

0 commit comments

Comments
 (0)