Skip to content

Commit

Permalink
🧪 autogenerate sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hyusap committed Feb 14, 2024
1 parent 061dd83 commit 716e8ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
20 changes: 20 additions & 0 deletions scripts/syncronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,23 @@
destination_file_path = os.path.join(this_dir, "../sdk/honcho/sync_client.py")
with open(destination_file_path, "w") as destination_file:
destination_file.write(sync_code)


# tests

# Open the source file
source_file_path = os.path.join(this_dir, "../sdk/tests/test_async.py")
with open(source_file_path, "r") as source_file:
source_code = source_file.read()

# Use regex to remove async mentions
sync_code = re.sub(r"@pytest.mark.asyncio\n", "", source_code)
sync_code = re.sub(r"async\s", "", sync_code)
sync_code = re.sub(r"await\s", "", sync_code)
sync_code = re.sub(r"__anext__", "__next__", sync_code)
sync_code = re.sub(r"Async", "", sync_code)

# Write the modified code to the destination file
destination_file_path = os.path.join(this_dir, "../sdk/tests/test_sync.py")
with open(destination_file_path, "w") as destination_file:
destination_file.write(sync_code)
31 changes: 15 additions & 16 deletions sdk/tests/test_sync.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from honcho import GetSessionPage, GetMessagePage, GetMetamessagePage, Session, Message, Metamessage
from honcho import Client as Honcho
from uuid import uuid1
import pytest


def test_session_creation_retrieval():
app_id = str(uuid1())
Expand Down Expand Up @@ -30,8 +31,8 @@ def test_session_multiple_retrieval():


def test_session_update():
app_id = str(uuid1())
user_id = str(uuid1())
app_id = str(uuid1())
client = Honcho(app_id, "http://localhost:8000")
created_session = client.create_session(user_id)
assert created_session.update({"foo": "bar"})
Expand All @@ -40,8 +41,8 @@ def test_session_update():


def test_session_deletion():
app_id = str(uuid1())
user_id = str(uuid1())
app_id = str(uuid1())
client = Honcho(app_id, "http://localhost:8000")
created_session = client.create_session(user_id)
assert created_session.is_active is True
Expand All @@ -53,8 +54,8 @@ def test_session_deletion():


def test_messages():
app_id = str(uuid1())
user_id = str(uuid1())
app_id = str(uuid1())
client = Honcho(app_id, "http://localhost:8000")
created_session = client.create_session(user_id)
created_session.create_message(is_user=True, content="Hello")
Expand Down Expand Up @@ -128,13 +129,13 @@ def test_paginated_sessions_generator():
gen = client.get_sessions_generator(user_id)
# print(type(gen))

item = next(gen)
item = gen.__next__()
assert item.user_id == user_id
assert isinstance(item, Session)
assert next(gen) is not None
assert next(gen) is not None
assert gen.__next__() is not None
assert gen.__next__() is not None
with pytest.raises(StopIteration):
next(gen)
gen.__next__()

def test_paginated_out_of_bounds():
app_id = str(uuid1())
Expand Down Expand Up @@ -183,7 +184,6 @@ def test_paginated_messages():

assert next_page is None


def test_paginated_messages_generator():
app_id = str(uuid1())
user_id = str(uuid1())
Expand All @@ -193,17 +193,16 @@ def test_paginated_messages_generator():
created_session.create_message(is_user=False, content="Hi")
gen = created_session.get_messages_generator()

item = next(gen)
item = gen.__next__()
assert isinstance(item, Message)
assert item.content == "Hello"
assert item.is_user is True
item2 = next(gen)
item2 = gen.__next__()
assert item2 is not None
assert item2.content == "Hi"
assert item2.is_user is False
with pytest.raises(StopIteration):
next(gen)

gen.__next__()

def test_paginated_metamessages():
app_id = str(uuid1())
Expand Down Expand Up @@ -246,16 +245,16 @@ def test_paginated_metamessages_generator():
created_session.create_metamessage(message=message, metamessage_type="thought", content="Test 2")
gen = created_session.get_metamessages_generator()

item = next(gen)
item = gen.__next__()
assert isinstance(item, Metamessage)
assert item.content == "Test 1"
assert item.metamessage_type == "thought"
item2 = next(gen)
item2 = gen.__next__()
assert item2 is not None
assert item2.content == "Test 2"
assert item2.metamessage_type == "thought"
with pytest.raises(StopIteration):
next(gen)
gen.__next__()



0 comments on commit 716e8ca

Please sign in to comment.