mirror of
https://github.com/open-thought/reasoning-gym.git
synced 2026-04-28 17:29:39 +00:00
Add sentence reordering and unit tests to validate it
This commit is contained in:
parent
31a5b5cb76
commit
09634db2cf
4 changed files with 128 additions and 0 deletions
45
tests/test_sentence_reordering.py
Normal file
45
tests/test_sentence_reordering.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import pytest
|
||||
from reasoning_gym.algorithmic.sentence_reordering import (
|
||||
SentenceReorderingConfig,
|
||||
SentenceReorderingDataset,
|
||||
)
|
||||
|
||||
@pytest.fixture
|
||||
def config():
|
||||
return SentenceReorderingConfig(num_of_words_in_sentence=5, seed=42, size=10)
|
||||
|
||||
@pytest.fixture
|
||||
def dataset(config):
|
||||
return SentenceReorderingDataset(config=config)
|
||||
|
||||
def test_config_validation(config):
|
||||
# Test that the config validation does not raise any exceptions
|
||||
try:
|
||||
config.validate()
|
||||
except Exception as e:
|
||||
pytest.fail(f"Config validation raised an exception: {e}")
|
||||
|
||||
def test_generate_sentence_dataset(dataset):
|
||||
sentence = "This is a test sentence for reordering"
|
||||
result = dataset._generate_sentence_dataset(sentence, seed=42, idx=0, shuffle=True)
|
||||
assert "input" in result
|
||||
assert "goal" in result
|
||||
assert result["input"] != result["goal"]
|
||||
assert sorted(result["input"].split()) == sorted(result["goal"].split())
|
||||
|
||||
def test_getitem(dataset, config):
|
||||
item = dataset[0]
|
||||
assert "question" in item
|
||||
assert "answer" in item
|
||||
assert "metadata" in item
|
||||
assert item["metadata"]["num_of_words_in_sentence"] >= config.num_of_words_in_sentence
|
||||
|
||||
def test_key_error_in_getitem(dataset):
|
||||
# Modify the dataset to include an incorrect key
|
||||
def mock_generate_sentence_dataset(*args, **kwargs):
|
||||
return {"input": "mock input", "goal": "mock goal", "extra": "extra key"}
|
||||
|
||||
dataset._generate_sentence_dataset = mock_generate_sentence_dataset
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
dataset[0]
|
||||
Loading…
Add table
Add a link
Reference in a new issue