|
15 | 15 | */
|
16 | 16 | package org.javalite.activejdbc.dialects;
|
17 | 17 |
|
18 |
| -import java.util.ArrayList; |
19 |
| -import java.util.Arrays; |
| 18 | +import org.javalite.activejdbc.CaseInsensitiveMap; |
| 19 | +import org.javalite.activejdbc.associations.Many2ManyAssociation; |
20 | 20 | import org.javalite.activejdbc.test.ActiveJDBCTest;
|
21 |
| -import org.javalite.activejdbc.test_models.Patient; |
| 21 | +import org.javalite.activejdbc.test_models.Assignment; |
22 | 22 | import org.javalite.activejdbc.test_models.Person;
|
| 23 | +import org.javalite.activejdbc.test_models.Programmer; |
23 | 24 | import org.junit.AfterClass;
|
24 | 25 | import org.junit.BeforeClass;
|
25 | 26 | import org.junit.Test;
|
26 | 27 |
|
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Map; |
| 31 | + |
27 | 32 | public class DefaultDialectTest extends ActiveJDBCTest {
|
28 | 33 |
|
29 | 34 | private static Dialect dialect;
|
@@ -65,19 +70,133 @@ public void testCreateParametrizedInsert() {
|
65 | 70 | @Test
|
66 | 71 | public void testFormSelectWithoutTableName() {
|
67 | 72 | final String fullQuery = "SELECT name FROM people";
|
68 |
| - a(dialect.formSelect(null, new String[]{"name"}, fullQuery, new ArrayList<String>(), 1, 1)).shouldBeEqual(fullQuery); |
| 73 | + a(dialect.formSelect( |
| 74 | + null, |
| 75 | + new String[]{"name"}, |
| 76 | + fullQuery, |
| 77 | + new ArrayList(), |
| 78 | + 1, 1)) |
| 79 | + .shouldBeEqual(fullQuery); |
69 | 80 | }
|
70 | 81 |
|
71 | 82 | @Test
|
72 | 83 | public void testFormSelectWithTableName() {
|
73 |
| - a(dialect.formSelect("people", null, null, new ArrayList<String>(), 1, 1)).shouldBeEqual("SELECT * FROM people"); |
74 |
| - a(dialect.formSelect("people", null, "name = ?", new ArrayList<String>(), 1, 1)).shouldBeEqual("SELECT * FROM people WHERE name = ?"); |
75 |
| - a(dialect.formSelect("people", null, "name = ?", Arrays.asList("name"), 1, 1)).shouldBeEqual("SELECT * FROM people WHERE name = ? ORDER BY name"); |
76 |
| - a(dialect.formSelect("people", null, null, Arrays.asList("last_name", "name"), 1, 1)).shouldBeEqual("SELECT * FROM people ORDER BY last_name, name"); |
| 84 | + a(dialect.formSelect("people", null, null, new ArrayList<String>(), 1, 1)) |
| 85 | + .shouldBeEqual("SELECT * FROM people"); |
| 86 | + |
| 87 | + a(dialect.formSelect("people", null, "name = ?", new ArrayList<String>(), 1, 1)) |
| 88 | + .shouldBeEqual("SELECT * FROM people WHERE name = ?"); |
| 89 | + |
| 90 | + a(dialect.formSelect("people", null, "name = ?", Arrays.asList("name"), 1, 1)) |
| 91 | + .shouldBeEqual("SELECT * FROM people WHERE name = ? ORDER BY name"); |
| 92 | + |
| 93 | + a(dialect.formSelect("people", null, null, Arrays.asList("last_name", "name"), 1, 1)) |
| 94 | + .shouldBeEqual("SELECT * FROM people ORDER BY last_name, name"); |
77 | 95 | }
|
78 | 96 |
|
79 | 97 | @Test
|
80 | 98 | public void testFormSelectWithOrderBy() {
|
81 |
| - a(dialect.formSelect("people", null, " ORDER by last_name", new ArrayList<String>(), 1, 1)).shouldBeEqual("SELECT * FROM people ORDER by last_name"); |
| 99 | + a(dialect.formSelect( |
| 100 | + "people", |
| 101 | + null, |
| 102 | + " ORDER by last_name", |
| 103 | + new ArrayList<String>(), |
| 104 | + 1, 1)) |
| 105 | + .shouldBeEqual("SELECT * FROM people ORDER by last_name"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testMany2ManyAssoc() { |
| 110 | + |
| 111 | + String expectedJoinQuery = |
| 112 | + "SELECT " + |
| 113 | + "programmers_projects.*, " + |
| 114 | + "t.assignment_id AS programmer_id " + |
| 115 | + "FROM " + |
| 116 | + "programmers_projects " + |
| 117 | + "INNER JOIN issue_assignments t ON programmers_projects.id = t.programmer_id " + |
| 118 | + "WHERE t.assignment_id IN (?)"; |
| 119 | + |
| 120 | + Many2ManyAssociation association = |
| 121 | + new Many2ManyAssociation( |
| 122 | + Programmer.class, |
| 123 | + Assignment.class, |
| 124 | + "issue_assignments", |
| 125 | + "assignment_id", |
| 126 | + "programmer_id"); |
| 127 | + |
| 128 | + String query = dialect.selectManyToManyAssociation( |
| 129 | + association, |
| 130 | + "programmer_id", |
| 131 | + 1); |
| 132 | + |
| 133 | + a(query).shouldBeEqual(expectedJoinQuery); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testDeleteManyToManyAssociation() { |
| 138 | + String expectedQuery = |
| 139 | + "DELETE FROM issue_assignments " + |
| 140 | + "WHERE assignment_id = ? AND programmer_id = ?"; |
| 141 | + |
| 142 | + Many2ManyAssociation association = |
| 143 | + new Many2ManyAssociation( |
| 144 | + Programmer.class, |
| 145 | + Assignment.class, |
| 146 | + "issue_assignments", |
| 147 | + "assignment_id", |
| 148 | + "programmer_id"); |
| 149 | + |
| 150 | + String query = dialect.deleteManyToManyAssociation(association); |
| 151 | + |
| 152 | + a(query).shouldBeEqual(expectedQuery); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testSelectExist() { |
| 157 | + a(dialect.selectExists( |
| 158 | + Person.getMetaModel())) |
| 159 | + .shouldBeEqual("SELECT id FROM people WHERE id = ?"); |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testInsertStatement(){ |
| 165 | + Map<String, Object> x = new CaseInsensitiveMap(); |
| 166 | + x.put("testField", "testValue"); |
| 167 | + |
| 168 | + a(dialect.insert( |
| 169 | + Person.getMetaModel(), |
| 170 | + x)) |
| 171 | + .shouldBeEqual("INSERT INTO people (testField) VALUES ('testValue')"); |
| 172 | + |
| 173 | + x.put("testField2", "testValue2"); |
| 174 | + |
| 175 | + a(dialect.insert( |
| 176 | + Person.getMetaModel(), |
| 177 | + x)) |
| 178 | + .shouldBeEqual("INSERT INTO people (testField, testField2) VALUES ('testValue', 'testValue2')"); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testInsertParametrized() { |
| 183 | + a(dialect.insertParametrized( |
| 184 | + Person.getMetaModel(), |
| 185 | + Arrays.asList("testField1", "testField2"), |
| 186 | + false)) |
| 187 | + .shouldBeEqual("INSERT INTO people (testField1, testField2) VALUES (?, ?)"); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testUpdateStatement() { |
| 192 | + Map<String, Object> x = new CaseInsensitiveMap(); |
| 193 | + x.put("testField", "testValue"); |
| 194 | + x.put("id", "1"); |
| 195 | + |
| 196 | + a(dialect.update( |
| 197 | + Person.getMetaModel(), |
| 198 | + x)) |
| 199 | + .shouldBeEqual("UPDATE people SET testField = 'testValue' WHERE id = 1"); |
| 200 | + |
82 | 201 | }
|
83 | 202 | }
|
0 commit comments