1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2024 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Tests for enhanced connection class for API functions.
 
  10 from pathlib import Path
 
  13 import sqlalchemy as sa
 
  17 async def test_run_scalar(api, table_factory):
 
  18     table_factory('foo', definition='that TEXT', content=(('a', ),))
 
  20     async with api.begin() as conn:
 
  21         assert await conn.scalar(sa.text('SELECT * FROM foo')) == 'a'
 
  25 async def test_run_execute(api, table_factory):
 
  26     table_factory('foo', definition='that TEXT', content=(('a', ),))
 
  28     async with api.begin() as conn:
 
  29         result = await conn.execute(sa.text('SELECT * FROM foo'))
 
  30         assert result.fetchone()[0] == 'a'
 
  34 async def test_get_property_existing_cached(api, table_factory):
 
  35     table_factory('nominatim_properties',
 
  36                   definition='property TEXT, value TEXT',
 
  37                   content=(('dbv', '96723'), ))
 
  39     async with api.begin() as conn:
 
  40         assert await conn.get_property('dbv') == '96723'
 
  42         await conn.execute(sa.text('TRUNCATE nominatim_properties'))
 
  44         assert await conn.get_property('dbv') == '96723'
 
  48 async def test_get_property_existing_uncached(api, table_factory):
 
  49     table_factory('nominatim_properties',
 
  50                   definition='property TEXT, value TEXT',
 
  51                   content=(('dbv', '96723'), ))
 
  53     async with api.begin() as conn:
 
  54         assert await conn.get_property('dbv') == '96723'
 
  56         await conn.execute(sa.text("UPDATE nominatim_properties SET value = '1'"))
 
  58         assert await conn.get_property('dbv', cached=False) == '1'
 
  62 @pytest.mark.parametrize('param', ['foo', 'DB:server_version'])
 
  63 async def test_get_property_missing(api, table_factory, param):
 
  64     table_factory('nominatim_properties',
 
  65                   definition='property TEXT, value TEXT')
 
  67     async with api.begin() as conn:
 
  68         with pytest.raises(ValueError):
 
  69             await conn.get_property(param)
 
  73 async def test_get_db_property_existing(api):
 
  74     async with api.begin() as conn:
 
  75         assert await conn.get_db_property('server_version') > 0
 
  79 async def test_get_db_property_existing(api):
 
  80     async with api.begin() as conn:
 
  81         with pytest.raises(ValueError):
 
  82             await conn.get_db_property('dfkgjd.rijg')