Skip to content

Commit

Permalink
tests: migrate away from SQlite In-Memory db.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrillkuettel committed Jul 2, 2024
1 parent 61bb501 commit df1507c
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 136 deletions.
27 changes: 27 additions & 0 deletions i18n.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ if [ ! -f "$LOCALES_PATH"/$DOMAIN.pot ]; then
touch "$LOCALES_PATH"/$DOMAIN.pot
fi

# Function to insert a space before "msgid"
insert_space_before_msgid() {
local input="$1"
echo "$input" | sed 's/msgid/ msgid/'
}

# Function to remove the fourth space in a string
remove_fourth_space() {
local input="$1"
local part1=$(echo "$input" | cut -d' ' -f1-4)
local part2=$(echo "$input" | cut -d' ' -f5-)
echo "$part1$part2"
}


# no arguments, extract and update
if [ $# -eq 0 ]; then
echo "Extract messages"
Expand All @@ -47,6 +62,18 @@ if [ $# -eq 0 ]; then
echo "Compile message catalogs"
for po in "$LOCALES_PATH"/*/LC_MESSAGES/*.po; do
msgfmt --statistics -o "${po%.*}.mo" "$po"

untranslated_messages=$(msggrep -v -T -e "." "$po" | grep -n "msgid" | grep -v '""')
if [ -n "$untranslated_messages" ]; then
echo -n "${po}:"
while IFS= read -r line; do
formatted_line=$(insert_space_before_msgid "$line")
formatted_line=$(remove_fourth_space "$formatted_line")
echo "$formatted_line"
done <<< "$untranslated_messages"
echo # Add a newline after all untranslated messages
fi
echo # Add a newline after all untranslated messages
done

# first argument represents language identifier, create catalog
Expand Down
Binary file modified src/privatim/locale/de/LC_MESSAGES/privatim.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion src/privatim/locale/de/LC_MESSAGES/privatim.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE 1.0\n"
"POT-Creation-Date: 2024-07-02 14:17+0200\n"
"POT-Creation-Date: 2024-07-02 23:44+0200\n"
"PO-Revision-Date: 2024-05-21 21:20+0200\n"
"Last-Translator: cyrill <[email protected]>\n"
"Language-Team: German <[email protected]>\n"
Expand Down Expand Up @@ -338,6 +338,10 @@ msgstr "Ihr Kommentar"
msgid "Search Results"
msgstr "Suchergebnisse"

#: src/privatim/views/templates/search_results.pt
msgid "No results containing all your search terms were found."
msgstr "Es wurden keine Ergebnisse gefunden, die alle Ihre Suchbegriffe enthalten."

#: src/privatim/views/templates/search_results.pt
#: src/privatim/views/templates/activities.pt
msgid "Show Details"
Expand Down
Binary file modified src/privatim/locale/fr/LC_MESSAGES/privatim.mo
Binary file not shown.
14 changes: 9 additions & 5 deletions src/privatim/locale/fr/LC_MESSAGES/privatim.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE 1.0\n"
"POT-Creation-Date: 2024-07-02 14:17+0200\n"
"POT-Creation-Date: 2024-07-02 23:44+0200\n"
"PO-Revision-Date: 2024-04-11 15:53+0200\n"
"Last-Translator: cyrill <[email protected]>\n"
"Language-Team: French <[email protected]>\n"
Expand Down Expand Up @@ -335,7 +335,11 @@ msgstr "Votre commentaire"

#: src/privatim/views/templates/search_results.pt
msgid "Search Results"
msgstr ""
msgstr "Résultats de la recherche"

#: src/privatim/views/templates/search_results.pt
msgid "No results containing all your search terms were found.."
msgstr "Aucun résultat ne contient tous les termes de votre recherche."

#: src/privatim/views/templates/search_results.pt
#: src/privatim/views/templates/activities.pt
Expand All @@ -348,11 +352,11 @@ msgstr "Changer le mot de passe"

#: src/privatim/views/templates/activities.pt
msgid "Unbekannter Ersteller"
msgstr ""
msgstr "Créateur inconnu"

#: src/privatim/views/templates/activities.pt
msgid "Kein Leiter"
msgstr ""
msgstr "Pas de chef"

#: src/privatim/views/templates/activities.pt
msgid "Show Meeting"
Expand Down Expand Up @@ -607,7 +611,7 @@ msgstr "Membres"

#: src/privatim/forms/search_form.py
msgid "Search"
msgstr ""
msgstr "Suchen"

#: src/privatim/forms/widgets/widgets.py
msgid "Uploaded file"
Expand Down
6 changes: 5 additions & 1 deletion src/privatim/locale/privatim.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE 1.0\n"
"POT-Creation-Date: 2024-07-02 14:17+0200\n"
"POT-Creation-Date: 2024-07-02 23:44+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -328,6 +328,10 @@ msgstr ""
msgid "Search Results"
msgstr ""

#: ./src/privatim/views/templates/search_results.pt
msgid "No results containing all your search terms were found.."
msgstr ""

#: ./src/privatim/views/templates/search_results.pt
#: ./src/privatim/views/templates/activities.pt
msgid "Show Details"
Expand Down
2 changes: 0 additions & 2 deletions src/privatim/models/searchable.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ def reindex_full_text_search(session: 'Session') -> None:
"""
models = searchable_models()
# todo: remove later
assert len(models) != 0, "No models with searchable fields found"
for model in models:
for locale, language in locales.items():
assert language == 'german' # todo: remove later
Expand Down
7 changes: 7 additions & 0 deletions src/privatim/views/templates/search_results.pt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
<!-- Main content area (2/3 width) -->
<div class="col-md-8 order-md-1">
<!-- Displaying a search result -->

<div tal:condition="not search_results" class="mt-4">
<div class="alert alert-info" role="alert">
<i class="fas fa-info-circle"></i>
<span i18n:translate="">No results containing all your search terms were found..</span>
</div>
</div>
<div tal:repeat="result search_results" class="mt-4">
<tal:block tal:condition="python: result.type != 'Comment'">
<div class="card d-flex flex-row activity-card-wrapper">
Expand Down
16 changes: 8 additions & 8 deletions tests/cli/test_upgrade.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from privatim.cli.upgrade import UpgradeContext


def test_has_table(config):
upgrade = UpgradeContext(config.dbsession)
def test_has_table(pg_config):
upgrade = UpgradeContext(pg_config.dbsession)
assert upgrade.has_table('consultations')
assert not upgrade.has_table('bogus')


def test_drop_table(config):
upgrade = UpgradeContext(config.dbsession)
assert upgrade.has_table('meetings')
assert upgrade.drop_table('meetings')
def test_drop_table(pg_config):
upgrade = UpgradeContext(pg_config.dbsession)
assert upgrade.has_table('agenda_items')
assert upgrade.drop_table('agenda_items')
assert not upgrade.has_table('meetings')
assert not upgrade.drop_table('bogus')


def test_has_column(config):
upgrade = UpgradeContext(config.dbsession)
def test_has_column(pg_config):
upgrade = UpgradeContext(pg_config.dbsession)
assert upgrade.has_column('meetings', 'id')
assert not upgrade.has_column('meetings', 'bogus')
119 changes: 34 additions & 85 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import warnings
import pytest
import transaction
from libcloud.storage.drivers.local import LocalStorageDriver
from pyramid import testing
from sqlalchemy import engine_from_config
from privatim import main
Expand All @@ -10,90 +9,45 @@
from privatim.models.consultation import Status, Consultation
from privatim.orm import Base, get_engine, get_session_factory, get_tm_session
from privatim.testing import DummyRequest, DummyMailer, MockRequests
from sqlalchemy_file.storage import StorageManager

from tests.shared.client import Client


from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pyramid.config import Configurator


@pytest.fixture
def base_config(_postgresql):
@pytest.fixture(scope='function')
def base_config(postgresql):
msg = '.*SQLAlchemy must convert from floating point.*'
warnings.filterwarnings('ignore', message=msg)

# config = testing.setUp(settings={
# 'sqlalchemy.url': 'sqlite:///:memory:',
# })

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


@pytest.fixture
def config(base_config, monkeypatch, tmpdir) -> 'Configurator':
""" Returns the config used in tests. Note that this has side effects
on `DummyRequest` in that the session becomes available. """

base_config.include('privatim.models')
base_config.include('pyramid_chameleon')
base_config.include('pyramid_layout')
settings = base_config.get_settings()

engine = get_engine(settings)

# enable foreign key constraints in sqlite, so we can rely on them
# working during testing.
# sqlalchemy.event.listen(
# engine,
# 'connect',
# lambda c, r: c.execute('pragma foreign_keys=ON')
# )

Base.metadata.create_all(engine)
session_factory = get_session_factory(engine)

dbsession = get_tm_session(session_factory, transaction.manager)
base_config.dbsession = dbsession

orig_init = DummyRequest.__init__

def init_with_dbsession(self, *args, dbsession=dbsession, **kwargs):
orig_init(self, *args, dbsession=dbsession, **kwargs)

monkeypatch.setattr(DummyRequest, '__init__', init_with_dbsession)

# Store static files in a temporary directory
if not StorageManager._storages:
# NOTE: StorageManager does not expose any method to check if some
# storage has already been added. However, if you attempt to add a
# storage that already exists, StorageManager raises a RuntimeError.
tmpdir.mkdir('assets')
container = LocalStorageDriver(tmpdir).get_container('assets')
StorageManager.add_storage("default", container)
@pytest.fixture(scope='function', autouse=True)
def run_around_tests(engine):
# todo: check if this is actually needed?

return base_config
# This fixture will run before and after each test
# Thanks to the autouse=True parameter
yield
# After the test, we ensure all tables are dropped
Base.metadata.drop_all(bind=engine)


# requires pytest-postgresql:
@pytest.fixture(scope='session')
def pg_config(_postgresql, monkeypatch):
@pytest.fixture(scope='function')
def pg_config(postgresql, monkeypatch):
config = testing.setUp(settings={
'sqlalchemy.url': (
f'postgresql+psycopg://{_postgresql.info.user}:@'
f'{_postgresql.info.host}:{_postgresql.info.port}'
f'/{_postgresql.info.dbname}'
f'postgresql+psycopg://{postgresql.info.user}:@'
f'{postgresql.info.host}:{postgresql.info.port}'
f'/{postgresql.info.dbname}'
),
})
config.include('privatim.models')
Expand Down Expand Up @@ -125,13 +79,13 @@ def init_with_dbsession(self, *args, dbsession=dbsession, **kwargs):
transaction.abort()


@pytest.fixture
def session(config):
@pytest.fixture(scope='function')
def session(pg_config):
# convenience fixture
return config.dbsession
return pg_config.dbsession


@pytest.fixture
@pytest.fixture(scope='function')
def user(session):
user = User(
email='[email protected]',
Expand Down Expand Up @@ -169,13 +123,13 @@ def user_with_working_group(session):


@pytest.fixture
def mailer(config):
def mailer(pg_config):
mailer = DummyMailer()
config.registry.registerUtility(mailer)
pg_config.registry.registerUtility(mailer)
return mailer


@pytest.fixture(scope='session')
@pytest.fixture(scope='function')
def engine(app_settings):
engine = engine_from_config(app_settings)
Base.metadata.create_all(engine)
Expand All @@ -186,40 +140,35 @@ def engine(app_settings):
return engine


@pytest.fixture(scope='session')
@pytest.fixture(scope='function')
def connection(engine):
connection = engine.connect()
yield connection
connection.close()


@pytest.fixture(scope='session')
def _postgresql(postgresql):
return postgresql


@pytest.fixture(scope='session')
def app_settings(_postgresql):
@pytest.fixture(scope='function')
def app_settings(postgresql):
yield {'sqlalchemy.url': (
f'postgresql+psycopg://{_postgresql.info.user}:@'
f'{_postgresql.info.host}:{_postgresql.info.port}'
f'/{_postgresql.info.dbname}'
f'postgresql+psycopg://{postgresql.info.user}:@'
f'{postgresql.info.host}:{postgresql.info.port}'
f'/{postgresql.info.dbname}'
)}


@pytest.fixture(scope="session")
@pytest.fixture(scope='function')
def app_inner(app_settings):
app = main({}, **app_settings)
yield app


@pytest.fixture(scope='session')
@pytest.fixture(scope='function')
def app(app_inner, connection):
app_inner.app.app.registry["dbsession_factory"].kw["bind"] = connection
yield app_inner


@pytest.fixture(scope='session')
@pytest.fixture(scope='function')
def client(app, engine):

client = Client(app)
Expand Down
2 changes: 1 addition & 1 deletion tests/forms/test_forms_meeting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def getlist(self, key):


@pytest.mark.skip()
def test_meeting_form_time_not_optional(config):
def test_meeting_form_time_not_optional(pg_config):
meeting = create_meeting()
session = config.dbsession
session.add(meeting)
Expand Down
Loading

0 comments on commit df1507c

Please sign in to comment.