예제 #1
0
    def test_upc_a2e2a(self, faker, num_samples):
        from faker.providers.barcode import Provider
        provider = Provider(faker)

        for _ in range(num_samples):
            upc_a = faker.upc_a(upc_ae_mode=True)
            assert self.upc_a_pattern.fullmatch(upc_a)

            # Convert UPC-A to UPC-E
            upc_e = provider._convert_upc_a2e(upc_a)

            # Number system and check digits must be the same
            assert int(upc_a[0]) == int(upc_e[0])
            assert int(upc_a[-1]) == int(upc_e[-1])

            # Create a new UPC-A barcode based on the UPC-E barcode
            new_upc_a = faker.upc_a(upc_ae_mode=True,
                                    base=upc_e[1:-1],
                                    number_system_digit=int(upc_e[0]))

            # New UPC-A barcode must be the same as the original
            assert upc_a == new_upc_a
예제 #2
0
    def test_upc_a2e_bad_values(self):
        from faker.providers.barcode import Provider
        provider = Provider(self.fake)

        # Invalid data type
        with self.assertRaises(TypeError):
            provider._convert_upc_a2e(12345678)

        # Invalid string
        with self.assertRaises(ValueError):
            provider._convert_upc_a2e('abcdef')