Skip to content

Commit

Permalink
Altered unit test to check for changed value instead of string content.
Browse files Browse the repository at this point in the history
Removed id column from set statements. Switched to a entryset iterator.
  • Loading branch information
dessalines committed Mar 25, 2015
1 parent 11c48eb commit 0dc6eee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
Expand Up @@ -19,9 +19,11 @@
import org.javalite.activejdbc.associations.Many2ManyAssociation;
import org.javalite.common.Convert;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import static org.javalite.common.Util.*;
Expand Down Expand Up @@ -228,25 +230,29 @@ public String update(MetaModel metaModel, Map<String, Object> attributes) {
} else {

query.append("SET ");

Iterator<String> keysetIt = attributes.keySet().iterator();
Iterator<Object> valueIt = attributes.values().iterator();

while (valueIt.hasNext()) {
query.append(keysetIt.next() + " = ");
appendValue(query, valueIt.next()); // Accomodates the different types

if (valueIt.hasNext()) {
query.append(", ");
} else{
query.append(" ");
}
String idName = metaModel.getIdName().toUpperCase();

// don't include the id name in the set portion
Map<String, Object> attributesWithoutId = new HashMap<String, Object>(attributes);
attributesWithoutId.remove(idName);

Iterator<Entry<String, Object>> attributesIt = attributesWithoutId.entrySet().iterator();
while (attributesIt.hasNext()) {
Entry<String, Object> attribute = attributesIt.next();
String key = attribute.getKey();
Object val = attribute.getValue();

query.append(key + " = ");
appendValue(query, val); // Accommodates the different types

if (attributesIt.hasNext()) {
query.append(", ");
} else{
query.append(" ");
}

}

String idName = metaModel.getIdName();
query.append("WHERE ").append(idName).append(" = " + attributes.get(idName));


}
return query.toString();
}
Expand Down
26 changes: 21 additions & 5 deletions activejdbc/src/test/java/org/javalite/activejdbc/ModelTest.java
Expand Up @@ -484,17 +484,33 @@ public void shouldGenerateCorrectInsertSQL() {
}

@Test
public void shouldGenerateCorrectUpdateSQL(){
public void shouldGenerateCorrectUpdateSQL() {

// Create the student
deleteAndPopulateTable("students");
Student s = new Student();
s.set("first_name", "Jim");
s.set("last_name", "Cary");
s.set("dob", new java.sql.Date(getDate(1965, 12, 1).getTime()));
s.set("id", 1);
s.set("id", 1);
s.saveIt();

// find them, and change a column
s = Student.findById(1);
s.set("first_name", "Drew");
s.saveIt();
String updateSQL = s.toUpdate();
System.out.println(updateSQL);
Base.exec(updateSQL);

// Find them again
s = Student.findById(1);

the(updateSQL).shouldBeEqual("UPDATE students SET dob = DATE '1965-12-01', first_name = 'Jim', id = 1, last_name = 'Cary' WHERE id = 1");
}
// Verify that the first name column changed
the(s.get("first_name")).shouldBeEqual("Drew");
System.out.println(updateSQL);


}

@Test
public void shouldGenerateValidInsertSQL() {
Expand Down

0 comments on commit 0dc6eee

Please sign in to comment.