db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r355740 - in /db/torque/test/trunk/test-project/src/java/org/apache/torque: DataTest.java DocsTest.java
Date Sat, 10 Dec 2005 11:26:59 GMT
Author: tfischer
Date: Sat Dec 10 03:26:53 2005
New Revision: 355740

URL: http://svn.apache.org/viewcvs?rev=355740&view=rev
Log:
Added a test case for subqueries

Modified:
    db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java
    db/torque/test/trunk/test-project/src/java/org/apache/torque/DocsTest.java

Modified: db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java
URL: http://svn.apache.org/viewcvs/db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java?rev=355740&r1=355739&r2=355740&view=diff
==============================================================================
--- db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java (original)
+++ db/torque/test/trunk/test-project/src/java/org/apache/torque/DataTest.java Sat Dec 10
03:26:53 2005
@@ -1196,10 +1196,76 @@
         assertFalse("Book compared with book with different id "
         		+ "should not be equal", 
         		book.equals(bookNotEqual));
+    }
+
+    /**
+     * Tests whether we can execute subqueries
+     * @throws Exception if the test fails
+     */
+    public void testSubqueries() throws Exception
+    {
+        cleanBookstore();
+        Author author1 = new Author();
+        author1.setName("author1");
+        author1.save();
+        Author author2 = new Author();
+        author2.setName("author2");
+        author2.save();
+        Author author2b = new Author();
+        author2b.setName("author2");
+        author2b.save();
         
+        // subquery with "in" clause
+        {
+            Criteria subquery = new Criteria();
+            subquery.addSelectColumn(AuthorPeer.AUTHOR_ID);
+            subquery.add(AuthorPeer.NAME, author2.getName());
+            Criteria criteria = new Criteria();
+            criteria.add(AuthorPeer.AUTHOR_ID, subquery, Criteria.IN);
+            criteria.addDescendingOrderByColumn(AuthorPeer.AUTHOR_ID);
+            
+            List result = AuthorPeer.doSelect(criteria);
+            assertEquals("Expected result of size 2 but got " + result.size(),
+                    result.size(), 
+                    2);
+            Author author = (Author) result.get(0);
+            assertEquals("Expected author with Id " 
+                    + author2b.getAuthorId() 
+                    + " at first position but got "
+                    + author.getAuthorId(),
+                    author2b.getAuthorId(),
+                    author.getAuthorId());
+            author = (Author) result.get(1);
+            assertEquals("Expected author with Id " 
+                    + author2.getAuthorId() 
+                    + " at second position but got "
+                    + author.getAuthorId(),
+                    author2.getAuthorId(),
+                    author.getAuthorId());
+        }
         
+        // subquery with =
+        {
+            Criteria subquery = new Criteria();
+            subquery.addSelectColumn(AuthorPeer.AUTHOR_ID);
+            subquery.add(AuthorPeer.NAME, author1.getName());
+            Criteria criteria = new Criteria();
+            criteria.add(AuthorPeer.AUTHOR_ID, subquery);
+            
+            List result = AuthorPeer.doSelect(criteria);
+            assertEquals("Expected result of size 1 but got " + result.size(),
+                    result.size(), 
+                    1);
+            Author author = (Author) result.get(0);
+            assertEquals("Expected author with Id " 
+                    + author1.getAuthorId() 
+                    + " but got "
+                    + author.getAuthorId(),
+                    author1.getAuthorId(),
+                    author.getAuthorId());
+        }
     }
-
+    
     /**
      * Tests whether shutdown complains about anything
      * @throws TorqueException if shutdown does not exit cleanly

Modified: db/torque/test/trunk/test-project/src/java/org/apache/torque/DocsTest.java
URL: http://svn.apache.org/viewcvs/db/torque/test/trunk/test-project/src/java/org/apache/torque/DocsTest.java?rev=355740&r1=355739&r2=355740&view=diff
==============================================================================
--- db/torque/test/trunk/test-project/src/java/org/apache/torque/DocsTest.java (original)
+++ db/torque/test/trunk/test-project/src/java/org/apache/torque/DocsTest.java Sat Dec 10
03:26:53 2005
@@ -356,4 +356,22 @@
             + author.getName(),
             AUTHOR_2_NAME.equals(author.getName()));
     }
+    
+    
+    /**
+     * Criteria Howto, section "subselects"
+     */
+    public void testSubselects() throws TorqueException
+    {
+        Criteria subquery = new Criteria();
+        subquery.addSelectColumn("MAX(" + AuthorPeer.AUTHOR_ID + ")");
+    
+        Criteria criteria = new Criteria();
+        criteria.add(AuthorPeer.AUTHOR_ID, subquery);
+    
+        List authors = AuthorPeer.doSelect(criteria);
+        assertEquals("Expected list of size 1 but got " + authors.size(),
+                1,
+                authors.size());
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message