Skip to content

Commit fb2c6b0

Browse files
committed
Add unit tests for schema classes
1 parent 6efee97 commit fb2c6b0

1 file changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package sqlancer;
2+
3+
import org.junit.jupiter.api.Test;
4+
import sqlancer.common.schema.AbstractSchema;
5+
import sqlancer.common.schema.AbstractTable;
6+
import sqlancer.common.schema.AbstractTableColumn;
7+
import sqlancer.common.schema.TableIndex;
8+
9+
import java.util.*;
10+
import java.util.stream.Collectors;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
public class TestCommonSchema {
15+
static class TestTable extends AbstractTable<TestTableColumn, TestIndex, GlobalState<?, ?, ?>> {
16+
TestTable(String name, List<TestTableColumn> columns, List<TestIndex> indexes, boolean isView) {
17+
super(name, columns, indexes, isView);
18+
}
19+
20+
@Override
21+
public long getNrRows(GlobalState<?, ?, ?> globalState) {
22+
return 0;
23+
}
24+
}
25+
26+
static class TestTableColumn extends AbstractTableColumn<TestTable, String> {
27+
TestTableColumn(String name, TestTable table, String type) {
28+
super(name, table, type);
29+
}
30+
}
31+
32+
static class TestSchema extends AbstractSchema<GlobalState<?, ?, ?>, TestTable> {
33+
TestSchema(List<TestTable> tables) {
34+
super(tables);
35+
}
36+
}
37+
38+
static class TestIndex extends TableIndex {
39+
TestIndex(String name) {
40+
super(name);
41+
}
42+
}
43+
44+
private TestTable createTestTable(String name, List<TestIndex> indexes, boolean isView, String... columns) {
45+
List<TestTableColumn> cols = Arrays.stream(columns)
46+
.map(col -> new TestTableColumn(col, null, "VARCHAR"))
47+
.collect(Collectors.toList());
48+
return new TestTable(name, cols, indexes, isView);
49+
}
50+
51+
private TestTableColumn createTestColumn(String name, TestTable table, String type) {
52+
return new TestTableColumn(name, table, type);
53+
}
54+
55+
private TestSchema createTestSchema(TestTable... tables) {
56+
return new TestSchema(Arrays.asList(tables));
57+
}
58+
59+
@Test
60+
void testColumnManagement() {
61+
TestTable table = createTestTable("products", Collections.emptyList(), false, "sku", "price");
62+
TestTableColumn randomCol = table.getRandomColumn();
63+
64+
assertEquals("sku", table.getColumns().get(0).getName());
65+
assertEquals("VARCHAR", table.getColumns().get(1).getType());
66+
67+
assertTrue(table.getColumns().contains(randomCol));
68+
}
69+
70+
@Test
71+
void testIndexManagement() {
72+
TestIndex idx1 = new TestIndex("idx_sku");
73+
TestIndex idx2 = new TestIndex("idx_price");
74+
TestTable table = createTestTable("products", Arrays.asList(idx1, idx2), false, "sku", "price");
75+
TableIndex randomIndex = table.getRandomIndex();
76+
77+
assertTrue(table.hasIndexes());
78+
assertEquals(2, table.getIndexes().size());
79+
assertTrue(table.getIndexes().contains(randomIndex));
80+
}
81+
82+
@Test
83+
void testViewManagement() {
84+
TestTable view1 = createTestTable("v1", Collections.emptyList(),true, "col1");
85+
TestTable view2 = createTestTable("v2", Collections.emptyList(),true, "col2");
86+
TestTable table = createTestTable("t1", Collections.emptyList(), false, "col3");
87+
TestSchema schema = createTestSchema(view1, view2, table);
88+
89+
assertAll(
90+
() -> assertEquals(2, schema.getViews().size(), "Should detect 2 views"),
91+
() -> assertEquals(1, schema.getDatabaseTablesWithoutViews().size(), "Should detect 1 normal table"),
92+
() -> assertEquals("t1", schema.getDatabaseTablesWithoutViews().get(0).getName())
93+
);
94+
}
95+
96+
@Test
97+
void testFreeColumnNameGeneration() {
98+
TestTable table = createTestTable("users", Collections.emptyList(), false, "id", "name");
99+
Set<String> generatedNames = new HashSet<>();
100+
101+
for (int i = 0; i < 100; i++) {
102+
String newName = table.getFreeColumnName();
103+
assertTrue(generatedNames.add(newName), "Duplicate: " + newName);
104+
105+
List<TestTableColumn> newColumns = new ArrayList<>(table.getColumns());
106+
newColumns.add(new TestTableColumn(newName, table, "TEXT"));
107+
table = new TestTable(table.getName(), newColumns, table.getIndexes(), table.isView());
108+
}
109+
}
110+
111+
@Test
112+
void testObjectComparison() {
113+
TestTable tableA = createTestTable("A", Collections.emptyList(), false, "x", "y");
114+
TestTable tableB = createTestTable("B", Collections.emptyList(), false, "b");
115+
116+
TestTableColumn colA1 = new TestTableColumn("x", tableA, "INT");
117+
TestTableColumn colA2 = new TestTableColumn("y", tableA, "INT");
118+
TestTableColumn colB1 = new TestTableColumn("b", tableB, "TEXT");
119+
120+
assertAll(
121+
() -> assertTrue(colA1.compareTo(colA2) < 0, "Columns should be ordered by name"),
122+
() -> assertTrue(colA1.compareTo(colB1) < 0, "Columns should be ordered by name"),
123+
() -> assertTrue(tableA.compareTo(tableB) > 0, "Tables should be ordered reverse-alphabetically"),
124+
() -> assertEquals(0, tableA.compareTo(tableA), "Same table should be equal")
125+
);
126+
}
127+
128+
@Test
129+
void testEquality() {
130+
TestTable table1 = createTestTable("t1", Collections.emptyList(), false, "id");
131+
TestTable table2 = createTestTable("t2", Collections.emptyList(), false, "id");
132+
133+
TestTableColumn col1 = new TestTableColumn("id", table1, "INT");
134+
TestTableColumn col2 = new TestTableColumn("id", table1, "INT");
135+
TestTableColumn col3 = new TestTableColumn("id", table2, "INT");
136+
TestTableColumn col4 = new TestTableColumn("name", table1, "TEXT");
137+
138+
assertAll(
139+
() -> assertEquals(col1, col2, "Same table/column should be equal"),
140+
() -> assertNotEquals(col1, col3, "Different tables should not be equal"),
141+
() -> assertNotEquals(col1, col4, "Different columns should not be equal"),
142+
() -> assertNotEquals(col1, "invalid_object", "Different types should not be equal")
143+
);
144+
}
145+
146+
@Test
147+
void testBoundaryConditions() {
148+
String longName = "a".repeat(256);
149+
TestTableColumn longCol = new TestTableColumn(longName, null, "TEXT");
150+
assertEquals(longName, longCol.getName());
151+
152+
TestTableColumn col2 = createTestColumn("orphan", null, "UNKNOWN");
153+
assertEquals("orphan", col2.getFullQualifiedName());
154+
assertNull(col2.getTable());
155+
}
156+
}

0 commit comments

Comments
 (0)