From 148d9c28ca3bac84241f2afb5f5d6bb910bd8893 Mon Sep 17 00:00:00 2001
From: ABW <andrzejl@syncad.com>
Date: Tue, 22 Sep 2020 02:13:20 +0200
Subject: [PATCH] [ABW]: list_comments with by_root, by_parent, by_last_update
 and by_author_last_update have their main parameters required (nonempty)
 list_comments_by_cashout_time requires valid start post when not completely
 skipped [Fix] list_comments_by_cashout_time and list_comments_by_permlink
 would in some cases select special null post in inner query giving one post
 below limit [Fix] list_comments_by_last_update incorrectly used found parent
 author id (matching it with post id) tests rebound to proper version (above
 and earlier changes required a lot of changes in database api tests)

---
 .gitlab-ci.yaml                     | 22 ++++++----------------
 hive/db/schema.py                   | 17 ++++++++++-------
 hive/server/database_api/methods.py | 12 ++++++------
 tests/tests_api                     |  2 +-
 4 files changed, 23 insertions(+), 30 deletions(-)

diff --git a/.gitlab-ci.yaml b/.gitlab-ci.yaml
index 432f55431..3184bf824 100644
--- a/.gitlab-ci.yaml
+++ b/.gitlab-ci.yaml
@@ -214,16 +214,6 @@ condenser_api_smoketest_negative:
     reports:
       junit: api_smoketest_condenser_api_negative.xml
 
-database_api_smoketest_old:
-  <<: *common_api_smoketest_job
-
-  script:
-    - scripts/ci_start_api_smoketest.sh localhost "$HIVEMIND_HTTP_PORT" test_database_api_patterns.tavern.yaml api_smoketest_database_api_old.xml
-
-  artifacts:
-    reports:
-      junit: api_smoketest_database_api_old.xml
-
 database_api_smoketest:
   <<: *common_api_smoketest_job
 
@@ -244,25 +234,25 @@ database_api_smoketest_negative:
     reports:
       junit: api_smoketest_database_api_negative.xml
 
-follow_api_smoketest_old:
+follow_api_smoketest:
   <<: *common_api_smoketest_job
 
   script:
-    - scripts/ci_start_api_smoketest.sh localhost "$HIVEMIND_HTTP_PORT" test_follow_api_patterns.tavern.yaml api_smoketest_follow_api_old.xml
+    - scripts/ci_start_api_smoketest.sh localhost "$HIVEMIND_HTTP_PORT" follow_api_patterns/ api_smoketest_follow_api.xml
 
   artifacts:
     reports:
-      junit: api_smoketest_follow_api_old.xml
+      junit: api_smoketest_follow_api.xml
 
-follow_api_smoketest:
+follow_api_smoketest_negative:
   <<: *common_api_smoketest_job
 
   script:
-    - scripts/ci_start_api_smoketest.sh localhost "$HIVEMIND_HTTP_PORT" follow_api_patterns/ api_smoketest_follow_api.xml
+    - scripts/ci_start_api_smoketest.sh localhost "$HIVEMIND_HTTP_PORT" follow_api_negative/ api_smoketest_follow_api_negative.xml
 
   artifacts:
     reports:
-      junit: api_smoketest_follow_api.xml
+      junit: api_smoketest_follow_api_negative.xml
 
 tags_api_smoketest:
   <<: *common_api_smoketest_job
diff --git a/hive/db/schema.py b/hive/db/schema.py
index 40c556a68..e6706004e 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -993,7 +993,7 @@ def setup(db):
         BEGIN
 
         _VOTER_ID = find_account_id( _VOTER, _VOTER != '' );
-        _POST_ID = find_comment_id( _AUTHOR, _PERMLINK, _AUTHOR != '' OR _PERMLINK != '' );
+        _POST_ID = find_comment_id( _AUTHOR, _PERMLINK, True );
 
         RETURN QUERY
         (
@@ -1041,7 +1041,7 @@ def setup(db):
         BEGIN
 
         _VOTER_ID = find_account_id( _VOTER, _VOTER != '' );
-        _POST_ID = find_comment_id( _AUTHOR, _PERMLINK, _AUTHOR != '' OR _PERMLINK != '' );
+        _POST_ID = find_comment_id( _AUTHOR, _PERMLINK, True );
 
         RETURN QUERY
         (
@@ -1132,7 +1132,7 @@ def setup(db):
         DECLARE
           __post_id INT;
         BEGIN
-          __post_id = find_comment_id(_author,_permlink, False);
+          __post_id = find_comment_id(_author,_permlink, True);
           RETURN QUERY
           SELECT
               hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
@@ -1156,7 +1156,7 @@ def setup(db):
                   AND NOT hp1.is_muted
                   AND hp1.cashout_time > _cashout_time
                   OR hp1.cashout_time = _cashout_time
-                  AND hp1.id >= __post_id
+                  AND hp1.id >= __post_id AND hp1.id != 0
               ORDER BY
                   hp1.cashout_time ASC,
                   hp1.id ASC
@@ -1207,6 +1207,7 @@ def setup(db):
                   AND ha.name > _author
                   OR ha.name = _author
                   AND hpd.permlink >= _permlink
+                  AND hp1.id != 0
               ORDER BY
                   ha.name ASC,
                   hpd.permlink ASC
@@ -1335,9 +1336,9 @@ def setup(db):
         $function$
         DECLARE
           __post_id INT;
-          __parent_id INT;
+          __parent_author_id INT;
         BEGIN
-          __parent_id = find_account_id(_parent_author, True);
+          __parent_author_id = find_account_id(_parent_author, True);
           __post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
           RETURN QUERY
           SELECT
@@ -1357,10 +1358,12 @@ def setup(db):
                 hp1.id
               FROM
                 hive_posts hp1
+              JOIN
+                hive_posts hp2 ON hp1.parent_id = hp2.id
               WHERE
                 hp1.counter_deleted = 0
                 AND NOT hp1.is_muted
-                AND hp1.parent_id = __parent_id
+                AND hp2.author_id = __parent_author_id
                 AND (
                   hp1.updated_at < _updated_at
                   OR hp1.updated_at = _updated_at
diff --git a/hive/server/database_api/methods.py b/hive/server/database_api/methods.py
index 1f77de630..b524a012e 100644
--- a/hive/server/database_api/methods.py
+++ b/hive/server/database_api/methods.py
@@ -41,9 +41,9 @@ async def list_comments(context, start: list, limit: int, order: str):
     elif order == 'by_root':
         assert len(start) == 4, "Expecting 4 arguments"
         root_author = start[0]
-        valid_account(root_author, allow_empty=True)
+        valid_account(root_author)
         root_permlink = start[1]
-        valid_permlink(root_permlink, allow_empty=True)
+        valid_permlink(root_permlink)
         start_post_author = start[2]
         valid_account(start_post_author, allow_empty=True)
         start_post_permlink = start[3]
@@ -53,9 +53,9 @@ async def list_comments(context, start: list, limit: int, order: str):
     elif order == 'by_parent':
         assert len(start) == 4, "Expecting 4 arguments"
         parent_author = start[0]
-        valid_account(parent_author, allow_empty=True)
+        valid_account(parent_author)
         parent_permlink = start[1]
-        valid_permlink(parent_permlink, allow_empty=True)
+        valid_permlink(parent_permlink)
         start_post_author = start[2]
         valid_account(start_post_author, allow_empty=True)
         start_post_permlink = start[3]
@@ -65,7 +65,7 @@ async def list_comments(context, start: list, limit: int, order: str):
     elif order == 'by_last_update':
         assert len(start) == 4, "Expecting 4 arguments"
         parent_author = start[0]
-        valid_account(parent_author, allow_empty=True)
+        valid_account(parent_author)
         updated_at = start[1]
         valid_date(updated_at)
         start_post_author = start[2]
@@ -77,7 +77,7 @@ async def list_comments(context, start: list, limit: int, order: str):
     elif order == 'by_author_last_update':
         assert len(start) == 4, "Expecting 4 arguments"
         author = start[0]
-        valid_account(author, allow_empty=True)
+        valid_account(author)
         updated_at = start[1]
         valid_date(updated_at)
         start_post_author = start[2]
diff --git a/tests/tests_api b/tests/tests_api
index c83d55ec4..3cd1c5651 160000
--- a/tests/tests_api
+++ b/tests/tests_api
@@ -1 +1 @@
-Subproject commit c83d55ec47ba9f44d80397dbf82190bc02233625
+Subproject commit 3cd1c56516255b8ed00fa4809da0982c27875900
-- 
GitLab