Skip to content
This repository has been archived by the owner on Jul 21, 2021. It is now read-only.

fix ProtoList with int type #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions mercator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,16 @@ def cast(self, value):
:param value: a python object that is compatible with the given ``target_type``
:returns: list of items target type coerced into the ``target_type``. Supports ProtoMappings by automatically calling :py:meth:`~mercator.ProtoMapping.to_protobuf`.
"""
result = super().cast(value)

if result is None:
if value is None:
return

if not isinstance(value, (list, tuple)):
raise TypeCastError(f'ProtoList.cast() received a non-list value '
f'(type {type(value).__name__}): {value}')

if self.target_type is None:
return value

if issubclass(self.target_type, ProtoMapping):
return [self.target_type(item).to_protobuf() for item in value]

Expand Down
34 changes: 34 additions & 0 deletions tests/unit/test_proto_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,37 @@ def test_proto_list_native_type():
t1, t2 = result
t1.should.be.a(str)
t2.should.be.a(str)


def test_proto_list_int_type():
"ProtoList() should return a list of integers if target_type is int"

field = ProtoList('user_ids', int)
result = field.cast(['1', 2])

result.should.be.a(list)
result.should.have.length_of(2)
result.should.equal([1, 2])

t1, t2 = result
t1.should.be.a(int)
t2.should.be.a(int)


def test_proto_list_with_target_type_none():
"ProtoList() cast should return an origin value if target_type is None"
field = ProtoList('user_ids')
result = field.cast(['1', 2])

result.should.be.a(list)
result.should.have.length_of(2)
result.should.equal(['1', 2])


def test_proto_list_with_target_type_none_and_non_list_tuple():
"ProtoList() should raise an error if target_type is None and not fed with a list or tuple"

field = ProtoList('tokens')
when_called = field.cast.when.called_with('some tokens as string')

when_called.should.have.raised(TypeCastError)