# encoding: utf-8
from __future__ import unicode_literals

import urllib

import i18naddress
import pytest
from django.http import QueryDict

from saleor.userprofile import forms, models, i18n

@pytest.fixture
def billing_address(db):
    return models.Address.objects.create(
        first_name='John', last_name='Doe',
        company_name='Mirumee Software',
        street_address_1='Tęczowa 7',
        city='Wrocław',
        postal_code='53-601',
        country='PL')


@pytest.mark.parametrize('country', ['CN', 'PL', 'US'])
def test_address_form_for_country(country):
    data = {
        'first_name': 'John',
        'last_name': 'Doe',
        'country': country}
    form = forms.get_address_form(data, country_code=country)[0]
    errors = form.errors
    rules = i18naddress.get_validation_rules({'country_code': country})
    required = rules.required_fields
    if 'street_address' in required:
        assert 'street_address_1' in errors
    else:
        assert 'street_address_1' not in errors
    if 'city' in required:
        assert 'city' in errors
    else:
        assert 'city' not in errors
    if 'city_area' in required:
        assert 'city_area' in errors
    else:
        assert 'city_area' not in errors
    if 'country_area' in required:
        assert 'country_area' in errors
    else:
        assert 'country_area' not in errors
    if 'postal_code' in required:
        assert 'postal_code' in errors
    else:
        assert 'postal_code' not in errors


def test_address_form_postal_code_validation():
    data = {
        'first_name': 'John',
        'last_name': 'Doe',
        'country': 'PL',
        'postal_code': 'XXX'}
    form = forms.get_address_form(data, country_code='PL')[0]
    errors = form.errors
    assert 'postal_code' in errors

@pytest.mark.parametrize('form_data, form_valid, expected_preview, expected_country',[
    ({'preview': True}, False, True, 'PL'),
    ({'preview': False,
      'street_address_1': 'Foo bar',
      'postal_code': '00-123',
      'city': 'Warsaw'
      }, True, False, 'PL'),
    ({'preview': True, 'country': 'US'}, False, True, 'US'),
    ({'preview': False,
      'street_address_1': 'Foo bar',
      'postal_code': '0213',
      'city': 'Warsaw'
      }, False, False, 'PL'),
])
def test_get_address_form(form_data, form_valid, expected_preview, expected_country):
    data = {
        'first_name': 'John',
        'last_name': 'Doe',
        'country': 'PL'}
    data.update(form_data)
    query_dict = urllib.urlencode(data)
    form, preview = forms.get_address_form(
        data=QueryDict(query_dict), country_code=data['country'])
    assert preview is expected_preview
    assert form.is_valid() is form_valid
    assert form.i18n_country_code == expected_country


def test_country_aware_form_has_only_supported_countries():

    default_form = i18n.COUNTRY_FORMS['US']
    instance = default_form()
    country_field = instance.fields['country']
    country_choices = [code for code, label in country_field.choices]

    for country in i18n.UNKNOWN_COUNTRIES:
        assert country not in i18n.COUNTRY_FORMS
        assert country not in country_choices

@@ -56,21 +56,134 @@
 ls%0A%0A
-import urllib
+try:%0A    # Python 3%0A    from urllib.parse import urlencode%0Aexcept ImportError:%0A    # Python 2%0A    from urllib import urlencode
 %0A%0Aim
@@ -2608,23 +2608,16 @@
 _dict = 
-urllib.
 urlencod

Fix python 3.x compatibility in tests