-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlStatementGuardTest.java
More file actions
77 lines (67 loc) · 2.99 KB
/
SqlStatementGuardTest.java
File metadata and controls
77 lines (67 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package io.github.evoschema;
import io.github.evoschema.processor.dbscanner.SqlStatementGuard;
import io.github.evoschema.processor.exception.EvoSchemaException;
import org.junit.Assert;
import org.junit.Test;
public class SqlStatementGuardTest
{
@Test
public void shouldAllowStandardDmlForDbdml()
{
SqlStatementGuard.validateDmlOnly("dbdml.insert", "INSERT INTO demo_table(id) VALUES (1)");
SqlStatementGuard.validateDmlOnly("dbdml.update", "UPDATE demo_table SET status = 'DONE' WHERE id = 1");
SqlStatementGuard.validateDmlOnly("dbdml.delete", "DELETE FROM demo_table WHERE id = 1");
}
@Test
public void shouldRejectReplaceAndCallForDbdml()
{
EvoSchemaException replaceError = Assert.assertThrows(
EvoSchemaException.class,
() -> SqlStatementGuard.validateDmlOnly("dbdml.replace", "REPLACE INTO demo_table(id) VALUES (1)")
);
EvoSchemaException callError = Assert.assertThrows(
EvoSchemaException.class,
() -> SqlStatementGuard.validateDmlOnly("dbdml.call", "CALL sync_demo_data()")
);
Assert.assertEquals(EvoSchemaException.ProcesssError.DML_SCRIPT_ERROR, replaceError.getReason());
Assert.assertEquals(EvoSchemaException.ProcesssError.DML_SCRIPT_ERROR, callError.getReason());
}
@Test
public void shouldAllowDmlAndQueryForDbScript()
{
SqlStatementGuard.validateDmlPlusQuery("dbscript.select", "SELECT id FROM demo_table");
SqlStatementGuard.validateDmlPlusQuery("dbscript.call", "CALL sync_demo_data()");
SqlStatementGuard.validateDmlPlusQuery(
"dbscript.with",
"WITH ready_orders AS (SELECT id FROM demo_table) SELECT id FROM ready_orders"
);
}
@Test
public void shouldRejectDdlForDbScript()
{
EvoSchemaException error = Assert.assertThrows(
EvoSchemaException.class,
() -> SqlStatementGuard.validateDmlPlusQuery("dbscript.ddl", "ALTER TABLE demo_table ADD COLUMN note VARCHAR(32)")
);
Assert.assertEquals(EvoSchemaException.ProcesssError.DML_SCRIPT_ERROR, error.getReason());
}
@Test
public void shouldAllowQueryOnlyForDmlAssert()
{
SqlStatementGuard.validateQueryOnly("assert.select", "SELECT COUNT(1) FROM demo_table");
SqlStatementGuard.validateQueryOnly("assert.show", "SHOW COLUMNS FROM demo_table");
SqlStatementGuard.validateQueryOnly(
"assert.with",
"WITH result AS (SELECT COUNT(1) AS total FROM demo_table) SELECT total FROM result"
);
}
@Test
public void shouldRejectUpdateForDmlAssert()
{
EvoSchemaException error = Assert.assertThrows(
EvoSchemaException.class,
() -> SqlStatementGuard.validateQueryOnly("assert.update", "UPDATE demo_table SET status = 'DONE'")
);
Assert.assertEquals(EvoSchemaException.ProcesssError.DML_SCRIPT_ERROR, error.getReason());
}
}