Skip to content

Commit

Permalink
Test with testweight
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrillkuettel committed Jul 12, 2024
1 parent b2f26c9 commit 9115b63
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/privatim/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from privatim.forms.search_form import SearchForm
from privatim.layouts import Layout
from privatim.i18n import locales
from privatim.models import Consultation
from privatim.models.associated_file import SearchableAssociatedFiles

from privatim.models.file import SearchableFile
Expand Down Expand Up @@ -249,6 +250,12 @@ def _create_rank_expression(
"""
weights = {'primary': 'A', 'high': 'B', 'medium': 'C', 'low': 'D'}

consultation_id = self.session.execute(
select(Consultation)
).scalar_one_or_none()
breakpoint()
# query
weighted_vectors: list[Function[Any]] = [
func.setweight(
func.to_tsvector(self.lang, field),
Expand Down
21 changes: 12 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@
from tests.shared.client import Client


docker_config = {
'host': '172.17.0.2', # or the IP of your Docker host
'port': 5433, # the port you mapped in your Docker run command
'user': 'postgres',
'password': 'password',
'dbname': 'test_db_1'
connection_config = {
'host': 'localhost',
'port': 5432,
'user': 'dev',
'password': 'postgres',
'dbname': 'privatim'
}

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(message)s')


bunch_docker_config = Bunch(**{'info': Bunch(**docker_config)})
bunch_docker_config = Bunch(**{'info': Bunch(**connection_config)})


@pytest.fixture(scope='function')
def pg_config(monkeypatch):
postgresql = bunch_docker_config

logger = logging.getLogger(__name__)
logger.debug('Setting up pg_config fixture')
logger.info('Setting up pg_config fixture')

config = testing.setUp(settings={
'sqlalchemy.url': (
f'postgresql+psycopg://{postgresql.info.user}:{postgresql.info.password}@'
f'{postgresql.info.host}:{postgresql.info.port}'
f'/{postgresql.info.dbname}'
),

})
config.include('privatim.models')
config.include('pyramid_chameleon')
Expand All @@ -55,6 +55,9 @@ def pg_config(monkeypatch):
dbsession = get_tm_session(session_factory, transaction.manager)
config.dbsession = dbsession

# initialize file storage
setup_filestorage(settings)

orig_init = DummyRequest.__init__

def init_with_dbsession(self, *args, dbsession=dbsession, **kwargs):
Expand Down
33 changes: 30 additions & 3 deletions tests/views/client/test_search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging

import pytest
from sqlalchemy import select
import transaction
from sqlalchemy import select, text
from sqlalchemy.orm import Mapped, mapped_column
from webtest import Upload

Expand All @@ -13,6 +14,7 @@
from tests.shared.utils import create_consultation

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(message)s')
logger = logging.getLogger(__name__)


@pytest.fixture(scope='function')
Expand Down Expand Up @@ -68,7 +70,6 @@ def test_search_client(client, pdf_vemz):
# postgresql.commit()
# cur.close()

logger = logging.getLogger(__name__)
logger.debug('Starting test_search')

client.login_admin()
Expand Down Expand Up @@ -102,12 +103,14 @@ def test_search_client(client, pdf_vemz):
logger.debug('Performed search')


def test_search(session, pdf_vemz):
def test_session_only_search(session, pdf_vemz):
logger.info('test_search start')

documents = [SearchableFile(*pdf_vemz)]
consultation = create_consultation(documents=documents)
session.add(consultation)
session.flush()
logger.info('Consultation created')

query = 'grundsätzlichen Fragen:'
collection: SearchCollection = SearchCollection(term=query, session=session)
Expand All @@ -125,3 +128,27 @@ def test_search(session, pdf_vemz):
# result.headlines['title'] = first_item[1]
#
# search_results.append(result)


def test_setweight(session):
transaction.begin()

session.execute(text("""
CREATE TABLE IF NOT EXISTS test_table (
id SERIAL PRIMARY KEY,
content TEXT
)
"""))

session.execute(text("INSERT INTO test_table (content) VALUES ('test content')"))

result = session.execute(text(
"SELECT setweight(to_tsvector('english', content), 'A') FROM test_table"
)).scalar_one()

print(f"Setweight result: {result}")

transaction.commit()

session.execute(text("DROP TABLE IF EXISTS test_table"))
transaction.commit()

0 comments on commit 9115b63

Please sign in to comment.