Skip to content

Commit

Permalink
add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhPavel committed Feb 3, 2024
1 parent 929012a commit d871590
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 30 deletions.
16 changes: 8 additions & 8 deletions src/adaptix/_internal/code_tools/context_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def add(self, name: str, value: object) -> None:
...

@abstractmethod
def __contains__(self, item: str) -> bool:
def try_add(self, name: str, value: object) -> bool:
...


Expand All @@ -29,13 +29,13 @@ def __init__(
self._occupied = occupied

def add(self, name: str, value: object) -> None:
if name in self._occupied:
if not self.try_add(name, value):
raise KeyError(f"Key {name} is duplicated")

def try_add(self, name: str, value: object) -> bool:
if name in self._occupied:
return False
if name in self.dict:
if value is self.dict[name]:
return
raise KeyError(f"Key {name} is duplicated")
return value is self.dict[name]
self.dict[name] = value

def __contains__(self, item: str) -> bool:
return item in self.dict or item in self._occupied
return True
6 changes: 2 additions & 4 deletions src/adaptix/_internal/conversion/broaching/code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ def register_next_id(self, prefix: str, obj: object) -> str:
return self.register_mangled(name, obj)

def register_mangled(self, base: str, obj: object) -> str:
if base not in self._ctx_namespace:
self._ctx_namespace.add(base, obj)
if self._ctx_namespace.try_add(base, obj):
return base

for i in itertools.count(1):
name = f'{base}_{i}'
if name not in self._ctx_namespace:
self._ctx_namespace.add(base, obj)
if self._ctx_namespace.try_add(name, obj):
return name
raise RuntimeError

Expand Down
12 changes: 6 additions & 6 deletions src/adaptix/_internal/conversion/converter_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _provide_converter(self, mediator: Mediator, request: ConverterRequest) -> C
mediator=mediator,
dst_shape=dst_shape,
src_shape=src_shape,
owner_binding_src=BindingSource(),
owner_binding_src=BindingSource(source_model_field),
owner_binding_dst=BindingDest(),
extra_params=tuple(map(BindingSource, extra_params)),
)
Expand Down Expand Up @@ -182,11 +182,11 @@ def _get_nested_models_sub_plan(
try:
dst_shape = provide_generic_resolved_shape(
mediator,
InputShapeRequest(loc_stack=binding_src.to_loc_stack())
InputShapeRequest(loc_stack=binding_dst.to_loc_stack())
)
src_shape = provide_generic_resolved_shape(
mediator,
OutputShapeRequest(loc_stack=binding_dst.to_loc_stack())
OutputShapeRequest(loc_stack=binding_src.to_loc_stack())
)
except CannotProvide:
return None
Expand All @@ -202,14 +202,14 @@ def _get_nested_models_sub_plan(

def _binding_source_to_plan(self, binding_src: BindingSource) -> BroachingPlan:
return reduce(
function=lambda plan, item: (
lambda plan, item: (
AccessorElement(
target=plan,
accessor=item.accessor,
)
),
sequence=binding_src.tail,
initial=cast(BroachingPlan, ParameterElement(binding_src.head.id)),
binding_src.tail,
cast(BroachingPlan, ParameterElement(binding_src.head.id)),
)

def _get_coercer_sub_plan(
Expand Down
25 changes: 13 additions & 12 deletions src/adaptix/_internal/retort/operating_retort.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,20 @@ def get_request_context_notes(self, request: Request) -> Iterable[str]:
if not isinstance(request, LocatedRequest):
return

location_desc = ', '.join(
f'{key}={value!r}'
for key, value in sorted(
(
(key, value)
for loc in request.last_map.values()
for key, value in vars(loc).items()
),
key=lambda item: self._LOC_KEYS_ORDER.get(item[0], 1000),
if request.loc_stack:
location_desc = ', '.join(
f'{key}={value!r}'
for key, value in sorted(
(
(key, value)
for loc in request.last_map.values()
for key, value in vars(loc).items()
),
key=lambda item: self._LOC_KEYS_ORDER.get(item[0], 1000),
)
)
)
if location_desc:
yield f'Location: {location_desc}'
if location_desc:
yield f'Location: {location_desc}'

try:
owner_loc_map, field_loc_map = find_owner_with_field(request.loc_stack)
Expand Down
Empty file.
155 changes: 155 additions & 0 deletions tests/integration/conversion/test_basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
from dataclasses import dataclass
from typing import Any

from adaptix.conversion import impl_converter


def test_copy(accum):
@dataclass
class ExampleAny:
field1: Any
field2: Any

@impl_converter
def copy(a: ExampleAny) -> ExampleAny:
...

obj1 = ExampleAny(field1=1, field2=2)
assert copy(obj1) == obj1
assert copy(obj1) is not obj1


def test_same_shape(accum):
@dataclass
class SourceModel:
field1: Any
field2: Any

@dataclass
class DestModel:
field1: Any
field2: Any

@impl_converter
def convert(a: SourceModel) -> DestModel:
...

assert convert(SourceModel(field1=1, field2=2)) == DestModel(field1=1, field2=2)


def test_downcast(accum):
@dataclass
class SourceModel:
field1: Any
field2: Any
field3: Any

@dataclass
class DestModel:
field1: Any
field2: Any

@impl_converter
def convert(a: SourceModel) -> DestModel:
...

assert convert(SourceModel(field1=1, field2=2, field3=3)) == DestModel(field1=1, field2=2)


def test_upcast(accum):
@dataclass
class SourceModel:
field1: Any
field2: Any

@dataclass
class DestModel:
field1: Any
field2: Any
field3: Any

@impl_converter
def convert(a: SourceModel, field3: Any) -> DestModel:
...

assert convert(SourceModel(field1=1, field2=2), field3=3) == DestModel(field1=1, field2=2, field3=3)


def test_nested(accum):
@dataclass
class SourceModelNested:
field1: Any

@dataclass
class SourceModel:
field1: Any
field2: Any
nested: SourceModelNested

@dataclass
class DestModelNested:
field1: Any

@dataclass
class DestModel:
field1: Any
field2: Any
nested: DestModelNested

@impl_converter
def convert(a: SourceModel) -> DestModel:
...

assert convert(
SourceModel(
field1=1,
field2=2,
nested=SourceModelNested(field1=3),
)
) == DestModel(
field1=1,
field2=2,
nested=DestModelNested(field1=3),
)


def test_same_nested(accum):
@dataclass
class SourceModelNested:
field1: Any

@dataclass
class SourceModel:
field1: Any
field2: Any
nested1: SourceModelNested
nested2: SourceModelNested

@dataclass
class DestModelNested:
field1: Any

@dataclass
class DestModel:
field1: Any
field2: Any
nested1: DestModelNested
nested2: DestModelNested

@impl_converter
def convert(a: SourceModel) -> DestModel:
...

assert convert(
SourceModel(
field1=1,
field2=2,
nested1=SourceModelNested(field1=3),
nested2=SourceModelNested(field1=4),
)
) == DestModel(
field1=1,
field2=2,
nested1=DestModelNested(field1=3),
nested2=DestModelNested(field1=4),
)

0 comments on commit d871590

Please sign in to comment.