Skip to content

Commit

Permalink
Merge pull request #55 from savoirfairelinux/8.0-product_gtin_tests
Browse files Browse the repository at this point in the history
8.0 product gtin tests
  • Loading branch information
bwrsandman committed Apr 23, 2015
2 parents 082aac9 + 579afdc commit 8670b90
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 12 deletions.
64 changes: 52 additions & 12 deletions product_gtin/product_gtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
_logger = logging.getLogger(__name__)

from openerp.osv import orm, fields
import operator
Expand All @@ -34,6 +36,19 @@ def is_pair(x):


def check_ean8(eancode):
"""Check if the given ean code answer ean8 requirements
For more details: http://en.wikipedia.org/wiki/EAN-8
:param eancode: string, ean-8 code
:return: boolean
"""
if not eancode or not eancode.isdigit():
return False

if not len(eancode) == 8:
_logger.warn('Ean8 code has to have a length of 8 characters.')
return False

sum = 0
ean_len = int(len(eancode))
for i in range(ean_len-1):
Expand All @@ -44,28 +59,54 @@ def check_ean8(eancode):
check = 10 - operator.mod(sum, 10)
if check == 10:
check = 0
if check != int(eancode[-1]):

return check == int(eancode[-1])


def check_upc(upccode):
"""Check if the given code answers upc requirements
For more details:
http://en.wikipedia.org/wiki/Universal_Product_Code
:param upccode: string, upc code
:return: bool
"""
if not upccode or not upccode.isdigit():
return False
return True

if not len(upccode) == 12:
_logger.warn('UPC code has to have a length of 12 characters.')
return False

def check_upc(eancode):
sum_pair = 0
ean_len = int(len(eancode))
ean_len = int(len(upccode))
for i in range(ean_len-1):
if is_pair(i):
sum_pair += int(eancode[i])
sum_pair += int(upccode[i])
sum = sum_pair * 3
for i in range(ean_len-1):
if not is_pair(i):
sum += int(eancode[i])
sum += int(upccode[i])
check = ((sum/10 + 1) * 10) - sum
if check != int(eancode[-1]):
return False
return True

return check == int(upccode[-1])


def check_ean13(eancode):
"""Check if the given ean code answer ean13 requirements
For more details:
http://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29
:param eancode: string, ean-13 code
:return: boolean
"""
if not eancode or not eancode.isdigit():
return False

if not len(eancode) == 13:
_logger.warn('Ean13 code has to have a length of 13 characters.')
return False

sum = 0
ean_len = int(len(eancode))
for i in range(ean_len-1):
Expand All @@ -77,9 +118,8 @@ def check_ean13(eancode):
check = 10 - operator.mod(sum, 10)
if check == 10:
check = 0
if check != int(eancode[-1]):
return False
return True

return check == int(eancode[-1])


def check_ean11(eancode):
Expand Down
21 changes: 21 additions & 0 deletions product_gtin/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Therp BV (<http://therp.nl>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from . import test_product_gtin_functions
174 changes: 174 additions & 0 deletions product_gtin/tests/test_product_gtin_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# -*- coding: utf-8 -*-
# #############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Therp BV (<http://therp.nl>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# it is not useful to use odoo unittest suite here as only
# methods without odoo tools are tested here.
import unittest2
import logging
_logger = logging.getLogger(__name__)

from openerp.addons.product_gtin import product_gtin


class TestIsPair(unittest2.TestCase):
def test_returns(self):
"""Check the return of the function."""
# http://en.wikipedia.org/wiki/Parity_of_zero
self.assertTrue(product_gtin.is_pair(0))

# Testing random numbers.
self.assertTrue(product_gtin.is_pair(2))
self.assertTrue(product_gtin.is_pair(4))
self.assertTrue(product_gtin.is_pair(40))

self.assertFalse(product_gtin.is_pair(1))
self.assertFalse(product_gtin.is_pair(3))
self.assertFalse(product_gtin.is_pair(5))
self.assertFalse(product_gtin.is_pair(77))


VALID_EAN8_CODES = [
# http://www.softmatic.com/barcode-ean-8.html
"40123455",
# http://www.barcodeisland.com/ean8.phtml
"04210009",
]

VALID_EAN13_CODES = [
# http://www.barcodeisland.com/ean13.phtml
"0075678164125",
"2000021262157",
]

VALID_UPC_CODES = [
"012345678905",
"080047440694",
"123456789012",
]


class TestCheckUpc(unittest2.TestCase):
"""The codes have been tested against
http://www.hipaaspace.com/Medical_Data_Validation/Universal_Product_Code/UPC_Validation.aspx # noqa
"""
def test_upc_codes(self):
for code in VALID_UPC_CODES:
_logger.debug('code: {}'.format(code))
self.assertTrue(product_gtin.check_upc(code))

def test_returns_wrong_upc_codes(self):
self.assertFalse(product_gtin.check_upc(""))
# test string
self.assertFalse(product_gtin.check_upc("odoo_oca"))
# less than 12 numbers
self.assertFalse(product_gtin.check_upc("12345678901"))
# 12 random numbers
self.assertFalse(product_gtin.check_upc("123456789013"))
# more than 12 numbers
self.assertFalse(product_gtin.check_upc("12345678980123"))

def test_ean8_codes(self):
"""Ean8 codes should not be valid for UPC."""
for code in VALID_EAN8_CODES:
_logger.debug('code: {}'.format(code))
self.assertFalse(product_gtin.check_upc(code))

def test_ean13_codes(self):
"""Ean13 codes should not be valid for UPC."""
for code in VALID_EAN13_CODES:
_logger.debug('code: {}'.format(code))
self.assertFalse(product_gtin.check_upc(code))


class TestCheckEan8(unittest2.TestCase):

def test_returns_earn8_codes(self):
for code in VALID_EAN8_CODES:
self.assertTrue(product_gtin.check_ean8(code))

def test_returns_wrong_ean8_codes(self):
self.assertFalse(product_gtin.check_ean8(""))
# test string
self.assertFalse(product_gtin.check_ean8("odoo_oca"))
# less than 8 numbers
self.assertFalse(product_gtin.check_ean8("1234567"))
# 8 random numbers
self.assertFalse(product_gtin.check_ean8("12345678"))
self.assertFalse(product_gtin.check_ean8("82766678"))
# 9 numbers
self.assertFalse(product_gtin.check_ean8("123456789"))

def test_return_ean8_codes(self):
"""Ean8 should not accept ean13"""
for code in VALID_EAN13_CODES:
self.assertFalse(product_gtin.check_ean8(code))

def test_return_upc_codes(self):
"""Ean8 should not accept UPC"""
for code in VALID_UPC_CODES:
self.assertFalse(product_gtin.check_ean8(code))


class TestCheckEan13(unittest2.TestCase):

def test_return_ean13_codes(self):
"""test valid ean 13 number."""
for code in VALID_EAN13_CODES:
self.assertTrue(product_gtin.check_ean13(code))

def test_wrong_ean13_codes(self):
self.assertFalse(product_gtin.check_ean13(""))
# test string
self.assertFalse(product_gtin.check_ean8("odoo_oca_sflx"))
# less than 13 numbers
self.assertFalse(product_gtin.check_ean13("123456789012"))
# 13 random numbers
self.assertFalse(product_gtin.check_ean13("1234567890123"))
self.assertFalse(product_gtin.check_ean13("1234514728123"))
# 14 numbers
self.assertFalse(product_gtin.check_ean13("12345147281234"))

def test_returns_ean8_codes(self):
"""Ean13 should not accept ean8"""
for code in VALID_EAN8_CODES:
self.assertFalse(product_gtin.check_ean13(code))

def test_returns_upc_codes(self):
"""Ean13 should not accept UPC"""
for code in VALID_UPC_CODES:
self.assertFalse(product_gtin.check_ean13(code))


class TestCheckEan(unittest2.TestCase):

def test_dict_check_ean(self):
"""Check if the dict DICT_CHECK_EAN exists."""
self.assertTrue(product_gtin.DICT_CHECK_EAN)

def test_dict_pair(self):
self.assertEqual(
product_gtin.DICT_CHECK_EAN[8], product_gtin.check_ean8
)
self.assertEqual(
product_gtin.DICT_CHECK_EAN[12], product_gtin.check_upc
)
self.assertEqual(
product_gtin.DICT_CHECK_EAN[13], product_gtin.check_ean13
)

0 comments on commit 8670b90

Please sign in to comment.