Skip to content

Commit a1b81ee

Browse files
authored
chore: Add parameters to bin/test (#228)
1 parent a6fb399 commit a1b81ee

File tree

9 files changed

+90
-32
lines changed

9 files changed

+90
-32
lines changed

bin/fmt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#/ Description: Formats and lints the code
44
source bin/helpers/_utils.sh
55
set_source_and_root_dir
6+
ensure_virtual_env
67

78
if [[ "$1" == "--check" ]]; then
89
black --check .

bin/helpers/_utils.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ set_source_and_root_dir() {
1313
root_dir=$(cd "$source_dir" && cd ../ && pwd)
1414
cd "$root_dir"
1515
}
16+
17+
ensure_virtual_env() {
18+
if [ -z "$VIRTUAL_ENV" ]; then
19+
echo "Virtual environment not activated. Activating now..."
20+
if [ ! -f env/bin/activate ]; then
21+
echo "Virtual environment not found. Please run 'python -m venv env' first."
22+
exit 1
23+
fi
24+
source env/bin/activate
25+
fi
26+
}

bin/setup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ set_source_and_root_dir
77
if [ ! -d "env" ]; then
88
python3 -m venv env
99
fi
10+
1011
source env/bin/activate
1112
pip install -e ".[dev,test]"

bin/test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
source bin/helpers/_utils.sh
55
set_source_and_root_dir
66

7-
pytest
7+
ensure_virtual_env
8+
9+
# Pass through all arguments to pytest
10+
pytest "$@"

posthog/test/ai/anthropic/test_anthropic.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
from unittest.mock import patch
44

55
import pytest
6-
from anthropic.types import Message, Usage
76

8-
from posthog.ai.anthropic import Anthropic, AsyncAnthropic
7+
try:
8+
from anthropic.types import Message, Usage
9+
10+
from posthog.ai.anthropic import Anthropic, AsyncAnthropic
11+
12+
ANTHROPIC_AVAILABLE = True
13+
except ImportError:
14+
ANTHROPIC_AVAILABLE = False
915

1016
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
1117

18+
# Skip all tests if Anthropic is not available
19+
pytestmark = pytest.mark.skipif(not ANTHROPIC_AVAILABLE, reason="Anthropic package is not available")
20+
1221

1322
@pytest.fixture
1423
def mock_client():

posthog/test/ai/langchain/test_callbacks.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,42 @@
88
from unittest.mock import patch
99

1010
import pytest
11-
from langchain_anthropic.chat_models import ChatAnthropic
12-
from langchain_community.chat_models.fake import FakeMessagesListChatModel
13-
from langchain_community.llms.fake import FakeListLLM, FakeStreamingListLLM
14-
from langchain_core.messages import AIMessage, HumanMessage
15-
from langchain_core.prompts import ChatPromptTemplate
16-
from langchain_core.runnables import RunnableLambda
17-
from langchain_core.tools import tool
18-
from langchain_openai.chat_models import ChatOpenAI
19-
from langgraph.graph.state import END, START, StateGraph
20-
from langgraph.prebuilt import create_react_agent
21-
22-
from posthog.ai.langchain import CallbackHandler
23-
from posthog.ai.langchain.callbacks import GenerationMetadata, SpanMetadata
11+
12+
try:
13+
from langchain_anthropic.chat_models import ChatAnthropic
14+
from langchain_community.chat_models.fake import FakeMessagesListChatModel
15+
from langchain_community.llms.fake import FakeListLLM, FakeStreamingListLLM
16+
from langchain_core.messages import AIMessage, HumanMessage
17+
from langchain_core.prompts import ChatPromptTemplate
18+
from langchain_core.runnables import RunnableLambda
19+
from langchain_core.tools import tool
20+
from langchain_openai.chat_models import ChatOpenAI
21+
from langgraph.graph.state import END, START, StateGraph
22+
from langgraph.prebuilt import create_react_agent
23+
24+
from posthog.ai.langchain import CallbackHandler
25+
from posthog.ai.langchain.callbacks import GenerationMetadata, SpanMetadata
26+
27+
LANGCHAIN_AVAILABLE = True
28+
except ImportError:
29+
30+
class FakeListLLM:
31+
pass
32+
33+
class FakeStreamingListLLM:
34+
pass
35+
36+
class HumanMessage:
37+
pass
38+
39+
class AIMessage:
40+
pass
41+
42+
LANGCHAIN_AVAILABLE = False
43+
44+
45+
# Skip all tests if LangChain is not available
46+
pytestmark = pytest.mark.skipif(not LANGCHAIN_AVAILABLE, reason="LangChain package is not available")
2447

2548
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
2649
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")

posthog/test/ai/openai/test_openai.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
33
from unittest.mock import patch
44

55
import pytest
6-
from openai.types.chat import ChatCompletion, ChatCompletionMessage
7-
from openai.types.chat.chat_completion import Choice
8-
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
9-
from openai.types.chat.chat_completion_chunk import Choice as ChoiceChunk
10-
from openai.types.chat.chat_completion_chunk import ChoiceDelta, ChoiceDeltaToolCall, ChoiceDeltaToolCallFunction
11-
from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall, Function
12-
from openai.types.completion_usage import CompletionUsage
13-
from openai.types.create_embedding_response import CreateEmbeddingResponse, Usage
14-
from openai.types.embedding import Embedding
15-
from openai.types.responses import Response, ResponseOutputMessage, ResponseOutputText, ResponseUsage
16-
17-
from posthog.ai.openai import OpenAI
6+
7+
try:
8+
from openai.types.chat import ChatCompletion, ChatCompletionMessage
9+
from openai.types.chat.chat_completion import Choice
10+
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
11+
from openai.types.chat.chat_completion_chunk import Choice as ChoiceChunk
12+
from openai.types.chat.chat_completion_chunk import ChoiceDelta, ChoiceDeltaToolCall, ChoiceDeltaToolCallFunction
13+
from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall, Function
14+
from openai.types.completion_usage import CompletionUsage
15+
from openai.types.create_embedding_response import CreateEmbeddingResponse, Usage
16+
from openai.types.embedding import Embedding
17+
from openai.types.responses import Response, ResponseOutputMessage, ResponseOutputText, ResponseUsage
18+
19+
from posthog.ai.openai import OpenAI
20+
21+
OPENAI_AVAILABLE = True
22+
except ImportError:
23+
OPENAI_AVAILABLE = False
24+
25+
# Skip all tests if OpenAI is not available
26+
pytestmark = pytest.mark.skipif(not OPENAI_AVAILABLE, reason="OpenAI package is not available")
1827

1928

2029
@pytest.fixture

posthog/test/test_feature_flag.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ def test_flag_reason_from_json(self):
6969
resp = {"code": "user_in_segment"}
7070
reason = FlagReason.from_json(resp)
7171
self.assertEqual(reason.code, "user_in_segment")
72-
self.assertEqual(reason.condition_index, 0) # default value
73-
self.assertEqual(reason.description, "") # default value
72+
self.assertIsNone(reason.condition_index) # default value
73+
self.assertEqual(reason.description, "")
7474

7575
# Test with None
7676
self.assertIsNone(FlagReason.from_json(None))

posthog/test/test_feature_flags.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,15 +1801,16 @@ def test_match_properties_icontains(self):
18011801
self.assertFalse(match_property(property_b, {"key": "three"}))
18021802

18031803
def test_match_properties_regex(self):
1804-
property_a = self.property(key="key", value="\.com$", operator="regex") # noqa: W605
1804+
property_a = self.property(key="key", value=r"\.com$", operator="regex")
18051805
self.assertTrue(match_property(property_a, {"key": "value.com"}))
18061806
self.assertTrue(match_property(property_a, {"key": "value2.com"}))
1807+
self.assertFalse(match_property(property_a, {"key": "value2com"}))
18071808

18081809
self.assertFalse(match_property(property_a, {"key": ".com343tfvalue5"}))
18091810
self.assertFalse(match_property(property_a, {"key": "Alakazam"}))
18101811
self.assertFalse(match_property(property_a, {"key": 123}))
18111812
self.assertFalse(match_property(property_a, {"key": "valuecom"}))
1812-
self.assertFalse(match_property(property_a, {"key": "value\com"})) # noqa: W605
1813+
self.assertFalse(match_property(property_a, {"key": r"value\com"}))
18131814

18141815
property_b = self.property(key="key", value="3", operator="regex")
18151816
self.assertTrue(match_property(property_b, {"key": "3"}))

0 commit comments

Comments
 (0)