from __future__ import unicode_literals

import csv
from mock import Mock
try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

from django.contrib.sites.models import Site
from django.core.files import File

from saleor.discount.models import Sale
from saleor.product.models import Category
from saleor.data_feeds.google_merchant import (get_feed_items,
                                               item_attributes,
                                               item_google_product_category,
                                               update_feed)


def test_saleor_feed_items(product_in_stock):
    valid_variant = product_in_stock.variants.first()
    items = get_feed_items()
    assert len(items) == 1
    categories = Category.objects.all()
    discounts = []
    category_paths = {}
    current_site = Site.objects.get_current()
    attributes = item_attributes(items[0], categories, category_paths,
                                 current_site, discounts)
    assert attributes.get('mpn') == valid_variant.sku
    assert attributes.get('availability') == 'in stock'


def test_category_formatter(db):
    main_category = Category(name='Main', slug='main')
    main_category.save()
    main_category_item = Mock(
        product=Mock(get_first_category=lambda: main_category))
    sub_category = Category(name='Sub', slug='sub', parent=main_category)
    sub_category.save()
    sub_category_item = Mock(
        product=Mock(get_first_category=lambda: sub_category))
    assert item_google_product_category(
        main_category_item, {}) == 'Main'
    assert item_google_product_category(
        sub_category_item, {}) == 'Main > Sub'


def test_feed_updater(product_in_stock, monkeypatch):
    variant = product_in_stock.variants.first()
    buffer = StringIO()
    fake_file = Mock(spec=File,
                     __enter__=lambda x: buffer,
                     __exit__=lambda x, y, z, a: None)
    monkeypatch.setattr(
        'saleor.data_feeds.google_merchant.default_storage.open',
        lambda path, mode: fake_file)
    monkeypatch.setattr(
        'saleor.data_feeds.google_merchant.COMPRESSION', False)
    update_feed()
    buffer.seek(0)
    categories = Category.objects.all()
    discounts = Sale.objects.all().prefetch_related('products',
                                                    'categories')
    category_paths = {}
    current_site = Site.objects.get_current()
    reader = csv.DictReader(buffer, dialect=csv.excel_tab)
    for generated_item in reader:
        attributes = item_attributes(variant, categories, category_paths,
                                     current_site, discounts)
        for key in attributes.keys():
            assert attributes[key] == generated_item[key]

@@ -71,175 +71,162 @@
 ock%0A
-try:%0A    from StringIO import StringIO%0Aexcept ImportError:%0A    from io import StringIO%0A%0Afrom django.contrib.sites.models import Site%0Afrom django.core.files import File
+%0Afrom django.contrib.sites.models import Site%0Afrom django.utils import six%0Aif six.PY3:%0A    from io import StringIO%0Aelse:%0A    from StringIO import StringIO
 %0A%0Afr
@@ -553,28 +553,27 @@
             
-upda
+wri
 te_feed)%0A%0A%0Ad
@@ -1682,20 +1682,18 @@
 est_
-feed_updater
+write_feed
 (pro
@@ -1801,393 +1801,99 @@
     
-fake_file = Mock(spec=File,%0A                     __enter__=lambda x: buffer,%0A                     __exit__=lambda x, y, z, a: None)%0A    monkeypatch.setattr(%0A        'saleor.data_feeds.google_merchant.default_storage.open',%0A        lambda path, mode: fake_file)%0A    monkeypatch.setattr(%0A        'saleor.data_feeds.google_merchant.COMPRESSION', False)%0A    update_feed()%0A    buffer.seek(0
+write_feed(buffer)%0A    buffer.seek(0)%0A    assert csv.Sniffer().has_header(buffer.getvalue()
 )%0A  

Update test