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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
from odoo.exceptions import ValidationError
from odoo.tests import common
class TestBarcodeNomenclature(common.TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Creates an empty nomenclature (rules will be added in the tests).
cls.nomenclature = cls.env['barcode.nomenclature'].create({
'name': 'Barcode Nomenclature Test',
})
def test_barcode_nomenclature_parse_barcode_ean8_01(self):
""" Parses some barcodes with a simple EAN-8 barcode rule and checks the result.
"""
self.env['barcode.rule'].create({
'name': 'Rule Test #1',
'barcode_nomenclature_id': self.nomenclature.id,
'encoding': 'ean8',
'pattern': '........',
})
# Must fail because too short.
res = self.nomenclature.parse_barcode('0002')
self.assertEqual(res['code'], '0002')
self.assertEqual(res['type'], 'error', "Must fail because the barcode is too short")
self.assertEqual(res['encoding'], '')
self.assertEqual(res['base_code'], '0002')
self.assertEqual(res['value'], 0)
# Must fail because wrong checksum (last digit).
res = self.nomenclature.parse_barcode('12345678')
self.assertEqual(res['code'], '12345678')
self.assertEqual(res['type'], 'error', "Must fail because the checksum digit is wrong")
self.assertEqual(res['encoding'], '')
self.assertEqual(res['base_code'], '12345678')
self.assertEqual(res['value'], 0)
# Must pass (right number of digits, right checksum).
res = self.nomenclature.parse_barcode('12345670')
self.assertEqual(res['code'], '12345670')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean8')
self.assertEqual(res['base_code'], '12345670')
self.assertEqual(res['value'], 0, "No value must be located into the barcode")
# Must pass (right number of digits, right checksum).
res = self.nomenclature.parse_barcode('02003405')
self.assertEqual(res['code'], '02003405')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean8')
self.assertEqual(res['base_code'], '02003405')
self.assertEqual(res['value'], 0, "No value must be located into the barcode")
def test_barcode_nomenclature_parse_barcode_ean8_02_validation_error(self):
""" Try to parse a barcode with a wrong barcode rule.
"""
barcode_rule = self.env['barcode.rule'].create({
'name': 'Rule Test #1',
'barcode_nomenclature_id': self.nomenclature.id,
'encoding': 'ean8',
})
with self.assertRaises(ValidationError), self.cr.savepoint():
# Must fail because empty braces.
barcode_rule.pattern = '......{}..'
with self.assertRaises(ValidationError), self.cr.savepoint():
# Must fail because decimal can't be before integer.
barcode_rule.pattern = '......{DN}'
with self.assertRaises(ValidationError), self.cr.savepoint():
# Must fail because a pattern can't have multiple braces group.
barcode_rule.pattern = '....{NN}{DD}'
with self.assertRaises(ValidationError), self.cr.savepoint():
# Must fail because '*' isn't accepted (should be '.*' instead).
barcode_rule.pattern = '*'
with self.assertRaises(ValidationError), self.cr.savepoint():
# Must fail because '**' isn't even a valid regular expression.
barcode_rule.pattern = '**>>>{ND}'
# Allowed since it leads to a valid regular expression.
barcode_rule.pattern = '..>>>{ND}'
def test_barcode_nomenclature_parse_barcode_ean8_03_value(self):
""" Parses some barcodes with a EAN-8 barcode rule who convert the
barcode into value and checks the result.
"""
self.env['barcode.rule'].create({
'name': 'Rule Test #2',
'barcode_nomenclature_id': self.nomenclature.id,
'encoding': 'ean8',
'pattern': '{NNNNNNNN}',
})
res = self.nomenclature.parse_barcode('0002')
self.assertEqual(res['code'], '0002')
self.assertEqual(res['type'], 'error', "Must fail because the barcode is too short")
self.assertEqual(res['encoding'], '')
self.assertEqual(res['base_code'], '0002')
self.assertEqual(res['value'], 0)
res = self.nomenclature.parse_barcode('12345678')
self.assertEqual(res['code'], '12345678')
self.assertEqual(res['type'], 'error', "Must fail because the checksum digit is wrong")
self.assertEqual(res['encoding'], '')
self.assertEqual(res['base_code'], '12345678')
self.assertEqual(res['value'], 0)
# Must pass (right number of digits, right checksum).
res = self.nomenclature.parse_barcode('12345670')
self.assertEqual(res['code'], '12345670')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean8')
self.assertEqual(res['base_code'], '00000000',
"All the barcode should be consumed into the value")
self.assertEqual(res['value'], 12345670.0, "The barcode must be converted into value")
# Must pass (right number of digits, right checksum).
res = self.nomenclature.parse_barcode('02003405')
self.assertEqual(res['code'], '02003405')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean8')
self.assertEqual(res['base_code'], '00000000',
"All the barcode should be consumed into the value")
self.assertEqual(res['value'], 2003405.0, "The barcode must be converted into value")
def test_barcode_nomenclature_parse_barcode_ean8_04_multiple_rules(self):
""" Parses some barcodes with a nomenclature containing multiple EAN-8
barcode rule and checks the right one is took depending of the pattern.
"""
self.env['barcode.rule'].create({
'name': 'Rule Test #1',
'barcode_nomenclature_id': self.nomenclature.id,
'encoding': 'ean8',
'pattern': '11.....{N}',
})
self.env['barcode.rule'].create({
'name': 'Rule Test #1',
'barcode_nomenclature_id': self.nomenclature.id,
'encoding': 'ean8',
'pattern': '66{NN}....',
})
# Only fits the second barcode rule.
res = self.nomenclature.parse_barcode('11012344')
self.assertEqual(res['code'], '11012344')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean8')
self.assertEqual(res['base_code'], '11012340')
self.assertEqual(res['value'], 4)
# Only fits the second barcode rule.
res = self.nomenclature.parse_barcode('66012344')
self.assertEqual(res['code'], '66012344')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean8')
self.assertEqual(res['base_code'], '66002344')
self.assertEqual(res['value'], 1)
# Doesn't fit any barcode rule.
res = self.nomenclature.parse_barcode('16012344')
self.assertEqual(res['code'], '16012344')
self.assertEqual(res['type'], 'error')
self.assertEqual(res['encoding'], '')
self.assertEqual(res['base_code'], '16012344')
self.assertEqual(res['value'], 0)
def test_barcode_nomenclature_parse_barcode_ean13_01(self):
""" Parses some barcodes with a EAN-13 barcode rule who contains a value
and checks the result.
"""
self.env['barcode.rule'].create({
'name': 'Rule Test #3',
'barcode_nomenclature_id': self.nomenclature.id,
'encoding': 'ean13',
'pattern': '1........{NND}.',
})
# Must fail because too short.
res = self.nomenclature.parse_barcode('0002')
self.assertEqual(res['code'], '0002')
self.assertEqual(res['type'], 'error', "Must fail because the barcode is too short")
self.assertEqual(res['encoding'], '')
self.assertEqual(res['base_code'], '0002')
self.assertEqual(res['value'], 0)
# Must fail because wrong checksum (last digit).
res = self.nomenclature.parse_barcode('12345678')
self.assertEqual(res['code'], '12345678')
self.assertEqual(res['type'], 'error', "Must fail because the checksum digit is wrong")
self.assertEqual(res['encoding'], '')
self.assertEqual(res['base_code'], '12345678')
self.assertEqual(res['value'], 0)
# Must pass (right number of digits, right checksum).
res = self.nomenclature.parse_barcode('1020034051259')
self.assertEqual(res['code'], '1020034051259')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean13')
self.assertEqual(res['base_code'], '1020034050009')
self.assertEqual(res['value'], 12.5, "Should taken only the value part (NND)")
def test_barcode_nomenclature_parse_barcode_ean13_02_sequence(self):
""" Parses some barcodes with a nomenclature containing two EAN-13
barcode rule and checks the good one is took depending of its sequence.
"""
first_created_rule = self.env['barcode.rule'].create({
'name': 'Rule Test #1',
'barcode_nomenclature_id': self.nomenclature.id,
'encoding': 'ean13',
'pattern': '.....{NNNDDDD}.',
'sequence': 3,
})
self.env['barcode.rule'].create({
'name': 'Rule Test #2',
'barcode_nomenclature_id': self.nomenclature.id,
'encoding': 'ean13',
'pattern': '22......{NNDD}.',
'sequence': 2,
})
# Invalids the cache to reset the nomenclature barcode rules' order.
self.nomenclature.invalidate_recordset(['rule_ids'])
# Only fits the second barcode rule.
res = self.nomenclature.parse_barcode('2012345610255')
self.assertEqual(res['code'], '2012345610255')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean13')
self.assertEqual(res['base_code'], '2012300000008')
self.assertEqual(res['value'], 456.1025)
# Fits the two barcode rules, but should take the second one (lower sequence).
res = self.nomenclature.parse_barcode('2212345610259')
self.assertEqual(res['code'], '2212345610259')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean13')
self.assertEqual(res['base_code'], '2212345600007')
self.assertEqual(res['value'], 10.25)
first_created_rule.sequence = 1
# Invalids the cache to reset the nomenclature barcode rules' order.
self.nomenclature.invalidate_recordset(['rule_ids'])
# Should take the first one now (lower sequence).
res = self.nomenclature.parse_barcode('2212345610259')
self.assertEqual(res['code'], '2212345610259')
self.assertEqual(res['type'], 'product')
self.assertEqual(res['encoding'], 'ean13')
self.assertEqual(res['base_code'], '2212300000002')
self.assertEqual(res['value'], 456.1025)
def test_barcode_uri_conversion(self):
""" This test ensures URIs are correctly converted into barcode data."""
uri = 'urn:epc:class:lgtin : 4012345.012345.998877'
barcode_data = self.nomenclature.parse_barcode(uri)
self.assertEqual(len(barcode_data), 2)
product_part, lot_part = barcode_data
self.assertEqual(product_part['type'], 'product')
self.assertEqual(product_part['value'], '04012345123456')
self.assertEqual(lot_part['type'], 'lot')
self.assertEqual(lot_part['value'], '998877')
uri = 'urn:epc:id:sgtin:9521141.012345.4711'
barcode_data = self.nomenclature.parse_barcode(uri)
self.assertEqual(len(barcode_data), 2)
product_part, serial_part = barcode_data
self.assertEqual(product_part['type'], 'product')
self.assertEqual(product_part['value'], '09521141123454')
self.assertEqual(serial_part['type'], 'lot')
self.assertEqual(serial_part['value'], '4711')
uri = 'urn:epc:tag:sgtin-96 : 1.358378.0728089.620776'
barcode_data = self.nomenclature.parse_barcode(uri)
self.assertEqual(len(barcode_data), 2)
product_part, serial_part = barcode_data
self.assertEqual(product_part['type'], 'product')
self.assertEqual(product_part['value'], '03583787280898')
self.assertEqual(serial_part['type'], 'lot')
self.assertEqual(serial_part['value'], '620776')
uri = 'urn:epc:id:sscc:952656789012.03456'
barcode_data = self.nomenclature.parse_barcode(uri)
self.assertEqual(len(barcode_data), 1)
self.assertEqual(barcode_data[0]['type'], 'package')
self.assertEqual(barcode_data[0]['value'], '095265678901234568')
|