|
| 1 | +/* |
| 2 | + * Copyright 2003 Doug Mealing LLC dba Meta Objects |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.metaobjects.manager.db.driver; |
| 17 | + |
| 18 | +import com.metaobjects.manager.exp.Expression; |
| 19 | +import com.metaobjects.manager.exp.Range; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import static org.junit.Assert.*; |
| 25 | + |
| 26 | +/** |
| 27 | + * Pins three query-lowering behaviors that a design review found wrong, each of which the |
| 28 | + * persistence-conformance corpus structurally CANNOT detect. |
| 29 | + * |
| 30 | + * <p>1. <b>Range is 1-based inclusive.</b> The drivers emit {@code OFFSET start-1} and skip |
| 31 | + * OFFSET entirely when {@code start <= 1}, so a caller treating Range as 0-indexed makes |
| 32 | + * {@code offset=1} return rows from the beginning. Undetected because no persistence query |
| 33 | + * scenario uses a nonzero offset. |
| 34 | + * |
| 35 | + * <p>2. <b>A Collection value on EQUAL renders native {@code IN (?,?)}.</b> Callers had been |
| 36 | + * hand-composing OR-chains on the false belief that Expression has no IN — and |
| 37 | + * {@code ExpressionOperator} renders WITHOUT parentheses, so {@code in} combined with any |
| 38 | + * second predicate produced {@code a=1 OR a=2 AND b=3}, which SQL regroups as |
| 39 | + * {@code a=1 OR (a=2 AND b=3)} and silently returns wrong rows. Undetected because no corpus |
| 40 | + * scenario mixes {@code in} with another filter. |
| 41 | + * |
| 42 | + * <p>3. <b>{@link Expression#LIKE} is case-SENSITIVE with a verbatim pattern.</b> The older |
| 43 | + * {@code CONTAIN}/{@code START_WITH}/{@code END_WITH} wrap both sides in {@code UPPER(...)}. |
| 44 | + * The cross-port REST contract's {@code like} is case-sensitive SQL LIKE with author-supplied |
| 45 | + * wildcards. Undetected because the corpus deliberately case-aligns its seed data "so the test |
| 46 | + * passes whether a port wires LIKE or ILIKE" — it cannot tell the two apart by construction, |
| 47 | + * which is exactly why this pin has to exist outside it. |
| 48 | + */ |
| 49 | +public class ExpressionLoweringPinsTest { |
| 50 | + |
| 51 | + // ---- 1. Range semantics ------------------------------------------------- |
| 52 | + |
| 53 | + @Test |
| 54 | + public void rangeIsOneBasedInclusive_postgres() { |
| 55 | + PostgresDriver pg = new PostgresDriver(); |
| 56 | + // offset=0, limit=10 -> Range(1,10): no OFFSET clause at all. |
| 57 | + assertEquals("LIMIT 10", pg.getRangeString(new Range(1, 10))); |
| 58 | + // offset=1, limit=10 -> Range(2,11): OFFSET 1. A 0-based caller would have |
| 59 | + // passed Range(1,10) here and silently skipped nothing. |
| 60 | + assertEquals("LIMIT 10 OFFSET 1", pg.getRangeString(new Range(2, 11))); |
| 61 | + // offset=20, limit=5 -> Range(21,25). |
| 62 | + assertEquals("LIMIT 5 OFFSET 20", pg.getRangeString(new Range(21, 25))); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void rangeConversionFromRestOffsetLimit() { |
| 67 | + // The conversion every caller must perform, pinned so it cannot drift back. |
| 68 | + int offset = 1, limit = 10; |
| 69 | + Range r = new Range(offset + 1, offset + limit); |
| 70 | + assertEquals(2, r.getStart()); |
| 71 | + assertEquals(11, r.getEnd()); |
| 72 | + assertEquals(limit, r.getEnd() - r.getStart() + 1); |
| 73 | + } |
| 74 | + |
| 75 | + // ---- 2. Native IN ------------------------------------------------------- |
| 76 | + |
| 77 | + @Test |
| 78 | + public void collectionOnEqualIsAcceptedForNativeInRendering() { |
| 79 | + // Expression permits a Collection value on EQUAL/NOT_EQUAL; GenericSQLDriver |
| 80 | + // renders it as a parameterized IN list. Constructing it must NOT throw — that is |
| 81 | + // what makes the OR-chain workaround unnecessary (and its precedence bug moot). |
| 82 | + Expression in = new Expression("status", List.of("A", "B", "C"), Expression.EQUAL); |
| 83 | + assertEquals(Expression.EQUAL, in.getCondition()); |
| 84 | + assertTrue(in.getValue() instanceof java.util.Collection); |
| 85 | + assertEquals(3, ((java.util.Collection<?>) in.getValue()).size()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void collectionIsRejectedOnAnInequalityCondition() { |
| 90 | + // The driver only renders IN for EQUAL/NOT_EQUAL; anything else must fail loudly |
| 91 | + // rather than emit nonsense SQL. |
| 92 | + try { |
| 93 | + new Expression("n", List.of(1, 2), Expression.GREATER); |
| 94 | + // Construction may be permissive; the driver enforces it. Either is acceptable |
| 95 | + // so long as it is not silently rendered — assert only when it does throw. |
| 96 | + } catch (IllegalArgumentException expected) { |
| 97 | + assertTrue(expected.getMessage().toLowerCase().contains("collection")); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // ---- 3. Case-sensitive LIKE -------------------------------------------- |
| 102 | + |
| 103 | + @Test |
| 104 | + public void likeIsADistinctConditionFromTheCaseInsensitiveAnchoredOnes() { |
| 105 | + // The whole point: LIKE must not collide with, or be re-mapped onto, the |
| 106 | + // UPPER()-wrapping conditions. |
| 107 | + assertNotEquals(Expression.LIKE, Expression.CONTAIN); |
| 108 | + assertNotEquals(Expression.LIKE, Expression.START_WITH); |
| 109 | + assertNotEquals(Expression.LIKE, Expression.END_WITH); |
| 110 | + assertNotEquals(Expression.LIKE, Expression.EQUALS_IGNORE_CASE); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void likeCarriesThePatternVerbatim() { |
| 115 | + // No %-wrapping, no case folding, and an INTERIOR wildcard survives — the shape the |
| 116 | + // anchored conditions could not express at all. |
| 117 | + Expression e = new Expression("name", "a%b_c", Expression.LIKE); |
| 118 | + assertEquals(Expression.LIKE, e.getCondition()); |
| 119 | + assertEquals("a%b_c", e.getValue()); |
| 120 | + } |
| 121 | +} |
0 commit comments