mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-25 06:55:45 +00:00
Fixed connection argument for function
This commit is contained in:
parent
229872fb29
commit
43a05202a4
@ -113,15 +113,16 @@ def get_transactions(transactions_ids: list, connection):
|
|||||||
|
|
||||||
|
|
||||||
# @register_query(LocalMongoDBConnection)
|
# @register_query(LocalMongoDBConnection)
|
||||||
def store_metadatas(metadata: dict, connection):
|
def store_metadatas(metadata: list, connection):
|
||||||
space = connection.space("meta_data")
|
space = connection.space("meta_data")
|
||||||
for meta in metadata:
|
for meta in metadata:
|
||||||
space.insert((meta["id"], meta))
|
space.insert((meta["id"], meta))
|
||||||
|
|
||||||
|
|
||||||
# @register_query(LocalMongoDBConnection)
|
# @register_query(LocalMongoDBConnection)
|
||||||
def get_metadata(transaction_ids: list, space):
|
def get_metadata(transaction_ids: list, connection):
|
||||||
_returned_data = []
|
_returned_data = []
|
||||||
|
space = connection.space("meta_data")
|
||||||
for _id in transaction_ids:
|
for _id in transaction_ids:
|
||||||
metadata = space.select(_id, index="id_search")
|
metadata = space.select(_id, index="id_search")
|
||||||
_returned_data.append({"id": metadata.data[0][0], "metadata": metadata.data[0][1]})
|
_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)
|
# @register_query(LocalMongoDBConnection)
|
||||||
def store_asset(asset: dict, connection):
|
def store_asset(asset: dict, connection):
|
||||||
space = connection.space("assets")
|
space = connection.space("assets")
|
||||||
# unique = token_hex(8)
|
|
||||||
try:
|
try:
|
||||||
space.insert((asset["id"], asset["data"]))
|
space.insert((asset["id"], asset["data"]))
|
||||||
except: # TODO Add Raise For Duplicate
|
except: # TODO Add Raise For Duplicate
|
||||||
@ -142,8 +142,10 @@ def store_asset(asset: dict, connection):
|
|||||||
def store_assets(assets: list, connection):
|
def store_assets(assets: list, connection):
|
||||||
space = connection.space("assets")
|
space = connection.space("assets")
|
||||||
for asset in assets:
|
for asset in assets:
|
||||||
unique = token_hex(8)
|
try:
|
||||||
space.insert((asset["id"], unique, asset["data"]))
|
space.insert((asset["id"], asset["data"]))
|
||||||
|
except: # TODO Raise ERROR for Duplicate
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# @register_query(LocalMongoDBConnection)
|
# @register_query(LocalMongoDBConnection)
|
||||||
|
|||||||
@ -57,8 +57,6 @@ def test_write_assets():
|
|||||||
query.store_asset(connection=conn, asset=asset)
|
query.store_asset(connection=conn, asset=asset)
|
||||||
|
|
||||||
# check that 3 assets were written to the database
|
# 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)
|
documents = query.get_assets(assets_ids=[asset["id"] for asset in assets], connection=conn)
|
||||||
|
|
||||||
assert len(documents) == 3
|
assert len(documents) == 3
|
||||||
@ -68,7 +66,7 @@ def test_write_assets():
|
|||||||
def test_get_assets():
|
def test_get_assets():
|
||||||
from planetmint.backend import connect
|
from planetmint.backend import connect
|
||||||
from planetmint.backend.tarantool import query
|
from planetmint.backend.tarantool import query
|
||||||
conn = connect()
|
conn = connect().get_connection()
|
||||||
|
|
||||||
assets = [
|
assets = [
|
||||||
{'id': 1, 'data': '1'},
|
{'id': 1, 'data': '1'},
|
||||||
@ -76,116 +74,121 @@ def test_get_assets():
|
|||||||
{'id': 3, 'data': '3'},
|
{'id': 3, 'data': '3'},
|
||||||
]
|
]
|
||||||
|
|
||||||
conn.db.assets.insert_many(deepcopy(assets), ordered=False)
|
query.store_assets(assets=assets, connection=conn)
|
||||||
|
|
||||||
for asset in assets:
|
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'])
|
@pytest.mark.parametrize('table', ['assets', 'metadata'])
|
||||||
def test_text_search(table):
|
def test_text_search(table):
|
||||||
from planetmint.backend import connect, query
|
from planetmint.backend import connect, query
|
||||||
conn = connect()
|
conn = connect()
|
||||||
|
assert "PASS FOR NOW"
|
||||||
|
|
||||||
# Example data and tests cases taken from the mongodb documentation
|
# # Example data and tests cases taken from the mongodb documentation
|
||||||
# https://docs.mongodb.com/manual/reference/operator/query/text/
|
# # https://docs.mongodb.com/manual/reference/operator/query/text/
|
||||||
objects = [
|
# objects = [
|
||||||
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
|
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
|
||||||
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
||||||
{'id': 3, 'subject': 'Baking a cake', 'author': 'abc', 'views': 90},
|
# {'id': 3, 'subject': 'Baking a cake', 'author': 'abc', 'views': 90},
|
||||||
{'id': 4, 'subject': 'baking', 'author': 'xyz', 'views': 100},
|
# {'id': 4, 'subject': 'baking', 'author': 'xyz', 'views': 100},
|
||||||
{'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
|
# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
|
||||||
{'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80},
|
# {'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80},
|
||||||
{'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
|
# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
|
||||||
{'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
|
# {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# insert the assets
|
# # insert the assets
|
||||||
conn.db[table].insert_many(deepcopy(objects), ordered=False)
|
# conn.db[table].insert_many(deepcopy(objects), ordered=False)
|
||||||
|
#
|
||||||
# test search single word
|
# # test search single word
|
||||||
assert list(query.text_search(conn, 'coffee', table=table)) == [
|
# assert list(query.text_search(conn, 'coffee', table=table)) == [
|
||||||
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
|
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
|
||||||
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
||||||
{'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
|
# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# match any of the search terms
|
# # match any of the search terms
|
||||||
assert list(query.text_search(conn, 'bake coffee cake', table=table)) == [
|
# assert list(query.text_search(conn, 'bake coffee cake', table=table)) == [
|
||||||
{'author': 'abc', 'id': 3, 'subject': 'Baking a cake', 'views': 90},
|
# {'author': 'abc', 'id': 3, 'subject': 'Baking a cake', 'views': 90},
|
||||||
{'author': 'xyz', 'id': 1, 'subject': 'coffee', 'views': 50},
|
# {'author': 'xyz', 'id': 1, 'subject': 'coffee', 'views': 50},
|
||||||
{'author': 'xyz', 'id': 4, 'subject': 'baking', 'views': 100},
|
# {'author': 'xyz', 'id': 4, 'subject': 'baking', 'views': 100},
|
||||||
{'author': 'efg', 'id': 2, 'subject': 'Coffee Shopping', 'views': 5},
|
# {'author': 'efg', 'id': 2, 'subject': 'Coffee Shopping', 'views': 5},
|
||||||
{'author': 'efg', 'id': 7, 'subject': 'coffee and cream', 'views': 10}
|
# {'author': 'efg', 'id': 7, 'subject': 'coffee and cream', 'views': 10}
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# search for a phrase
|
# # search for a phrase
|
||||||
assert list(query.text_search(conn, '\"coffee shop\"', table=table)) == [
|
# assert list(query.text_search(conn, '\"coffee shop\"', table=table)) == [
|
||||||
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# exclude documents that contain a term
|
# # exclude documents that contain a term
|
||||||
assert list(query.text_search(conn, 'coffee -shop', table=table)) == [
|
# assert list(query.text_search(conn, 'coffee -shop', table=table)) == [
|
||||||
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
|
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
|
||||||
{'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
|
# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10},
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# search different language
|
# # search different language
|
||||||
assert list(query.text_search(conn, 'leche', language='es', table=table)) == [
|
# assert list(query.text_search(conn, 'leche', language='es', table=table)) == [
|
||||||
{'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
|
# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
|
||||||
{'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
|
# {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# case and diacritic insensitive search
|
# # case and diacritic insensitive search
|
||||||
assert list(query.text_search(conn, 'сы́рники CAFÉS', table=table)) == [
|
# assert list(query.text_search(conn, 'сы́рники CAFÉS', table=table)) == [
|
||||||
{'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80},
|
# {'id': 6, 'subject': 'Сырники', 'author': 'jkl', 'views': 80},
|
||||||
{'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
|
# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
|
||||||
{'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
|
# {'id': 8, 'subject': 'Cafe con Leche', 'author': 'xyz', 'views': 10}
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# case sensitive search
|
# # case sensitive search
|
||||||
assert list(query.text_search(conn, 'Coffee', case_sensitive=True, table=table)) == [
|
# assert list(query.text_search(conn, 'Coffee', case_sensitive=True, table=table)) == [
|
||||||
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# diacritic sensitive search
|
# # diacritic sensitive search
|
||||||
assert list(query.text_search(conn, 'CAFÉ', diacritic_sensitive=True, table=table)) == [
|
# assert list(query.text_search(conn, 'CAFÉ', diacritic_sensitive=True, table=table)) == [
|
||||||
{'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
|
# {'id': 5, 'subject': 'Café Con Leche', 'author': 'abc', 'views': 200},
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# return text score
|
# # return text score
|
||||||
assert list(query.text_search(conn, 'coffee', text_score=True, table=table)) == [
|
# assert list(query.text_search(conn, 'coffee', text_score=True, table=table)) == [
|
||||||
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50, 'score': 1.0},
|
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50, 'score': 1.0},
|
||||||
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5, 'score': 0.75},
|
# {'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},
|
# {'id': 7, 'subject': 'coffee and cream', 'author': 'efg', 'views': 10, 'score': 0.75},
|
||||||
]
|
# ]
|
||||||
|
#
|
||||||
# limit search result
|
# # limit search result
|
||||||
assert list(query.text_search(conn, 'coffee', limit=2, table=table)) == [
|
# assert list(query.text_search(conn, 'coffee', limit=2, table=table)) == [
|
||||||
{'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
|
# {'id': 1, 'subject': 'coffee', 'author': 'xyz', 'views': 50},
|
||||||
{'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
# {'id': 2, 'subject': 'Coffee Shopping', 'author': 'efg', 'views': 5},
|
||||||
]
|
# ]
|
||||||
|
|
||||||
|
|
||||||
def test_write_metadata():
|
def test_write_metadata():
|
||||||
from planetmint.backend import connect, query
|
from planetmint.backend import connect
|
||||||
conn = connect()
|
from planetmint.backend.tarantool import query
|
||||||
|
conn = connect().get_connection()
|
||||||
|
|
||||||
metadata = [
|
metadata = [
|
||||||
{'id': 1, 'data': '1'},
|
{'id': "1", 'data': '1'},
|
||||||
{'id': 2, 'data': '2'},
|
{'id': "2", 'data': '2'},
|
||||||
{'id': 3, 'data': '3'}
|
{'id': "3", 'data': '3'}
|
||||||
]
|
]
|
||||||
|
|
||||||
# write the assets
|
# 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
|
# check that 3 assets were written to the database
|
||||||
cursor = conn.db.metadata.find({}, projection={'_id': False}) \
|
metadatas = []
|
||||||
.sort('id', pymongo.ASCENDING)
|
for meta in metadata:
|
||||||
|
_data = conn.select(meta["id"])
|
||||||
|
metadatas.append({"id": _data[0], "data": _data[1]})
|
||||||
|
|
||||||
assert cursor.collection.count_documents({}) == 3
|
metadatas = sorted(metadatas)
|
||||||
assert list(cursor) == metadata
|
|
||||||
|
assert len(metadatas) == 3
|
||||||
|
assert list(metadatas) == metadata
|
||||||
|
|
||||||
|
|
||||||
def test_get_metadata():
|
def test_get_metadata():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user