# -*- coding: utf-8 -*-
from dace.util import getAllBusinessAction
import transaction

from novaideo.content.idea import Idea
from novaideo.graphql.schema import schema
from novaideo.testing import FunctionalTests


def create_idea(request, title, text, keywords):
    """Create an Idea."""
    container = request.root
    idea = Idea(
        title=title,
        text=text,
        keywords=keywords
    )
    actions = getAllBusinessAction(
        container, request, process_id='ideamanagement',
        node_id='creatandpublish')
    create_action = actions[0]
    create_action.execute(
        container, request, {'_object_data': idea})


class TestGraphQLSchema(FunctionalTests):

    def setUp(self):
        super(TestGraphQLSchema, self).setUp()
        self.default_novaideo_config()
        params = {
            'title': 'My great idea',
            'text': 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
            'keywords': ['foo', 'bar']
        }
        create_idea(self.request, **params)
        params['title'] = 'My other idea'
        create_idea(self.request, **params)
        transaction.commit()

    def test_get_id_for_all_ideas(self):
        query = '{ ideas { edges { node { id, title } } } }'
        result = schema.execute(query)
        self.assertIsNone(result.errors)
        edges = result.data['ideas']['edges']
        self.assertEqual(len(edges), 2)
        self.assertEqual(edges[0]['node']['title'], 'My great idea')
        self.assertEqual(edges[1]['node']['title'], 'My other idea')

@@ -1418,52 +1418,74 @@
     
-self.assertEqual(edges%5B0%5D%5B'node'%5D%5B'title'%5D, 
+ideas = %5Be%5B'node'%5D%5B'title'%5D for e in edges%5D%0A        self.assertIn(
 'My 
@@ -1495,16 +1495,23 @@
 at idea'
+, ideas
 )%0A      
@@ -1527,41 +1527,11 @@
 sert
-Equal(edges%5B1%5D%5B'node'%5D%5B'title'%5D, 
+In(
 'My 
@@ -1541,10 +1541,17 @@
 er idea'
+, ideas
 )%0A

fix test