Hi, all,

Here's an updated test case that eliminates the criteria re-use issue
and demonstrates that the delete has the desired behavior when no join
is used.

Peace,
--Carl

package com.activx.apps.ms.quan.util.tests;

import org.apache.torque.TorqueException;
import org.apache.torque.util.CountHelper;
import org.apache.torque.util.Criteria;

import com.activx.lims.tests.TorqueTestCase;
import com.activx.om.T1Peer;
import com.activx.om.T2;
import com.activx.om.T2Peer;

/**
 * create table t1 (t1_id bigint(20) not null auto_increment, primary
key (t1_id));
 * create table t2 (t2_id bigint(20) not null auto_increment, t1_id
bigint(20) not null, primary key (t2_id));
 */
public class JoinedDeleteTest extends TorqueTestCase {
        private static final int        PRESENT_FK      = 50;
        private static final int        MISSING_FK      = 100;

        /**
         * delete from t2 where t1_id = 100
         * passes
         */
        public void testDirectDelete() throws Exception {
                delete(direct(), direct());
        }

        private static Criteria direct() {
                return new Criteria().add(T2Peer.T1_ID, MISSING_FK);
        }
        
        /**
         * delete from t2 where t1.t1_id = 100 and t2.t1_id = t1.t1_id
         * fails because all records are deleted
         */
        public void testJoinedDelete() throws Exception {
                delete(joined(), joined());
        }

        private static Criteria joined() {
                return new Criteria().add(T1Peer.T1_ID,
MISSING_FK).addJoin(T1Peer.T1_ID, T2Peer.T1_ID);
        }

        private void delete(Criteria c1, Criteria c2) throws Exception {
                // there are concerns that re-use of a Criteria can
                // have undesirable effects.  So we need two identical
                // criteria here.
                assertEquals(c1, c2);
                assertNotSame(c1, c2);
                
                // start with an empty table
                T2Peer.doDelete(new Criteria());
                assertEquals(0, countT2s());

                // add one record with a foreign key of 50
                T2 t2 = new T2();
                t2.setT1Id(PRESENT_FK);
                t2.save();

                // see that the record made it in
                assertEquals(1, countT2s());

                // there are no t2 records with the MISSING_FK
                assertEquals(0, new CountHelper().count(c1));
                // try to delete only records with the MISSING_FK
                T2Peer.doDelete(c2);
                
                // expect that no records were deleted
                assertEquals(1, countT2s());
        }

        private int countT2s() throws TorqueException {
                return new CountHelper().count(new
Criteria().add(T2Peer.T2_ID, 0, Criteria.GREATER_EQUAL));
        }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to