class task_factory_test_1(unittest.TestCase):
    
    def setUp(self):
        self.app_tt_select = Apptt_select(short_name="sh_tt1",title="title1")
    
    def tearDown(self):
        if not self.app_tt_select == None:
            pbclient.delete_app(self.app_tt_select.app_id)
    
    # testing functions

    def test_get_task_01(self):
        self.app_tt_select.add_task(task_info={"answer":"Yes", "page":1})
        self.app_tt_select.add_task(task_info={"answer":"Yes", "page":2})
        self.app_tt_select.add_task(task_info={"answer":"No", "page":3})
        
        tasks = pbclient.get_tasks(self.app_tt_select.app_id)
        
        try:
            for task in tasks:
                t2 = task_factory.get_task(task.id)
                if task.id == t2.task.id:
                    self.assertEquals(t2.task.info["page"], task.info["page"])
                    self.assertEquals(t2.task.info["answer"], task.info["answer"])
                    self.assertEquals(t2.app_short_name, self.app_tt_select.short_name)
        
        except Exception as e:
            print e
            assert False

    def test_get_task_02(self):
        try:
            task_factory.get_task(-1)
        except Meb_task_factory_exception as e:
            self.assertEquals(e.code, 1)
def create_apps(book_id):
    """
    Celery queued task that creates tt_apps and tt1 tasks

    :arg book_id: Internet archive book id
    :returns: book indicating if the applications were created
    :rtype: bool

    """

    try:
        imgs = meb_util.get_tt_images(book_id)
        bookInfo = meb_util.get_archive_book_data(book_id)
        
        app_tt_select = Apptt_select(short_name=book_id + "_tt1", title=bookInfo['title'], book_info=bookInfo)
        app_tt_meta = Apptt_meta(short_name=book_id + "_tt2", title=bookInfo['title'])
        app_tt_struct = Apptt_struct(short_name=book_id + "_tt3", title=bookInfo['title'])
        app_tt_transcribe = Apptt_transcribe(short_name=book_id + "_tt4", title=bookInfo['title'])
        
        app_tt_select.add_app_infos(bookInfo)
        app_tt_meta.add_app_infos(bookInfo)
        app_tt_struct.add_app_infos(bookInfo)
        app_tt_transcribe.add_app_infos(bookInfo)
        
        if len(app_tt_select.get_tasks()) == 0:
            for img in imgs:
                task = app_tt_select.add_task(img)
                workflow_transaction_info = dict(task_id_1=task.id, task_id_2=None, task_id_3=None, task_id_4=None)
                data_mngr.record_workflow_transaction(workflow_transaction_info)

        return True

    except Exception as e:
        return False
    def setUp(self):
        bookinfo = dict(
            bookid="sh",
            title="title1",
            contributor="cont1",
            publisher="pub1",
            volume="1",
            img="http://archive.org/download/livro1/n1",
        )

        self.app = Apptt_select(short_name="sh_tt1", title="title1", book_info=bookinfo)
        self.app.add_task(
            task_info=dict(
                url_m="http://archive.org/download/livro1/n1", url_b="http://archive.org/download/livro1/n1", page=1
            )
        )
        self.app.add_task(
            task_info=dict(
                url_m="http://archive.org/download/livro1/n2", url_b="http://archive.org/download/livro1/n2", page=2
            )
        )

        tasks = pbclient.get_tasks(app_id=self.app.app_id)

        self.task1 = TTTask1(tasks[0].id, app_short_name=self.app.short_name)
        self.task2 = TTTask1(tasks[1].id, app_short_name=self.app.short_name)

        self.base_url = app_flask.config["PYBOSSA_URL"]
        self.fb_user = authenticate_fb_user(self.base_url)
def close_t1(book_id):
    """
    Celery queued task that set's pybossa task type 1 (selection) 
    state to completed and close it. This function is is useful into case 
    that the book has only pages with tables.

    :arg task_id: Integer pybossa task id

    """
    
    tt_select = Apptt_select(short_name=book_id + "_tt1")
    tasks = tt_select.get_tasks()
    
    for task in tasks:
        tt_task = task_factory.get_task(task.id)
        tt_task.special_close_task()
        tt_task.add_next_task()
 def setUp(self):
     self.app = Apptt_select(short_name="sh_tt1", title="title1")
     self.app.add_task(task_info={"info1":1, "info2":2})
     self.app.add_task(task_info={"info3":3, "info4":4})
     
     tasks = pbclient.get_tasks(app_id=self.app.app_id)
     
     self.pb_task1 = pb_task(tasks[0].id, app_short_name=self.app.short_name)
     self.pb_task2 = pb_task(tasks[1].id, app_short_name=self.app.short_name)
class pb_task_test(unittest.TestCase):
    
    def setUp(self):
        self.app = Apptt_select(short_name="sh_tt1", title="title1")
        self.app.add_task(task_info={"info1":1, "info2":2})
        self.app.add_task(task_info={"info3":3, "info4":4})
        
        tasks = pbclient.get_tasks(app_id=self.app.app_id)
        
        self.pb_task1 = pb_task(tasks[0].id, app_short_name=self.app.short_name)
        self.pb_task2 = pb_task(tasks[1].id, app_short_name=self.app.short_name)

    def tearDown(self):
        pbclient.delete_task(self.pb_task1.task.id)
        pbclient.delete_task(self.pb_task2.task.id)
        pbclient.delete_app(self.app.app_id)
    
    # testing functions

    def test_init_01(self):
        try:
            t1 = pb_task( -1, "sh_tt1")
        except Meb_pb_task_exception as e:
            self.assertEquals(e.code, 1)
            self.assertEquals(e.msg, "MEB-PB-TASK-1: Cannot find task | task_id : -1 | app_short_name : sh_tt1")
    
    def test_init_02(self):
        try:
            t1 = pb_task( self.pb_task1.task.id, "sh")
        except Meb_pb_task_exception as e:
            self.assertEquals(e.code, 2)
            msg = "MEB-PB-TASK-2: Invalid shortname | task_id : %d | app_short_name : %s" % (self.pb_task1.task.id, "sh")
            self.assertEquals(e.msg, msg)
    
    def test_init_03(self):
        try:
            t1 = pb_task( self.pb_task1.task.id, "sh_tt")
        except Meb_pb_task_exception as e:
            self.assertEquals(e.code, 2)
            msg = "MEB-PB-TASK-2: Invalid shortname | task_id : %d | app_short_name : %s" % (self.pb_task1.task.id, "sh_tt")
            self.assertEquals(e.msg, msg)
    
    def test_get_book_id_01(self):
        try:
            self.assertEquals(self.pb_task1.app_short_name, self.app.short_name)
            self.assertEquals(self.pb_task1.get_book_id(), "sh")
        except Exception as e:
            assert False
    
    def test_close_task_01(self):
        try:
            self.pb_task1.close_task()
            self.assertEquals(self.pb_task1.task.state, 'completed')
        except Exception:
            assert False
    
    def test_get_task_runs_01(self):
        try:
            trs = self.pb_task1.get_task_runs()
            self.assertEquals(len(trs), 0)
            self.assertFalse(self.pb_task1.task.info.has_key('answer'))
            self.assertFalse(self.pb_task2.task.info.has_key('answer'))
        except Exception:
            assert False
class tt1_task_test(unittest.TestCase):
    def setUp(self):
        bookinfo = dict(
            bookid="sh",
            title="title1",
            contributor="cont1",
            publisher="pub1",
            volume="1",
            img="http://archive.org/download/livro1/n1",
        )

        self.app = Apptt_select(short_name="sh_tt1", title="title1", book_info=bookinfo)
        self.app.add_task(
            task_info=dict(
                url_m="http://archive.org/download/livro1/n1", url_b="http://archive.org/download/livro1/n1", page=1
            )
        )
        self.app.add_task(
            task_info=dict(
                url_m="http://archive.org/download/livro1/n2", url_b="http://archive.org/download/livro1/n2", page=2
            )
        )

        tasks = pbclient.get_tasks(app_id=self.app.app_id)

        self.task1 = TTTask1(tasks[0].id, app_short_name=self.app.short_name)
        self.task2 = TTTask1(tasks[1].id, app_short_name=self.app.short_name)

        self.base_url = app_flask.config["PYBOSSA_URL"]
        self.fb_user = authenticate_fb_user(self.base_url)

    def tearDown(self):
        next_app = None
        next_app_list = pbclient.find_app(short_name=self.app.short_name[:-1] + "2")

        if len(next_app_list) > 0:
            next_app = next_app_list[0]

            next_task = None
            tasks = pbclient.get_tasks(next_app.id)
            for t in tasks:
                if t.info["page"] == self.task1.task.info["page"]:
                    pbclient.delete_task(task_id=t.id)

            pbclient.delete_app(app_id=next_app.id)

        pbclient.delete_task(self.task1.task.id)
        pbclient.delete_task(self.task2.task.id)
        pbclient.delete_app(self.app.app_id)

        data_mngr.delete_book(self.task1.get_book_id())

    # testing functions

    def test_init_01(self):
        try:
            t1 = TTTask1(-1, "sh_tt1")
        except Meb_pb_task_exception as e:
            self.assertEquals(e.code, 1)

    def test_init_02(self):
        try:
            t1 = TTTask1(self.app.app_id, "sh_t")
        except Meb_pb_task_exception as e:
            self.assertEquals(e.code, 2)

    def test_init_03(self):
        try:
            book_mb = data_mngr.get_book(self.task1.get_book_id())

            self.assertEquals(book_mb.id, "sh")
            self.assertEquals(book_mb.title, "title1")
            self.assertEquals(book_mb.publisher, "pub1")
            self.assertEquals(book_mb.contributor, "cont1")
            self.assertEquals(book_mb.volume, "1")
            self.assertEquals(book_mb.img_url, "http://archive.org/download/livro1/n1")

        except Exception as e:
            print e
            assert False

    def test_get_next_app_01(self):
        try:
            nx_app = self.task1.get_next_app()
            self.assertEquals(nx_app.short_name, self.app.short_name[:-1] + "2")
        except Exception:
            assert False

    def test_check_answer_01(self):
        try:
            task_run1 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="Yes")
            task_run2 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="Yes")

            # Anonymous submission
            submit_answer(self.base_url, task_run1)

            # FB authenticated user submission
            task_run2["facebook_user_id"] = "12345"
            submit_answer(self.base_url, task_run2)

            time.sleep(2)

            trs = self.task1.get_task_runs()

            self.assertEquals(trs[0].info, "Yes")
            self.assertEquals(trs[1].info, "Yes")
            self.assertTrue(self.task1.check_answer())

        except Exception as e:
            print e
            assert False

    def test_check_answer_02(self):
        try:
            task_run1 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="Yes")
            task_run2 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="No")

            # Anonymous submission
            submit_answer(self.base_url, task_run1)

            # FB authenticated user submission
            task_run2["facebook_user_id"] = "12345"
            submit_answer(self.base_url, task_run2)

            time.sleep(2)

            self.assertFalse(self.task1.check_answer())

            trs = self.task1.get_task_runs()

            self.assertEquals(trs[0].info, "Yes")
            self.assertEquals(trs[1].info, "No")

        except Exception as e:
            print e
            assert False

    def test_check_answer_03(self):
        try:
            task_run1 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="BLA")
            task_run2 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="No")

            # Anonymous submission
            submit_answer(self.base_url, task_run1)

            # FB authenticated user submission
            task_run2["facebook_user_id"] = "12345"
            submit_answer(self.base_url, task_run2)

            time.sleep(2)

            self.task1.check_answer()

        except Meb_exception_tt1 as e:
            self.assertEquals(e.code, 2)

    def test_add_next_task_01(self):
        try:
            task_run1 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="Yes")
            task_run2 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="Yes")

            # Anonymous submission
            submit_answer(self.base_url, task_run1)

            # FB authenticated user submission
            task_run2["facebook_user_id"] = "12345"
            submit_answer(self.base_url, task_run2)

            time.sleep(2)

            self.assertTrue(self.task1.check_answer())
            self.assertTrue(self.task1.add_next_task())

        except Exception as e:
            print e
            assert False

    def test_add_next_task_02(self):
        try:
            task_run1 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="Yes")
            task_run2 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="No")

            # Anonymous submission
            submit_answer(self.base_url, task_run1)

            # FB authenticated user submission
            task_run2["facebook_user_id"] = "12345"
            submit_answer(self.base_url, task_run2)

            time.sleep(2)

            self.assertFalse(self.task1.check_answer())
            self.assertFalse(self.task1.add_next_task())

        except Meb_exception_tt1 as e:
            self.assertEquals(e.code, 1)

    def test_add_next_task_03(self):
        try:
            task_run1 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="Yes")
            task_run2 = dict(app_id=self.app.app_id, task_id=self.task1.task.id, info="Yes")

            # Anonymous submission
            submit_answer(self.base_url, task_run1)

            # FB authenticated user submission
            task_run2["facebook_user_id"] = "12345"
            submit_answer(self.base_url, task_run2)

            time.sleep(2)

            self.assertTrue(self.task1.check_answer())
            self.assertTrue(self.task1.add_next_task())

            self.task1.add_next_task()

            assert False

        except Meb_exception_tt1 as e:
            self.assertEquals(e.code, 3)
 def setUp(self):
     self.app_tt_select = Apptt_select(short_name="sh_tt1",title="title1")