parent
c47fca8f6b
commit
d9a550013f
@ -0,0 +1,19 @@ |
||||
package de.kreth.dbmanager; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
public class UniqueConstraint { |
||||
private List<String> names = new ArrayList<>(); |
||||
|
||||
public UniqueConstraint(ColumnDefinition... defs) { |
||||
for(ColumnDefinition c: defs) { |
||||
names.add(c.getColumnName()); |
||||
} |
||||
} |
||||
|
||||
public List<String> getNames() { |
||||
return Collections.unmodifiableList(names); |
||||
} |
||||
} |
||||
@ -1,28 +1,69 @@ |
||||
package de.kreth.dbmanager; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertNotNull; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertNotNull; |
||||
|
||||
public class DbManagerCreateTablesTest { |
||||
@Test |
||||
public void testCreateSqlStatement() { |
||||
List<ColumnDefinition> columns = new ArrayList<ColumnDefinition>(); |
||||
TableDefinition def = new TableDefinition("testtable", columns); |
||||
String sql = DbManager.createSqlStatement(def); |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
assertNotNull(sql); |
||||
assertEquals("CREATE TABLE testtable (\n" + " _id INTEGER primary key AUTO_INCREMENT\n" + ")", sql); |
||||
} |
||||
|
||||
@Test |
||||
public void testCreateSqlStatement() { |
||||
public void testCreateTable2Columns() { |
||||
List<ColumnDefinition> columns = new ArrayList<ColumnDefinition>(); |
||||
|
||||
columns.add(new ColumnDefinition(DataType.VARCHAR100, "name", " NOT NULL")); |
||||
columns.add(new ColumnDefinition(DataType.DATETIME, "theDate")); |
||||
|
||||
TableDefinition def = new TableDefinition("testtable", columns); |
||||
String sql = DbManager.createSqlStatement(def); |
||||
|
||||
assertNotNull(sql); |
||||
System.out.println(sql); |
||||
assertEquals("CREATE TABLE testtable (\n" + " _id INTEGER primary key AUTO_INCREMENT\n" + ")", sql); |
||||
assertEquals("CREATE TABLE testtable (\n" |
||||
+ " _id INTEGER primary key AUTO_INCREMENT,\n" |
||||
+ " name VARCHAR(100) NOT NULL,\n" |
||||
+ " theDate DATETIME\n" + ")", |
||||
sql); |
||||
} |
||||
|
||||
@Test |
||||
public void testCreateTable2ColumnsUnique() { |
||||
List<ColumnDefinition> columns = new ArrayList<ColumnDefinition>(); |
||||
|
||||
columns.add(new ColumnDefinition(DataType.VARCHAR100, "name", " NOT NULL")); |
||||
columns.add(new ColumnDefinition(DataType.DATETIME, "theDate")); |
||||
|
||||
UniqueConstraint unique = new UniqueConstraint(columns.get(0), columns.get(1)); |
||||
TableDefinition def = new TableDefinition("testtable", columns, unique); |
||||
String sql = DbManager.createSqlStatement(def); |
||||
|
||||
assertNotNull(sql); |
||||
|
||||
String expected = "CREATE TABLE testtable (\n" |
||||
+ " _id INTEGER primary key AUTO_INCREMENT,\n" |
||||
+ " name VARCHAR(100) NOT NULL,\n" |
||||
+ " theDate DATETIME,\n" |
||||
+ " CONSTRAINT UNIQUE (name,theDate)\n" |
||||
+ ")"; |
||||
|
||||
assertEquals(expected, sql); |
||||
} |
||||
|
||||
@Before |
||||
public void setUp() throws Exception {} |
||||
} |
||||
|
||||
|
||||
//~ Formatted by Jindent --- http://www.jindent.com
|
||||
|
||||
Loading…
Reference in new issue