Python promise.dataloader.DataLoader() Examples
The following are 6
code examples of promise.dataloader.DataLoader().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
promise.dataloader
, or try the search function
.
Example #1
Source File: test_dataloader.py From promise with MIT License | 6 votes |
def test_supports_loading_multiple_keys_in_one_call(): def call_fn(keys): return Promise.resolve(keys) identity_loader = DataLoader(call_fn) promise_all = identity_loader.load_many([1, 2]) assert isinstance(promise_all, Promise) values = promise_all.get() assert values == [1, 2] promise_all = identity_loader.load_many([]) assert isinstance(promise_all, Promise) values = promise_all.get() assert values == []
Example #2
Source File: test_dataloader.py From promise with MIT License | 5 votes |
def id_loader(**options): load_calls = [] resolve = options.pop("resolve", Promise.resolve) def fn(keys): load_calls.append(keys) return resolve(keys) identity_loader = DataLoader(fn, **options) return identity_loader, load_calls
Example #3
Source File: test_dataloader.py From promise with MIT License | 5 votes |
def test_build_a_simple_data_loader(): def call_fn(keys): return Promise.resolve(keys) identity_loader = DataLoader(call_fn) promise1 = identity_loader.load(1) assert isinstance(promise1, Promise) value1 = promise1.get() assert value1 == 1
Example #4
Source File: test_dataloader_awaitable_35.py From promise with MIT License | 5 votes |
def id_loader(**options): load_calls = [] resolve = options.pop("resolve", Promise.resolve) def fn(keys): load_calls.append(keys) return resolve(keys) identity_loader = DataLoader(fn, **options) return identity_loader, load_calls
Example #5
Source File: test_thread_safety.py From promise with MIT License | 4 votes |
def test_dataloader_thread_safety(): """ Dataloader should only batch `load` calls that happened on the same thread. Here we assert that `load` calls on thread 2 are not batched on thread 1 as thread 1 batches its own `load` calls. """ def load_many(keys): thead_name = threading.current_thread().getName() return Promise.resolve([thead_name for key in keys]) thread_name_loader = DataLoader(load_many) event_1 = threading.Event() event_2 = threading.Event() event_3 = threading.Event() assert_object = { 'is_same_thread_1': True, 'is_same_thread_2': True, } def task_1(): @Promise.safe def do(): promise = thread_name_loader.load(1) event_1.set() event_2.wait() # Wait for thread 2 to call `load` assert_object['is_same_thread_1'] = ( promise.get() == threading.current_thread().getName() ) event_3.set() # Unblock thread 2 do().get() def task_2(): @Promise.safe def do(): promise = thread_name_loader.load(2) event_2.set() event_3.wait() # Wait for thread 1 to run `dispatch_queue_batch` assert_object['is_same_thread_2'] = ( promise.get() == threading.current_thread().getName() ) do().get() thread_1 = threading.Thread(target=task_1) thread_1.start() event_1.wait() # Wait for thread 1 to call `load` thread_2 = threading.Thread(target=task_2) thread_2.start() for thread in (thread_1, thread_2): thread.join() assert assert_object['is_same_thread_1'] assert assert_object['is_same_thread_2']
Example #6
Source File: test_dataloader.py From graphql-core-legacy with MIT License | 4 votes |
def test_batches_correctly(executor): # type: (SyncExecutor) -> None Business = GraphQLObjectType( "Business", lambda: { "id": GraphQLField(GraphQLID, resolver=lambda root, info, **args: root) }, ) Query = GraphQLObjectType( "Query", lambda: { "getBusiness": GraphQLField( Business, args={"id": GraphQLArgument(GraphQLNonNull(GraphQLID))}, resolver=lambda root, info, **args: info.context.business_data_loader.load( args.get("id") ), ) }, ) schema = GraphQLSchema(query=Query) doc = """ { business1: getBusiness(id: "1") { id } business2: getBusiness(id: "2") { id } } """ doc_ast = parse(doc) load_calls = [] class BusinessDataLoader(DataLoader): def batch_load_fn(self, keys): # type: (List[str]) -> Promise load_calls.append(keys) return Promise.resolve(keys) class Context(object): business_data_loader = BusinessDataLoader() result = execute(schema, doc_ast, None, context_value=Context(), executor=executor) assert not result.errors assert result.data == {"business1": {"id": "1"}, "business2": {"id": "2"}} assert load_calls == [["1", "2"]]