From 1c57cfbb03584e78dc375be8cb1d8687063a3dfe Mon Sep 17 00:00:00 2001 From: Anton Zuev Date: Fri, 27 Nov 2026 14:50:39 +0300 Subject: [PATCH] Add tests for gstat with tablespaces. --- tests/utilities/gstat/__init__.py | 0 tests/utilities/gstat/conftest.py | 38 ++++++++++ tests/utilities/gstat/tablespace/__init__.py | 1 + .../gstat/tablespace/test_all_statistics.py | 74 +++++++++++++++++++ .../gstat/tablespace/test_data_page.py | 56 ++++++++++++++ .../utilities/gstat/tablespace/test_index.py | 56 ++++++++++++++ .../gstat/tablespace/test_specify_ts.py | 73 ++++++++++++++++++ .../gstat/tablespace/test_ts_not_found.py | 43 +++++++++++ 8 files changed, 341 insertions(+) create mode 100644 tests/utilities/gstat/__init__.py create mode 100644 tests/utilities/gstat/conftest.py create mode 100644 tests/utilities/gstat/tablespace/__init__.py create mode 100644 tests/utilities/gstat/tablespace/test_all_statistics.py create mode 100644 tests/utilities/gstat/tablespace/test_data_page.py create mode 100644 tests/utilities/gstat/tablespace/test_index.py create mode 100644 tests/utilities/gstat/tablespace/test_specify_ts.py create mode 100644 tests/utilities/gstat/tablespace/test_ts_not_found.py diff --git a/tests/utilities/gstat/__init__.py b/tests/utilities/gstat/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/utilities/gstat/conftest.py b/tests/utilities/gstat/conftest.py new file mode 100644 index 000000000..a92c9d0b3 --- /dev/null +++ b/tests/utilities/gstat/conftest.py @@ -0,0 +1,38 @@ +# coding:utf-8 + +# Common functions for all gstat tests + +import pytest +import re +from typing import Literal + + +class Helpers: + + @staticmethod + def get_metric(data: str, table: str, metric: str, tablespace: str = 'PUBLIC') -> int: + table = f'\\"{tablespace}\\"\\.\\"{table}\\"' + pattern = re.compile( + f'^{table}.*\\n(^\\s+.*\\n)*?\\s+(?:[\\s\\w\\d.:]+, )*?{metric}: (\\d+\\.?\\d*)', flags=re.M) + result = pattern.search(data) + if result: + return float(result.group(2)) + else: + return + + @staticmethod + def get_full_stat(data: str, start: str, target: Literal['table', 'text'] = 'text', tablespace: str = 'PUBLIC') -> str: + if target == 'table': + start = f'\\"{tablespace}\\"\\.\\"{start}\\"' + + pattern = re.compile(f'^{start}.*\\n((^.*\\n)*)^Gstat completion', flags=re.M) + result = pattern.search(data) + if result: + return result.group(1) + else: + return + + +@pytest.fixture +def gstat_helpers(): + return Helpers diff --git a/tests/utilities/gstat/tablespace/__init__.py b/tests/utilities/gstat/tablespace/__init__.py new file mode 100644 index 000000000..4adc98407 --- /dev/null +++ b/tests/utilities/gstat/tablespace/__init__.py @@ -0,0 +1 @@ +# Python module diff --git a/tests/utilities/gstat/tablespace/test_all_statistics.py b/tests/utilities/gstat/tablespace/test_all_statistics.py new file mode 100644 index 000000000..9cb7109f3 --- /dev/null +++ b/tests/utilities/gstat/tablespace/test_all_statistics.py @@ -0,0 +1,74 @@ +# coding:utf-8 +""" +ID: utilites.gstat.tablespace.test_all_statistics +TITLE: Checking retrieval of all statistics in a user tablespaces via gstat. +DESCRIPTION: + The test checks 2 ways of retrieving statistics: + - via direct launch of gstat with the -ts key + - via the service manager with the sts_user_tablespaces option + Both methods should produce identical results. +""" + +import pytest +from firebird.qa import * + +init_script = """ + CREATE TABLESPACE TS1 FILE './test1.dat'; + CREATE TABLESPACE TS2 FILE './test2.dat'; + create table TEST(STR CHAR(1500)); + commit; + create INDEX IDX_TEST on TEST(STR) IN TABLESPACE TS2; + commit; +""" + +substitutions=[ + (r'"PUBLIC"."TEST" \(\d+\)', '"PUBLIC"."TEST" (X)'), + ('File .*(test.fdb|TEST.FDB)', 'File test.fdb'), + ('Full path: .*(test|TEST)', 'Full path: test'), + ('DAT', 'dat'), +] + +db = db_factory(init=init_script) +act = python_act('db', substitutions=substitutions) + +expected_stdout = """ + Tablespaces: + TS1 (2) + Full path: test1.dat + Tablespace is empty + TS2 (3) + Full path: test2.dat + Indices: + "PUBLIC"."IDX_TEST" ("PUBLIC"."TEST") + Analyzing database pages ... + "PUBLIC"."TEST" (128) + Index "PUBLIC"."IDX_TEST" (0) + Tablespace: TS2 + Root page: 3, depth: 1, leaf buckets: 1, nodes: 0 + Average node length: 0.00, total dup: 0, max dup: 0 + Average key length: 0.00, compression ratio: 0.00 + Average prefix length: 0.00, average data length: 0.00 + Clustering factor: 0, ratio: 0.00 + Fill distribution: + 0 - 19% = 1 + 20 - 39% = 0 + 40 - 59% = 0 + 60 - 79% = 0 + 80 - 99% = 0 +""" + + +@pytest.mark.version('>=6.0') +def test_1(act: Action, gstat_helpers): + for method in ('gstat', 'svcmgr'): + act.reset() + if method == 'gstat': + act.gstat(switches=[act.db.db_path, '-ts'], connect_db=False) + else: + act.svcmgr(switches=['-action_db_stats', 'dbname', act.db.db_path, 'sts_user_tablespaces']) + + start = '\\s+\\*END\\*' + stats = gstat_helpers.get_full_stat(act.stdout, start) + act.stdout = stats + act.expected_stdout = expected_stdout + assert act.clean_stdout == act.clean_expected_stdout, f"Method: {method}" diff --git a/tests/utilities/gstat/tablespace/test_data_page.py b/tests/utilities/gstat/tablespace/test_data_page.py new file mode 100644 index 000000000..365b5f595 --- /dev/null +++ b/tests/utilities/gstat/tablespace/test_data_page.py @@ -0,0 +1,56 @@ +# coding:utf-8 +""" +ID: utilites.gstat.tablespace.test_data_page +TITLE: Checking retrieval of data pages statistics in a user tablespaces via gstat. +DESCRIPTION: + The test checks 2 ways of retrieving statistics: + - via direct launch of gstat with -d and -ts keys + - via the service manager with sts_data_pages and sts_user_tablespaces options + Both methods should produce identical results. +""" + +import pytest +from firebird.qa import * + +init_script = """ + CREATE TABLESPACE TS1 FILE './test1.dat'; + CREATE TABLESPACE TS2 FILE './test2.dat'; + create table TEST(STR CHAR(1500)) IN TABLESPACE TS2; + commit; +""" + +db = db_factory(init=init_script) + +act = python_act('db', substitutions=[(r'"PUBLIC"."TEST" \(\d+\)', '"PUBLIC"."TEST" (X)')]) + +expected_stdout = """ + "PUBLIC"."TEST" (X) + Tablespace: TS2 + Primary pointer page: 3, Index root page: 4 + Pointer pages: 1, data page slots: 0 + Data pages: 0, average fill: 0% + Primary pages: 0, secondary pages: 0, swept pages: 0 + Empty pages: 0, full pages: 0 + + Fill distribution: + 0 - 19% = 0 + 20 - 39% = 0 + 40 - 59% = 0 + 60 - 79% = 0 + 80 - 99% = 0 +""" + + +@pytest.mark.version('>=6.0') +def test_1(act: Action, gstat_helpers): + for method in ('gstat', 'svcmgr'): + act.reset() + if method == 'gstat': + act.gstat(switches=[act.db.db_path, '-d', '-ts'], connect_db=False) + else: + act.svcmgr(switches=['-action_db_stats', 'dbname', act.db.db_path, 'sts_data_pages', 'sts_user_tablespaces']) + + stats = gstat_helpers.get_full_stat(act.stdout, 'Analyzing database pages') + act.expected_stdout = expected_stdout + act.stdout = stats + assert act.clean_stdout == act.clean_expected_stdout, f"Method: {method}" diff --git a/tests/utilities/gstat/tablespace/test_index.py b/tests/utilities/gstat/tablespace/test_index.py new file mode 100644 index 000000000..4039d8c9f --- /dev/null +++ b/tests/utilities/gstat/tablespace/test_index.py @@ -0,0 +1,56 @@ +# coding:utf-8 +""" +ID: utilites.gstat.tablespace.test_index +TITLE: Checking retrieval of index statistics in a user tablespaces via gstat. +DESCRIPTION: + The test checks 2 ways of retrieving statistics: + - via direct launch of gstat with -i and -ts keys + - via the service manager with sts_idx_pages and sts_user_tablespaces options + Both methods should produce identical results. +""" + +import pytest +from firebird.qa import * + +init_script = """ + CREATE TABLESPACE TS1 FILE './test1.dat'; + CREATE TABLESPACE TS2 FILE './test2.dat'; + create table TEST(STR CHAR(1500)); + commit; + create INDEX IDX_TEST on TEST(STR) IN TABLESPACE TS2; + commit; +""" + +db = db_factory(init=init_script) +act = python_act('db', substitutions=[(r'TEST" \(\d+\)', 'TEST" (X)')]) + +expected_stdout = """ + Index "PUBLIC"."IDX_TEST" (X) + Tablespace: TS2 + Root page: 3, depth: 1, leaf buckets: 1, nodes: 0 + Average node length: 0.00, total dup: 0, max dup: 0 + Average key length: 0.00, compression ratio: 0.00 + Average prefix length: 0.00, average data length: 0.00 + Clustering factor: 0, ratio: 0.00 + Fill distribution: + 0 - 19% = 1 + 20 - 39% = 0 + 40 - 59% = 0 + 60 - 79% = 0 + 80 - 99% = 0 +""" + + +@pytest.mark.version('>=6.0') +def test_1(act: Action, gstat_helpers): + for method in ('gstat', 'svcmgr'): + act.reset() + if method == 'gstat': + act.gstat(switches=[act.db.db_path, '-i', '-ts'], connect_db=False) + else: + act.svcmgr(switches=['-action_db_stats', 'dbname', act.db.db_path, 'sts_idx_pages', 'sts_user_tablespaces']) + + stats = gstat_helpers.get_full_stat(act.stdout, 'TEST', 'table') + act.expected_stdout = expected_stdout + act.stdout = stats + assert act.clean_stdout == act.clean_expected_stdout, f"Method: {method}" diff --git a/tests/utilities/gstat/tablespace/test_specify_ts.py b/tests/utilities/gstat/tablespace/test_specify_ts.py new file mode 100644 index 000000000..6e571dda0 --- /dev/null +++ b/tests/utilities/gstat/tablespace/test_specify_ts.py @@ -0,0 +1,73 @@ +# coding:utf-8 +""" +ID: utilites.gstat.tablespace.test_several_ts +TITLE: Checking retrieval of statistics in specified user tablespaces via gstat. +DESCRIPTION: + The test checks 2 ways of retrieving statistics: + - via direct launch of gstat with several values of the -ts key + - via the service manager with several sts_tablespace options + Both methods should produce identical results: statistics only for specified tablespaces. +""" + +import pytest +from firebird.qa import * + +init_script = """ + CREATE TABLESPACE TS1 FILE './test1.dat'; + CREATE TABLESPACE TS2 FILE './test2.dat'; + CREATE TABLESPACE TS3 FILE './test3.dat'; + create table TEST(STR CHAR(1500)) IN TABLESPACE TS1; + commit; + create INDEX IDX_TEST on TEST(STR) IN TABLESPACE TS2; + commit; +""" +db = db_factory(init=init_script) + +substitutions = [ + (r'"PUBLIC"."TEST" \(\d+\)', '"PUBLIC"."TEST" (X)'), + ('File .*(test.fdb|TEST.FDB)', 'File test.fdb'), + ('Full path: .*(test|TEST)', 'Full path: test'), + ('DAT', 'dat') +] +act = python_act('db', substitutions=substitutions) + +expected_stdout = """ +Tablespaces: + TS1 (2) + Full path: test1.dat +Tables: + "PUBLIC"."TEST" + TS3 (4) + Full path: test3.dat + Tablespace is empty + Analyzing database pages ... + "PUBLIC"."TEST" (X) + Tablespace: TS1 + Primary pointer page: 3, Index root page: 4 + Pointer pages: 1, data page slots: 0 + Data pages: 0, average fill: 0% + Primary pages: 0, secondary pages: 0, swept pages: 0 + Empty pages: 0, full pages: 0 + Fill distribution: + 0 - 19% = 0 + 20 - 39% = 0 + 40 - 59% = 0 + 60 - 79% = 0 + 80 - 99% = 0 +""" + + +@pytest.mark.version('>=6.0') +def test_1(act: Action, gstat_helpers): + for method in ('gstat', 'svcmgr'): + act.reset() + if method == 'gstat': + act.gstat(switches=[act.db.db_path, '-ts', 'TS1', 'TS3'], connect_db=False) + else: + act.svcmgr(switches=['-action_db_stats', 'dbname', act.db.db_path, 'sts_tablespace', 'TS1', 'sts_tablespace', 'TS3']) + + start = '\\s+\\*END\\*' + stats = gstat_helpers.get_full_stat(act.stdout, start) + act.stdout = stats + act.expected_stdout = expected_stdout + assert act.clean_stdout == act.clean_expected_stdout, f"Method: {method}" diff --git a/tests/utilities/gstat/tablespace/test_ts_not_found.py b/tests/utilities/gstat/tablespace/test_ts_not_found.py new file mode 100644 index 000000000..df5ada090 --- /dev/null +++ b/tests/utilities/gstat/tablespace/test_ts_not_found.py @@ -0,0 +1,43 @@ +# coding:utf-8 +""" +ID: utilites.gstat.tablespace.test_ts_not_found +TITLE: Checking retrieval of statistics for non-existent ts name +DESCRIPTION: + The test checks 2 ways of retrieving statistics: + - via direct launch of gstat with the -ts key + - via the service manager with the sts_tablespace option + Specify a name of the non-existent tablespace as the value. + Both methods should produce identical errors. +""" + +import pytest +from firebird.qa import * + +init_script = """ + CREATE TABLESPACE TS1 FILE './test1.dat'; + CREATE TABLESPACE TS2 FILE './test2.dat'; + create table TEST(STR CHAR(1500)); + commit; + create INDEX IDX_TEST on TEST(STR) IN TABLESPACE TS1; + commit; +""" + +db = db_factory(init=init_script) +act = python_act('db') + +expected_stderr = """ +tablespace TS3 not found +""" + + +@pytest.mark.version('>=6.0') +def test_1(act: Action): + for method in ('gstat', 'svcmgr'): + act.reset() + act.expected_stderr = expected_stderr + if method == 'gstat': + act.gstat(switches=[act.db.db_path, '-ts', 'TS3'], connect_db=False) + else: + act.svcmgr(switches=['-action_db_stats', 'dbname', act.db.db_path, 'sts_tablespace', 'TS3']) + + assert act.clean_stderr == act.clean_expected_stderr, f"Method: {method}"