Skip to content

Commit 79d86a2

Browse files
authored
Merge pull request #196 from macbre/tests-decrease-cardinality
Decrease cardinality to stabilize tests
2 parents 3a65b5d + 8f63286 commit 79d86a2

File tree

5 files changed

+95
-7
lines changed

5 files changed

+95
-7
lines changed

.github/workflows/python.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Test against different Python version
2+
3+
on:
4+
push:
5+
branches: master
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
python_tests:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
# Do not fail if one the tests did not pass
15+
fail-fast: false
16+
17+
matrix:
18+
# Python version(s) to use when running the tests
19+
python:
20+
- "3.6"
21+
- "3.8"
22+
- "3.9-dev"
23+
24+
# Docker images of MySQL-compliant databases to run the tests suite on
25+
database:
26+
- "mysql:8.0.20"
27+
28+
services:
29+
mysql:
30+
image: ${{ matrix.database }}
31+
env:
32+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
33+
MYSQL_DATABASE: index_digest
34+
ports:
35+
- "3306:3306"
36+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
37+
38+
steps:
39+
- uses: actions/checkout@v2
40+
- name: Set up Python
41+
uses: actions/setup-python@v1
42+
with:
43+
python-version: ${{ matrix.python }}
44+
45+
# https://github.com/actions/cache/blob/main/examples.md#using-pip-to-get-cache-location
46+
- name: Get pip cache dir
47+
id: pip-cache
48+
run: |
49+
echo "::set-output name=dir::$(pip cache dir)"
50+
51+
- name: pip cache
52+
uses: actions/cache@v2
53+
with:
54+
path: ${{ steps.pip-cache.outputs.dir }}
55+
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt', '**/setup.py') }}
56+
restore-keys: |
57+
${{ runner.os }}-pip-
58+
59+
- name: Install dependencies
60+
run: make install
61+
62+
- name: Linter
63+
run: make lint
64+
65+
- name: Set up the database
66+
run: |
67+
docker ps
68+
mysql --protocol=tcp -u root -v < setup.sql
69+
# import the test schema files
70+
"./sql/populate.sh"
71+
mysql --protocol=tcp -uindex_digest -pqwerty index_digest -v -e '\s; SHOW TABLES; SHOW DATABASES;'
72+
73+
- name: Tests
74+
run: make test

.github/workflows/tests.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ name: Integration tests
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: master
66
pull_request:
77
branches: [ master ]
8-
jobs:
98

9+
jobs:
1010
integrations_tests:
1111
runs-on: ubuntu-latest
1212

@@ -16,7 +16,8 @@ jobs:
1616

1717
matrix:
1818
# Python version(s) to use when running the tests
19-
python: [ 3.8, 3.9-dev ]
19+
python:
20+
- "3.8"
2021

2122
# Docker images of MySQL-compliant databases to run the tests suite on
2223
database:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ install:
55
pip install -U -e .[dev]
66

77
test:
8-
pytest -x $(project_name)
8+
pytest -x $(project_name) -o log_cli=true -o log_cli_level=warning
99

1010
coverage:
1111
rm -f .coverage*

indexdigest/test/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
from ..database import Database
24

35
from unittest import TestCase
@@ -17,6 +19,7 @@ def read_queries_from_log(log_file):
1719

1820
class DatabaseTestMixin(object):
1921
DSN = 'mysql://index_digest:qwerty@127.0.0.1/index_digest'
22+
DBNAME = 'index_digest'
2023

2124
@property
2225
def connection(self):
@@ -31,6 +34,7 @@ class BigTableTest(TestCase, DatabaseTestMixin):
3134
ROWS = 100000 # how many rows to generate
3235
BATCH = 5000 # perform INSERT in batches
3336

37+
BIG_TABLE_NAME = '0020_big_table'
3438
PREPARED = False
3539

3640
def setUp(self):
@@ -85,7 +89,7 @@ def _prepare_big_table(self):
8589
# no? populate it
8690
for row in self._rows():
8791
# Report low cardinality indices, use only a few distinct values (#31)
88-
num = row % 3
92+
num = row % 2
8993

9094
values.append((row, val, '{:05x}'.format(row)[:5], num))
9195

@@ -107,6 +111,14 @@ def _prepare_big_table(self):
107111
# update key distribution statistics (#31)
108112
self.connection.query('ANALYZE TABLE 0020_big_table')
109113

114+
cardinality_stats = self.connection.query_dict_rows(
115+
"select TABLE_NAME, INDEX_NAME, COLUMN_NAME, CARDINALITY from"
116+
" INFORMATION_SCHEMA.STATISTICS where"
117+
" TABLE_NAME = '{table_name}' AND TABLE_SCHEMA = '{database_name}'".format(
118+
table_name=self.BIG_TABLE_NAME, database_name=self.DBNAME)
119+
)
120+
logging.warning('Big table initialized, cardinality: %r', list(cardinality_stats))
121+
110122
def table_populated(self):
111123
"""
112124
:rtype: bool

indexdigest/test/linters/test_0031_low_cardinality_index.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_get_low_cardinality_indices(self):
2020
assert index[0] == '0020_big_table'
2121
assert index[2]['INDEX_NAME'] == 'num_idx'
2222
assert index[2]['COLUMN_NAME'] == 'num'
23-
assert index[2]['CARDINALITY'] > 1
23+
assert index[2]['CARDINALITY'] >= 1
2424
assert index[2]['CARDINALITY'] <= INDEX_CARDINALITY_THRESHOLD
2525

2626
def test_low_cardinality_index(self):
@@ -37,4 +37,5 @@ def test_low_cardinality_index(self):
3737
assert reports[0].context['column_name'] == 'num'
3838
assert reports[0].context['index_name'] == 'num_idx'
3939
assert isinstance(reports[0].context['index_cardinality'], int)
40-
assert 33 <= int(reports[0].context['value_usage']) <= 35
40+
41+
self.assertAlmostEqual(int(reports[0].context['value_usage']), 50, delta=5)

0 commit comments

Comments
 (0)