def test_load_from_file(tmpdir): tmpdir.join('my_service.thrift').write(''' struct Foo { 1: required string a 2: optional string b } ''') my_service = Loader().load(str(tmpdir.join('my_service.thrift'))) my_service.Foo(b='b', a='a')
def test_include_as_disabled(tmpdir): loader = Loader() tmpdir.join('a.thrift').write('include t "./b.thrift"') tmpdir.join('b.thrift').write('typedef string UUID') with pytest.raises(ThriftCompilerError) as exc_info: loader.load(str(tmpdir.join('a.thrift'))) assert 'Cannot include "b" as "t"' in str(exc_info) assert '"include-as" syntax is currently disabled' in str(exc_info)
def test_load_from_file_non_strict_missing_requiredness(tmpdir): tmpdir.join('my_service.thrift').write(''' struct Foo { 1: required string a 2: string b } ''') loader = Loader(strict=False) my_service = loader.load(str(tmpdir.join('my_service.thrift'))) my_service.Foo(b='b', a='a')
def test_caching(tmpdir, monkeypatch): tmpdir.join('my_service.thrift').write(''' struct Foo { 1: required string a 2: optional string b } ''') path = str(tmpdir.join('my_service.thrift')) loader = Loader() mod1 = loader.load(path) mod2 = loader.load(path) assert mod1 is mod2
def test_load_from_file_non_strict_missing_requiredness(tmpdir): tmpdir.join('my_service.thrift').write(''' struct Foo { 1: required string a 2: string b } exception Bar { 1: string message } ''') loader = Loader(strict=False) m = loader.load(str(tmpdir.join('my_service.thrift'))) m.Foo(b='b', a='a') m.Bar(message='foo') m.Bar()
def test_caching(tmpdir, monkeypatch): tmpdir.join('my_service.thrift').write(''' struct Foo { 1: required string a 2: optional string b } ''') path = str(tmpdir.join('my_service.thrift')) loader = Loader() mod1 = loader.load(path) assert path in loader.compiled_modules mod2 = loader.load(path) assert mod1 is mod2 mod3 = loader.load(path, force=True) assert mod3 is not mod2
def test_load_from_file_missing_requiredness(tmpdir): tmpdir.join('my_service.thrift').write(''' struct Foo { 1: required string a 2: string b } ''') with pytest.raises(ThriftCompilerError) as exc_info: Loader().load(str(tmpdir.join('my_service.thrift'))) assert ('"b" of "Foo" on line 4 does not explicitly specify requiredness.' in str(exc_info))
import pytest from thriftrw.loader import Loader from thriftrw.protocol import BinaryProtocol from thriftrw.wire import mtype service = Loader(BinaryProtocol()).loads( 'runtime_service', ''' exception GreatSadness { 1: optional string message } service Service { oneway void write(1: binary data) i32 read() throws (1: GreatSadness sadness) } ''' ) Service = service.Service @pytest.mark.parametrize('name, seqid, typ, obj, bs', [ (b'write', 42, mtype.ONEWAY, Service.write.request(b'hello'), [ 0x00, 0x00, 0x00, 0x05, # length:4 = 5 0x77, 0x72, 0x69, 0x74, 0x65, # 'write' 0x04, # type:1 = ONEWAY
def loads(request): return partial(Loader(BinaryProtocol()).loads, request.node.name)
def loads(request): return partial(Loader().loads, request.node.name)