# pylint: disable=invalid-name, too-many-arguments
import transaction
from .common import check_if_widget_itmes_present


def test_on_similar_posts_widget_user_sees_ordered_published_items(
        browser, web_server, site, navigator, fakefactory, dbsession):

    shown_items_number = 10
    tags_items_number = shown_items_number + 5

    with transaction.manager:

        tags = fakefactory.TagFactory.create_batch(size=tags_items_number)
        post = fakefactory.PostFactory(tags=tags)
        similar_posts = [
            fakefactory.PostFactory(tags=tags[:i])
            for i in range(tags_items_number, 0, -1)]
        fakefactory.PostFactory.create_batch(size=tags_items_number * 10)  # noise elements
        dbsession.expunge_all()

    navigator().navigate(site)
    # XXX: redesign url making func, currently is inflexible
    browser.visit(web_server + '/programming/' + post.slug)

    check_if_widget_itmes_present(
        '.similar', browser, similar_posts[:shown_items_number], web_server)


def test_on_similar_posts_widget_user_doesnt_see_not_published_posts(
        web_server, browser, site, navigator, fakefactory, dbsession):

    with transaction.manager:
        tags = fakefactory.TagFactory.create_batch(size=2)
        post = fakefactory.PostFactory(tags=tags)
        similar_post = fakefactory.PostFactory(tags=tags)
        fakefactory.PostFactory.create_batch(2, state='draft', tags=tags)
        dbsession.expunge_all()

    navigator().navigate(site)
    # XXX: redesign url making func, currently is inflexible
    browser.visit(web_server + '/programming/' + post.slug)

    assert len(browser.find_by_css('.similar .list-group-item')) == 1
    assert browser.find_by_css('.similar .list-group-item-heading').text == similar_post.title

@@ -63,16 +63,30 @@
 saction%0A
+import random%0A
 from .co
@@ -344,11 +344,11 @@
 ber 
-+ 5
+* 3
 %0A%0A  
@@ -497,32 +497,33 @@
 (tags=tags)%0A
+%0A
         
 similar_post
@@ -514,222 +514,251 @@
     
-similar_posts = %5B%0A            fakefactory.PostFactory(tags=tags%5B:i%5D)%0A            for i in range(tags_items_number, 0, -1)%5D%0A        fakefactory.PostFactory.create_batch(size=tags_items_number * 10)  # noise elements
+tags_sets = %5Btags%5Bi:%5D for i, _ in enumerate(tags)%5D%0A        random.shuffle(tags_sets)%0A        similar_posts = %5Bfakefactory.PostFactory(tags=tag_sets) for tag_sets in tags_sets%5D%0A        similar_posts.sort(key=lambda i: len(i.tags), reverse=True)
 %0A   

fix similar items order test