def test_union_name(): def resolve_name(state, name, **kw): assert name == 'foobar' return state foobar = union( lambda state, *a, **kw: state.get('type'), a=record(id=fields.integer(key=True))() )(resolve_name)('foobar') foobar({'id': 1234, 'type': 'a'}, {'id': None})
def test_nested_record_resolve_many(): rec = record(foo=fields.integer(key=True), bar=record(baz=fields.string(key=True))(many=True)) foobar = rec()('foobar') # value picking values = foobar({'foo': '1234'}, {'foo': None}) with pytest.raises(KeyError): values['bar'] assert values.get('foo') == 1234 # implicit key fetching values = foobar({'foo': 1234, 'bar': [{'baz': 1234}]}, {'bar': None}) assert values.get('foo') == 1234 assert values['bar'][0]['baz'] == '1234' # fetch all values = foobar({'foo': 1234, 'bar': [{'baz': 1234}]}) assert values.get('foo') == 1234 assert values['bar'][0]['baz'] == '1234' values = foobar({'foo': 1234, 'bar': [{'baz': 1234}]}, {}) assert values.get('foo') == 1234 assert values['bar'][0]['baz'] == '1234'
def test_query_deep_py_keys(): @query(foo=record(id_field=fields.integer(key=True))()) def foo_bar_query(records, **props): return { 'foo': {'id_field': '1234', 'foo': 'foo'} } values = foo_bar_query('foo_bar_query')({'foo': {'idField': None}}) assert values['foo']['idField'] == 1234 with pytest.raises(KeyError): values['foo']['foo']
def test_js_keys(): rec = record(foo_bar=fields.integer(key=True), bar_baz_boz=fields.string()) # single record cases = rec()('cases') values = cases({'foo_bar': 1234, 'bar_baz_boz': 'foo'}) assert values.get('fooBar') == 1234 assert values.get('barBazBoz') == 'foo' # many records cases = rec(many=True)('cases') values = cases([{'foo_bar': 1234, 'bar_baz_boz': 'foo'}]) assert values[0].get('fooBar') == 1234 assert values[0].get('barBazBoz') == 'foo'
import pytest from radar_server import union, record, fields Foo = record(id=fields.integer(key=True)) Bar = record(id=fields.string(key=True)) FooBarUnion = union( lambda state, *a, **kw: state.get('type'), foo=Foo(), bar=Bar() ) def test_union_name(): def resolve_name(state, name, **kw): assert name == 'foobar' return state foobar = union( lambda state, *a, **kw: state.get('type'), a=record(id=fields.integer(key=True))() )(resolve_name)('foobar') foobar({'id': 1234, 'type': 'a'}, {'id': None}) def test_union_resolve_member(): foobar = FooBarUnion()('foobar') values = foobar({'id': 1234, 'type': 'foo'}, {'id': None}) assert isinstance(values['foo']['id'], int)
def test_missing_key_raises(): with pytest.raises(MissingRecordKey): record(foo=fields.string())
def test_too_many_keys_raises(): with pytest.raises(TooManyRecordKeys): record(foo=fields.string(key=True), bar=fields.string(key=True))
import pytest from radar_server import (record, fields, MissingRecordKey, EmptyRecordKey, FieldNotFound, TooManyRecordKeys, RecordIsNull) FoobarRecord = record(foo=fields.integer(key=True), bar=fields.string()) def test_missing_key_raises(): with pytest.raises(MissingRecordKey): record(foo=fields.string()) def test_too_many_keys_raises(): with pytest.raises(TooManyRecordKeys): record(foo=fields.string(key=True), bar=fields.string(key=True)) def test_empty_key_raises(): foobar = FoobarRecord()('foobar') with pytest.raises(EmptyRecordKey): foobar({'bar': 'foo', 'foo': None}) with pytest.raises(EmptyRecordKey): foobar({'bar': 'foo'}, {'bar': None}) def test_empty_requested_value_raises(): foobar = FoobarRecord()('foobar') with pytest.raises(KeyError): foobar({'foo': 1234})
import pytest from radar_server import query, record, fields, RecordIsNull Foo = record(id=fields.integer(key=True), foo=fields.string()) Bar = record(id=fields.integer(key=True), bar=fields.string()) @query(foo=Foo()) def foo_query(records, **props): return { 'foo': {'id': '1234', 'foo': 'foo'} } @query(foo=Foo(), bar=Bar()) def foo_bar_query(records, **props): return { 'foo': {'id': '1234', 'foo': 'foo'}, 'bar': {'id': '1235', 'bar': 'bar'} } def test_query_name(): @query(foo=Foo()) def foo_query(records, query_name=None, **props): assert query_name == 'foo_query' return { 'foo': {'id': '1234', 'foo': 'foo'} }
def bench_record_nested(): foobar = record(foo=fields.string(key=True), bar=record(baz=fields.integer(key=True))())()('foobar') Timer(foobar, {'foo': 'bar', 'bar': {'baz': 1234}}).time(1E5)
def bench_record_create(): Timer(record(foo=fields.string(key=True), bar=fields.integer())).time(1E5)
def bench_record_resolve_many(): foo = record(foo=fields.string(key=True), bar=fields.integer())(many=True)('foo') Timer(foo, [{'foo': 'bar', 'bar': 1234}]).time(1E5)
def bench_record_resolve_one(): foo = record(foo=fields.string(key=True), bar=fields.integer())()('foo') Timer(foo, {'foo': 'bar'}, {'foo': None}).time(1E5)
def bench_record_resolve_all(): foo = record(foo=fields.string(key=True), bar=fields.integer())()('foo') Timer(foo, {'foo': 'bar', 'bar': 1234}).time(1E5)
def bench_record_full_init(): Timer(lambda: record(foo=fields.string(key=True), bar=fields.integer())() ('foo')).time(1E5)
def bench_record_init(): rec = record(foo=fields.string(key=True), bar=fields.integer())() Timer(rec, 'foo').time(1E5)