Browse Source

Added test code for patterns

filename-pattern
Johann Schmitz 3 years ago
parent
commit
1fbde38a03
Signed by: ercpe <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
2 changed files with 69 additions and 0 deletions
  1. +3
    -0
      amavisvt/patterns.py
  2. +66
    -0
      tests/test_patterns.py

+ 3
- 0
amavisvt/patterns.py View File

@@ -8,6 +8,9 @@ chunk_re = re.compile(r'[_\-\.]', re.IGNORECASE | re.UNICODE)
MIN_CHUNKS = 2

def calculate(s, choices):
if not (s or "").strip():
return None

chunks = chunk_re.split(s)

if len(chunks) <= MIN_CHUNKS:


+ 66
- 0
tests/test_patterns.py View File

@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
from amavisvt import patterns


class TestPatterns(object):

def test_empty_or_null(self):
assert patterns.calculate(None, []) is None
assert patterns.calculate('', []) is None

def test_no_chunks(self):
assert patterns.calculate('abc', []) is None

def test_too_few_chunks(self):
assert patterns.calculate('abc-def', []) is None

def test_no_choices(self):
assert patterns.calculate('abc-def-ghi', []) is None

def test_no_matching_choice(self):
# not enough chunks in choices
assert patterns.calculate('abc-def-ghi', [
'foo'
]) is None

# no choice has the same number of chunks as the s argument
assert patterns.calculate('abc-def-ghi', [
'foo',
'foo-bar',
'foo-bar-baz-bat'
]) is None

def test_too_many_different_chunks(self):
# all chunks are different
assert patterns.calculate('abc-def-ghi', [
'foo-bar-baz'
]) is None

# more than one is different
assert patterns.calculate('abc-def-ghi', [
'abc-jkl-mno'
]) is None

def test_all_chunks_equal(self):
assert patterns.calculate('foo-bar-baz', ['foo-bar-baz']) is None

def test_pattern_ok(self):
assert patterns.calculate('foo-bar-baz', [
'foo-bar-bat'
]) == 'foo-bar-[RANDOM]'

assert patterns.calculate('foo-bar-baz', [
'foo-bar-bat',
'foo-bar-bar',
'foo-bar-123',
]) == 'foo-bar-[RANDOM]'

def test_string_split(self):
for separator in '_', '-', '.':
assert patterns.calculate('foo-bar-baz', [
'foo-bar-bat'.replace('-', separator)
]) == 'foo-bar-[RANDOM]'

assert patterns.calculate('foo-bar-baz'.replace('-', separator), [
'foo-bar-bat'
]) == 'foo-bar-[RANDOM]'

Loading…
Cancel
Save