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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import unittest
from unittest.mock import DEFAULT, Mock, call
from trytond.rpc import RPC
from trytond.tests.test_tryton import activate_module, with_transaction
from trytond.transaction import Transaction
class RPCTestCase(unittest.TestCase):
"Test RPC"
@classmethod
def setUpClass(cls):
activate_module('ir')
@with_transaction()
def test_simple(self):
"Test simple"
rpc = RPC(check_access=False)
self.assertEqual(
rpc.convert(None, 'foo', {}),
(['foo'], {}, {}, None))
@with_transaction()
def test_keyword_argument(self):
"Test keyword argument"
rpc = RPC(check_access=False)
self.assertEqual(
rpc.convert(None, 'foo', bar=True, context={}),
(['foo'], {'bar': True}, {}, None))
@with_transaction()
def test_wrong_context_type(self):
"Test wrong context type"
rpc = RPC()
with self.assertRaises(TypeError) as cm:
rpc.convert(None, context=None)
self.assertEqual(
str(cm.exception), "context must be a dictionary")
@with_transaction()
def test_missing_context(self):
"Test missing context"
rpc = RPC()
with self.assertRaises(ValueError) as cm:
rpc.convert(None)
self.assertEqual(
str(cm.exception), "Missing context argument")
@with_transaction()
def test_clean_context(self):
"Test clean context"
rpc = RPC(check_access=False)
self.assertEqual(
rpc.convert(None, {'_foo': True, '_datetime': None}),
([], {}, {'_datetime': None}, None))
@with_transaction()
def test_timestamp(self):
"Test context timestamp"
rpc = RPC(check_access=False)
self.assertEqual(
rpc.convert(None, {'_timestamp': 'test'}),
([], {}, {}, 'test'))
@with_transaction()
def test_instantiate(self):
"Test instantiate"
def side_effect(*args, **kwargs):
self.assertEqual(Transaction().context, {'test': True})
return DEFAULT
rpc = RPC(instantiate=0, check_access=True)
obj = Mock()
obj.return_value = instance = Mock()
obj.side_effect = side_effect
# Integer
self.assertEqual(
rpc.convert(obj, 1, {'test': True}),
([instance], {}, {'test': True, '_check_access': True}, None))
obj.assert_called_once_with(1)
obj.reset_mock()
# Dictionary
self.assertEqual(
rpc.convert(obj, {'foo': 'bar'}, {'test': True}),
([instance], {}, {'test': True, '_check_access': True}, None))
obj.assert_called_once_with(foo='bar')
obj.reset_mock()
obj.browse.return_value = instances = Mock()
# List
self.assertEqual(
rpc.convert(obj, [1, 2, 3], {'test': True}),
([instances], {}, {'test': True, '_check_access': True}, None))
obj.browse.assert_called_once_with([1, 2, 3])
@with_transaction()
def test_instantiate_unique(self):
"Test instantiate unique"
rpc = RPC(instantiate=0, unique=True)
obj = Mock()
rpc.convert(obj, [1, 2], {})
obj.browse.assert_called_once_with([1, 2])
obj.reset_mock()
with self.assertRaises(ValueError):
rpc.convert(obj, [1, 1], {})
@with_transaction()
def test_instantiate_slice(self):
"Test instantiate with slice"
rpc = RPC(instantiate=slice(0, 2), check_access=False)
obj = Mock()
obj.return_value = instance = Mock()
self.assertEqual(
rpc.convert(obj, 1, 2, {}),
([instance, instance], {}, {}, None))
obj.assert_has_calls([call(1), call(2)])
@with_transaction()
def test_check_access(self):
"Test check_access"
rpc_no_access = RPC(check_access=False)
self.assertEqual(
rpc_no_access.convert(None, {}),
([], {}, {}, None))
rpc_with_access = RPC(check_access=True)
self.assertEqual(
rpc_with_access.convert(None, {}),
([], {}, {'_check_access': True}, None))
|