async def test_run__passes_correct_blocklists_to_subscriptions(mock_client):
    submission = MockSubmission("12322")
    api = MockExportAPI().with_submission(submission)
    watcher = SubscriptionWatcher(api, mock_client)
    method_called = MockMethod([submission])
    watcher._get_new_results = method_called.async_call
    watcher.BACK_OFF = 1
    watcher.blocklists = {156: {"test", "ych"}, -200: {"example"}}
    sub1 = MockSubscription("deer", 156)
    sub2 = MockSubscription("dog", -232)
    watcher.subscriptions = [sub1, sub2]

    task = asyncio.get_event_loop().create_task(watcher_killer(watcher))
    # Run watcher
    await watcher.run()
    await task

    assert submission in sub1.submissions_checked
    assert len(sub1.blocklists) == 1
    assert sub1.blocklists[0] in [
        AndQuery([NotQuery(WordQuery("test")),
                  NotQuery(WordQuery("ych"))]),
        AndQuery([NotQuery(WordQuery("ych")),
                  NotQuery(WordQuery("test"))])
    ]
    assert submission in sub2.submissions_checked
    assert len(sub2.blocklists) == 1
    assert sub2.blocklists[0] == AndQuery([])
    assert method_called.called
Esempio n. 2
0
def test_connectors():
    assert parse_query("first and document") == AndQuery([WordQuery("first"), WordQuery("document")])
    assert parse_query("first or document") == OrQuery([WordQuery("first"), WordQuery("document")])
    assert parse_query("first AND doc OR document") == OrQuery(
        [AndQuery([WordQuery("first"), WordQuery("doc")]), WordQuery("document")]
    )
    assert parse_query("first doc OR document") == OrQuery(
        [AndQuery([WordQuery("first"), WordQuery("doc")]), WordQuery("document")]
    )
Esempio n. 3
0
def test_fields():
    assert parse_query("keyword:first document") == AndQuery(
        [WordQuery("first", KeywordField()), WordQuery("document")]
    )
    assert parse_query("keyword:\"first document\"") == PhraseQuery("first document", KeywordField())
    with pytest.raises(InvalidQueryException):
        parse_query("keyword:(first and document)")
    assert parse_query("@keyword first document") == AndQuery(
        [WordQuery("first", KeywordField()), WordQuery("document")]
    )
Esempio n. 4
0
def test_negation():
    assert parse_query("-first") == NotQuery(WordQuery("first"))
    assert parse_query("!first") == NotQuery(WordQuery("first"))
    assert parse_query("! first") == NotQuery(WordQuery("first"))
    assert parse_query("not first") == NotQuery(WordQuery("first"))
    assert parse_query("NOT first") == NotQuery(WordQuery("first"))
    assert parse_query("first not second") == AndQuery([WordQuery("first"), NotQuery(WordQuery("second"))])
Esempio n. 5
0
def test_brackets():
    assert parse_query("(first)") == WordQuery("first")
    assert parse_query("first and (doc or document)") == AndQuery(
        [WordQuery("first"), OrQuery([WordQuery("doc"), WordQuery("document")])]
    )
    assert parse_query("first (doc or document)") == AndQuery(
        [WordQuery("first"), OrQuery([WordQuery("doc"), WordQuery("document")])]
    )
    with pytest.raises(InvalidQueryException):
        parse_query("first (doc or document")
def test_matches_result__doesnt_match_blocklisted_tag():
    query = "an-example"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="Test submission",
        description="this submission is just an-example",
        keywords=["example", "submission",
                  "keywords"]).build_full_submission()

    match = subscription.matches_result(submission,
                                        NotQuery(WordQuery("test")))

    assert not match
Esempio n. 7
0
def whoosh_to_custom(q: Query) -> 'Query':
    if isinstance(q, Or):
        return OrQuery([whoosh_to_custom(w) for w in q.subqueries])
    if isinstance(q, And):
        return AndQuery([whoosh_to_custom(w) for w in q.subqueries])
    if isinstance(q, Term):
        if q.fieldname == "rating":
            return RatingQuery(rating_dict[q.text])
        field = get_field_for_name(q.fieldname)
        return WordQuery(q.text, field)
    if isinstance(q, Prefix):
        field = get_field_for_name(q.fieldname)
        return PrefixQuery(q.text, field)
    if isinstance(q, Wildcard):
        field = get_field_for_name(q.fieldname)
        regex = fnmatch.translate(q.text)
        return RegexQuery(regex, field)
    if isinstance(q, Phrase):
        field = get_field_for_name(q.fieldname)
        quote = " ".join(q.words)
        return PhraseQuery(quote, field)
    raise NotImplementedError
Esempio n. 8
0
def test_description_field():
    assert parse_query("description:first") == WordQuery("first", DescriptionField())
    assert parse_query("desc:first") == WordQuery("first", DescriptionField())
    assert parse_query("message:first") == WordQuery("first", DescriptionField())
Esempio n. 9
0
def test_parser():
    assert parse_query("first") == WordQuery("first")
    assert parse_query("first document") == AndQuery([WordQuery("first"), WordQuery("document")])
Esempio n. 10
0
def test_title_field():
    assert parse_query("title:first") == WordQuery("first", TitleField())
    assert parse_query("TITLE: first") == WordQuery("first", TitleField())
Esempio n. 11
0
def test_keyword_field():
    assert parse_query("keywords:ych") == WordQuery("ych", KeywordField())
    assert parse_query("tags:deer") == WordQuery("deer", KeywordField())
    assert parse_query("tag:dragon") == WordQuery("dragon", KeywordField())
Esempio n. 12
0
def test_rating_field():
    assert parse_query("rating:general") == RatingQuery(Rating.GENERAL)
    assert parse_query("-rating:adult") == NotQuery(RatingQuery(Rating.ADULT))
    assert parse_query("ych rating:mature") == AndQuery([WordQuery("ych"), RatingQuery(Rating.MATURE)])
Esempio n. 13
0
def test_word_starting_not():
    assert parse_query("notice") == WordQuery("notice")
Esempio n. 14
0
def test_exceptions():
    assert parse_query("multi* except (multitude or \"no multi\" or multicol*)") == ExceptionQuery(
        PrefixQuery("multi"), LocationOrQuery([
            WordQuery("multitude"), PhraseQuery("no multi"), PrefixQuery("multicol")
        ])
    )
Esempio n. 15
0
def test_exception():
    assert parse_query("multi* except multitude") == ExceptionQuery(
        PrefixQuery("multi"), LocationOrQuery([WordQuery("multitude")])
    )
Esempio n. 16
0
def test_quotes_and_brackets():
    assert parse_query("not (hello \"first :) document\")") == NotQuery(
        AndQuery([WordQuery("hello"), PhraseQuery("first :) document")])
    )
Esempio n. 17
0
def test_artist_field():
    assert parse_query("artist:rajii") == WordQuery("rajii", ArtistField())
    assert parse_query("author:va-artist") == WordQuery("va-artist", ArtistField())
    assert parse_query("poster:fender") == WordQuery("fender", ArtistField())
    assert parse_query("lower:fender") == WordQuery("fender", ArtistField())
    assert parse_query("uploader:zephyr42") == WordQuery("zephyr42", ArtistField())