diff options
author | Marcel Klehr <mklehr@gmx.net> | 2020-09-11 18:53:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-11 18:53:54 +0300 |
commit | 4711d6507fd3e736fe15b104c7bbe54d276fac5b (patch) | |
tree | 4f28f7e40e2864e9613776091a4d392f91f46fa3 /tests | |
parent | d94e35e379201282a84a0e3a1b8e56caf2bb9398 (diff) |
DB: Refactor bookmarks queries (#1209)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/BookmarkControllerTest.php | 26 | ||||
-rw-r--r-- | tests/BookmarkMapperTest.php | 33 | ||||
-rw-r--r-- | tests/FindTest.php | 20 | ||||
-rw-r--r-- | tests/TagMapperTest.php | 7 |
4 files changed, 40 insertions, 46 deletions
diff --git a/tests/BookmarkControllerTest.php b/tests/BookmarkControllerTest.php index 2ea2b05f..77a1b2b0 100644 --- a/tests/BookmarkControllerTest.php +++ b/tests/BookmarkControllerTest.php @@ -20,6 +20,7 @@ use OCA\Bookmarks\Exception\AlreadyExistsError; use OCA\Bookmarks\Exception\UnsupportedOperation; use OCA\Bookmarks\Exception\UrlParseError; use OCA\Bookmarks\Exception\UserLimitExceededError; +use OCA\Bookmarks\QueryParameters; use OCA\Bookmarks\Service\Authorizer; use OCA\Bookmarks\Service\BookmarkService; use OCA\Bookmarks\Service\FolderService; @@ -358,7 +359,8 @@ class BookmarkControllerTest extends TestCase { $this->assertEquals('success', $res->getData()['status'], var_export($res->getData(), true)); // the bookmark should exist - $this->bookmarkMapper->findByUrl($this->userId, 'https://www.heise.de'); + $params = new QueryParameters(); + $this->assertCount(1, $this->bookmarkMapper->findAll($this->userId, $params->setUrl('https://www.heise.de'))); // user should see this bookmark $output = $this->controller->getBookmarks(); @@ -435,13 +437,8 @@ class BookmarkControllerTest extends TestCase { $id = $res->getData()['item']['id']; $this->controller->deleteBookmark($id); - $exception = null; - try { - $this->bookmarkMapper->findByUrl($this->userId, 'https://www.google.com'); - } catch (\Exception $e) { - $exception = $e; - } - $this->assertInstanceOf(DoesNotExistException::class, $exception, 'Expected bookmark not to exist and throw'); + $params = new QueryParameters(); + $this->assertCount(0, $this->bookmarkMapper->findAll($this->userId, $params->setUrl('https://www.google.com'))); } /** @@ -629,7 +626,9 @@ class BookmarkControllerTest extends TestCase { $this->assertEquals('success', $res->getData()['status'], var_export($res->getData(), true)); // the bookmark should exist - $this->bookmarkMapper->findByUrl($this->userId, 'https://www.heise.de'); + + $params = new QueryParameters(); + $this->assertCount(1, $this->bookmarkMapper->findAll($this->userId, $params->setUrl('https://www.heise.de'))); // user should see this bookmark $this->authorizer->setUserId($this->userId); @@ -689,13 +688,8 @@ class BookmarkControllerTest extends TestCase { $res = $this->otherController->deleteBookmark($id); $this->assertEquals('success', $res->getData()['status'], var_export($res->getData(), true)); - $exception = null; - try { - $this->bookmarkMapper->findByUrl($this->userId, 'https://www.google.com'); - } catch (\Exception $e) { - $exception = $e; - } - $this->assertInstanceOf(DoesNotExistException::class, $exception, 'Expected bookmark not to exist and throw'); + $params = new QueryParameters(); + $this->assertCount(0, $this->bookmarkMapper->findAll($this->userId, $params->setUrl('https://www.google.com'))); } /** diff --git a/tests/BookmarkMapperTest.php b/tests/BookmarkMapperTest.php index 744bd56e..4016b611 100644 --- a/tests/BookmarkMapperTest.php +++ b/tests/BookmarkMapperTest.php @@ -8,6 +8,7 @@ use OCA\Bookmarks\Db; use OCA\Bookmarks\Exception\AlreadyExistsError; use OCA\Bookmarks\Exception\UrlParseError; use OCA\Bookmarks\Exception\UserLimitExceededError; +use OCA\Bookmarks\QueryParameters; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\MultipleObjectsReturnedException; @@ -79,31 +80,14 @@ class BookmarkMapperTest extends TestCase { * @throws UrlParseError * @throws UserLimitExceededError */ - public function testFindByUrl(Entity $bookmark) { - $bookmark->setUserId($this->userId); - $bookmark = $this->bookmarkMapper->insert($bookmark); - - $foundEntity = $this->bookmarkMapper->findByUrl($this->userId, $bookmark->getUrl()); - $this->assertSame($bookmark->getUrl(), $foundEntity->getUrl()); - } - - /** - * @depends testInsertAndFind - * @depends testFindByUrl - * @dataProvider singleBookmarksProvider - * @param Entity $bookmark - * @return void - * @throws AlreadyExistsError - * @throws DoesNotExistException - * @throws MultipleObjectsReturnedException - * @throws UrlParseError - * @throws UserLimitExceededError - */ public function testUpdate(Entity $bookmark) { $bookmark->setUserId($this->userId); $bookmark = $this->bookmarkMapper->insert($bookmark); - $entity = $this->bookmarkMapper->findByUrl($this->userId, $bookmark->getUrl()); + $params = new QueryParameters(); + $entities = $this->bookmarkMapper->findAll($this->userId, $params->setUrl($bookmark->getUrl())); + $this->assertCount(1, $entities); + $entity = $entities[0]; $entity->setTitle('foobar'); $this->bookmarkMapper->update($entity); $foundEntity = $this->bookmarkMapper->find($entity->getId()); @@ -112,7 +96,6 @@ class BookmarkMapperTest extends TestCase { /** * @depends testInsertAndFind - * @depends testFindByUrl * @dataProvider singleBookmarksProvider * @param Entity $bookmark * @return void @@ -126,7 +109,11 @@ class BookmarkMapperTest extends TestCase { $bookmark->setUserId($this->userId); $bookmark = $this->bookmarkMapper->insert($bookmark); - $foundEntity = $this->bookmarkMapper->findByUrl($this->userId, $bookmark->getUrl()); + $params = new QueryParameters(); + + $foundEntities = $this->bookmarkMapper->findAll($this->userId, $params->setUrl($bookmark->getUrl())); + $this->assertCount(1, $foundEntities); + $foundEntity = $foundEntities[0]; $this->bookmarkMapper->delete($foundEntity); $this->expectException(DoesNotExistException::class); $this->bookmarkMapper->find($foundEntity->getId()); diff --git a/tests/FindTest.php b/tests/FindTest.php index bcd6b4ad..2ec72adb 100644 --- a/tests/FindTest.php +++ b/tests/FindTest.php @@ -70,28 +70,38 @@ class FindTest extends TestCase { } public function testFindAll() { - $bookmarks = $this->bookmarkMapper->findAll($this->userId, ['wikipedia'], new QueryParameters()); + $params = new QueryParameters(); + $bookmarks = $this->bookmarkMapper->findAll($this->userId, $params->setSearch(['wikipedia'])); $this->assertCount(1, $bookmarks); } public function testFindAllWithAnd() { - $bookmarks = $this->bookmarkMapper->findAll($this->userId, ['wikipedia', 'nextcloud'], new QueryParameters()); + $params = new QueryParameters(); + $bookmarks = $this->bookmarkMapper->findAll($this->userId, $params->setSearch(['wikipedia', 'nextcloud'])); $this->assertCount(0, $bookmarks); - $bookmarks = $this->bookmarkMapper->findAll($this->userId, ['.com'], new QueryParameters()); + $params = new QueryParameters(); + $bookmarks = $this->bookmarkMapper->findAll($this->userId, $params->setSearch(['.com'])); $this->assertCount(2, $bookmarks); } public function testFindAllWithOr() { $params = new QueryParameters(); - $bookmarks = $this->bookmarkMapper->findAll($this->userId, ['wikipedia', 'nextcloud'], $params->setConjunction(QueryParameters::CONJ_OR)); + $bookmarks = $this->bookmarkMapper->findAll($this->userId, $params->setSearch(['wikipedia', 'nextcloud'])->setConjunction(QueryParameters::CONJ_OR)); $this->assertCount(2, $bookmarks); } public function testFindByTags() { - $bookmarks = $this->bookmarkMapper->findByTags($this->userId, ['one', 'three'], new QueryParameters()); + $params = new QueryParameters(); + $bookmarks = $this->bookmarkMapper->findALl($this->userId, $params->setTags(['one', 'three'])); + $this->assertCount(1, $bookmarks); + } + + public function testFindByTagsAndSearch() { + $params = new QueryParameters(); + $bookmarks = $this->bookmarkMapper->findALl($this->userId, $params->setTags(['one'])->setSearch(['php'])); $this->assertCount(1, $bookmarks); } diff --git a/tests/TagMapperTest.php b/tests/TagMapperTest.php index 00f764c1..900a0ca6 100644 --- a/tests/TagMapperTest.php +++ b/tests/TagMapperTest.php @@ -6,6 +6,7 @@ namespace OCA\Bookmarks\Tests; use OCA\Bookmarks\Db; use OCA\Bookmarks\Db\Bookmark; use OCA\Bookmarks\Exception\UrlParseError; +use OCA\Bookmarks\QueryParameters; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\AppFramework\QueryException; @@ -132,7 +133,8 @@ class TagMapperTest extends TestCase { * @throws UrlParseError */ public function testRemoveAllFrom(array $tags, Bookmark $bookmark) { - $bookmark = $this->bookmarkMapper->findByUrl($this->userId, $bookmark->getUrl()); + $params = new QueryParameters(); + $bookmark = $this->bookmarkMapper->findAll($this->userId, $params->setUrl($bookmark->getUrl()))[0]; $this->tagMapper->removeAllFrom($bookmark->getId()); $tags = $this->tagMapper->findByBookmark($bookmark->getId()); $this->assertEmpty($tags); @@ -149,7 +151,8 @@ class TagMapperTest extends TestCase { * @throws UrlParseError */ public function testSetOn(array $tags, Bookmark $bookmark) { - $bookmark = $this->bookmarkMapper->findByUrl($this->userId, $bookmark->getUrl()); + $params = new QueryParameters(); + $bookmark = $this->bookmarkMapper->findAll($this->userId, $params->setUrl($bookmark->getUrl()))[0]; $newTags = ['foo', 'bar']; $this->tagMapper->setOn($newTags, $bookmark->getId()); $actualTags = $this->tagMapper->findByBookmark($bookmark->getId()); |