From b434c20c18a389521bb0a30fa7c0c025bb6dd50c Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Wed, 31 Oct 2012 16:51:36 +0100 Subject: Added unit test testinsertIfNotExistDontOverwrite. --- tests/lib/db.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'tests/lib') diff --git a/tests/lib/db.php b/tests/lib/db.php index 5d30f6ac46c..ead4b19b38e 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -93,4 +93,41 @@ class Test_DB extends UnitTestCase { $this->assertTrue($result); $this->assertEqual($result->numRows(), '4'); } + + public function testinsertIfNotExistDontOverwrite() { + $fullname = 'fullname test'; + $uri = 'uri_1'; + $carddata = 'This is a vCard'; + + // Normal test to have same known data inserted. + $query = OC_DB::prepare('INSERT INTO *PREFIX*'.$this->table2.' (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)'); + $result = $query->execute(array($fullname, $uri, $carddata)); + $this->assertTrue($result); + $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?'); + $result = $query->execute(array($uri)); + $this->assertTrue($result); + $row = $result->fetchRow(); + $this->assertArrayHasKey('carddata', $row); + $this->assertEqual($row['carddata'], $carddata); + $this->assertEqual($result->numRows(), '1'); + + // Try to insert a new row + $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2, + array( + 'fullname' => $fullname, + 'uri' => $uri, + )); + $this->assertTrue($result); + + $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?'); + $result = $query->execute(array($uri)); + $this->assertTrue($result); + $row = $result->fetchRow(); + $this->assertArrayHasKey('carddata', $row); + // Test that previously inserted data isn't overwritten + $this->assertEqual($row['carddata'], $carddata); + // And that a new row hasn't been inserted. + $this->assertEqual($result->numRows(), '1'); + + } } -- cgit v1.2.3