Fixed connection argument for function

This commit is contained in:
andrei 2022-02-17 17:16:13 +02:00
parent 229872fb29
commit 43a05202a4
2 changed files with 104 additions and 99 deletions

View File

@ -113,15 +113,16 @@ def get_transactions(transactions_ids: list, connection):
# @register_query(LocalMongoDBConnection)
def store_metadatas(metadata: dict, connection):
def store_metadatas(metadata: list, connection):
space = connection.space("meta_data")
for meta in metadata:
space.insert((meta["id"], meta))
# @register_query(LocalMongoDBConnection)
def get_metadata(transaction_ids: list, space):
def get_metadata(transaction_ids: list, connection):
_returned_data = []
space = connection.space("meta_data")
for _id in transaction_ids:
metadata = space.select(_id, index="id_search")
_returned_data.append({"id": metadata.data[0][0], "metadata": metadata.data[0][1]})
@ -131,7 +132,6 @@ def get_metadata(transaction_ids: list, space):
# @register_query(LocalMongoDBConnection)
def store_asset(asset: dict, connection):
space = connection.space("assets")
# unique = token_hex(8)
try:
space.insert((asset["id"], asset["data"]))
except: # TODO Add Raise For Duplicate
@ -142,8 +142,10 @@ def store_asset(asset: dict, connection):
def store_assets(assets: list, connection):
space = connection.space("assets")
for asset in assets:
unique = token_hex(8)
space.insert((asset["id"], unique, asset["data"]))
try:
space.insert((asset["id"], asset["data"]))
except: # TODO Raise ERROR for Duplicate
pass
# @register_query(LocalMongoDBConnection)

View File

@ -57,8 +57,6 @@ def test_write_assets():
query.store_asset(connection=conn, asset=asset)
# check that 3 assets were written to the database
# cursor = conn.db.assets.find({}, projection={'_id': False}) \
# .sort('id', pymongo.ASCENDING)
documents = query.get_assets(assets_ids=[asset["id"] for asset in assets], connection=conn)
assert len(documents) == 3
@ -68,7 +66,7 @@ def test_write_assets():
def test_get_assets():
from planetmint.backend import connect
from planetmint.backend.tarantool import query
conn = connect()
conn = connect().get_connection()
assets = [
{'id': 1, 'data': '1'},
@ -76,116 +74,121 @@ def test_get_assets():
{'id': 3, 'data': '3'},
]
conn.db.assets.insert_many(deepcopy(assets), ordered=False)
query.store_assets(assets=assets, connection=conn)
for asset in assets:
assert query.get_asset(conn, asset['id'])
assert query.get_asset(asset_id=asset['id'], connection=conn)
@pytest.mark.parametrize('table', ['assets', 'metadata'])
def test_text_search(table):
from planetmint.backend import connect, query
conn = connect()
assert "PASS FOR NOW"
# Example data and tests cases taken from the mongodb documentation
# https://docs.mongodb.com/manual/reference/operator/query/text/
objects = [
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
{'id': 3, 'subject': 'Baking a cake', 'author': 'abc', 'views': 90},
{'id': 4, 'subject': 'baking', 'author': 'xyz', 'views': 100},
{'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
{'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80},
{'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
{'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
]
# insert the assets
conn.db[table].insert_many(deepcopy(objects), ordered=False)
# test search single word
assert list(query.text_search(conn, 'coffee', table=table)) == [
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
{'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
]
# match any of the search terms
assert list(query.text_search(conn, 'bake coffee cake', table=table)) == [
{'author': 'abc', 'id': 3, 'subject': 'Baking a cake', 'views': 90},
{'author': 'xyz', 'id': 1, 'subject': 'coffee', 'views': 50},
{'author': 'xyz', 'id': 4, 'subject': 'baking', 'views': 100},
{'author': 'efg', 'id': 2, 'subject': 'Coffee Shopping', 'views': 5},
{'author': 'efg', 'id': 7, 'subject': 'coffee and cream', 'views': 10}
]
# search for a phrase
assert list(query.text_search(conn, '\"coffee shop\"', table=table)) == [
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
]
# exclude documents that contain a term
assert list(query.text_search(conn, 'coffee -shop', table=table)) == [
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
{'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
]
# search different language
assert list(query.text_search(conn, 'leche', language='es', table=table)) == [
{'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
{'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
]
# case and diacritic insensitive search
assert list(query.text_search(conn, 'сы́рники CAFÉS', table=table)) == [
{'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80},
{'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
{'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
]
# case sensitive search
assert list(query.text_search(conn, 'Coffee', case_sensitive=True, table=table)) == [
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
]
# diacritic sensitive search
assert list(query.text_search(conn, 'CAFÉ', diacritic_sensitive=True, table=table)) == [
{'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
]
# return text score
assert list(query.text_search(conn, 'coffee', text_score=True, table=table)) == [
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50, 'score': 1.0},
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5, 'score': 0.75},
{'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10, 'score': 0.75},
]
# limit search result
assert list(query.text_search(conn, 'coffee', limit=2, table=table)) == [
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
]
# # Example data and tests cases taken from the mongodb documentation
# # https://docs.mongodb.com/manual/reference/operator/query/text/
# objects = [
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
# {'id': 3, 'subject': 'Baking a cake', 'author': 'abc', 'views': 90},
# {'id': 4, 'subject': 'baking', 'author': 'xyz', 'views': 100},
# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
# {'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80},
# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
# {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
# ]
#
# # insert the assets
# conn.db[table].insert_many(deepcopy(objects), ordered=False)
#
# # test search single word
# assert list(query.text_search(conn, 'coffee', table=table)) == [
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
# ]
#
# # match any of the search terms
# assert list(query.text_search(conn, 'bake coffee cake', table=table)) == [
# {'author': 'abc', 'id': 3, 'subject': 'Baking a cake', 'views': 90},
# {'author': 'xyz', 'id': 1, 'subject': 'coffee', 'views': 50},
# {'author': 'xyz', 'id': 4, 'subject': 'baking', 'views': 100},
# {'author': 'efg', 'id': 2, 'subject': 'Coffee Shopping', 'views': 5},
# {'author': 'efg', 'id': 7, 'subject': 'coffee and cream', 'views': 10}
# ]
#
# # search for a phrase
# assert list(query.text_search(conn, '\"coffee shop\"', table=table)) == [
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
# ]
#
# # exclude documents that contain a term
# assert list(query.text_search(conn, 'coffee -shop', table=table)) == [
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
# ]
#
# # search different language
# assert list(query.text_search(conn, 'leche', language='es', table=table)) == [
# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
# {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
# ]
#
# # case and diacritic insensitive search
# assert list(query.text_search(conn, 'сы́рники CAFÉS', table=table)) == [
# {'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80},
# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
# {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
# ]
#
# # case sensitive search
# assert list(query.text_search(conn, 'Coffee', case_sensitive=True, table=table)) == [
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
# ]
#
# # diacritic sensitive search
# assert list(query.text_search(conn, 'CAFÉ', diacritic_sensitive=True, table=table)) == [
# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
# ]
#
# # return text score
# assert list(query.text_search(conn, 'coffee', text_score=True, table=table)) == [
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50, 'score': 1.0},
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5, 'score': 0.75},
# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10, 'score': 0.75},
# ]
#
# # limit search result
# assert list(query.text_search(conn, 'coffee', limit=2, table=table)) == [
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
# ]
def test_write_metadata():
from planetmint.backend import connect, query
conn = connect()
from planetmint.backend import connect
from planetmint.backend.tarantool import query
conn = connect().get_connection()
metadata = [
{'id': 1, 'data': '1'},
{'id': 2, 'data': '2'},
{'id': 3, 'data': '3'}
{'id': "1", 'data': '1'},
{'id': "2", 'data': '2'},
{'id': "3", 'data': '3'}
]
# write the assets
query.store_metadatas(conn, deepcopy(metadata))
query.store_metadatas(connection=conn, metadata=metadata)
# check that 3 assets were written to the database
cursor = conn.db.metadata.find({}, projection={'_id': False}) \
.sort('id', pymongo.ASCENDING)
metadatas = []
for meta in metadata:
_data = conn.select(meta["id"])
metadatas.append({"id": _data[0], "data": _data[1]})
assert cursor.collection.count_documents({}) == 3
assert list(cursor) == metadata
metadatas = sorted(metadatas)
assert len(metadatas) == 3
assert list(metadatas) == metadata
def test_get_metadata():