From 0391230a254fe90c84a769f1349522d0cde43b3f Mon Sep 17 00:00:00 2001
From: ABW <andrzejl@syncad.com>
Date: Tue, 1 Dec 2020 00:58:24 +0100
Subject: [PATCH] [ABW]: find_tag_id/find_category_id moved to
 utility_functions.sql and changed so they follow example of find_comment_id
 with their error handling added extra promotion and new ignored accounts in
 mock data for observer tests observer in get_ranked_posts is now validated
 (it was actually validated already, but only after main query, now it happens
 as early as possible) fixed problem with default value of limit in
 get_ranked_posts (when explicit null was passed); also all string params now
 are empty instead of None

---
 hive/db/schema.py                             |  1 -
 .../bridge_get_ranked_post_for_all.sql        | 12 ++---
 ...bridge_get_ranked_post_for_communities.sql | 14 +++---
 .../bridge_get_ranked_post_for_tag.sql        | 12 ++---
 hive/db/sql_scripts/condenser_tags.sql        |  2 +-
 hive/db/sql_scripts/db_upgrade.sh             |  1 -
 hive/db/sql_scripts/find_tag_id.sql           | 41 ----------------
 hive/db/sql_scripts/utility_functions.sql     | 48 +++++++++++++++++++
 hive/server/bridge_api/methods.py             | 12 ++---
 hive/server/condenser_api/tags.py             |  2 +-
 mock_data/block_data/community_op/flow.txt    |  1 +
 .../mock_block_data_community.json            |  9 ++++
 .../follow_op/mock_block_data_follow.json     |  2 +-
 tests/tests_api                               |  2 +-
 14 files changed, 87 insertions(+), 72 deletions(-)
 delete mode 100644 hive/db/sql_scripts/find_tag_id.sql

diff --git a/hive/db/schema.py b/hive/db/schema.py
index f4d2d9c9e..4e0a29d34 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -563,7 +563,6 @@ def setup(db):
       "update_feed_cache.sql",
       "payout_stats_view.sql",
       "update_hive_posts_mentions.sql",
-      "find_tag_id.sql",
       "mutes.sql",
       "bridge_get_ranked_post_type.sql",
       "bridge_get_ranked_post_for_communities.sql",
diff --git a/hive/db/sql_scripts/bridge_get_ranked_post_for_all.sql b/hive/db/sql_scripts/bridge_get_ranked_post_for_all.sql
index 229985da2..426d7b798 100644
--- a/hive/db/sql_scripts/bridge_get_ranked_post_for_all.sql
+++ b/hive/db/sql_scripts/bridge_get_ranked_post_for_all.sql
@@ -8,7 +8,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   RETURN QUERY SELECT
       hp.id,
       hp.author,
@@ -76,7 +76,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT hp.sc_hot INTO __hot_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
@@ -220,7 +220,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT ( hp.payout + hp.pending_payout ) INTO __payout_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
@@ -294,7 +294,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT ( hp.payout + hp.pending_payout ) INTO __payout_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
@@ -369,7 +369,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT hp.promoted INTO __promoted_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
@@ -442,7 +442,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT hp.sc_trend INTO __trending_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
diff --git a/hive/db/sql_scripts/bridge_get_ranked_post_for_communities.sql b/hive/db/sql_scripts/bridge_get_ranked_post_for_communities.sql
index a6f1f91f7..4658fcf76 100644
--- a/hive/db/sql_scripts/bridge_get_ranked_post_for_communities.sql
+++ b/hive/db/sql_scripts/bridge_get_ranked_post_for_communities.sql
@@ -6,7 +6,7 @@ $function$
 DECLARE
   __observer_id INT;
 BEGIN
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   RETURN QUERY SELECT
       hp.id,
       hp.author,
@@ -67,7 +67,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT hp.sc_trend INTO __trending_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
@@ -142,7 +142,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT hp.promoted INTO __promoted_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
@@ -217,7 +217,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id( _observer, False );
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT ( hp.payout + hp.pending_payout ) INTO __payout_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
@@ -292,7 +292,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT ( hp.payout + hp.pending_payout ) INTO __payout_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
@@ -438,7 +438,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   IF __post_id <> 0 THEN
       SELECT hp.sc_hot INTO __hot_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
@@ -511,7 +511,7 @@ DECLARE
   __observer_id INT;
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   RETURN QUERY SELECT
       hp.id,
       hp.author,
diff --git a/hive/db/sql_scripts/bridge_get_ranked_post_for_tag.sql b/hive/db/sql_scripts/bridge_get_ranked_post_for_tag.sql
index dd0983867..7c40c81a7 100644
--- a/hive/db/sql_scripts/bridge_get_ranked_post_for_tag.sql
+++ b/hive/db/sql_scripts/bridge_get_ranked_post_for_tag.sql
@@ -10,7 +10,7 @@ DECLARE
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
   __hive_tag = ARRAY_APPEND( __hive_tag, find_tag_id( _tag, True ));
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   RETURN QUERY SELECT
       hp.id,
       hp.author,
@@ -85,7 +85,7 @@ BEGIN
       SELECT hp.sc_hot INTO __hot_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
   __hive_tag = ARRAY_APPEND( __hive_tag, find_tag_id( _tag, True ));
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   RETURN QUERY SELECT
       hp.id,
       hp.author,
@@ -233,7 +233,7 @@ BEGIN
       SELECT ( hp.payout + hp.pending_payout ) INTO __payout_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
   __hive_category = find_category_id( _category, True );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   RETURN QUERY SELECT
       hp.id,
       hp.author,
@@ -310,7 +310,7 @@ BEGIN
   END IF;
   __hive_category = find_category_id( _category, True );
   __head_block_time = head_block_time();
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   RETURN QUERY SELECT
       hp.id,
       hp.author,
@@ -386,7 +386,7 @@ BEGIN
       SELECT hp.promoted INTO __promoted_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
   __hive_tag = ARRAY_APPEND( __hive_tag,  find_tag_id( _tag, True ) );
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   RETURN QUERY SELECT
       hp.id,
       hp.author,
@@ -461,7 +461,7 @@ BEGIN
       SELECT hp.sc_trend INTO __trending_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
   __hive_tag = ARRAY_APPEND( __hive_tag, find_tag_id( _tag, True ));
-  __observer_id = find_account_id(_observer, False);
+  __observer_id = find_account_id( _observer, True );
   RETURN QUERY SELECT
       hp.id,
       hp.author,
diff --git a/hive/db/sql_scripts/condenser_tags.sql b/hive/db/sql_scripts/condenser_tags.sql
index 0812832b4..684da1c61 100644
--- a/hive/db/sql_scripts/condenser_tags.sql
+++ b/hive/db/sql_scripts/condenser_tags.sql
@@ -26,7 +26,7 @@ DECLARE
   __category_id INT;
   __payout_limit hive_posts.payout%TYPE;
 BEGIN
-  __category_id = find_category_id( _category, _category <> '' );
+  __category_id = find_category_id( _category, True );
   IF __category_id <> 0 THEN
       SELECT SUM(hp.payout + hp.pending_payout) INTO __payout_limit
       FROM hive_posts hp
diff --git a/hive/db/sql_scripts/db_upgrade.sh b/hive/db/sql_scripts/db_upgrade.sh
index c45d40913..a6ec3b57f 100755
--- a/hive/db/sql_scripts/db_upgrade.sh
+++ b/hive/db/sql_scripts/db_upgrade.sh
@@ -21,7 +21,6 @@ for sql in postgres_handle_view_changes.sql \
           update_feed_cache.sql \
           payout_stats_view.sql \
           update_hive_posts_mentions.sql \
-          find_tag_id.sql \
           mutes.sql \
           bridge_get_ranked_post_type.sql \
           bridge_get_ranked_post_for_communities.sql \
diff --git a/hive/db/sql_scripts/find_tag_id.sql b/hive/db/sql_scripts/find_tag_id.sql
deleted file mode 100644
index f35fef406..000000000
--- a/hive/db/sql_scripts/find_tag_id.sql
+++ /dev/null
@@ -1,41 +0,0 @@
-DROP FUNCTION IF EXISTS public.find_tag_id CASCADE;
-CREATE OR REPLACE FUNCTION public.find_tag_id(
-    in _tag_name hive_tag_data.tag%TYPE,
-    in _check BOOLEAN
-)
-RETURNS INTEGER
-LANGUAGE 'plpgsql' STABLE
-AS
-$BODY$
-DECLARE
-  __tag_id INTEGER;
-BEGIN
-  SELECT INTO __tag_id COALESCE( ( SELECT id FROM hive_tag_data WHERE tag=_tag_name ), 0 );
-  IF _check AND __tag_id = 0 THEN
-      RAISE EXCEPTION 'Tag % does not exist', _tag_name;
-  END IF;
-  RETURN __tag_id;
-END
-$BODY$
-;
-
-DROP FUNCTION IF EXISTS public.find_category_id CASCADE;
-CREATE OR REPLACE FUNCTION public.find_category_id(
-    in _category_name hive_category_data.category%TYPE,
-    in _check BOOLEAN
-)
-RETURNS INTEGER
-LANGUAGE 'plpgsql' STABLE
-AS
-$BODY$
-DECLARE
-  __category_id INTEGER;
-BEGIN
-  SELECT INTO __category_id COALESCE( ( SELECT id FROM hive_category_data WHERE category=_category_name ), 0 );
-  IF _check AND __category_id = 0 THEN
-      RAISE EXCEPTION 'Category % does not exist', _category_name;
-  END IF;
-  RETURN __category_id;
-END
-$BODY$
-;
diff --git a/hive/db/sql_scripts/utility_functions.sql b/hive/db/sql_scripts/utility_functions.sql
index 098af5bf6..896de4fe5 100644
--- a/hive/db/sql_scripts/utility_functions.sql
+++ b/hive/db/sql_scripts/utility_functions.sql
@@ -76,3 +76,51 @@ BEGIN
 END
 $function$
 ;
+
+DROP FUNCTION IF EXISTS public.find_tag_id CASCADE
+;
+CREATE OR REPLACE FUNCTION public.find_tag_id(
+    in _tag_name hive_tag_data.tag%TYPE,
+    in _check BOOLEAN
+)
+RETURNS INTEGER
+LANGUAGE 'plpgsql' STABLE
+AS
+$function$
+DECLARE
+  __tag_id INT = 0;
+BEGIN
+  IF (_tag_name <> '') THEN
+    SELECT INTO __tag_id COALESCE( ( SELECT id FROM hive_tag_data WHERE tag=_tag_name ), 0 );
+    IF _check AND __tag_id = 0 THEN
+      RAISE EXCEPTION 'Tag % does not exist', _tag_name;
+    END IF;
+  END IF;
+  RETURN __tag_id;
+END
+$function$
+;
+
+DROP FUNCTION IF EXISTS public.find_category_id CASCADE
+;
+CREATE OR REPLACE FUNCTION public.find_category_id(
+    in _category_name hive_category_data.category%TYPE,
+    in _check BOOLEAN
+)
+RETURNS INTEGER
+LANGUAGE 'plpgsql' STABLE
+AS
+$function$
+DECLARE
+  __category_id INT = 0;
+BEGIN
+  IF (_category_name <> '') THEN
+    SELECT INTO __category_id COALESCE( ( SELECT id FROM hive_category_data WHERE category=_category_name ), 0 );
+    IF _check AND __category_id = 0 THEN
+      RAISE EXCEPTION 'Category % does not exist', _category_name;
+    END IF;
+  END IF;
+  RETURN __category_id;
+END
+$function$
+;
diff --git a/hive/server/bridge_api/methods.py b/hive/server/bridge_api/methods.py
index 1250cb1d9..c9fb5ef0e 100644
--- a/hive/server/bridge_api/methods.py
+++ b/hive/server/bridge_api/methods.py
@@ -222,7 +222,7 @@ async def _get_ranked_posts_for_all( db, sort:str, start_author:str, start_perml
 
 @return_error_info
 async def get_ranked_posts(context, sort:str, start_author:str='', start_permlink:str='',
-                           limit:int=20, tag:str=None, observer:str=None):
+                           limit:int=20, tag:str='', observer:str=''):
     """Query posts, sorted by given method."""
     supported_sort_list = ['trending', 'hot', 'created', 'promoted', 'payout', 'payout_comments', 'muted']
     assert sort in supported_sort_list, "Unsupported sort, valid sorts: {}".format(", ".join(supported_sort_list))
@@ -241,11 +241,11 @@ async def get_ranked_posts(context, sort:str, start_author:str='', start_permlin
             posts.append(post)
         return posts
 
-    valid_account(start_author, allow_empty=True)
-    valid_permlink(start_permlink, allow_empty=True)
-    valid_limit(limit, 100, 20)
-    valid_tag(tag, allow_empty=True)
-    valid_account(observer, allow_empty=(tag != "my"))
+    start_author = valid_account(start_author, allow_empty=True)
+    start_permlink = valid_permlink(start_permlink, allow_empty=True)
+    limit = valid_limit(limit, 100, 20)
+    tag = valid_tag(tag, allow_empty=True)
+    observer = valid_account(observer, allow_empty=(tag != "my"))
 
     if tag == "my":
         result = await _get_ranked_posts_for_observer_communities(db, sort, start_author, start_permlink, limit, observer)
diff --git a/hive/server/condenser_api/tags.py b/hive/server/condenser_api/tags.py
index d6f05ca2b..d63cd62e0 100644
--- a/hive/server/condenser_api/tags.py
+++ b/hive/server/condenser_api/tags.py
@@ -16,7 +16,7 @@ async def get_trending_tags(context, start_tag: str = '', limit: int = 250):
     """Get top 250 trending tags among pending posts, with stats."""
 
     limit = valid_limit(limit, 250, 250)
-    start_tag = valid_tag(start_tag or '', allow_empty=True)
+    start_tag = valid_tag(start_tag, allow_empty=True)
 
     sql = "SELECT * FROM condenser_get_trending_tags( (:tag)::VARCHAR, :limit )"
 
diff --git a/mock_data/block_data/community_op/flow.txt b/mock_data/block_data/community_op/flow.txt
index ceb7dcbd7..dd8778f8d 100644
--- a/mock_data/block_data/community_op/flow.txt
+++ b/mock_data/block_data/community_op/flow.txt
@@ -11,6 +11,7 @@ account_create_operation( `hive-149232` )
 account_create_operation( `hive-104647` )
 comment_operation( `hive-135485`, `test-safari`, `secrets1`)
 comment_operation( `hive-135485`, `test-safari`, `secrets2`)
+transfer_opearation( `test-safari`, `null`, `0.010666 HBD`, `@test-safari/secrets2` ) - post promotion (with bad amount precision to see what happens - rounding occurs)
 comment_operation( `hive-117600`, `test-safari`, `secrets3`)
 comment_operation( `hive-117600`, `test-safari`, `secrets4`)
 comment_operation( `hive-117600`, `test-safari`, `secrets5`)
diff --git a/mock_data/block_data/community_op/mock_block_data_community.json b/mock_data/block_data/community_op/mock_block_data_community.json
index 081b18678..4804a706a 100644
--- a/mock_data/block_data/community_op/mock_block_data_community.json
+++ b/mock_data/block_data/community_op/mock_block_data_community.json
@@ -436,6 +436,15 @@
               "json_metadata": "{}"
             }
           },
+          {
+            "type": "transfer_operation",
+            "value": {
+              "from": "test-safari",
+              "to": "null",
+              "amount": "0.010666 HBD",
+              "memo": "@test-safari/secrets2"
+            }
+          },
           {
             "type": "comment_operation",
             "value": {
diff --git a/mock_data/block_data/follow_op/mock_block_data_follow.json b/mock_data/block_data/follow_op/mock_block_data_follow.json
index 2d6c189d2..f7ac08d2d 100644
--- a/mock_data/block_data/follow_op/mock_block_data_follow.json
+++ b/mock_data/block_data/follow_op/mock_block_data_follow.json
@@ -748,7 +748,7 @@
                 "ignoreall"
               ],
               "id": "follow",
-              "json": "[\"follow\",{\"follower\":\"ignoreall\",\"following\":[\"gtg\",\"alice\",\"davr86\"],\"what\":[\"ignore\"]}]"
+              "json": "[\"follow\",{\"follower\":\"ignoreall\",\"following\":[\"gtg\",\"alice\",\"davr86\",\"fyrstikken\",\"gavvet\",\"ooak\",\"kental\",\"r4fken\",\"roland.haynes\",\"agartha\",\"feline1991\"],\"what\":[\"ignore\"]}]"
             }
           }
         ]
diff --git a/tests/tests_api b/tests/tests_api
index 87c445782..204e7fa21 160000
--- a/tests/tests_api
+++ b/tests/tests_api
@@ -1 +1 @@
-Subproject commit 87c4457827200ef54f8fe64f942df641a9f1b1af
+Subproject commit 204e7fa2128efdfd5c197bc2ab76fc37e414cc88
-- 
GitLab