Files
secondo-py/tests/test_core.py
2026-01-24 11:30:02 +08:00

180 lines
4.7 KiB
Python

"""
Tests for Phase 1: Core functionality
"""
from pysecondo.storage.memory import MemoryStorage
from pysecondo.core.type_system import TypeChecker
from pysecondo.core.types import (
BaseType, TupleType, RelationType, Attribute,
parse_type, type_to_string
)
from pysecondo.core.nested_list import NestedList, atom, list_nl
import sys
sys.path.insert(0, '.')
def test_nested_list():
"""Test nested list creation and operations"""
print("Testing NestedList...")
# Atomic values
nl_int = atom(42)
nl_str = atom("Beijing")
nl_bool = atom(True)
assert nl_int.is_atom()
assert nl_int.value == 42
assert str(nl_int) == "42"
assert nl_str.is_atom()
assert nl_str.value == "Beijing"
assert str(nl_str) == '"Beijing"'
# Lists
nl_list = list_nl(1, 2, 3)
assert nl_list.is_list()
assert len(nl_list) == 3
assert str(nl_list) == "(1 2 3)"
# Nested structures (tuple)
nl_tuple = list_nl("Beijing", 21540000)
assert str(nl_tuple) == '("Beijing" 21540000)'
# Nested structures (relation)
nl_rel = list_nl(
list_nl("Beijing", 21540000),
list_nl("Shanghai", 24280000)
)
assert str(nl_rel) == '(("Beijing" 21540000) ("Shanghai" 24280000))'
# to_python conversion
assert nl_int.to_python() == 42
assert nl_list.to_python() == [1, 2, 3]
assert nl_tuple.to_python() == ["Beijing", 21540000]
print(" ✓ NestedList tests passed")
def test_type_system():
"""Test type system"""
print("Testing Type System...")
# Parse basic types
int_type = parse_type("int")
assert int_type == BaseType.INT
string_type = parse_type("string")
assert string_type == BaseType.STRING
# Parse tuple type
tuple_type_str = "(tuple ((Name string)(Population int)))"
tuple_type = parse_type(tuple_type_str)
assert isinstance(tuple_type, TupleType)
assert len(tuple_type.attributes) == 2
assert tuple_type.attributes[0].name == "Name"
assert tuple_type.attributes[0].type == BaseType.STRING
# Parse relation type
rel_type_str = "(rel (tuple ((Name string)(Population int))))"
rel_type = parse_type(rel_type_str)
assert isinstance(rel_type, RelationType)
assert isinstance(rel_type.tuple_type, TupleType)
# Type to string
assert type_to_string(BaseType.INT) == "int"
assert "Name" in type_to_string(tuple_type)
assert "(rel" in type_to_string(rel_type)
print(" ✓ Type system tests passed")
def test_type_checker():
"""Test type checking"""
print("Testing Type Checker...")
checker = TypeChecker()
# Check basic types
assert checker.check(atom(42), BaseType.INT)
assert checker.check(atom("hello"), BaseType.STRING)
assert not checker.check(atom("hello"), BaseType.INT)
# Check tuple type
city_tuple_type = TupleType([
Attribute("Name", BaseType.STRING),
Attribute("Population", BaseType.INT)
])
beijing = list_nl("Beijing", 21540000)
assert checker.check(beijing, city_tuple_type)
wrong_tuple = list_nl(123, 21540000)
assert not checker.check(wrong_tuple, city_tuple_type)
# Check relation type
cities_rel_type = RelationType(city_tuple_type)
cities = list_nl(
list_nl("Beijing", 21540000),
list_nl("Shanghai", 24280000)
)
assert checker.check(cities, cities_rel_type)
print(" ✓ Type checker tests passed")
def test_storage():
"""Test in-memory storage"""
print("Testing Memory Storage...")
storage = MemoryStorage()
# Create object
city_tuple_type = TupleType([
Attribute("Name", BaseType.STRING),
Attribute("Population", BaseType.INT)
])
beijing = list_nl("Beijing", 21540000)
storage.create_object("beijing", beijing, city_tuple_type)
# Get object
retrieved = storage.get_object("beijing")
assert retrieved == beijing
retrieved_type = storage.get_type("beijing")
assert retrieved_type == city_tuple_type
# Update object
shanghai = list_nl("Shanghai", 24280000)
storage.update_object("beijing", shanghai, city_tuple_type)
assert storage.get_object("beijing") == shanghai
# List objects
storage.create_object("city2", beijing, city_tuple_type)
objects = storage.list_objects()
assert "beijing" in objects
assert "city2" in objects
assert len(objects) == 2
# Delete object
storage.delete_object("city2")
assert len(storage.list_objects()) == 1
print(" ✓ Storage tests passed")
if __name__ == "__main__":
print("=" * 50)
print("Phase 1: Core Functionality Tests")
print("=" * 50)
test_nested_list()
test_type_system()
test_type_checker()
test_storage()
print("\n" + "=" * 50)
print("All Phase 1 tests passed! ✓")
print("=" * 50)