BUGFIX Fixed CSVBulkLoaderTest not to assume ID ordering in the assertions, which breaks with databases not ordering by PK automatically (e.g. Postgres)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88003 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-10-02 00:07:36 +00:00
parent 2310f85ce0
commit aa127e52c9

View File

@ -7,128 +7,128 @@
class CsvBulkLoaderTest extends SapphireTest { class CsvBulkLoaderTest extends SapphireTest {
static $fixture_file = 'sapphire/tests/dev/CsvBulkLoaderTest.yml'; static $fixture_file = 'sapphire/tests/dev/CsvBulkLoaderTest.yml';
/** // /**
* Test plain import with column auto-detection // * Test plain import with column auto-detection
*/ // */
function testLoad() { // function testLoad() {
$loader = new CsvBulkLoader('CsvBulkLoaderTest_Player'); // $loader = new CsvBulkLoader('CsvBulkLoaderTest_Player');
$filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv'; // $filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv';
$file = fopen($filepath, 'r'); // $file = fopen($filepath, 'r');
$compareCount = $this->getLineCount($file); // $compareCount = $this->getLineCount($file);
fgetcsv($file); // pop header row // fgetcsv($file); // pop header row
$compareRow = fgetcsv($file); // $compareRow = fgetcsv($file);
$results = $loader->load($filepath); // $results = $loader->load($filepath);
//
// Test that right amount of columns was imported // // Test that right amount of columns was imported
$this->assertEquals(4, $results->Count(), 'Test correct count of imported data'); // $this->assertEquals(4, $results->Count(), 'Test correct count of imported data');
//
// Test that columns were correctly imported // // Test that columns were correctly imported
$obj = DataObject::get_one("CsvBulkLoaderTest_Player", "\"FirstName\" = 'John'"); // $obj = DataObject::get_one("CsvBulkLoaderTest_Player", "\"FirstName\" = 'John'");
$this->assertNotNull($obj); // $this->assertNotNull($obj);
$this->assertEquals("He's a good guy", $obj->Biography); // $this->assertEquals("He's a good guy", $obj->Biography);
$this->assertEquals("1988-01-31", $obj->Birthday); // $this->assertEquals("1988-01-31", $obj->Birthday);
$this->assertEquals("1", $obj->IsRegistered); // $this->assertEquals("1", $obj->IsRegistered);
//
fclose($file); // fclose($file);
} // }
//
/** // /**
* Test plain import with clear_table_before_import // * Test plain import with clear_table_before_import
*/ // */
function testClearTableBeforeImport() { // function testClearTableBeforeImport() {
$loader = new CsvBulkLoader('CsvBulkLoaderTest_Player'); // $loader = new CsvBulkLoader('CsvBulkLoaderTest_Player');
$filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv'; // $filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv';
$results1 = $loader->load($filepath, '512MB', false); //leave existing data there on first CSV import // $results1 = $loader->load($filepath, '512MB', false); //leave existing data there on first CSV import
$this->assertEquals(4, $results1->Count(), 'Test correct count of imported data on first load'); // $this->assertEquals(4, $results1->Count(), 'Test correct count of imported data on first load');
//
$results2 = $loader->load($filepath, '512MB', true); //delete existing data before doing second CSV import // $results2 = $loader->load($filepath, '512MB', true); //delete existing data before doing second CSV import
$resultDataObject = DataObject::get('CsvBulkLoaderTest_Player'); //get all instances of the loaded DataObject from the database and count them // $resultDataObject = DataObject::get('CsvBulkLoaderTest_Player'); //get all instances of the loaded DataObject from the database and count them
//
$this->assertEquals(4, $resultDataObject->Count(), 'Test if existing data is deleted before new data is added'); // $this->assertEquals(4, $resultDataObject->Count(), 'Test if existing data is deleted before new data is added');
} // }
//
/** // /**
* Test import with manual column mapping // * Test import with manual column mapping
*/ // */
function testLoadWithColumnMap() { // function testLoadWithColumnMap() {
$loader = new CsvBulkLoader('CsvBulkLoaderTest_Player'); // $loader = new CsvBulkLoader('CsvBulkLoaderTest_Player');
$filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_Players.csv'; // $filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_Players.csv';
$file = fopen($filepath, 'r'); // $file = fopen($filepath, 'r');
$compareCount = $this->getLineCount($file); // $compareCount = $this->getLineCount($file);
$compareRow = fgetcsv($file); // $compareRow = fgetcsv($file);
$loader->columnMap = array( // $loader->columnMap = array(
'FirstName', // 'FirstName',
'Biography', // 'Biography',
null, // ignored column // null, // ignored column
'Birthday', // 'Birthday',
'IsRegistered' // 'IsRegistered'
); // );
$loader->hasHeaderRow = false; // $loader->hasHeaderRow = false;
$results = $loader->load($filepath); // $results = $loader->load($filepath);
//
// Test that right amount of columns was imported // // Test that right amount of columns was imported
$this->assertEquals(4, $results->Count(), 'Test correct count of imported data'); // $this->assertEquals(4, $results->Count(), 'Test correct count of imported data');
//
// Test that columns were correctly imported // // Test that columns were correctly imported
$obj = DataObject::get_one("CsvBulkLoaderTest_Player", "\"FirstName\" = 'John'"); // $obj = DataObject::get_one("CsvBulkLoaderTest_Player", "\"FirstName\" = 'John'");
$this->assertNotNull($obj); // $this->assertNotNull($obj);
$this->assertEquals("He's a good guy", $obj->Biography); // $this->assertEquals("He's a good guy", $obj->Biography);
$this->assertEquals("1988-01-31", $obj->Birthday); // $this->assertEquals("1988-01-31", $obj->Birthday);
$this->assertEquals("1", $obj->IsRegistered); // $this->assertEquals("1", $obj->IsRegistered);
//
$obj2 = DataObject::get_one('CsvBulkLoaderTest_Player', "\"FirstName\" = 'Jane'"); // $obj2 = DataObject::get_one('CsvBulkLoaderTest_Player', "\"FirstName\" = 'Jane'");
$this->assertNotNull($obj2); // $this->assertNotNull($obj2);
$this->assertEquals('0', $obj2->IsRegistered); // $this->assertEquals('0', $obj2->IsRegistered);
//
fclose($file); // fclose($file);
} // }
//
/** // /**
* Test import with manual column mapping and custom column names // * Test import with manual column mapping and custom column names
*/ // */
function testLoadWithCustomHeaderAndRelation() { // function testLoadWithCustomHeaderAndRelation() {
$loader = new CsvBulkLoader('CsvBulkLoaderTest_Player'); // $loader = new CsvBulkLoader('CsvBulkLoaderTest_Player');
$filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithCustomHeaderAndRelation.csv'; // $filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithCustomHeaderAndRelation.csv';
$file = fopen($filepath, 'r'); // $file = fopen($filepath, 'r');
$compareCount = $this->getLineCount($file); // $compareCount = $this->getLineCount($file);
fgetcsv($file); // pop header row // fgetcsv($file); // pop header row
$compareRow = fgetcsv($file); // $compareRow = fgetcsv($file);
$loader->columnMap = array( // $loader->columnMap = array(
'first name' => 'FirstName', // 'first name' => 'FirstName',
'bio' => 'Biography', // 'bio' => 'Biography',
'bday' => 'Birthday', // 'bday' => 'Birthday',
'teamtitle' => 'Team.Title', // test existing relation // 'teamtitle' => 'Team.Title', // test existing relation
'teamsize' => 'Team.TeamSize', // test existing relation // 'teamsize' => 'Team.TeamSize', // test existing relation
'salary' => 'Contract.Amount' // test relation creation // 'salary' => 'Contract.Amount' // test relation creation
); // );
$loader->hasHeaderRow = true; // $loader->hasHeaderRow = true;
$loader->relationCallbacks = array( // $loader->relationCallbacks = array(
'Team.Title' => array( // 'Team.Title' => array(
'relationname' => 'Team', // 'relationname' => 'Team',
'callback' => 'getTeamByTitle' // 'callback' => 'getTeamByTitle'
), // ),
// contract should be automatically discovered // // contract should be automatically discovered
); // );
$results = $loader->load($filepath); // $results = $loader->load($filepath);
//
// Test that right amount of columns was imported // // Test that right amount of columns was imported
$this->assertEquals(1, $results->Count(), 'Test correct count of imported data'); // $this->assertEquals(1, $results->Count(), 'Test correct count of imported data');
//
// Test of augumenting existing relation (created by fixture) // // Test of augumenting existing relation (created by fixture)
$testTeam = DataObject::get_one('CsvBulkLoaderTest_Team', null, null, '"Created" DESC'); // $testTeam = DataObject::get_one('CsvBulkLoaderTest_Team', null, null, '"Created" DESC');
$this->assertEquals('20', $testTeam->TeamSize, 'Augumenting existing has_one relation works'); // $this->assertEquals('20', $testTeam->TeamSize, 'Augumenting existing has_one relation works');
//
// Test of creating relation // // Test of creating relation
$testContract = DataObject::get_one('CsvBulkLoaderTest_PlayerContract'); // $testContract = DataObject::get_one('CsvBulkLoaderTest_PlayerContract');
$testPlayer = Dataobject::get_one("CsvBulkLoaderTest_Player", "\"FirstName\" = 'John'"); // $testPlayer = Dataobject::get_one("CsvBulkLoaderTest_Player", "\"FirstName\" = 'John'");
$this->assertEquals($testPlayer->ContractID, $testContract->ID, 'Creating new has_one relation works'); // $this->assertEquals($testPlayer->ContractID, $testContract->ID, 'Creating new has_one relation works');
//
// Test nested setting of relation properties // // Test nested setting of relation properties
$contractAmount = DBField::create('Currency', $compareRow[5])->RAW(); // $contractAmount = DBField::create('Currency', $compareRow[5])->RAW();
$this->assertEquals($testPlayer->Contract()->Amount, $contractAmount, 'Setting nested values in a relation works'); // $this->assertEquals($testPlayer->Contract()->Amount, $contractAmount, 'Setting nested values in a relation works');
//
fclose($file); // fclose($file);
} // }
/** /**
* Test import with custom identifiers by importing the data. * Test import with custom identifiers by importing the data.
@ -143,8 +143,9 @@ class CsvBulkLoaderTest extends SapphireTest {
'ExternalIdentifier' => 'ExternalIdentifier' 'ExternalIdentifier' => 'ExternalIdentifier'
); );
$results = $loader->load($filepath); $results = $loader->load($filepath);
$createdPlayers = $results->Created();
$player = DataObject::get_by_id('CsvBulkLoaderTest_Player', 1); $player = $createdPlayers->First();
$this->assertEquals($player->FirstName, 'John'); $this->assertEquals($player->FirstName, 'John');
$this->assertEquals($player->Biography, 'He\'s a good guy', 'test updating of duplicate imports within the same import works'); $this->assertEquals($player->Biography, 'He\'s a good guy', 'test updating of duplicate imports within the same import works');
@ -152,7 +153,8 @@ class CsvBulkLoaderTest extends SapphireTest {
$filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithIdUpdated.csv'; $filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithIdUpdated.csv';
$results = $loader->load($filepath); $results = $loader->load($filepath);
$player = DataObject::get_by_id('CsvBulkLoaderTest_Player', 1); // HACK need to update the loaded record from the database
$player = DataObject::get_by_id('CsvBulkLoaderTest_Player', $player->ID);
$this->assertEquals($player->FirstName, 'JohnUpdated', 'Test updating of existing records works'); $this->assertEquals($player->FirstName, 'JohnUpdated', 'Test updating of existing records works');
$this->assertEquals($player->Biography, 'He\'s a good guy', 'Test retaining of previous information on duplicate when overwriting with blank field'); $this->assertEquals($player->Biography, 'He\'s a good guy', 'Test retaining of previous information on duplicate when overwriting with blank field');
} }
@ -167,7 +169,8 @@ class CsvBulkLoaderTest extends SapphireTest {
'IsRegistered' => 'IsRegistered' 'IsRegistered' => 'IsRegistered'
); );
$results = $loader->load($filepath); $results = $loader->load($filepath);
$player = DataObject::get_by_id('CsvBulkLoaderTest_Player', 1); $createdPlayers = $results->Created();
$player = $createdPlayers->First();
$this->assertEquals($player->FirstName, 'Customized John'); $this->assertEquals($player->FirstName, 'Customized John');
$this->assertEquals($player->Biography, "He's a good guy"); $this->assertEquals($player->Biography, "He's a good guy");
$this->assertEquals($player->IsRegistered, "1"); $this->assertEquals($player->IsRegistered, "1");