def testCreateInstance(self):
        test_table = tn('pyodps_t_tmp_create_instance')

        task = SQLTask(query='drop table if exists %s' % test_table)
        instance = self.odps._project.instances.create(task=task)
        instance.wait_for_completion()
        self.assertTrue(instance.is_successful())
        self.assertFalse(self.odps.exist_table(test_table))

        task = SQLTask(query='create table %s(id string);' % test_table)
        instance = self.odps._project.instances.create(task=task)
        instance.wait_for_completion()
        self.assertTrue(instance.is_successful())
        self.assertTrue(self.odps.exist_table(test_table))

        instance = self.odps.execute_sql('drop table %s' % test_table)
        self.assertTrue(instance.is_successful())
        self.assertFalse(self.odps.exist_table(test_table))

        tasks = instance.get_tasks()
        self.assertTrue(any(map(lambda task: isinstance(task, SQLTask),
                                tasks)))

        for name in instance.get_task_names():
            self.assertIsNotNone(instance.get_task_detail(name))
            self.assertIsNotNone(instance.get_task_detail2(name))

        # test stop
        self.assertRaises(errors.InvalidStateSetting, instance.stop)
    def testSQLTaskToXML(self):
        query = 'select * from dual'

        task = SQLTask(query=query)
        to_xml = task.serialize()
        right_xml = template % {'sql': query}

        self.assertEqual(to_str(to_xml), to_str(right_xml))

        task = Task.parse(None, to_xml)
        self.assertIsInstance(task, SQLTask)
    def testSQLTaskToXML(self):
        query = 'select * from dual'

        task = SQLTask(query=query)
        to_xml = task.serialize()
        right_xml = template % {'sql': query}

        self.assertEqual(to_str(to_xml), to_str(right_xml))

        task = Task.parse(None, to_xml)
        self.assertIsInstance(task, SQLTask)
    def testCreateInstance(self):
        test_table = 'pyodps_t_tmp_create_instance'

        task = SQLTask(query='drop table if exists %s' % test_table)
        instance = self.odps._project.instances.create(task=task)
        instance.wait_for_completion()
        self.assertTrue(instance.is_successful())
        self.assertFalse(self.odps.exist_table(test_table))

        task = SQLTask(query='create table %s(id string);' % test_table)
        instance = self.odps._project.instances.create(task=task)
        instance.wait_for_completion()
        self.assertTrue(instance.is_successful())
        self.assertTrue(self.odps.exist_table(test_table))

        instance = self.odps.execute_sql('drop table %s' % test_table)
        self.assertTrue(instance.is_successful())
        self.assertFalse(self.odps.exist_table(test_table))

        # test stop
        self.assertRaises(errors.InvalidStateSetting, instance.stop)
    def testCreateInstanceXML(self):
        instances = self.odps._project.instances

        uuid = '359696d4-ac73-4e6c-86d1-6649b01f1a22'
        query = 'select * from dual;'
        priority = 5

        task = SQLTask(query=query)
        job = instances._create_job(task=task, priority=priority, uuid_=uuid)
        xml = instances._get_submit_instance_content(job)
        expected_xml = expected_xml_template % {
            'query': query,
            'uuid': uuid,
            'priority': priority
        }
        self.assertEqual(to_str(xml), to_str(expected_xml))
    def testSQLTaskToXMLTimezone(self):
        from odps.lib import tzlocal
        query = 'select * from dual'

        try:
            options.local_timezone = True
            local_zone_name = tzlocal.get_localzone().zone
            task = SQLTask(query=query)
            task.update_sql_settings()
            to_xml = task.serialize()
            right_xml = sql_tz_template % {'sql': query, 'tz': local_zone_name}

            self.assertEqual(to_str(to_xml), to_str(right_xml))

            options.local_timezone = False
            task = SQLTask(query=query)
            task.update_sql_settings()
            to_xml = task.serialize()
            right_xml = sql_tz_template % {'sql': query, 'tz': 'Etc/GMT'}

            self.assertEqual(to_str(to_xml), to_str(right_xml))

            options.local_timezone = pytz.timezone('Asia/Shanghai')
            task = SQLTask(query=query)
            task.update_sql_settings()
            to_xml = task.serialize()
            right_xml = sql_tz_template % {
                'sql': query,
                'tz': options.local_timezone.zone
            }

            self.assertEqual(to_str(to_xml), to_str(right_xml))
        finally:
            options.local_timezone = None