(t *testing.T)
| 21 | ) |
| 22 | |
| 23 | func TestIsIdempotentUpdateStmt(t *testing.T) { |
| 24 | var tests = []struct { |
| 25 | query string |
| 26 | idempotent bool |
| 27 | hasError bool |
| 28 | msg string |
| 29 | }{ |
| 30 | {"UPDATE table SET a = 0", true, false, "simple table"}, |
| 31 | {"UPDATE table SET a = 0;", true, false, "semicolong"}, |
| 32 | {"UPDATE table SET a = 0, b = 0", true, false, "multiple updates"}, |
| 33 | {"UPDATE ks.table SET a = 0", true, false, "simple qualified table"}, |
| 34 | {"UPDATE ks.table USING TIMESTAMP 1234 SET a = 0", true, false, "using timestamp"}, |
| 35 | {"UPDATE ks.table USING TIMESTAMP 1234 AND TTL 5678 SET a = 0", true, false, "using timestamp and ttl"}, |
| 36 | {"UPDATE ks.table USING TTL 1234 SET a = 0", true, false, "using ttl"}, |
| 37 | {"UPDATE ks.table USING TTL 1234 AND TIMESTAMP 5678 SET a = 0", true, false, "using ttl and timestamp"}, |
| 38 | {"UPDATE ks.table SET a = 0 WHERE a > 100", true, false, "where clause"}, |
| 39 | |
| 40 | // Invalid |
| 41 | {"UPDATE table", false, true, "no 'SET'"}, |
| 42 | {"UPDATE table a = 0", false, true, "no 'SET' w/ update operation"}, |
| 43 | {"UPDATE table a = 0 WHERE", false, true, "where clause no relations"}, |
| 44 | {"UPDATE table SET a = 0,", true, false, "multiple updates no operation"}, |
| 45 | {"UPDATE ks. SET a = 0", false, true, "no table"}, |
| 46 | {"UPDATE table USING SET a = 0", false, true, "no timestamp or ttl"}, |
| 47 | {"UPDATE table USING TTL SET a = 0", false, true, "ttl no value"}, |
| 48 | {"UPDATE table USING TTL 1234 AND SET a = 0", false, true, "no ttl/timestamp after 'AND' in using clause"}, |
| 49 | |
| 50 | // Not idempotent |
| 51 | {"UPDATE table SET a = now()", false, false, "simple w/ now()"}, |
| 52 | {"UPDATE table SET a = a + 1", false, false, "add to counter"}, |
| 53 | {"UPDATE table USING TIMESTAMP 1234 SET a = now()", false, false, "using clause w/ now()"}, |
| 54 | {"UPDATE table SET a = 0 WHERE a > toTimestamp(toDate(now()))", false, false, "where clause w/ now()"}, |
| 55 | {"UPDATE table SET a = 0 WHERE a > 0 IF EXISTS", false, false, "where clause w/ LWT"}, |
| 56 | {"UPDATE table SET a = 0 IF EXISTS", false, false, "LWT"}, |
| 57 | {"UPDATE table SET a = 0 IF EXISTS;", false, false, "LWT and semicolon"}, |
| 58 | } |
| 59 | |
| 60 | for _, tt := range tests { |
| 61 | idempotent, err := IsQueryIdempotent(tt.query) |
| 62 | assert.True(t, (err != nil) == tt.hasError, tt.msg) |
| 63 | assert.Equal(t, tt.idempotent, idempotent, "invalid idempotency", tt.msg) |
| 64 | } |
| 65 | } |
nothing calls this directly
no test coverage detected