Skip to content

Commit

Permalink
fix 1 test, fix bugs, make cache key into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
northdpole committed Oct 24, 2023
1 parent a9d57ad commit d18184b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 29 deletions.
14 changes: 6 additions & 8 deletions application/database/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import uuid

from application.utils.gap_analysis import get_path_score
from application.utils.hash import make_array_hash
from application.utils.hash import make_array_hash, make_cache_key


from .. import sqla # type: ignore
Expand Down Expand Up @@ -1654,7 +1654,7 @@ def add_gap_analysis_result(self, cache_key: str, ga_object: dict):
self.session.add(res)
self.session.commit()
else:
return existing.ga_object
return existing


def dbNodeFromNode(doc: cre_defs.Node) -> Optional[Node]:
Expand Down Expand Up @@ -1836,15 +1836,13 @@ def gap_analysis(
cache_key = make_array_hash(node_names)

# conn.set(cache_key, flask_json.dumps({"result": grouped_paths}))
if cre_db:
cre_db.add_gap_analysis_result(
cache_key=cache_key, ga_object={"result": extra_paths_dict[key]}
cre_db.add_gap_analysis_result(
cache_key=cache_key, ga_object={"result": grouped_paths}
)

for key in extra_paths_dict:
if cre_db:
cre_db.add_gap_analysis_result(
cache_key=cache_key, ga_object={"result": extra_paths_dict[key]}
cre_db.add_gap_analysis_result(
cache_key=make_cache_key(node_names,key), ga_object={"result": extra_paths_dict[key]}
)
# conn.set(
# cache_key + "->" + key,
Expand Down
23 changes: 9 additions & 14 deletions application/tests/db_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ def test_gap_analysis_duplicate_link_path_existing_higher_and_in_extras(

@patch.object(redis, "from_url")
@patch.object(db.NEO_DB, "gap_analysis")
def test_gap_analysis_dump_to_cache(self, gap_mock, redis_conn_mock):
def test_gap_analysis_dump_to_cache(self, gap_mock):
collection = db.Node_collection()
collection.neo_db.connected = True
path = [
Expand Down Expand Up @@ -1567,19 +1567,14 @@ def test_gap_analysis_dump_to_cache(self, gap_mock, redis_conn_mock):
response = db.gap_analysis(collection.neo_db, ["a", "b"], True)

self.assertEqual(response, (expected_response[0], {}, {}))

redis_conn_mock.return_value.set.assert_has_calls(
[
mock.call(
"d8160c9b3dc20d4e931aeb4f45262155",
flask_json.dumps({"result": expected_response[1]}),
),
mock.call(
"d8160c9b3dc20d4e931aeb4f45262155->a",
flask_json.dumps({"result": expected_response[2]["a"]}),
),
]
)
self.assertEqual(
collection.get_gap_analysis_result("d8160c9b3dc20d4e931aeb4f45262155"),
flask_json.dumps({"result": expected_response[1]})
)
self.assertEqual(
collection.get_gap_analysis_result("d8160c9b3dc20d4e931aeb4f45262155->a"),
flask_json.dumps({"result": expected_response[2]["a"]}),
)

def test_neo_db_parse_node_code(self):
name = "name"
Expand Down
9 changes: 6 additions & 3 deletions application/tests/web_main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from application.defs import cre_defs as defs
from application.defs import osib_defs
from application.web import web_main

from application.utils.hash import make_array_hash, make_cache_key

class MockJob:
@property
Expand Down Expand Up @@ -686,8 +686,11 @@ def test_gap_analysis_weak_links_no_cache(self) -> None:
@patch.object(redis, "from_url")
def test_gap_analysis_weak_links_response(self, redis_conn_mock) -> None:
expected = {"result": "hello"}
redis_conn_mock.return_value.exists.return_value = True
redis_conn_mock.return_value.get.return_value = json.dumps(expected)
collection = db.Node_collection()
standards =["aaa","bbb"]
key = "ccc"
cache_key = make_cache_key(standards=standards,key=key)
collection.add_gap_analysis_result(cache_key=cache_key,ga_object=expected)
with self.app.test_client() as client:
response = client.get(
"/rest/v1/map_analysis_weak_links?standard=aaa&standard=bbb&key=ccc`",
Expand Down
2 changes: 2 additions & 0 deletions application/utils/hash.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import hashlib

def make_cache_key(standards:list, key:str)->str:
return make_array_hash(standards) + "->" + key

def make_array_hash(array: list):
return hashlib.md5(":".join(array).encode("utf-8")).hexdigest()
6 changes: 2 additions & 4 deletions application/web/web_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from application.utils.spreadsheet import write_csv
import oauthlib
import google.auth.transport.requests
from application.utils.hash import make_array_hash
from application.utils.hash import make_array_hash, make_cache_key

ITEMS_PER_PAGE = 20

Expand Down Expand Up @@ -269,9 +269,7 @@ def gap_analysis() -> Any:
def gap_analysis_weak_links() -> Any:
standards = request.args.getlist("standard")
key = request.args.get("key")
conn = redis.connect()
standards_hash = make_array_hash(standards)
cache_key = standards_hash + "->" + key
cache_key = make_cache_key(standards=standards,key=key)

database = db.Node_collection()
result = database.get_gap_analysis_result(cache_key=cache_key)
Expand Down

0 comments on commit d18184b

Please sign in to comment.