Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
38 changes: 38 additions & 0 deletions tests/utilities/gstat/conftest.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/utilities/gstat/tablespace/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Python module
74 changes: 74 additions & 0 deletions tests/utilities/gstat/tablespace/test_all_statistics.py
Original file line number Diff line number Diff line change
@@ -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}"
56 changes: 56 additions & 0 deletions tests/utilities/gstat/tablespace/test_data_page.py
Original file line number Diff line number Diff line change
@@ -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}"
56 changes: 56 additions & 0 deletions tests/utilities/gstat/tablespace/test_index.py
Original file line number Diff line number Diff line change
@@ -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}"
73 changes: 73 additions & 0 deletions tests/utilities/gstat/tablespace/test_specify_ts.py
Original file line number Diff line number Diff line change
@@ -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}"
43 changes: 43 additions & 0 deletions tests/utilities/gstat/tablespace/test_ts_not_found.py
Original file line number Diff line number Diff line change
@@ -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}"