Skip to content

Commit 569ec2c

Browse files
committed
- added testMany2ManyAssoc
- added testDeleteManyToManyAssociation - added testSelectExist - added testInsertStatement - added testInsertParametrized - added testUpdateStatement
1 parent a463b7d commit 569ec2c

File tree

1 file changed

+128
-9
lines changed

1 file changed

+128
-9
lines changed

activejdbc/src/test/java/org/javalite/activejdbc/dialects/DefaultDialectTest.java

Lines changed: 128 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@
1515
*/
1616
package org.javalite.activejdbc.dialects;
1717

18-
import java.util.ArrayList;
19-
import java.util.Arrays;
18+
import org.javalite.activejdbc.CaseInsensitiveMap;
19+
import org.javalite.activejdbc.associations.Many2ManyAssociation;
2020
import org.javalite.activejdbc.test.ActiveJDBCTest;
21-
import org.javalite.activejdbc.test_models.Patient;
21+
import org.javalite.activejdbc.test_models.Assignment;
2222
import org.javalite.activejdbc.test_models.Person;
23+
import org.javalite.activejdbc.test_models.Programmer;
2324
import org.junit.AfterClass;
2425
import org.junit.BeforeClass;
2526
import org.junit.Test;
2627

28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.Map;
31+
2732
public class DefaultDialectTest extends ActiveJDBCTest {
2833

2934
private static Dialect dialect;
@@ -65,19 +70,133 @@ public void testCreateParametrizedInsert() {
6570
@Test
6671
public void testFormSelectWithoutTableName() {
6772
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);
6980
}
7081

7182
@Test
7283
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");
7795
}
7896

7997
@Test
8098
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+
82201
}
83202
}

0 commit comments

Comments
 (0)