-
Notifications
You must be signed in to change notification settings - Fork 285
Add unit tests for Python to Engine format conversion #315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import dataclasses | ||
| import uuid | ||
| import datetime | ||
| from dataclasses import dataclass | ||
| import pytest | ||
| from cocoindex.convert import to_engine_value | ||
|
|
||
| @dataclass | ||
| class Order: | ||
| order_id: str | ||
| name: str | ||
| price: float | ||
|
|
||
| @dataclass | ||
| class Basket: | ||
| items: list | ||
|
|
||
| @dataclass | ||
| class Customer: | ||
| name: str | ||
| order: Order | ||
|
|
||
| def test_to_engine_value_basic_types(): | ||
| assert to_engine_value(123) == 123 | ||
| assert to_engine_value(3.14) == 3.14 | ||
| assert to_engine_value("hello") == "hello" | ||
| assert to_engine_value(True) is True | ||
|
|
||
| def test_to_engine_value_uuid(): | ||
| u = uuid.uuid4() | ||
| assert to_engine_value(u) == u.bytes | ||
|
|
||
| def test_to_engine_value_date_time_types(): | ||
| d = datetime.date(2024, 1, 1) | ||
| assert to_engine_value(d) == d | ||
| t = datetime.time(12, 30) | ||
| assert to_engine_value(t) == t | ||
| dt = datetime.datetime(2024, 1, 1, 12, 30) | ||
| assert to_engine_value(dt) == dt | ||
|
|
||
| def test_to_engine_value_struct(): | ||
| order = Order(order_id="O123", name="mixed nuts", price=25.0) | ||
| assert to_engine_value(order) == ["O123", "mixed nuts", 25.0] | ||
|
|
||
| def test_to_engine_value_list_of_structs(): | ||
| orders = [Order("O1", "item1", 10.0), Order("O2", "item2", 20.0)] | ||
| assert to_engine_value(orders) == [["O1", "item1", 10.0], ["O2", "item2", 20.0]] | ||
|
|
||
| def test_to_engine_value_struct_with_list(): | ||
| basket = Basket(items=["apple", "banana"]) | ||
| assert to_engine_value(basket) == [["apple", "banana"]] | ||
|
|
||
| def test_to_engine_value_nested_struct(): | ||
| customer = Customer(name="Alice", order=Order("O1", "item1", 10.0)) | ||
| assert to_engine_value(customer) == ["Alice", ["O1", "item1", 10.0]] | ||
|
|
||
| def test_to_engine_value_empty_list(): | ||
| assert to_engine_value([]) == [] | ||
| assert to_engine_value([[]]) == [[]] | ||
|
|
||
badmonster0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def test_to_engine_value_tuple(): | ||
| assert to_engine_value(()) == [] | ||
| assert to_engine_value((1, 2, 3)) == [1, 2, 3] | ||
| assert to_engine_value(((1, 2), (3, 4))) == [[1, 2], [3, 4]] | ||
| assert to_engine_value(([],)) == [[]] | ||
| assert to_engine_value(((),)) == [[]] | ||
|
|
||
| def test_to_engine_value_none(): | ||
| assert to_engine_value(None) is None | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.