-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndb_mocks.py
62 lines (56 loc) · 2.12 KB
/
ndb_mocks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
from unittest import mock
import grpc
from google.cloud.ndb import context
class MockFuture(grpc.Future):
def __init__(self, val):
self.val = val
def add_done_callback(self, fn):
fn(self)
def result(self, timeout=None):
return self.val
def done(self):
return True
def running(self):
return False
def cancelled(self):
return False
def cancel(self):
return False
def traceback(self):
return None
def exception(self):
return None
def MockUnaryUnaryCallable():
unun = mock.MagicMock(spec=grpc.UnaryUnaryMultiCallable)
unun.with_call = lambda *args, **kwargs: unun(*args,**kwargs)
unun.future = lambda *args, **kwargs: MockFuture(unun(*args, **kwargs))
def set_val(val):
unun.return_value = val
unun.set_val = set_val
def set_side_effect(side_effect):
unun.side_effect = side_effect
unun.set_side_effect = set_side_effect
return unun
def MockDatastoreStub():
stub = mock.Mock(spec=("lookup", "run_query", "run_aggregation_query","begin_transaction", "commit","allocate_ids", "rollback", "reserve_ids", "delete_operation", "cancel_operation", "get_operation", "list_operation"))
stub.lookup = MockUnaryUnaryCallable()
stub.run_query = MockUnaryUnaryCallable()
stub.run_aggregation_query = MockUnaryUnaryCallable()
stub.begin_transaction = MockUnaryUnaryCallable()
stub.commit = MockUnaryUnaryCallable()
stub.rollback = MockUnaryUnaryCallable()
stub.allocate_ids = MockUnaryUnaryCallable()
stub.reserve_ids = MockUnaryUnaryCallable()
stub.get_operation = MockUnaryUnaryCallable()
stub.list_operations = MockUnaryUnaryCallable()
stub.delete_operation = MockUnaryUnaryCallable()
stub.cancel_operation = MockUnaryUnaryCallable()
return stub
def MockNdbClient():
client = mock.Mock(spec=("project","namespace","stub", "context"))
client.project="blah"
client.namespace=None
client.stub = MockDatastoreStub()
client().context = context.Context(client).use
return client