Skip to content

Commit

Permalink
Hit a few more edgecases with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erik committed Jan 1, 2019
1 parent f0a0810 commit 2bfaac5
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 12 deletions.
5 changes: 1 addition & 4 deletions squabble/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ def explain(cls):
return cls.__doc__.strip()

def asdict(self):
name = self.__class__.__name__
snake_name = re.sub('(?!^)([A-Z]+)', r'_\1', name).lower()

return {
'message_id': snake_name,
'message_id': self.__class__.__name__,
'message_text': self.format(),
'message_template': self.TEMPLATE,
'message_params': self.kwargs,
Expand Down
2 changes: 1 addition & 1 deletion squabble/rules/add_column_disallow_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def enable(self, ctx, config):
constraints = set()

for c in disallowed:
ty = self._CONSTRAINT_MAP[c.upper()]
ty = self._CONSTRAINT_MAP.get(c.upper())
if ty is None:
raise RuleConfigurationException(
self, 'unknown constraint: `%s`' % c)
Expand Down
2 changes: 1 addition & 1 deletion tests/sql/add_column_disallow_constraints.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- enable:AddColumnDisallowConstraints disallowed=DEFAULT,FOREIGN
-- >>> {"line": 4, "column": 46, "message_id": "constraint_not_allowed"}
-- >>> {"line": 4, "column": 46, "message_id": "ConstraintNotAllowed"}

ALTER TABLE foobar ADD COLUMN colname coltype DEFAULT baz;

Expand Down
2 changes: 1 addition & 1 deletion tests/sql/disallow_change_column_type.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- enable:DisallowChangeColumnType
-- >>> {"line": 4, "column": 47, "message_id": "change_type_not_allowed"}
-- >>> {"line": 4, "column": 47, "message_id": "ChangeTypeNotAllowed"}

ALTER TABLE foo ALTER COLUMN bar SET DATA TYPE baz;

Expand Down
2 changes: 1 addition & 1 deletion tests/sql/disallow_rename_enum_value.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- enable:DisallowRenameEnumValue
-- >>> {"message_id": "rename_not_allowed"}
-- >>> {"message_id": "RenameNotAllowed"}

ALTER TYPE this_is_fine ADD VALUE '!!!';
ALTER TYPE this_is_not_fine RENAME VALUE '!!!' TO 'something else';
4 changes: 2 additions & 2 deletions tests/sql/require_columns.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- enable:RequireColumns required="created_at,timestamp with time zone","updated_at"
-- >>> {"line": 10, "column": 13, "message_id": "missing_required_column", "message_params": {"tbl": "missing_columns", "col": "updated_at"}}
-- >>> {"line": 15, "column": 2, "message_id": "column_wrong_type", "message_params": {"tbl": "type_mismatch", "col": "created_at", "actual": "integer", "required": "timestamp with time zone"}}
-- >>> {"line": 10, "column": 13, "message_id": "MissingRequiredColumn", "message_params": {"tbl": "missing_columns", "col": "updated_at"}}
-- >>> {"line": 15, "column": 2, "message_id": "ColumnWrongType", "message_params": {"tbl": "type_mismatch", "col": "created_at", "actual": "integer", "required": "timestamp with time zone"}}

CREATE TABLE has_all_columns(
created_at timestamp with time zone,
Expand Down
2 changes: 1 addition & 1 deletion tests/sql/require_concurrent_index.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- enable:RequireConcurrentIndex
-- >>> {"line": 10, "column": 24, "message_id": "index_not_concurrent"}
-- >>> {"line": 10, "column": 24, "message_id": "IndexNotConcurrent"}

CREATE TABLE foo(id uuid);

Expand Down
2 changes: 1 addition & 1 deletion tests/sql/require_primary_key.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- enable:RequirePrimaryKey
-- >>> {"line": 14, "column": 13, "message_id": "missing_primary_key"}
-- >>> {"line": 14, "column": 13, "message_id": "MissingPrimaryKey"}

CREATE TABLE inline_pk(
pk int PRIMARY KEY
Expand Down
33 changes: 33 additions & 0 deletions tests/test_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
""" Odds and ends tests to hit corner cases etc in rules logic. """

import unittest

import pytest

import squabble.rule
from squabble import RuleConfigurationException, UnknownRuleException
from squabble.rules.add_column_disallow_constraints \
import AddColumnDisallowConstraints


class TestDisallowConstraints(unittest.TestCase):

def test_missing_constraints(self):
with pytest.raises(RuleConfigurationException):
AddColumnDisallowConstraints().enable(ctx={}, config={})

def test_unknown_constraints(self):
with pytest.raises(RuleConfigurationException):
AddColumnDisallowConstraints().enable(ctx={}, config={
'disallowed': ['UNIQUE', 'foo']
})


class TestRuleRegistry(unittest.TestCase):
def test_get_meta_unknown_name(self):
with pytest.raises(UnknownRuleException):
squabble.rule.Registry.get_meta('asdfg')

def test_get_class_unknown_name(self):
with pytest.raises(UnknownRuleException):
squabble.rule.Registry.get_class('asdfg')

0 comments on commit 2bfaac5

Please sign in to comment.