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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
# 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 shutil
import tempfile
import unittest
from sql import Literal
from trytond import filestore
from trytond.model import fields
from trytond.model.exceptions import (
RequiredValidationError, SQLConstraintError)
from trytond.pool import Pool
from trytond.tests.test_tryton import activate_module, with_transaction
from trytond.transaction import Transaction
cast = fields.Binary.cast
class FieldBinaryTestCase(unittest.TestCase):
"Test Field Binary"
@classmethod
def setUpClass(cls):
activate_module('tests')
def setUp(self):
super().setUp()
path = filestore.PATH
dtemp = tempfile.mkdtemp()
filestore.PATH = dtemp
self.addCleanup(setattr, filestore, 'PATH', path)
self.addCleanup(shutil.rmtree, dtemp)
@with_transaction()
def test_create(self):
"Test create binary"
Binary = Pool().get('test.binary')
binary, = Binary.create([{
'binary': cast(b'foo'),
}])
self.assertEqual(binary.binary, cast(b'foo'))
@with_transaction()
def test_create_without_default(self):
"Test create binary without default"
Binary = Pool().get('test.binary')
binary, = Binary.create([{}])
self.assertEqual(binary.binary, None)
@with_transaction()
def test_create_with_default(self):
"Test create binary with default"
Binary = Pool().get('test.binary_default')
binary, = Binary.create([{}])
self.assertEqual(binary.binary, cast(b'default'))
@with_transaction()
def test_create_required_without_value(self):
"Test create binary without value"
Binary = Pool().get('test.binary_required')
with self.assertRaises(RequiredValidationError):
binary, = Binary.create([{}])
@with_transaction()
def test_create_required_with_value(self):
"Test create binary with default"
Binary = Pool().get('test.binary_default')
binary, = Binary.create([{
'binary': cast(b'baz'),
}])
self.assertEqual(binary.binary, cast(b'baz'))
@with_transaction()
def test_create_required_with_empty(self):
"Test create binary with empty"
Binary = Pool().get('test.binary_required')
with self.assertRaises(RequiredValidationError):
binary, = Binary.create([{
'binary': cast(b''),
}])
@with_transaction()
def test_create_required_with_invalid_sql_constraint(self):
"Test create required binary with invalid SQL constraint"
Binary = Pool().get('test.binary_required_sql_constraint')
binary, = Binary.create([{
'binary': cast(b'foo'),
'constraint': True,
}])
with self.assertRaises(SQLConstraintError):
binary, = Binary.create([{
'binary': cast(b'foo'),
'constraint': False,
}])
@with_transaction()
def test_create_filestorage(self):
"Test create binary with filestorage"
Binary = Pool().get('test.binary_filestorage')
binary, = Binary.create([{
'binary': cast(b'foo'),
}])
self.assertEqual(binary.binary, cast(b'foo'))
self.assertTrue(binary.binary_id)
@with_transaction()
def test_create_empty_filestorage(self):
"Test create binary empty with filestorage"
Binary = Pool().get('test.binary_filestorage')
binary, = Binary.create([{
'binary': None,
}])
self.assertEqual(binary.binary, None)
self.assertEqual(binary.binary_id, None)
@with_transaction()
def test_create_with_sql_value(self):
"Test create binary with SQL value"
Binary = Pool().get('test.binary')
binary, = Binary.create([{
'binary': Literal('foo'),
}])
self.assertEqual(binary.binary, cast(b'foo'))
@with_transaction()
def test_set_sql_value(self):
"Test cannot set SQL value"
Binary = Pool().get('test.binary')
binary = Binary()
with self.assertRaises(ValueError):
binary.binary = Literal('foo')
@with_transaction()
def test_read_size(self):
"Test read binary size"
Binary = Pool().get('test.binary')
binary, = Binary.create([{
'binary': cast(b'foo'),
}])
with Transaction().set_context({'test.binary.binary': 'size'}):
binary = Binary(binary.id)
self.assertEqual(binary.binary, len(b'bar'))
@with_transaction()
def test_read_size_filestorage(self):
"Test read binary size with filestorage"
Binary = Pool().get('test.binary_filestorage')
binary, = Binary.create([{
'binary': cast(b'foo'),
}])
with Transaction().set_context(
{'test.binary_filestorage.binary': 'size'}):
binary = Binary(binary.id)
self.assertEqual(binary.binary, len(b'bar'))
@with_transaction()
def test_write(self):
"Test write binary"
Binary = Pool().get('test.binary')
binary, = Binary.create([{
'binary': cast(b'foo'),
}])
Binary.write([binary], {
'binary': cast(b'bar'),
})
self.assertEqual(binary.binary, cast(b'bar'))
@with_transaction()
def test_write_filestorage(self):
"Test write binary with filestorage"
Binary = Pool().get('test.binary_filestorage')
binary, = Binary.create([{
'binary': cast(b'foo'),
}])
Binary.write([binary], {
'binary': cast(b'bar'),
})
self.assertEqual(binary.binary, cast(b'bar'))
@with_transaction()
def test_write_empty_filestorage(self):
"Test write binary empty with filestorage"
Binary = Pool().get('test.binary_filestorage')
binary, = Binary.create([{
'binary': cast(b'foo'),
}])
Binary.write([binary], {
'binary': None,
})
self.assertEqual(binary.binary, None)
self.assertEqual(binary.binary_id, None)
@with_transaction()
def test_copy(self):
"Test copy binary"
pool = Pool()
Binary = pool.get('test.binary')
binary = Binary(binary=b'foo')
binary.save()
copy, = Binary.copy([binary])
self.assertEqual(binary.binary, copy.binary)
@with_transaction()
def test_copy_with_default(self):
"Test copy binary with default"
pool = Pool()
Binary = pool.get('test.binary')
binary = Binary(binary=b'foo')
binary.save()
copy, = Binary.copy([binary], default={'binary': b'bar'})
self.assertEqual(copy.binary, b'bar')
@with_transaction()
def test_copy_with_filestorage(self):
"Test copy binary with filestorage"
pool = Pool()
Binary = pool.get('test.binary_filestorage')
binary = Binary(binary=b'foo')
binary.save()
copy, = Binary.copy([binary])
self.assertEqual(binary.binary, copy.binary)
@with_transaction()
def test_copy_with_filestorage_default(self):
"Test copy binary with filestorage and default"
pool = Pool()
Binary = pool.get('test.binary_filestorage')
binary = Binary(binary=b'foo')
binary.save()
copy, = Binary.copy([binary], default={'binary': b'bar'})
self.assertEqual(copy.binary, b'bar')
|