diff --git a/src/Custom.php b/src/Custom.php index 00494e0..e4d2300 100644 --- a/src/Custom.php +++ b/src/Custom.php @@ -43,7 +43,12 @@ class Custom $stmt->bindValue($key, $value, $dataType); } if ($debug) echo '
SQL: ' . $stmt->queryString . '
'; - if ($stmt->execute()) return $stmt; + try { + if ($stmt->execute()) return $stmt; + } catch (PDOException $ex) { + $keyword = explode(' ', $query, 2)[0]; + throw new DocumentException("Error executing $keyword statement: " . $stmt->errorCode(), previous: $ex); + } $keyword = explode(' ', $query, 2)[0]; throw new DocumentException("Error executing $keyword statement: " . $stmt->errorCode()); } diff --git a/tests/integration/sqlite/CountTest.php b/tests/integration/sqlite/CountTest.php new file mode 100644 index 0000000..30e3c0d --- /dev/null +++ b/tests/integration/sqlite/CountTest.php @@ -0,0 +1,42 @@ +dbName = ThrowawayDb::create(); + } + + protected function tearDown(): void + { + ThrowawayDb::destroy($this->dbName); + parent::tearDown(); + } + + public function testAllSucceeds(): void + { + $count = Count::all(ThrowawayDb::TABLE); + $this->assertEquals(5, $count, 'There should have been 5 matching documents'); + } + + public function testByFieldsSucceedsForANumericRange(): void + { + $count = Count::byFields(ThrowawayDb::TABLE, [Field::BT('num_value', 10, 20)]); + $this->assertEquals(3, $count, 'There should have been 3 matching documents'); + } + + public function testByFieldsSucceedsForANonNumericRange(): void + { + $count = Count::byFields(ThrowawayDb::TABLE, [Field::BT('value', 'aardvark', 'apple')]); + $this->assertEquals(1, $count, 'There should have been 1 matching document'); + } +} diff --git a/tests/integration/sqlite/DocumentTest.php b/tests/integration/sqlite/DocumentTest.php new file mode 100644 index 0000000..ca5309b --- /dev/null +++ b/tests/integration/sqlite/DocumentTest.php @@ -0,0 +1,82 @@ +dbName = ThrowawayDb::create(); + } + + protected function tearDown(): void + { + ThrowawayDb::destroy($this->dbName); + parent::tearDown(); + } + + public function testInsertSucceeds(): void + { + Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble'))); + $doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); + $this->assertNotFalse($doc, 'There should have been a document inserted'); + $this->assertEquals('turkey', $doc->id, 'The ID was incorrect'); + $this->assertEquals('', $doc->value, 'The value was incorrect'); + $this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect'); + $this->assertNotNull($doc->sub, 'The sub-document should not have been null'); + $this->assertEquals('gobble', $doc->sub->foo, 'The sub-document foo property was incorrect'); + $this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect'); + } + + public function testInsertFailsForDuplicateKey(): void + { + $this->expectException(DocumentException::class); + Document::insert(ThrowawayDb::TABLE, new TestDocument('one')); + } + + public function testSaveSucceedsWhenADocumentIsInserted(): void + { + Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b'))); + $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); + $this->assertNotFalse($doc, 'There should have been a document returned'); + } + + public function testSaveSucceedsWhenADocumentIsUpdated(): void + { + Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44)); + $doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); + $this->assertNotFalse($doc, 'There should have been a document returned'); + $this->assertEquals(44, $doc->num_value, 'The numeric value was not updated'); + $this->assertNull($doc->sub, 'The sub-document should have been null'); + } + + public function testUpdateSucceedsWhenReplacingADocument(): void + { + Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z'))); + $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); + $this->assertNotFalse($doc, 'There should have been a document returned'); + $this->assertEquals('howdy', $doc->value, 'The value was incorrect'); + $this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect'); + $this->assertNotNull($doc->sub, 'The sub-document should not have been null'); + $this->assertEquals('y', $doc->sub->foo, 'The sub-document foo property was incorrect'); + $this->assertEquals('z', $doc->sub->bar, 'The sub-document bar property was incorrect'); + } + + public function testUpdateSucceedsWhenNoDocumentIsReplaced(): void + { + Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200')); + $doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class); + $this->assertFalse($doc, 'There should not have been a document returned'); + } +}