Benjamin-eecs/openrsi-commit-runtime-assets
0168
1#!/bin/bash2set -uxo pipefail3SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"4cd /workspace5git config --global --add safe.directory /workspace6mkdir -p /logs/verifier7git checkout f953dff5c77b5eeb96046aef4a29fd9d097085c3 -- tests/test_core.py || true8git apply --verbose --reject - <<'EOF_R2E_TEST_PATCH'9diff --git a/tests/test_core.py b/tests/test_core.py10index 1219659..48b8ca6 10064411--- a/tests/test_core.py12+++ b/tests/test_core.py13@@ -1,4 +1,5 @@14 import datetime15+import typing16 from unittest import mock17 18 import pytest19@@ -37,6 +38,9 @@ class MockRequestParser(Parser):20 def load_querystring(self, req, schema):21 return self._makeproxy(req.query, schema)22 23+ def load_form(self, req, schema):24+ return MultiDictProxy(req.form, schema)25+26 def load_json(self, req, schema):27 return req.json28 29@@ -1224,3 +1228,84 @@ def test_custom_default_schema_class(load_json, web_request):30 p = CustomParser()31 ret = p.parse(argmap, web_request)32 assert ret == {"value": "hello world"}33+34+35+def test_parser_pre_load(web_request):36+ class CustomParser(MockRequestParser):37+ # pre-load hook to strip whitespace from query params38+ def pre_load(self, data, *, schema, req, location):39+ if location == "query":40+ return {k: v.strip() for k, v in data.items()}41+ return data42+43+ parser = CustomParser()44+45+ # mock data for both query and json46+ web_request.query = web_request.json = {"value": " hello "}47+ argmap = {"value": fields.Str()}48+49+ # data gets through for 'json' just fine50+ ret = parser.parse(argmap, web_request)51+ assert ret == {"value": " hello "}52+53+ # but for 'query', the pre_load hook changes things54+ ret = parser.parse(argmap, web_request, location="query")55+ assert ret == {"value": "hello"}56+57+58+# this test is meant to be a run of the WhitspaceStrippingFlaskParser we give59+# in the docs/advanced.rst examples for how to use pre_load60+# this helps ensure that the example code is correct61+# rather than a FlaskParser, we're working with the mock parser, but it's62+# otherwise the same63+def test_whitespace_stripping_parser_example(web_request):64+ def _strip_whitespace(value):65+ if isinstance(value, str):66+ value = value.strip()67+ elif isinstance(value, typing.Mapping):68+ return {k: _strip_whitespace(value[k]) for k in value}69+ elif isinstance(value, (list, tuple)):70+ return type(value)(map(_strip_whitespace, value))71+ return value72+73+ class WhitspaceStrippingParser(MockRequestParser):74+ def pre_load(self, location_data, *, schema, req, location):75+ if location in ("query", "form"):76+ ret = _strip_whitespace(location_data)77+ return ret78+ return location_data79+80+ parser = WhitspaceStrippingParser()81+82+ # mock data for query, form, and json83+ web_request.form = web_request.query = web_request.json = {"value": " hello "}84+ argmap = {"value": fields.Str()}85+86+ # data gets through for 'json' just fine87+ ret = parser.parse(argmap, web_request)88+ assert ret == {"value": " hello "}89+90+ # but for 'query' and 'form', the pre_load hook changes things91+ for loc in ("query", "form"):92+ ret = parser.parse(argmap, web_request, location=loc)93+ assert ret == {"value": "hello"}94+95+ # check that it applies in the case where the field is a list type96+ # applied to an argument (logic for `tuple` is effectively the same)97+ web_request.form = web_request.query = web_request.json = {98+ "ids": [" 1", "3", " 4"],99+ "values": [" foo ", " bar"],100+ }101+ schema = Schema.from_dict(102+ {"ids": fields.List(fields.Int), "values": fields.List(fields.Str)}103+ )104+ for loc in ("query", "form"):105+ ret = parser.parse(schema, web_request, location=loc)106+ assert ret == {"ids": [1, 3, 4], "values": ["foo", "bar"]}107+108+ # json loading should also work even though the pre_load hook above109+ # doesn't strip whitespace from JSON data110+ # - values=[" foo ", ...] will have whitespace preserved111+ # - ids=[" 1", ...] will still parse okay because " 1" is valid for fields.Int112+ ret = parser.parse(schema, web_request, location="json")113+ assert ret == {"ids": [1, 3, 4], "values": [" foo ", " bar"]}114 115EOF_R2E_TEST_PATCH116R2E_APPLY_RC=$?117if [ "$R2E_APPLY_RC" -ne 0 ]; then118 echo "0.000000" > /logs/verifier/reward.txt119 printf '%s' '{"reward": 0.0, "resolved": false, "parse_status": "test_patch_apply_failed"}' > /logs/verifier/reward-details.json120 echo "R2E: test_patch failed to apply (rc=$R2E_APPLY_RC) — failing closed" >&2121 exit 0122fi123( python -m pytest -v tests/test_core.py ) > /logs/verifier/test_output.log 2>&1124TEST_EXIT_CODE=$?125cat /logs/verifier/test_output.log126python3 "$SCRIPT_DIR/verifier.py" --log /logs/verifier/test_output.log --f2p "$SCRIPT_DIR/f2p.json" --p2p "$SCRIPT_DIR/p2p.json" --test-cmds 'python -m pytest -v tests/test_core.py' --exit-code "$TEST_EXIT_CODE" --out-dir /logs/verifier || { [ "$TEST_EXIT_CODE" -eq 0 ] && echo "1.0" > /logs/verifier/reward.txt || echo "0.0" > /logs/verifier/reward.txt; }127git checkout f953dff5c77b5eeb96046aef4a29fd9d097085c3 -- tests/test_core.py || true128exit 0129 