-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 48 additions & 18 deletions
66
src/tribler-core/tribler_core/components/tag/rules/tests/test_general_rules.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,57 @@ | ||
from tribler_core.components.tag.rules.tag_rules import extract_only_valid_tags, extract_tags | ||
from tribler_core.components.tag.rules.tag_rules import brackets, delimiter, extract_only_valid_tags, extract_tags, \ | ||
square_brackets, tags_in_brackets, tags_in_square_brackets | ||
|
||
|
||
def test_extract_tags_from_brackets(): | ||
actual = set(extract_tags('[tag]')) | ||
expected = {'tag'} | ||
assert actual == expected | ||
def test_delimiter(): | ||
# assert that delimiter splits words correctly | ||
assert delimiter.findall('word1 word2 word3') == ['word1', 'word2', 'word3'] | ||
assert delimiter.findall('word1.word2.word3') == ['word1', 'word2', 'word3'] | ||
assert delimiter.findall('word1,word2,word3') == ['word1', 'word2', 'word3'] | ||
assert delimiter.findall('word1/word2/word3') == ['word1', 'word2', 'word3'] | ||
assert delimiter.findall('word1|word2|word3') == ['word1', 'word2', 'word3'] | ||
|
||
actual = set(extract_tags('[tag1, tag2]')) | ||
expected = {'tag1', 'tag2'} | ||
assert actual == expected | ||
assert delimiter.findall('word1 /.,word2') == ['word1', 'word2'] | ||
|
||
actual = set(extract_tags('[tag1, tag2][tag3, tag4]')) | ||
expected = {'tag1', 'tag2', 'tag3', 'tag4'} | ||
assert actual == expected | ||
|
||
actual = set(extract_tags('text [tag1] text [tag2] text')) | ||
expected = {'tag1', 'tag2'} | ||
assert actual == expected | ||
def test_square_brackets(): | ||
# test that square_brackets regex correctly defines content in square brackets | ||
assert square_brackets.findall('[word1] [word2 word3]') == ['word1', 'word2 word3'] | ||
assert square_brackets.findall('[word1 [word2] word3]') == ['word2'] | ||
|
||
actual = set(extract_tags('[[tag1]] [tag2 [tag3] tag4] [tag5')) | ||
expected = {'tag1', 'tag3'} | ||
assert actual == expected | ||
|
||
def test_brackets(): | ||
# test that brackets regex correctly defines content in brackets | ||
assert brackets.findall('(word1) (word2 word3)') == ['word1', 'word2 word3'] | ||
assert brackets.findall('(word1 (word2) word3)') == ['word2'] | ||
|
||
|
||
def test_tags_in_square_brackets(): | ||
# test that tags_in_square_brackets rule works correctly with extract_tags function | ||
text = 'text [tag1, tag2] text1 [tag3|tag4] text2, (tag5, tag6)' | ||
expected_tags = {'tag1', 'tag2', 'tag3', 'tag4'} | ||
|
||
actual_tags = set(extract_tags(text, rules=[tags_in_square_brackets])) | ||
assert actual_tags == expected_tags | ||
|
||
|
||
def test_tags_in_brackets(): | ||
# test that tags_in_brackets rule works correctly with extract_tags function | ||
text = 'text (tag1, tag2) text1 (tag3|tag4) text2, [tag5, tag6]' | ||
expected_tags = {'tag1', 'tag2', 'tag3', 'tag4'} | ||
|
||
actual_tags = set(extract_tags(text, rules=[tags_in_brackets])) | ||
assert actual_tags == expected_tags | ||
|
||
|
||
def test_default_rules(): | ||
# test that default_rules works correctly with extract_tags function | ||
text = 'text (tag1, tag2) text1 (tag3|tag4) text2, [tag5, tag6]' | ||
expected_tags = {'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6'} | ||
|
||
actual_tags = set(extract_tags(text)) | ||
assert actual_tags == expected_tags | ||
|
||
|
||
def test_extract_only_valid_tags(): | ||
assert set(extract_only_valid_tags('[valid-tag in va li d]')) == {'valid-tag'} | ||
# test that extract_only_valid_tags extracts only valid tags | ||
assert set(extract_only_valid_tags('[valid-tag, in va li d]')) == {'valid-tag'} |