Skip to content

Commit

Permalink
check_missing_primary_index: add test cases and tables for #34
Browse files Browse the repository at this point in the history
  • Loading branch information
macbre committed Dec 3, 2017
1 parent 600f9e8 commit f20be86
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
33 changes: 33 additions & 0 deletions indexdigest/test/linters/test_0034_missing_primary_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import print_function

from unittest import TestCase

from indexdigest.linters import check_missing_primary_index
from indexdigest.test import Database, DatabaseTestMixin


class LimitedViewDatabase(Database, DatabaseTestMixin):
"""
Limit test to tables from sql/0034-missing-primary-index
"""
def get_tables(self):
return ['0034_with_primary_key', '0034_with_unique_key', '0034_querycache']


class TestMissingPrimaryIndex(TestCase):
@property
def connection(self):
return LimitedViewDatabase.connect_dsn(DatabaseTestMixin.DSN)

def test_missing_primary_index(self):
reports = list(check_missing_primary_index(self.connection))

print(list(map(str, reports)))

self.assertEqual(len(reports), 1)

self.assertEqual(str(reports[0]),
'0034_querycache: "0034_querycache" table does not have any primary or unique index')
self.assertTrue('CREATE TABLE `0034_querycache` (' in reports[0].context['schema'])

# assert False
26 changes: 26 additions & 0 deletions sql/0034-missing-primary-index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Report missing primary or unique keys
--
-- https://github.com/macbre/index-digest/issues/34
DROP TABLE IF EXISTS `0034_with_primary_key`;
CREATE TABLE `0034_with_primary_key` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) CHARSET=utf8;

DROP TABLE IF EXISTS `0034_with_unique_key`;
CREATE TABLE `0034_with_unique_key` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
UNIQUE KEY idx (`id`)
) CHARSET=utf8;

-- https://github.com/Wikia/app/pull/9863
DROP TABLE IF EXISTS `0034_querycache`;
CREATE TABLE `0034_querycache` (
`qc_type` varbinary(32) NOT NULL,
`qc_value` int(10) unsigned NOT NULL DEFAULT '0',
`qc_namespace` int(11) NOT NULL DEFAULT '0',
`qc_title` varchar(255) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
KEY `qc_type` (`qc_type`,`qc_value`)
) CHARSET=utf8;

0 comments on commit f20be86

Please sign in to comment.