# Path: tests/api_tests.py
# def betamaxed(original_function):
#     @wraps(original_function)
#     def new_function(api, *args, **kwargs):
#         with Betamax(api.session).use_cassette(original_function.__name__):
#             return original_function(api, *args, **kwargs)
# 
#     return new_function
# 
# Path: openprovider/exceptions.py
# class NoSuchElement(OpenproviderError):
#     """The element you tried to retrieve could not be found."""
# 
# Path: openprovider/models.py
# class Address(Model):
#     """
#     A physical street address.
# 
#     street (required)
#     number (required)
#     suffix (optional)
#     zipcode (required)
#     city (required)
#     state (optional)
#     country (required)
#     """
# 
#     def __eq__(self, other):
#         attributes = ['street', 'number', 'suffix', 'zipcode', 'city', 'state', 'country']
#         return all(getattr(self, attr, None) == getattr(other, attr, None) for attr in attributes)
# 
# class Name(Model):
#     """
#     A person's name.
# 
#     initials (required)
#         Initials (first letters of first names, first letter of last name)
#     firstName (required)
#         First name
#     prefix (optional)
#         Prefix (often occuring in Dutch names; for example van de)
#     lastName (required)
#         Last name
#     """
# 
#     def __eq__(self, other):
#         attributes = ['initials', 'first_name', 'prefix', 'last_name']
#         return all(getattr(self, attr, None) == getattr(other, attr, None) for attr in attributes)
# 
#     def __str__(self):
#         if getattr(self, "prefix", None):
#             return "%s %s %s" % (self.first_name, self.prefix, self.last_name)
#         else:
#             return "%s %s" % (self.first_name, self.last_name)
# 
# class Phone(Model):
#     """
#     An international phone number.
# 
#     country_code (required)
#     area_code (required)
#     subscriber_number (required)
#     """
# 
#     country_code = textattribute("countryCode")
#     area_code = textattribute("areaCode")
#     subscriber_number = textattribute("subscriberNumber")
# 
#     def __eq__(self, other):
#         attributes = ['country_code', 'area_code', 'subscriber_number']
#         return all(getattr(self, attr, None) == getattr(other, attr, None) for attr in attributes)
# 
#     def __str__(self):
#         """Return the string representation of phone number."""
#         return "%s %s %s" % (self.country_code, self.area_code, self.subscriber_number)
# 
# Path: openprovider/util.py
# def parse_phone_number(number):
#     if isinstance(number, basestring):
#         match = PHONE_REGEX.search(number)
#         if not match:
#             raise ValueError("Invalid phone number")
#         return match.groups()
#     elif isinstance(number, (tuple, list)):
#         if len(number) != 3:
#             raise ValueError("Invalid phone number")
#         return number
#     else:
#         raise ValueError("Invalid phone number")


import pytest
import random
from . import betamaxed
from betamax import Betamax
from datetime import date
from faker import Factory
from openprovider.exceptions import NoSuchElement
from openprovider.models import Address, Name, Phone
from openprovider.util import parse_phone_number

def test_customer_modify_doesnt_wipe_additional_data(api, name, gender, address, phone, email):
    additional_data = {
            'birth_date': date(1980, 5, 10),
    }

    placeholders = list(_to_placeholders(name=name, gender=gender, address=address, phone=phone, email=email))

    with Betamax(api.session) as recorder:
        with recorder.use_cassette('test_customer_modify_doesnt_wipe_1', placeholders=placeholders):
            handle = api.customer.create_customer(name, gender, address, phone, email,
                                                  additional_data=additional_data)
            assert isinstance(handle, str)

        placeholders.append(_to_placeholder('handle', handle))
        with recorder.use_cassette('test_customer_modify_doesnt_wipe_2', placeholders=placeholders):
            customer = api.customer.retrieve_customer(handle, with_additional_data=True)
            assert customer.additional_data.birth_date == '1980-05-10'

            result = api.customer.modify_customer(handle, address, phone, email)
            assert result is True

        with recorder.use_cassette('test_customer_modify_doesnt_wipe_3', placeholders=placeholders):
            customer = api.customer.retrieve_customer(handle, with_additional_data=True)
            assert customer.additional_data.birth_date == '1980-05-10'

            result = api.customer.delete_customer(handle)
            assert result is True

        with recorder.use_cassette('test_customer_modify_doesnt_wipe_4', placeholders=placeholders):

# Path: tests/api_tests.py
# def betamaxed(original_function):
#     @wraps(original_function)
#     def new_function(api, *args, **kwargs):
#         with Betamax(api.session).use_cassette(original_function.__name__):
#             return original_function(api, *args, **kwargs)
# 
#     return new_function
# 
# Path: openprovider/exceptions.py
# class NoSuchElement(OpenproviderError):
#     """The element you tried to retrieve could not be found."""
# 
# Path: openprovider/models.py
# class Address(Model):
#     """
#     A physical street address.
# 
#     street (required)
#     number (required)
#     suffix (optional)
#     zipcode (required)
#     city (required)
#     state (optional)
#     country (required)
#     """
# 
#     def __eq__(self, other):
#         attributes = ['street', 'number', 'suffix', 'zipcode', 'city', 'state', 'country']
#         return all(getattr(self, attr, None) == getattr(other, attr, None) for attr in attributes)
# 
# class Name(Model):
#     """
#     A person's name.
# 
#     initials (required)
#         Initials (first letters of first names, first letter of last name)
#     firstName (required)
#         First name
#     prefix (optional)
#         Prefix (often occuring in Dutch names; for example van de)
#     lastName (required)
#         Last name
#     """
# 
#     def __eq__(self, other):
#         attributes = ['initials', 'first_name', 'prefix', 'last_name']
#         return all(getattr(self, attr, None) == getattr(other, attr, None) for attr in attributes)
# 
#     def __str__(self):
#         if getattr(self, "prefix", None):
#             return "%s %s %s" % (self.first_name, self.prefix, self.last_name)
#         else:
#             return "%s %s" % (self.first_name, self.last_name)
# 
# class Phone(Model):
#     """
#     An international phone number.
# 
#     country_code (required)
#     area_code (required)
#     subscriber_number (required)
#     """
# 
#     country_code = textattribute("countryCode")
#     area_code = textattribute("areaCode")
#     subscriber_number = textattribute("subscriberNumber")
# 
#     def __eq__(self, other):
#         attributes = ['country_code', 'area_code', 'subscriber_number']
#         return all(getattr(self, attr, None) == getattr(other, attr, None) for attr in attributes)
# 
#     def __str__(self):
#         """Return the string representation of phone number."""
#         return "%s %s %s" % (self.country_code, self.area_code, self.subscriber_number)
# 
# Path: openprovider/util.py
# def parse_phone_number(number):
#     if isinstance(number, basestring):
#         match = PHONE_REGEX.search(number)
#         if not match:
#             raise ValueError("Invalid phone number")
#         return match.groups()
#     elif isinstance(number, (tuple, list)):
#         if len(number) != 3:
#             raise ValueError("Invalid phone number")
#         return number
#     else:
#         raise ValueError("Invalid phone number")


# Path: tests/test_customer.py
import pytest
import random
from . import betamaxed
from betamax import Betamax
from datetime import date
from faker import Factory
from openprovider.exceptions import NoSuchElement
from openprovider.models import Address, Name, Phone
from openprovider.util import parse_phone_number


def test_customer_modify_doesnt_wipe_additional_data(api, name, gender, address, phone, email):
    additional_data = {
            'birth_date': date(1980, 5, 10),
    }

    placeholders = list(_to_placeholders(name=name, gender=gender, address=address, phone=phone, email=email))

    with Betamax(api.session) as recorder:
        with recorder.use_cassette('test_customer_modify_doesnt_wipe_1', placeholders=placeholders):
            handle = api.customer.create_customer(name, gender, address, phone, email,
                                                  additional_data=additional_data)
            assert isinstance(handle, str)

        placeholders.append(_to_placeholder('handle', handle))
        with recorder.use_cassette('test_customer_modify_doesnt_wipe_2', placeholders=placeholders):
            customer = api.customer.retrieve_customer(handle, with_additional_data=True)
            assert customer.additional_data.birth_date == '1980-05-10'

            result = api.customer.modify_customer(handle, address, phone, email)
            assert result is True

        with recorder.use_cassette('test_customer_modify_doesnt_wipe_3', placeholders=placeholders):
            customer = api.customer.retrieve_customer(handle, with_additional_data=True)
            assert customer.additional_data.birth_date == '1980-05-10'

            result = api.customer.delete_customer(handle)
            assert result is True

        with recorder.use_cassette('test_customer_modify_doesnt_wipe_4', placeholders=placeholders):

            with pytest.raises(NoSuchElement):