/******************************************************************************
   BDBDeleteRecordsTEST
   BDB
 
  @copyright 2004, 2005, 2006 Free Software Foundation, Inc.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA


  -------------------------------------------------------------------------
  Modification history

  09.01.2005 ola     initial version
  -------------------------------------------------------------------------
******************************************************************************/
#include <Encore/Encore.h>
#include <BDB/BDB.h>
#include <BDBDeleteRecordsTEST.h>


@implementation BDBDeleteRecordsTEST

- init {
  self = [super init];
  
  return self;
}


- setup {
  [BDBAbstractBTreeTEST deleteDatabase];
  return [super setup];
}

- testDeletions {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  BDBDatabaseEntry *keySimple, *keyDups, *keyLeave, *data;
  
  NSLog( @"BDBDeleteRecordsTEST::testDeletions: BEGIN..." );
  keySimple = [[[BDBDatabaseEntry alloc ] initWithObject: @"keySimple" ]
    autorelease];
  data = [[[BDBDatabaseEntry alloc] initWithObject: @"data" ]
    autorelease];
  [database putEntryWithTransaction: nil key:keySimple value: data];
  
  keyDups = [[[BDBDatabaseEntry alloc ] initWithObject: @"keyDups" ]
    autorelease];
  [database putEntryWithTransaction: nil key:keyDups value: data];
  data = [[[BDBDatabaseEntry alloc] initWithObject: @"data-2" ]
    autorelease];
  [database putEntryWithTransaction: nil key:keyDups value: data];  
  keyLeave = [[[BDBDatabaseEntry alloc ] initWithObject: @"keyLeave" ]
    autorelease];
  [database putEntryWithTransaction: nil key:keyLeave value: data]; 

  // now delete some entries
  [ database deleteEntryWithTransaction: nil key:keySimple]; 
  data = [[[BDBDatabaseEntry alloc] init] autorelease];
  
  ECAssertTrue( BDB_STATUS_NOTFOUND == [ database getEntryWithTransaction: nil
    key: keySimple data: data ], 
    @"Deletion of keySimple: Got an entry although requested to delete!" );
	
  // delete keyDups ==> all entries should be deleted!
  [database deleteEntryWithTransaction: nil key: keyDups];
  ECAssertTrue( BDB_STATUS_NOTFOUND == [ database getEntryWithTransaction: nil
    key: keyDups data: data ], 
    @"Deletion of keyDups: Got an entry although requested to delete all!" );
  
  // our entry keyLeave should still exist...
  ECAssertTrue( BDB_STATUS_SUCCESS == [database getEntryWithTransaction: nil
    key: keyLeave data: data], 
	@"keyLeave: Record not deleted but could not be retrieved!" );
  ECAssertTrue( [[data object] isEqual: @"data-2"],
    @"keyLeave: Retrieved data is incorrect!" );
	
  [pool release];
  NSLog( @"BDBDeleteRecordsTEST::testDeletions: FINISHED SUCCESSFULLY!" );
  return self;
}
@end
