-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from makrianast/main
added batching to matcher.py and unit tests
- Loading branch information
Showing
2 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,84 @@ | ||
import sys | ||
import os | ||
import unittest | ||
import numpy | ||
|
||
sys.path.append("../src") | ||
from unittest import TestCase, mock | ||
from harmony.matching.matcher import get_batch_size | ||
from harmony.matching.matcher import process_items_in_batches | ||
|
||
|
||
# Mock LLM function | ||
def mock_llm_function(batch): | ||
"""Simulates processing a batch.""" | ||
return [f"Processed: {item}" for item in batch] | ||
|
||
|
||
class TestMatcherBatching(TestCase): | ||
|
||
@mock.patch.dict(os.environ, {"BATCH_SIZE": "5"}) | ||
def test_batched_processing(self): | ||
"""Test that 10 items are divided into 2 batches of 5 each.""" | ||
items = [f"item{i}" for i in range(10)] # 10 items to process | ||
results = process_items_in_batches(items, mock_llm_function) | ||
|
||
self.assertEqual(len(results), 10) | ||
|
||
expected = [ | ||
"Processed: item0", "Processed: item1", "Processed: item2", "Processed: item3", "Processed: item4", | ||
"Processed: item5", "Processed: item6", "Processed: item7", "Processed: item8", "Processed: item9", | ||
] | ||
self.assertEqual(results, expected) | ||
|
||
@mock.patch.dict(os.environ, {"BATCH_SIZE": "5"}) | ||
def test_large_batch_size(self): | ||
"""Test batch size greater than input size.""" | ||
items = [f"item{i}" for i in range(3)] # Only 3 items | ||
results = process_items_in_batches(items, mock_llm_function) | ||
|
||
self.assertEqual(len(results), 3) | ||
|
||
expected = [ | ||
"Processed: item0", "Processed: item1", "Processed: item2", | ||
] | ||
self.assertEqual(results, expected) | ||
|
||
@mock.patch.dict(os.environ, {"BATCH_SIZE": "0"}) | ||
def test_no_batching(self): | ||
"""Test no batching (all items processed in one batch).""" | ||
items = [f"item{i}" for i in range(10)] # 10 items to process | ||
results = process_items_in_batches(items, mock_llm_function) | ||
|
||
self.assertEqual(len(results), 10) | ||
|
||
expected = [ | ||
"Processed: item0", "Processed: item1", "Processed: item2", "Processed: item3", "Processed: item4", | ||
"Processed: item5", "Processed: item6", "Processed: item7", "Processed: item8", "Processed: item9", | ||
] | ||
self.assertEqual(results, expected) | ||
|
||
@mock.patch.dict(os.environ, {"BATCH_SIZE": "-5"}) | ||
def test_negative_batch_size(self): | ||
"""Test when BATCH_SIZE is negative, it defaults to 0.""" | ||
items = [f"item{i}" for i in range(10)] | ||
results = process_items_in_batches(items, mock_llm_function) | ||
self.assertEqual(len(results), 10) | ||
|
||
@mock.patch.dict(os.environ, {}, clear=True) | ||
def test_default_batch_size(self): | ||
"""Test when BATCH_SIZE is not set, it defaults to 50.""" | ||
items = [f"item{i}" for i in range(10)] | ||
results = process_items_in_batches(items, mock_llm_function) | ||
self.assertEqual(len(results), 10) | ||
|
||
@mock.patch.dict(os.environ, {"BATCH_SIZE": "invalid"}) | ||
def test_invalid_batch_size(self): | ||
"""Test when BATCH_SIZE is invalid, it defaults to 50.""" | ||
items = [f"item{i}" for i in range(10)] | ||
results = process_items_in_batches(items, mock_llm_function) | ||
self.assertEqual(len(results), 10) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |