-
Notifications
You must be signed in to change notification settings - Fork 37
add check censored udf, tokenize udf #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
haileyok
wants to merge
5
commits into
main
Choose a base branch
from
hailey/add-censor-tokenize-udfs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
03bdd59
add check censored udf, tokenize udf
haileyok a83ff35
fix typo
haileyok d05d27d
add additional context to docstring
haileyok a5fab61
explicitly use unicode escapes in tokenize replace
haileyok 103d03f
more explicit curly apostrophe test cases
haileyok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
215 changes: 215 additions & 0 deletions
215
osprey_worker/src/osprey/engine/stdlib/udfs/tests/test_unicode_censored.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| import pytest | ||
| from osprey.engine.conftest import ExecuteFunction | ||
| from osprey.engine.stdlib.udfs.unicode_censored import StringCheckCensored | ||
| from osprey.engine.udf.registry import UDFRegistry | ||
|
|
||
| pytestmark = [ | ||
| pytest.mark.use_udf_registry( | ||
| UDFRegistry.with_udfs( | ||
| StringCheckCensored, | ||
| ) | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| class TestCheckCensoredUDF: | ||
| """Tests for the StringCheckCensored UDF.""" | ||
|
|
||
| def test_basic_match(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| Result = StringCheckCensored(s="cat", pattern="cat") | ||
| """) | ||
| assert data['Result'] is True | ||
|
|
||
| def test_censored_match(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| Result = StringCheckCensored(s="c@t", pattern="cat") | ||
| """) | ||
| assert data['Result'] is True | ||
|
|
||
| def test_no_match(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| Result = StringCheckCensored(s="dog", pattern="cat") | ||
| """) | ||
| assert data['Result'] is False | ||
|
|
||
| def test_unicode_lookalike_match(self, execute: ExecuteFunction): | ||
| # using cyrillic 'а' which looks like latin 'a' | ||
| data = execute(""" | ||
| Result = StringCheckCensored(s="cаt", pattern="cat") | ||
| """) | ||
| assert data['Result'] is True | ||
|
|
||
| data = execute(""" | ||
| Result = StringCheckCensored(s="𝒞𝞪𝔗", pattern="cat") | ||
| """) | ||
| assert data['Result'] is True | ||
|
|
||
| def test_plural_option_enabled(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| WithPlural = StringCheckCensored(s="cats", pattern="cat", plurals=True) | ||
| WithoutPlural = StringCheckCensored(s="cats", pattern="cat", plurals=False) | ||
| """) | ||
| assert data['WithPlural'] is True | ||
| assert data['WithoutPlural'] is False | ||
|
|
||
| def test_substring_option(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| WithSubstring = StringCheckCensored(s="concatenate", pattern="cat", substrings=True) | ||
| WithoutSubstring = StringCheckCensored(s="concatenate", pattern="cat", substrings=False) | ||
| """) | ||
| assert data['WithSubstring'] is True | ||
| assert data['WithoutSubstring'] is False | ||
|
|
||
| def test_must_be_censored_option(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| PlainText = StringCheckCensored(s="cat", pattern="cat", must_be_censored=True) | ||
| CensoredText = StringCheckCensored(s="c@t", pattern="cat", must_be_censored=True) | ||
| """) | ||
| assert data['PlainText'] is False | ||
| assert data['CensoredText'] is True | ||
|
|
||
| def test_must_be_censored_with_surrounding_test(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| PlainText = StringCheckCensored(s="the cat sat", pattern="cat", must_be_censored=True) | ||
| CensoredText = StringCheckCensored(s="the c@t sat", pattern="cat", must_be_censored=True) | ||
| """) | ||
| assert data['PlainText'] is False | ||
| assert data['CensoredText'] is True | ||
|
|
||
| def test_case_insensitive(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| Upper = StringCheckCensored(s="CAT", pattern="cat") | ||
| Mixed = StringCheckCensored(s="CaT", pattern="cat") | ||
| """) | ||
| assert data['Upper'] is True | ||
| assert data['Mixed'] is True | ||
|
|
||
| def test_with_special_chars_in_pattern(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| Result = StringCheckCensored(s="a.b", pattern="a.b") | ||
| """) | ||
| assert data['Result'] is True | ||
|
|
||
| def test_empty_string(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| Result = StringCheckCensored(s="", pattern="cat") | ||
| """) | ||
| assert data['Result'] is False | ||
|
|
||
| @pytest.mark.parametrize( | ||
| 'input_str,pattern,expected', | ||
| [ | ||
| ('hello world', 'hello', True), | ||
| ('h3ll0', 'hello', True), | ||
| ('h e l l o', 'hello', False), | ||
| ('HELLO', 'hello', True), | ||
| ('dog', 'cat', False), | ||
| ('', 'test', False), | ||
| ('c@t', 'cat', True), | ||
| ('h4ck3r', 'hacker', True), | ||
| ('p@$$w0rd', 'password', True), | ||
| ('t35t', 'test', True), | ||
| ('1337', 'leet', True), | ||
| ('n00b', 'noob', True), | ||
| ('ph1sh', 'phish', True), | ||
| ('саt', 'cat', True), | ||
| ('руthоn', 'python', True), | ||
| ('НЕLLО', 'hello', True), | ||
| ('Ηello', 'hello', True), | ||
| ('Αpple', 'apple', True), | ||
| ('Βank', 'bank', True), | ||
| ('Κing', 'king', True), | ||
| ('Νice', 'nice', True), | ||
| ('Οpen', 'open', True), | ||
| ('Ρython', 'python', True), | ||
| ('Τest', 'test', True), | ||
| ('Χmas', 'xmas', True), | ||
| ('Υes', 'yes', True), | ||
| ('Ζero', 'zero', True), | ||
| ('𝐜𝐚𝐭', 'cat', True), | ||
| ('𝑐𝑎𝑡', 'cat', True), | ||
| ('𝒄𝒂𝒕', 'cat', True), | ||
| ('𝓬𝓪𝓽', 'cat', True), | ||
| ('𝔠𝔞𝔱', 'cat', True), | ||
| ('𝕔𝕒𝕥', 'cat', True), | ||
| ('𝖈𝖆𝖙', 'cat', True), | ||
| ('𝗰𝗮𝘁', 'cat', True), | ||
| ('𝘤𝘢𝘵', 'cat', True), | ||
| ('𝙘𝙖𝙩', 'cat', True), | ||
| ('𝚌𝚊𝚝', 'cat', True), | ||
| ('cat', 'cat', True), | ||
| ('hello', 'hello', True), | ||
| ('HELLO', 'hello', True), | ||
| ('ᑕᗩT', 'cat', True), | ||
| ('ꓚꓮT', 'cat', True), | ||
| ('ⲤⲀT', 'cat', True), | ||
| ('ԁоg', 'dog', True), | ||
| ('bаnk', 'bank', True), | ||
| ('pаypаl', 'paypal', True), | ||
| ('аmаzоn', 'amazon', True), | ||
| ('s3cur1ty', 'security', True), | ||
| ('4dm1n', 'admin', True), | ||
| ('r00t', 'root', True), | ||
| ('z3r0', 'zero', True), | ||
| ('0n3', 'one', True), | ||
| ('tw0', 'two', True), | ||
| ('с@т', 'cat', True), | ||
| ('ρ@$$ωθrd', 'password', True), | ||
| ('𝕙𝕒𝕔𝕜', 'hack', True), | ||
| ('հello', 'hello', True), | ||
| ('ոice', 'nice', True), | ||
| ('քhone', 'phone', True), | ||
| ('ցame', 'game', True), | ||
| ('(at', 'cat', True), | ||
| ('<at', 'cat', True), | ||
| ('ca+', 'cat', True), | ||
| ('ca7', 'cat', True), | ||
| ('he!!o', 'hello', True), | ||
| ('he||o', 'hello', True), | ||
| ('c.a.t', 'cat', True), | ||
| ('c_a_t', 'cat', True), | ||
| ('c+a+t', 'cat', True), | ||
| ('c/a/t', 'cat', True), | ||
| ('c@a@t', 'cat', True), | ||
| ('c#a#t', 'cat', True), | ||
| # zero-width and invisible chars as separators | ||
| ('c\u200bat', 'cat', True), | ||
| ('c\u200cat', 'cat', True), | ||
| ('c\u200dat', 'cat', True), | ||
| ('ca\u200et', 'cat', True), | ||
| ('ca\u200ft', 'cat', True), | ||
| ('c\ufeffat', 'cat', True), | ||
| # multiple zero-width chars | ||
| ('c\u200b\u200bat', 'cat', True), | ||
| ('c\u200b\u200c\u200dat', 'cat', True), | ||
| # zero-width between every letter | ||
| ('c\u200ba\u200bt', 'cat', True), | ||
| ('h\u200be\u200bl\u200bl\u200bo', 'hello', True), | ||
| ('c\u200b_a\u200c_t', 'cat', True), | ||
| ('c.\u200ba.\u200bt', 'cat', True), | ||
| ], | ||
| ) | ||
| def test_various_inputs(self, execute: ExecuteFunction, input_str: str, pattern: str, expected: bool): | ||
| data = execute(f""" | ||
| Result = StringCheckCensored(s="{input_str}", pattern="{pattern}") | ||
| """) | ||
| assert data['Result'] is expected | ||
|
|
||
| def test_leet_speak_variations(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| Leet1 = StringCheckCensored(s="h3ll0", pattern="hello") | ||
| Leet2 = StringCheckCensored(s="t35t", pattern="test") | ||
| """) | ||
| assert data['Leet1'] is True | ||
| assert data['Leet2'] is True | ||
|
|
||
| def test_with_separator_characters(self, execute: ExecuteFunction): | ||
| data = execute(""" | ||
| Dots = StringCheckCensored(s="c.a.t", pattern="cat", substrings=True) | ||
| Underscores = StringCheckCensored(s="c_a_t", pattern="cat", substrings=True) | ||
| Mixed = StringCheckCensored(s="c.a_t", pattern="cat", substrings=True) | ||
| """) | ||
| assert data['Dots'] is True | ||
| assert data['Underscores'] is True | ||
| assert data['Mixed'] is True |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, what does first
replacedo?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah nice catch. there are two different apostrophes we want to replace with a "normal" apostrophe, and it looks like that first one actually might have gotten replaced with a normal apostrophe maybe through copy/paste or something. switched it to just use unicode escapes so its more obvious what it's doing
a5fab61
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!