From ae27f7cf34717514000b1c4bd4c69a35a8ec658a Mon Sep 17 00:00:00 2001 From: thrillfall Date: Tue, 24 Aug 2021 22:48:46 +0200 Subject: [PATCH] test unique index constraint for episode actions --- tests/Helper/DatabaseTransaction.php | 24 +++++++ .../EpisodeActionGuidMigrationTest.php | 65 +++++++++++++++++++ tests/bootstrap.php | 2 +- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/Helper/DatabaseTransaction.php create mode 100644 tests/Integration/EpisodeActionGuidMigrationTest.php diff --git a/tests/Helper/DatabaseTransaction.php b/tests/Helper/DatabaseTransaction.php new file mode 100644 index 0000000..03b068f --- /dev/null +++ b/tests/Helper/DatabaseTransaction.php @@ -0,0 +1,24 @@ +get(IDBConnection::class); + + $db->beginTransaction(); + } + + public function rollbackTransation() { + /* @var $db IDBConnection */ + $db = OC::$server->get(IDBConnection::class); + + $db->rollBack(); + } + +} diff --git a/tests/Integration/EpisodeActionGuidMigrationTest.php b/tests/Integration/EpisodeActionGuidMigrationTest.php new file mode 100644 index 0000000..9b36cee --- /dev/null +++ b/tests/Integration/EpisodeActionGuidMigrationTest.php @@ -0,0 +1,65 @@ +container = $app->getContainer(); + } + + /** + * @before + */ + public function before() + { + $this->startTransaction(); + } + + public function testCreateSameEpisodeActionTriggersUniqueConstraintViolationException() + { + self::expectException(UniqueConstraintViolationException::class); + + /** @var EpisodeActionWriter $episodeActionWriter */ + $episodeActionWriter = $this->container->get('OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter'); + + $episodeActionEntity = new EpisodeActionEntity(); + $episodeActionEntity->setPodcast("https://podcast_01.url"); + $episodeActionEntity->setEpisode("https://episode_01.url"); + $episodeActionEntity->setAction("PLAY"); + $episodeActionEntity->setPosition(5); + $episodeActionEntity->setStarted(0); + $episodeActionEntity->setTotal(123); + $episodeActionEntity->setTimestamp("Mon Aug 23 01:58:56 GMT+02:00 2021"); + $episodeActionEntity->setUserId("user0@127.0.0.1"); + $episodeActionWriter->save($episodeActionEntity); + + $episodeActionWriter->save($episodeActionEntity); + + } + + /** + * @after + */ + public function after() + { + $this->rollbackTransation(); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b5ef026..6616b67 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -5,7 +5,7 @@ if (!defined('PHPUNIT_RUN')) { } require_once __DIR__ . '/../../../lib/base.php'; - +require_once __DIR__ . '/Helper/DatabaseTransaction.php'; // Fix for "Autoload path not allowed: .../tests/lib/testcase.php" OC::$loader->addValidRoot(OC::$SERVERROOT . '/tests');