# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.test import TestCase
from django.core.management import call_command

from cities.models import Country, Region, Subregion, City, PostalCode
from cities.util import unicode_func


class ManageCommandTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        # Run the import command only once
        super(ManageCommandTestCase, cls).setUpClass()
        call_command('cities', force=True, **{
            'import': 'country,region,subregion,city,postal_code',
        })

    def test_no_invalid_slugs(self):
        self.assertEqual(Country.objects.filter(slug__startswith='invalid').count(), 0)
        self.assertEqual(Region.objects.filter(slug__startswith='invalid').count(), 0)
        self.assertEqual(Subregion.objects.filter(slug__startswith='invalid').count(), 0)
        self.assertEqual(City.objects.filter(slug__startswith='invalid').count(), 0)
        self.assertEqual(PostalCode.objects.filter(slug__startswith='invalid').count(), 0)

    def test_imported_all_countries(self):
        self.assertEqual(Country.objects.all().count(), 250)

    def test_imported_ad_regions(self):
        self.assertEqual(Region.objects.filter(country__code='AD').count(), 7)

    def test_imported_ua_regions(self):
        self.assertEqual(Region.objects.filter(country__code='UA').count(), 27)

    def test_imported_ua_cities(self):
        self.assertEqual(City.objects.filter(region__country__code='UA').count(), 1499)

    def test_postal_code_slugs(self):
        self.assertEqual(PostalCode.objects.get(country__code='RU', code='102104').code, '102104')
        self.assertTrue(len(PostalCode.objects.get(country__code='RU', code='102104').slug) <= 255)
        self.assertEqual(PostalCode.objects.get(country__code='RU', code='102104').slug, unicode_func('1-102104'))

@@ -553,16 +553,299 @@
       %7D)
+%0A        cls.counts = %7B%0A            'countries': Country.objects.count(),%0A            'regions': Region.objects.count(),%0A            'subregions': Subregion.objects.count(),%0A            'cities': City.objects.count(),%0A            'postal_codes': PostalCode.objects.count(),%0A        %7D
 %0A%0A    de
@@ -2143,8 +2143,546 @@
 2104'))%0A
+%0A    def test_idempotence(self):%0A        call_command('cities', force=True, **%7B%0A            'import': 'country,region,subregion,city,postal_code',%0A        %7D)%0A        self.assertEqual(Country.objects.count(), self.counts%5B'countries'%5D)%0A        self.assertEqual(Region.objects.count(), self.counts%5B'regions'%5D)%0A        self.assertEqual(Subregion.objects.count(), self.counts%5B'subregions'%5D)%0A        self.assertEqual(City.objects.count(), self.counts%5B'cities'%5D)%0A        self.assertEqual(PostalCode.objects.count(), self.counts%5B'postal_codes'%5D)%0A

Add test for idempotence