示例#1
0
    def test_trigger_problem_language(self):
        """Test to check if language in trigger problem page displays correctly"""
        curr_path = os.path.dirname(__file__)
        client = Client()

        zip_trigger_path = os.path.join(curr_path, ParseTest.ZIP_FOLDER,
                                        ParseTest.TRIGGER_OK)
        collection = create_collection('Colleccion de prueba XYZ')
        user = create_user('5555', 'pepe')
        problem = TriggerProblem(zipfile=zip_trigger_path,
                                 collection=collection,
                                 author=user)
        problem.clean()
        problem.save()
        client.login(username='******', password='******')

        client.cookies.load({settings.LANGUAGE_COOKIE_NAME: 'en'})
        url = reverse('judge:problem', args=[problem.pk])
        response = client.get(url, follow=True)
        self.assertIn('Executed statements', response.content.decode('utf-8'))
        self.assertIn('Expected result', response.content.decode('utf-8'))

        client.cookies.load({settings.LANGUAGE_COOKIE_NAME: 'es'})
        url = reverse('judge:problem', args=[problem.pk])
        response = client.get(url, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertIn('Sentencias ejecutadas',
                      response.content.decode('utf-8'))
        self.assertIn('Resultado esperado', response.content.decode('utf-8'))
示例#2
0
    def test_trigger(self):
        """Tests for TriggerProblem.judge()"""
        collection = Collection()
        collection.save()
        create = '''CREATE TABLE Club(
                        CIF CHAR(9) PRIMARY KEY, -- No puede ser NULL
                        Nombre VARCHAR2(40) NOT NULL UNIQUE,
                        Sede VARCHAR2(30) NOT NULL,
                        Num_Socios NUMBER(10,0) NOT NULL,
                        CONSTRAINT NumSociosPositivos CHECK (Num_Socios >= 0)
                    );'''
        insert = """INSERT INTO Club VALUES ('11111111X', 'Real Madrid CF', 'Concha Espina', 70000);
                    INSERT INTO Club VALUES ('11111112X', 'Futbol Club Barcelona', 'Aristides Maillol', 80000);
                    INSERT INTO Club VALUES ('11111113X', 'PSG', 'Rue du Commandant Guilbaud', 1000);"""
        solution = """
            CREATE OR REPLACE TRIGGER IncrementaNumSocios
            BEFORE INSERT OR UPDATE
            ON Club FOR EACH ROW
            DECLARE
                numClubes NUMBER;
            BEGIN
                SELECT 4000 INTO numClubes FROM DUAL;
                :NEW.Num_Socios := :NEW.Num_Socios + numClubes * 100;
            END;"""
        tests = """
            INSERT INTO Club VALUES ('22222222X', 'FALSO', 'Calle', 30000);
            UPDATE Club SET Num_Socios = 70000 WHERE CIF = '11111113X';"""

        # Time-limit
        tle = """
            CREATE OR REPLACE TRIGGER IncrementaNumSocios
            BEFORE INSERT OR UPDATE
            ON Club FOR EACH ROW
            DECLARE
                numClubes NUMBER;
                IN_TIME INT := 11; --num seconds
                v_now DATE;
            BEGIN
                LOOP
                    EXIT WHEN v_now + (IN_TIME * (1/86400)) <= SYSDATE;
                END LOOP;  
                SELECT 4000 INTO numClubes FROM DUAL;
                :NEW.Num_Socios := :NEW.Num_Socios + numClubes * 100;
            END;"""
        # Error in the trigger code
        compile_error = """
            CREATE OR REPLACE TRIGGER IncrementaNumSocios
            BEFORE INSERT OR UPDATE
            ON Club FOR EACH ROW
            DECLARE
                numClubes NUMBER;
            BEGIN
                SELECT 4000 INTO numClubes FROM DUAL;
                :NEW.Num_Socios := :NEW.Num_Socios + numClubes 100; -- missing operator
            END;"""
        # Division by zero
        runtime_error = """
            CREATE OR REPLACE TRIGGER IncrementaNumSocios
            BEFORE INSERT OR UPDATE
            ON Club FOR EACH ROW
            DECLARE
                numClubes NUMBER;
            BEGIN
                SELECT 4000 INTO numClubes FROM DUAL;
                :NEW.Num_Socios := :NEW.Num_Socios + numClubes / 0;
            END;"""
        wrong_answer = """
            CREATE OR REPLACE TRIGGER IncrementaNumSocios
            BEFORE INSERT OR UPDATE
            ON Club FOR EACH ROW
            DECLARE
                numClubes NUMBER;
            BEGIN
                SELECT 4000 INTO numClubes FROM DUAL;
                :NEW.Num_Socios := :NEW.Num_Socios + numClubes * 10000;
            END;"""

        oracle = OracleExecutor.get()
        problem = TriggerProblem(title_md='Test Function',
                                 text_md='bla bla bla',
                                 create_sql=create,
                                 insert_sql=insert,
                                 collection=collection,
                                 author=None,
                                 solution=solution,
                                 tests=tests)
        problem.clean()  # Needed to compute extra HTML fields and solutions
        problem.save()

        # Time-limit
        self.assert_executor_exception(lambda: problem.judge(tle, oracle),
                                       OracleStatusCode.TLE_USER_CODE)

        # Error when compiling user function
        self.assert_executor_exception(
            lambda: problem.judge(compile_error, oracle),
            OracleStatusCode.COMPILATION_ERROR)

        # Error when invoking user function
        self.assert_executor_exception(
            lambda: problem.judge(runtime_error, oracle),
            OracleStatusCode.EXECUTE_USER_CODE)

        # Correct solution
        self.assertEqual(problem.judge(solution, oracle)[0], VeredictCode.AC)

        # Incorrect solution
        self.assertEqual(
            problem.judge(wrong_answer, oracle)[0], VeredictCode.WA)