svn commit: r949609 - /ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r949609 - /ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java

doogie-3
Author: doogie
Date: Sun May 30 22:07:10 2010
New Revision: 949609

URL: http://svn.apache.org/viewvc?rev=949609&view=rev
Log:
Use the new equals implementations to test for a correct parse; also
test all the statements.

Modified:
    ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java

Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java?rev=949609&r1=949608&r2=949609&view=diff
==============================================================================
--- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java (original)
+++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java Sun May 30 22:07:10 2010
@@ -29,15 +29,37 @@ import javolution.util.FastMap;
 
 import junit.framework.TestCase;
 
+import org.ofbiz.sql.BetweenCondition;
+import org.ofbiz.sql.BooleanCondition;
 import org.ofbiz.sql.Condition;
+import org.ofbiz.sql.ConditionList;
+import org.ofbiz.sql.CountFunction;
 import org.ofbiz.sql.FieldAll;
 import org.ofbiz.sql.FieldDef;
 import org.ofbiz.sql.FieldValue;
 import org.ofbiz.sql.FunctionCall;
+import org.ofbiz.sql.InsertValues;
+import org.ofbiz.sql.InsertRow;
+import org.ofbiz.sql.Joined;
+import org.ofbiz.sql.Joiner;
+import org.ofbiz.sql.KeyMap;
+import org.ofbiz.sql.ListCondition;
 import org.ofbiz.sql.MathValue;
+import org.ofbiz.sql.NumberValue;
+import org.ofbiz.sql.OrderByItem;
+import org.ofbiz.sql.ParameterValue;
 import org.ofbiz.sql.Parser;
+import org.ofbiz.sql.Relation;
+import org.ofbiz.sql.SetField;
+import org.ofbiz.sql.SQLDelete;
+import org.ofbiz.sql.SQLInsert;
 import org.ofbiz.sql.SQLSelect;
 import org.ofbiz.sql.SQLStatement;
+import org.ofbiz.sql.SQLUpdate;
+import org.ofbiz.sql.SQLView;
+import org.ofbiz.sql.StringValue;
+import org.ofbiz.sql.Table;
+import org.ofbiz.sql.TableName;
 import org.ofbiz.sql.Value;
 
 import org.ofbiz.base.test.GenericTestCaseBase;
@@ -54,102 +76,191 @@ public class SQLTest extends GenericTest
         }
         Iterator<SQLStatement<?>> stmtIt = statements.iterator();
         assertTrue("has more statements", stmtIt.hasNext());
+
         {
+            SQLSelect select = new SQLSelect(
+                false,
+                list(
+                    new FieldAll("a", Collections.<String>emptySet()),
+                    new FieldAll("b", set("partyId")),
+                    new FieldAll("c", set("partyId"))
+                ),
+                GenericTestCaseBase.<String, FieldDef>map(
+                    "roleTypeId", new FieldDef(new FieldValue("d", "roleTypeId"), null),
+                    "roleDescription", new FieldDef(new FieldValue("d", "description"), "roleDescription"),
+                    "SUM",  new FieldDef(new FunctionCall("SUM", GenericTestCaseBase.<Value>list(new FieldValue("a", "partyId"))), null),
+                    "baz",  new FieldDef(new FunctionCall("FOO", GenericTestCaseBase.<Value>list(new FieldValue("a", "partyId"), new NumberValue<Integer>(Integer.valueOf(1)))), "baz"),
+                    "one",  new FieldDef(new MathValue("||", list(new FieldValue("a", "partyId"), new StringValue("-"), new FieldValue("a", "partyTypeId"))), "one"),
+                    "cnt1", new FieldDef(new CountFunction(false, new FieldValue("a", "partyId")), "cnt1"),
+                    "cnt2", new FieldDef(new CountFunction(false, new FieldValue(null, "partyId")), "cnt2"),
+                    "cnt3", new FieldDef(new CountFunction(true, new FieldValue("a", "partyId")), "cnt3")
+                ),
+                new Table(
+                    new TableName("Party", "a"),
+                    new Joined(true, new TableName("Person", "b"), list(new KeyMap("partyId", "partyId")),
+                        new Joined(true, new TableName("PartyGroup", "c"), list(new KeyMap("partyId", "partyId")),
+                            new Joined(false, new TableName("PartyRole", "d"), list(new KeyMap("partyId", "partyId"), new KeyMap("partyId", "partyId")))
+                        )
+                    )
+                ),
+                GenericTestCaseBase.<String, Relation>map(
+                    "MainAPerson", new Relation("one", "MainA", "Person", list(new KeyMap("partyId", "partyId"))),
+                    "MainBPerson", new Relation(null, "MainB", "Person", list(new KeyMap("partyId", "partyId"))),
+                    "Person", new Relation("one", null, "Person", list(new KeyMap("partyId", "partyId"))),
+                    "PartyGroup", new Relation(null, null, "PartyGroup", list(new KeyMap("partyId", "partyId")))
+                ),
+                new ConditionList(
+                    Joiner.OR,
+                    GenericTestCaseBase.<Condition>list(
+                        new ConditionList(
+                            Joiner.AND,
+                            list(
+                                new BooleanCondition(new FieldValue("a", "partyTypeId"), "=", new StringValue("PERSON")),
+                                new BooleanCondition(new FieldValue("b", "lastName"), "LIKE", new ParameterValue("lastName")),
+                                new BetweenCondition(new FieldValue("b", "birthDate"), new StringValue("1974-12-01"), new StringValue("1974-12-31"))
+                            )
+                        ),
+                        new ConditionList(
+                            Joiner.AND,
+                            list(
+                                new ListCondition(new FieldValue("b", "partyId"), "IN", GenericTestCaseBase.<Value>list(
+                                    new StringValue("1"),
+                                    new StringValue("2"),
+                                    new StringValue("3"),
+                                    new StringValue("4")
+                                )),
+                                new BooleanCondition(new FieldValue("b", "gender"), "=", new StringValue("M"))
+                            )
+                        )
+                    )
+                ),
+                new BooleanCondition(new FieldValue("b", "firstName"), "LIKE", new StringValue("%foo%")),
+                null,
+                list(
+                    new OrderByItem(OrderByItem.Order.DEFAULT, "LOWER", "lastName"),
+                    new OrderByItem(OrderByItem.Order.DEFAULT, null, "firstName"),
+                    new OrderByItem(OrderByItem.Order.DESCENDING, null, "birthDate")
+                ),
+                5,
+                10
+            );
             SQLStatement stmt = stmtIt.next();
-            assertTrue("is select", stmt instanceof SQLSelect);
-            SQLSelect select = (SQLSelect) stmt;
-            Iterator<FieldAll> fieldAllIt = select.getFieldAlls().iterator();
-
-            assertTrue("has first field all", fieldAllIt.hasNext());
-            FieldAll fieldAll = fieldAllIt.next();
-            assertEquals("first fieldAll.alias", "a", fieldAll.getAlias());
-            assertEquals("no excludes", GenericTestCaseBase.<FieldAll>set(), set(fieldAll));
-
-            assertTrue("has second field all", fieldAllIt.hasNext());
-            fieldAll = fieldAllIt.next();
-            assertEquals("first fieldAll.alias", "b", fieldAll.getAlias());
-            assertEquals("no excludes", set("partyId"), set(fieldAll));
-
-            assertTrue("has third field all", fieldAllIt.hasNext());
-            fieldAll = fieldAllIt.next();
-            assertEquals("first fieldAll.alias", "c", fieldAll.getAlias());
-            assertEquals("no excludes", set("partyId"), set(fieldAll));
-
-            assertFalse("has no more field all", fieldAllIt.hasNext());
-
-            Iterator<FieldDef> fieldDefIt = select.getFieldDefs().iterator();
-
-            assertTrue("has first field def", fieldDefIt.hasNext());
-            FieldDef fieldDef = fieldDefIt.next();
-            assertEquals("first fieldDef.alias", "roleTypeId", fieldDef.getAlias());
-            assertTrue("first is FieldDef", fieldDef instanceof FieldDef);
-            FieldDef fdfv = (FieldDef) fieldDef;
-            assertTrue("first has FieldValue", fdfv.getValue() instanceof FieldValue);
-            FieldValue fv = (FieldValue) fdfv.getValue();
-            assertEquals("first fieldDef.tableName", "d", fv.getTableName());
-            assertEquals("first fieldDef.fieldName", "roleTypeId", fv.getFieldName());
-            assertEquals("first fieldDef.defaultName", "roleTypeId", fv.getDefaultName());
-
-            assertTrue("has second field def", fieldDefIt.hasNext());
-            fieldDef = fieldDefIt.next();
-            assertEquals("second fieldDef.alias", "roleDescription", fieldDef.getAlias());
-            assertTrue("second is FieldDef", fieldDef instanceof FieldDef);
-            fdfv = (FieldDef) fieldDef;
-            assertTrue("second has FieldValue", fdfv.getValue() instanceof FieldValue);
-            fv = (FieldValue) fdfv.getValue();
-            assertEquals("second fieldDef.tableName", "d", fv.getTableName());
-            assertEquals("second fieldDef.fieldName", "description", fv.getFieldName());
-            assertEquals("second fieldDef.defaultName", "description", fv.getDefaultName());
-
-            assertTrue("has third field def", fieldDefIt.hasNext());
-            fieldDef = fieldDefIt.next();
-            assertEquals("third fieldDef.alias", "SUM", fieldDef.getAlias());
-            assertTrue("third is FieldDef", fieldDef instanceof FieldDef);
-            FieldDef fd = (FieldDef) fieldDef;
-            assertTrue("third fieldDefValue.staticValue is FunctionCall", fd.getValue() instanceof FunctionCall);
-            FunctionCall fc = (FunctionCall) fd.getValue();
-            assertEquals("third arg count", 1, fc.getArgCount());
-            Iterator<Value> valueIt = fc.iterator();
-            assertTrue("third args hasNext", valueIt.hasNext());
-            Value argValue = valueIt.next();
-            assertTrue("third first arg is FieldValue", argValue instanceof FieldValue);
-            FieldValue fieldValue = (FieldValue) argValue;
-            assertEquals("third first arg tableName", "a", fieldValue.getTableName());
-            assertEquals("third first arg fieldName", "partyId", fieldValue.getFieldName());
-            assertFalse("third no more args", valueIt.hasNext());
-
-            assertTrue("has fourth field def", fieldDefIt.hasNext());
-            fieldDef = fieldDefIt.next();
-            assertEquals("fourth fieldDef.alias", "baz", fieldDef.getAlias());
-            assertTrue("fourth is FieldDef", fieldDef instanceof FieldDef);
-            fd = (FieldDef) fieldDef;
-
-            assertTrue("has fifth field def", fieldDefIt.hasNext());
-            fieldDef = fieldDefIt.next();
-            assertEquals("fifth fieldDef.alias", "one", fieldDef.getAlias());
-            assertTrue("fifth is FieldDef", fieldDef instanceof FieldDef);
-            fd = (FieldDef) fieldDef;
-
-            assertTrue("has sixth field def", fieldDefIt.hasNext());
-            fieldDef = fieldDefIt.next();
-            assertEquals("sixth fieldDef.alias", "cnt1", fieldDef.getAlias());
-            assertTrue("sixth is FieldDef", fieldDef instanceof FieldDef);
-            fd = (FieldDef) fieldDef;
-
-            assertTrue("has seventh field def", fieldDefIt.hasNext());
-            fieldDef = fieldDefIt.next();
-            assertEquals("seventh fieldDef.alias", "cnt2", fieldDef.getAlias());
-            assertTrue("seventh is FieldDef", fieldDef instanceof FieldDef);
-            fd = (FieldDef) fieldDef;
-
-            assertTrue("has eighth field def", fieldDefIt.hasNext());
-            fieldDef = fieldDefIt.next();
-            assertEquals("eighth fieldDef.alias", "cnt3", fieldDef.getAlias());
-            assertTrue("eighth is FieldDef", fieldDef instanceof FieldDef);
-            fd = (FieldDef) fieldDef;
-
-            assertFalse("has no more field def", fieldDefIt.hasNext());
-
+            assertEquals("firstSelect", select, stmt);
+        }
+        {
+            SQLInsert insert = new SQLInsert(
+                new TableName("Party", null),
+                new InsertValues(
+                    list(
+                        new InsertRow(GenericTestCaseBase.<Value>list(new StringValue("a"), new StringValue("PERSON"), new StringValue("PARTY_DISABLED"))),
+                        new InsertRow(list(new NumberValue<Integer>(Integer.valueOf(5)), new StringValue("PARTY_GROUP"), new ParameterValue("name")))
+                    )
+                ),
+                list("partyId", "partyTypeId", "statusId")
+            );
+            SQLStatement stmt = stmtIt.next();
+            assertEquals("firstInsert", insert, stmt);
+        }
+        {
+            SQLInsert insert = new SQLInsert(
+                new TableName("Person", null),
+                new SQLSelect(
+                    false,
+                    null,
+                    GenericTestCaseBase.<String, FieldDef>map(
+                        "partyId", new FieldDef(new FieldValue(null, "partyId"), null),
+                        "firstName",  new FieldDef(new MathValue("||", list(new FieldValue(null, "partyId"), new StringValue("-auto"))), "firstName")
+                    ),
+                    new Table(new TableName("Party", null), null),
+                    null,
+                    new ListCondition(new FieldValue(null, "partyId"), "IN", GenericTestCaseBase.<Value>list(new StringValue("a"), new StringValue("b"))),
+                    null,
+                    null,
+                    null,
+                    -1,
+                    -1
+                ),
+                list("partyId", "firstName")
+            );
+            SQLStatement stmt = stmtIt.next();
+            assertEquals("secondInsert", insert, stmt);
+        }
+        {
+            SQLUpdate update = new SQLUpdate(
+                new Table(new TableName("Person", null), null),
+                list(
+                    new SetField("lastName", new MathValue("||", list(new StringValue("auto-"), new FieldValue(null, "partyId"))))
+                ),
+                new ListCondition(new FieldValue(null, "partyId"), "IN", GenericTestCaseBase.<Value>list(new StringValue("a"), new StringValue("b")))
+            );
+            SQLStatement stmt = stmtIt.next();
+            assertEquals("firstUpdate", update, stmt);
+        }
+        {
+            SQLUpdate update = new SQLUpdate(
+                new Table(new TableName("Person", null), null),
+                list(
+                    new SetField("lastName", new MathValue("||", list(new StringValue("auto-"), new FieldValue(null, "partyId")))),
+                    new SetField("height", new NumberValue<Integer>(Integer.valueOf(5))),
+                    new SetField("width", new NumberValue<Integer>(Integer.valueOf(7)))
+                ),
+                new ListCondition(new FieldValue(null, "partyId"), "IN", GenericTestCaseBase.<Value>list(new StringValue("a"), new StringValue("b")))
+            );
+            SQLStatement stmt = stmtIt.next();
+            assertEquals("secondUpdate", update, stmt);
+        }
+        {
+            SQLUpdate update = new SQLUpdate(
+                new Table(new TableName("Person", null), null),
+                list(
+                    new SetField("lastName", new MathValue("||", list(new StringValue("auto-"), new FieldValue(null, "partyId")))),
+                    new SetField("height", new NumberValue<Integer>(Integer.valueOf(6))),
+                    new SetField("width", new NumberValue<Integer>(Integer.valueOf(5))),
+                    new SetField("nickname", new StringValue("a"))
+                ),
+                new ListCondition(new FieldValue(null, "partyId"), "IN", GenericTestCaseBase.<Value>list(new StringValue("a"), new StringValue("b")))
+            );
+            SQLStatement stmt = stmtIt.next();
+            assertEquals("thirdUpdate", update, stmt);
+        }
+        {
+            SQLDelete delete = new SQLDelete(
+                new Table(new TableName("Person", null), null),
+                new ListCondition(new FieldValue(null, "partyId"), "IN", GenericTestCaseBase.<Value>list(new StringValue("a"), new StringValue("b")))
+            );
+            SQLStatement stmt = stmtIt.next();
+            assertEquals("firstDelete", delete, stmt);
+        }
+        {
+            SQLDelete delete = new SQLDelete(
+                new Table(new TableName("Party", null), null),
+                new ListCondition(new FieldValue(null, "partyId"), "IN", GenericTestCaseBase.<Value>list(new StringValue("a"), new StringValue("b")))
+            );
+            SQLStatement stmt = stmtIt.next();
+            assertEquals("secondDelete", delete, stmt);
+        }
+        {
+            SQLView view = new SQLView(
+                "viewOne",
+                new SQLSelect(
+                    false,
+                    list(new FieldAll("a", Collections.<String>emptySet())),
+                    null,
+                    new Table(new TableName("Party", "a"), null),
+                    null,
+                    null,
+                    null,
+                    null,
+                    null,
+                    -1,
+                    -1
+                )
+            );
+            SQLStatement stmt = stmtIt.next();
+            assertEquals("firstView", view, stmt);
         }
     }
+/*
+CREATE VIEW viewOne AS SELECT a.* FROM Party a;
+*/
 }