From 4d976d4dc334c621d3d7094d74425bdbe52ff3aa Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Thu, 12 Nov 2026 09:48:18 +1100 Subject: [PATCH] Ignore non-member submission identifiers in statistics reports --- .../design/first-time-design-submitters.sql | 5 +- .../first-time-development-submitters.sql | 5 +- test/statistics/README.md | 19 ++++++ test/statistics/test_first_submitters.py | 61 +++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 test/statistics/README.md create mode 100644 test/statistics/test_first_submitters.py diff --git a/sql/reports/statistics/design/first-time-design-submitters.sql b/sql/reports/statistics/design/first-time-design-submitters.sql index a411d60..c49d297 100644 --- a/sql/reports/statistics/design/first-time-design-submitters.sql +++ b/sql/reports/statistics/design/first-time-design-submitters.sql @@ -1,6 +1,9 @@ WITH development_submissions_ranked AS ( SELECT - s."memberId"::bigint AS member_id, + CASE WHEN s."memberId" ~ '^[0-9]{1,19}$' THEN + CASE WHEN s."memberId"::numeric <= 9223372036854775807 + THEN s."memberId"::bigint END + END AS member_id, s."challengeId" AS challenge_id, s."submittedDate" AS submitted_date, ROW_NUMBER() OVER ( diff --git a/sql/reports/statistics/development/first-time-development-submitters.sql b/sql/reports/statistics/development/first-time-development-submitters.sql index 280a4ff..060434a 100644 --- a/sql/reports/statistics/development/first-time-development-submitters.sql +++ b/sql/reports/statistics/development/first-time-development-submitters.sql @@ -1,6 +1,9 @@ WITH development_submissions_ranked AS ( SELECT - s."memberId"::bigint AS member_id, + CASE WHEN s."memberId" ~ '^[0-9]{1,19}$' THEN + CASE WHEN s."memberId"::numeric <= 9223372036854775807 + THEN s."memberId"::bigint END + END AS member_id, s."challengeId" AS challenge_id, s."submittedDate" AS submitted_date, ROW_NUMBER() OVER ( diff --git a/test/statistics/README.md b/test/statistics/README.md new file mode 100644 index 0000000..4877fe0 --- /dev/null +++ b/test/statistics/README.md @@ -0,0 +1,19 @@ +# First-time submitter SQL regression + +The public Design report failed with HTTP 500 when a submission's memberId was an +M2M client identifier (`…@clients`). Both first-time-submitter queries cast that +field to bigint. Guard the cast inside CASE so PostgreSQL predicate reordering +cannot evaluate it for non-member or out-of-range identifiers. Valid members, +earliest submissions, the three-month window, and maximum ratings are unchanged. + +Run the actual queries against PostgreSQL 16 with: + +```sh +STATISTICS_PSQL_COMMAND='psql postgresql://localhost/test_database' \ + python3 -m unittest discover -s test/statistics +``` + +The test uses unique fixture schemas in a rolled-back transaction. It includes +service accounts, null/empty/oversized identifiers, returning members, multiple +submissions, duplicate ratings, and an unrated member. No application tables are +changed. Without STATISTICS_PSQL_COMMAND the integration test is skipped. diff --git a/test/statistics/test_first_submitters.py b/test/statistics/test_first_submitters.py new file mode 100644 index 0000000..26d6da8 --- /dev/null +++ b/test/statistics/test_first_submitters.py @@ -0,0 +1,61 @@ +"""Run real PostgreSQL regressions with STATISTICS_PSQL_COMMAND set to a psql command. + +Example: STATISTICS_PSQL_COMMAND='docker exec -i statistics-query-tests psql -U postgres' +python3 -m unittest discover -s test/statistics +Uses transactional, uniquely named fixture schemas; no application tables are changed. +""" +import os +from pathlib import Path +import shlex +import subprocess +import unittest +import uuid + + +@unittest.skipUnless(os.environ.get('STATISTICS_PSQL_COMMAND'), 'PostgreSQL command not configured') +class FirstSubmittersTest(unittest.TestCase): + def test_reports_ignore_non_member_identifiers_without_losing_first_submission_semantics(self): + root = Path(__file__).resolve().parents[2] + for track, abbreviation in [('design', 'DS'), ('development', 'Dev')]: + with self.subTest(track=track): + suffix = uuid.uuid4().hex + schemas = {name: f'stats_test_{name}_{suffix}' for name in ['members', 'reviews', 'challenges']} + query = (root / f'sql/reports/statistics/{track}/first-time-{track}-submitters.sql').read_text() + sql = '''BEGIN; +CREATE SCHEMA members; +CREATE SCHEMA reviews; +CREATE SCHEMA challenges; +CREATE TABLE members.member ("userId" bigint, handle text); +CREATE TABLE members."memberMaxRating" ("userId" bigint, rating int); +CREATE TABLE challenges."ChallengeTrack" (id text, abbreviation text); +CREATE TABLE challenges."Challenge" (id text, name text, "trackId" text); +CREATE TABLE reviews.submission ("memberId" text, "challengeId" text, "submittedDate" timestamptz); +INSERT INTO members.member VALUES (101, 'new-member'), (102, 'returning-member'), (103, 'no-rating'); +INSERT INTO members."memberMaxRating" VALUES (101, 1000), (101, 1500); +INSERT INTO challenges."ChallengeTrack" VALUES ('track', 'TRACK_ABBREVIATION'); +INSERT INTO challenges."Challenge" VALUES ('first', 'First challenge', 'track'), ('second', 'Second challenge', 'track'); +INSERT INTO reviews.submission VALUES + ('101', 'first', now() - interval '2 days'), ('101', 'second', now() - interval '1 day'), + ('102', 'first', now() - interval '1 year'), ('102', 'second', now() - interval '1 day'), + ('103', 'first', now() - interval '1 day'), + ('service-account@clients', 'first', now()), ('', 'first', now()), + ('9223372036854775808', 'first', now()), ('999999999999999999999999999', 'first', now()), + (NULL, 'first', now()); +CREATE TEMP TABLE actual AS +QUERY +DO $$ BEGIN + IF (SELECT count(*) FROM actual) <> 2 THEN RAISE EXCEPTION 'Expected two real first-time members'; END IF; + IF NOT EXISTS (SELECT FROM actual WHERE "user.handle" = 'new-member' + AND "challenge.challenge_name" = 'First challenge' AND "submitter_profile.max_rating" = 1500) + THEN RAISE EXCEPTION 'Lost earliest submission or highest rating'; END IF; + IF NOT EXISTS (SELECT FROM actual WHERE "user.handle" = 'no-rating' AND "submitter_profile.max_rating" IS NULL) + THEN RAISE EXCEPTION 'Lost unrated member'; END IF; +END $$; +ROLLBACK; +'''.replace('TRACK_ABBREVIATION', abbreviation).replace('QUERY', query) + for name, replacement in schemas.items(): + sql = sql.replace(f'CREATE SCHEMA {name};', f'CREATE SCHEMA {replacement};') + sql = sql.replace(name + '.', replacement + '.') + result = subprocess.run(shlex.split(os.environ['STATISTICS_PSQL_COMMAND']) + ['-X', '-v', 'ON_ERROR_STOP=1'], + input=sql, text=True, capture_output=True) + self.assertEqual(0, result.returncode, result.stderr)