From b7243681dd70b25fdefc9fe62c63bab840691c94 Mon Sep 17 00:00:00 2001 From: Benjamin Gaussorgues Date: Wed, 12 Jun 2024 11:24:07 +0200 Subject: feat(dbal): add proper insert ignore conflict method for SQLite Signed-off-by: Benjamin Gaussorgues --- tests/lib/DB/AdapterTest.php | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/lib/DB/AdapterTest.php (limited to 'tests') diff --git a/tests/lib/DB/AdapterTest.php b/tests/lib/DB/AdapterTest.php new file mode 100644 index 00000000000..99b7cf4e099 --- /dev/null +++ b/tests/lib/DB/AdapterTest.php @@ -0,0 +1,66 @@ +connection = \OC::$server->getDatabaseConnection(); + $this->appId = uniqid('test_db_adapter', true); + } + + public function tearDown(): void { + $qb = $this->connection->getQueryBuilder(); + + $qb->delete('appconfig') + ->from('appconfig') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($this->appId))) + ->execute(); + } + + public function testInsertIgnoreOnConflictDuplicate(): void { + $configKey = uniqid('key', true); + $expected = [ + [ + 'configkey' => $configKey, + 'configvalue' => '1', + ] + ]; + $result = $this->connection->insertIgnoreConflict('appconfig', [ + 'appid' => $this->appId, + 'configkey' => $configKey, + 'configvalue' => '1', + ]); + $this->assertEquals(1, $result); + $rows = $this->getRows($configKey); + $this->assertSame($expected, $rows); + + + $result = $this->connection->insertIgnoreConflict('appconfig', [ + 'appid' => $this->appId, + 'configkey' => $configKey, + 'configvalue' => '2', + ]); + $this->assertEquals(0, $result); + $rows = $this->getRows($configKey); + $this->assertSame($expected, $rows); + } + + private function getRows(string $configKey): array { + $qb = $this->connection->getQueryBuilder(); + return $qb->select(['configkey', 'configvalue']) + ->from('appconfig') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($this->appId))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($configKey))) + ->execute() + ->fetchAll(); + } +} -- cgit v1.2.3