IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 24, 2005, 3:17:24 PM (21 years ago)
Author:
evanalst
Message:

Update test case for psDBDeleteRows to handle invalid arguments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/test/dataIO/tst_psDB.c

    r4383 r4390  
    99 *  @author Aaron Culliney, MHPCC
    1010 *
    11  *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2005-06-24 21:31:13 $
     11 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2005-06-25 01:17:24 $
    1313 *
    1414 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    109109                              {TPDBDumpCols,       851,  "dbDumpCols",        0, false},
    110110                              {TPDBUpdateRows,     852,  "dbUpdateRows",      0, false},
    111                               {TPDBDeleteRows,      -13, "dbDeleteRows",      0, false},
     111                              {TPDBDeleteRows,     853, "dbDeleteRows",      0, false},
    112112                              //{TPDBCreate,        -14, "dbCreate",          0, false},
    113113                              //{TPDBDrop,          -15, "dbDrop",            0, false},
     
    19921992}
    19931993
    1994 // Testpoint #XXX, TPDBDeleteRows shall update rows in a test table ...
     1994// Testpoint #853, TPDBDeleteRows shall update rows in a test table ...
    19951995psS32 TPDBDeleteRows( void )
    19961996{
    1997     psS32 failed = 0;
    1998     psDB *dbh = NULL;
    1999     const char* table = "table11";
    2000     psArray *ary=NULL, *rowSet=NULL;
    2001     psMetadata *where=NULL, *row=NULL, *md = _get_CreateTableMetadata();
    2002 
     1997    psDB*         dbh       = NULL;
     1998    const char*   table     = "table11";
     1999    psArray*      ary       = NULL;
     2000    psArray*      rowSet    = NULL;
     2001    psMetadata*   where     = NULL;
     2002    psMetadata*   row       = NULL;
     2003    psMetadata*   md        = NULL;
     2004    int           chgRows   = 0;
     2005
     2006    // Create database table definition
     2007    md = _get_CreateTableMetadata();
     2008
     2009    // Initialize database connection
    20032010    dbh = _init_psDB();
    20042011    if (dbh == NULL) {
     2012        psError(PS_ERR_UNKNOWN,true,"Did not expect return NULL for initialize database");
    20052013        return 1;
    20062014    }
    20072015
    2008     psLogMsg( __func__, PS_LOG_INFO, "psDBDeleteRows shall delete rows from a test table.\n" );
    2009 
    2010     failed = ! psDBCreateTable(dbh, table, md);
    2011     if (!failed) {
    2012         rowSet = psArrayAlloc(3);
    2013         rowSet->n = 0;
    2014 
    2015         row = _get_const_row1();
    2016         psArrayAdd(rowSet, 0, row);
    2017         psFree(row);
    2018 
    2019         row = _get_const_row2();
    2020         psArrayAdd(rowSet, 0, row);
    2021         psFree(row);
    2022 
    2023         row = _get_row();
    2024         psArrayAdd(rowSet, 0, row);
    2025         psFree(row);
    2026 
    2027         failed = ! psDBInsertRows(dbh, table, rowSet);
    2028         if (!failed) {
    2029             where = _get_where();
    2030             failed = (psDBDeleteRows(dbh, table, where) < 0);
    2031 
    2032             ary = psDBDumpRows(dbh, table);
    2033             if (ary == NULL) {
    2034                 failed = 1;
    2035             } else {
    2036                 if (ary->n != 2) {
    2037                     psLogMsg( __func__, PS_LOG_INFO, "oops, did not delete stuff!\n" );
    2038                     failed = 1;
    2039                 } else {
    2040                     psLogMsg( __func__, PS_LOG_INFO, "found expected two-row table.\n" );
    2041                 }
    2042 
    2043                 psFree(ary);
    2044             }
    2045 
    2046             psFree(where);
    2047         }
    2048         psFree(rowSet);
    2049         psDBDropTable(dbh, table);
    2050     }
     2016    // Create test table
     2017    if(!psDBCreateTable(dbh, table, md)) {
     2018        psError(PS_ERR_UNKNOWN,true,"Did not expect return false for creating test table");
     2019        psDBCleanup(dbh);
     2020        return 2;
     2021    }
     2022
     2023    // Create known data rows to put into test table
     2024    rowSet = psArrayAlloc(3);
     2025    rowSet->n = 0;
     2026    row = _get_const_row1();
     2027    psArrayAdd(rowSet, 0, row);
     2028    psFree(row);
     2029    row = _get_const_row2();
     2030    psArrayAdd(rowSet, 0, row);
     2031    psFree(row);
     2032    row = _get_row();
     2033    psArrayAdd(rowSet, 0, row);
     2034    psFree(row);
     2035    if(!psDBInsertRows(dbh, table, rowSet)) {
     2036        psError(PS_ERR_UNKNOWN,true,"Did not expect return false for inserting data into table");
     2037        psDBDropTable(dbh,table);
     2038        psDBCleanup(dbh);
     2039        return 3;
     2040    }
     2041
     2042    // Create where clause to specify the row to remove
     2043    where = _get_where();
     2044
     2045    // Delete row with valid arguments
     2046    chgRows = psDBDeleteRows(dbh,table,where);
     2047    if(chgRows != 1) {
     2048        psError(PS_ERR_UNKNOWN,true,"Deleted rows %ld not as expected %ld",
     2049                chgRows,1);
     2050        psDBDropTable(dbh,table);
     2051        psDBCleanup(dbh);
     2052        return 4;
     2053    }
     2054    // Verify table contents
     2055    ary = psDBDumpRows(dbh, table);
     2056    if (ary == NULL) {
     2057        psError(PS_ERR_UNKNOWN,true,"Did not expect return NULL for dump rows");
     2058        psDBDropTable(dbh,table);
     2059        psDBCleanup(dbh);
     2060        return 5;
     2061    }
     2062    if (ary->n != 2) {
     2063        psError(PS_ERR_UNKNOWN,true,"Remaining rows %d not as expected %d",
     2064                ary->n,2);
     2065        psDBDropTable(dbh,table);
     2066        psDBCleanup(dbh);
     2067        return 6;
     2068    }
     2069    psFree(ary);
     2070
     2071    // Attempt to delete row just deleted again
     2072    chgRows = psDBDeleteRows(dbh,table,where);
     2073    if(chgRows != 0) {
     2074        psError(PS_ERR_UNKNOWN,true,"Deleted rows %ld not as expected %ld",
     2075                chgRows,0);
     2076        psDBDropTable(dbh,table);
     2077        psDBCleanup(dbh);
     2078        return 7;
     2079    }
     2080    // Verify table contents
     2081    ary = psDBDumpRows(dbh, table);
     2082    if (ary == NULL) {
     2083        psError(PS_ERR_UNKNOWN,true,"Did not expect return NULL for dump rows");
     2084        psDBDropTable(dbh,table);
     2085        psDBCleanup(dbh);
     2086        return 8;
     2087    }
     2088    if (ary->n != 2) {
     2089        psError(PS_ERR_UNKNOWN,true,"Remaining rows %d not as expected %d",
     2090                ary->n,2);
     2091        psDBDropTable(dbh,table);
     2092        psDBCleanup(dbh);
     2093        return 9;
     2094    }
     2095    psFree(ary);
     2096
     2097    // Attempt to delete row with NULL database
     2098    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message for NULL database");
     2099    chgRows = psDBDeleteRows(NULL,table,where);
     2100    if(chgRows > 0) {
     2101        psError(PS_ERR_UNKNOWN,true,"Deleted rows %ld not as expected %ld",
     2102                chgRows, -1);
     2103        psDBDropTable(dbh,table);
     2104        psDBCleanup(dbh);
     2105        return 10;
     2106    }
     2107
     2108    // Attempt to delete row with invalid table
     2109    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message for invalid table");
     2110    chgRows = psDBDeleteRows(dbh,"table999",where);
     2111    if(chgRows > 0) {
     2112        psError(PS_ERR_UNKNOWN,true,"Deleted rows %ld not as expected %ld",
     2113                chgRows, -1);
     2114        psDBDropTable(dbh,table);
     2115        psDBCleanup(dbh);
     2116        return 11;
     2117    }
     2118
     2119    // Attempt to delete row with NULL where - deletes all rows
     2120    chgRows = psDBDeleteRows(dbh,table,NULL);
     2121    if(chgRows != 2) {
     2122        psError(PS_ERR_UNKNOWN,true,"Deleted rows %ld not as expected %ld",
     2123                chgRows, 2);
     2124        psDBDropTable(dbh,table);
     2125        psDBCleanup(dbh);
     2126        return 12;
     2127    }
     2128    // Verify table contents
     2129    ary = psDBDumpRows(dbh, table);
     2130    if (ary == NULL) {
     2131        psError(PS_ERR_UNKNOWN,true,"Did not expect return NULL for dump rows");
     2132        psDBDropTable(dbh,table);
     2133        psDBCleanup(dbh);
     2134        return 13;
     2135    }
     2136    if (ary->n != 0) {
     2137        psError(PS_ERR_UNKNOWN,true,"Remaining rows %d not as expected %d",
     2138                ary->n,0);
     2139        psDBDropTable(dbh,table);
     2140        psDBCleanup(dbh);
     2141        return 14;
     2142    }
     2143    psFree(ary);
     2144
     2145    // Attempt to delete row with NULL where - deletes all rows from an empty table
     2146    chgRows = psDBDeleteRows(dbh,table,NULL);
     2147    if(chgRows != 0) {
     2148        psError(PS_ERR_UNKNOWN,true,"Deleted rows %ld not as expected %ld",
     2149                chgRows, 0);
     2150        psDBDropTable(dbh,table);
     2151        psDBCleanup(dbh);
     2152        return 15;
     2153    }
     2154
     2155    psFree(where);
     2156    psFree(rowSet);
     2157    psDBDropTable(dbh, table);
    20512158
    20522159    psFree(md);
    20532160    psDBCleanup(dbh);
    20542161
    2055     return failed;
     2162    return 0;
    20562163}
    20572164
Note: See TracChangeset for help on using the changeset viewer.