Exemple #1
0
def test_get_items_by_url():
    os.environ["STAGE"] = "dev"
    with Session() as session:
        item = Item()
        item.content = "Test content"
        item.language = "de"
        item.status = "closed"
        item = item_handler.create_item(item, session)

        item2 = Item()
        item2.content = "Test content2"
        item2.language = "de"
        item2.status = "closed"
        item2 = item_handler.create_item(item2, session)

        url = URL()
        url.id = "url_id"
        url.url = "www.test.de/artikel1"

        item_url = ItemURL()
        item_url.id = "item_url_id"
        item_url.item_id = item.id
        item_url.url_id = url.id

        session.add_all([url, item_url])
        session.commit()

        # Testing get_items without url query param 204
        for event in [{}, {"queryStringParameters": None}]:
            response = get_closed_items(event, None)
            assert response['statusCode'] == 200
            body = json.loads(response['body'])
            assert len(body) == 2

        # Testing get_items with url query param 200
        for url in [url.url, url.url + '?queryparam=shouldbeignored']:
            response = get_closed_items(get_url_event(url), None)
            assert response['statusCode'] == 200
            body = json.loads(response['body'])
            assert len(body) == 1
            assert body[0]['id'] == item.id

        # Testing get_items with url query param 204 - no item with url
        for url in ['sinnloseUrl', 'www.test.de/artikel2', 'www.test.de']:
            response = get_closed_items(get_url_event(url), None)
            assert response['statusCode'] == 204

        # Testing get_items with url query param 204 - item not closed
        item.status = "open"
        session.merge(item)
        session.commit()
        response = get_closed_items(get_url_event("www.test.de/artikel1"),
                                    None)
        assert response['statusCode'] == 204
    def test_create_tagreport_1(self):
        os.environ["STAGE"] = "dev"

        with Session() as session:

            # creating items
            item = Item()
            item.content = "RKI bestätigt Covid-19 Sterblichkeitsrate von 0,01 Prozent in (...) - Corona Transition "
            item.language = "de"
            item = item_handler.create_item(item, session)
            list_tags = [
                'Corona-Maßnahmen', 'Senkung der Treibhausgasemissionen',
                'China', 'Film', 'Menschen', 'Kanzlerkandidatur', 'sars-cov-2',
                '#verunglimpft', 'g.co/privacytools', 'Gregor Gysi',
                'Bundestagswahl2021', '19', '-',
                'Statistik falsch interpretiert', 'Bild.de', 'JF',
                'MdB Hansjörg Müller (AFD)', '100 Tage', 'Gruene',
                '#notwendige Polizeimaßnahmen', 'Sonne'
            ]
            for _ in range(200):
                list_tags.append(
                    random.choice(list_tags) + "-" + random.choice(list_tags))
            # store tags
            event = {"item": item.to_dict(), "Tags": list_tags}
            context = ""
            EnrichItem.store_itemtags(event, context)

            event = ""
            context = ""
            s = time.perf_counter()
            GetTags.create_tagreport(event, context)
            elapsed = time.perf_counter() - s
            assert elapsed < 30
    def test_store_itemtag(self, monkeypatch):
        monkeypatch.setenv("DBNAME", "Test")
        os.environ["STAGE"] = "dev"

        session = get_db_session(True, None)

        # creating items
        item = Item()
        item.content = "RKI bestätigt Covid-19 Sterblichkeitsrate von 0,01 Prozent in (...) - Corona Transition "
        item.language = "de"
        item = item_handler.create_item(item, True, session)
        list_tags = ['RKI', 'Covid', 'Corona Transition']

        # store tags
        event = {"item": item.to_dict(), "Tags": list_tags}
        context = ""
        EnrichItem.store_itemtags(event, context, True, session)

        tag = tag_handler.get_tag_by_content(list_tags[0], True, session)
        assert tag.tag == list_tags[0]

        itemtag = tag_handler.get_itemtag_by_tag_and_item_id(
            tag.id, item.id, True, session)
        assert itemtag.id is not None

        event = {"pathParameters": {"item_id": item.id}}
        ret = GetTags.get_tags_for_item(event, context, True, session)
        body = ret['body']
        # Deserialize if body is string
        if isinstance(body, str):
            tags = json.loads(body)['Tags']
        else:
            tags = body['Tags']
        assert tags == list_tags
    def test_post_tags_for_item_2(self, monkeypatch):
        monkeypatch.setenv("DBNAME", "Test")
        os.environ["STAGE"] = "dev"

        session = get_db_session(True, None)

        # creating items
        item = Item()
        item.content = "https://corona-transition.org/rki-bestatigt-covid-19-sterblichkeitsrate-von-0-01-prozent-in" \
                       "-deutschland?fbclid=IwAR2vLIkW_3EejFaeC5_wC_410uKhN_WMpWDMAcI-dF9TTsZ43MwaHeSl4n8%22 "
        item.language = "de"
        item = item_handler.create_item(item, True, session)

        event = {"pathParameters": {"item_id": item.id}}
        context = ""
        response = GetTags.get_tags_for_item(event, context, True, session)
        body = response['body']
        # Deserialize if body is string
        if isinstance(body, str):
            tags = json.loads(body)['Tags']
        else:
            tags = body['Tags']
        assert tags == []

        json.dumps({"tags": ["RKI", "Covid-19"]})
        event = {
            "pathParameters": {
                "item_id": item.id
            },
            "body": json.dumps({"tags": ["RKI", "Covid-19"]})
        }
        response = GetTags.post_tags_for_item(event, context, True, session)
        body = response['body']
        # Deserialize if body is string
        if isinstance(body, str):
            tags_added = json.loads(body)['added tags']
            tags_removed = json.loads(body)['removed tags']
        else:
            tags_added = body['added tags']
            tags_removed = body['removed tags']
        assert 'RKI' in tags_added
        assert 'Covid-19' in tags_added
        assert len(tags_removed) == 0
        assert tags_removed == []
        response = GetTags.get_tags_for_item(event, context, True, session)
        body = response['body']
        # Deserialize if body is string
        if isinstance(body, str):
            tags = json.loads(body)['Tags']
        else:
            tags = body['Tags']
        assert tags == ['RKI', 'Covid-19']
    def test_get_online_factcheck_by_itemid_2(self, monkeypatch):
        monkeypatch.setenv("DBNAME", "Test")
        os.environ["STAGE"] = "dev"

        session = get_db_session(True, None)

        # creating items
        item = Item()
        item.content = "https://corona-transition.org/rki-bestatigt-covid-19-sterblichkeitsrate-von-0-01-prozent-in" \
                       "-deutschland?fbclid=IwAR2vLIkW_3EejFaeC5_wC_410uKhN_WMpWDMAcI-dF9TTsZ43MwaHeSl4n8%22 "
        item.language = "de"
        item = item_handler.create_item(item, True, session)

        # store a fact check
        event = {
            "item": {
                "id": item.id,
                "content": item.content,
                "language": item.language,
            },
            "KeyPhrases": [
                "das Zahlenmaterial", "es", "den letzten 7 Tagen", "das RKI",
                "sich"
            ],
            "Entities": [
                "RKI", "0,01 Prozent", "19 Sterblichkeitsrate",
                "Corona Transition", "Covid"
            ],
            "Sentiment":
            "NEUTRAL"
        }
        context = ""
        EnrichItem.store_itementities(event, context, True, session)
        EnrichItem.store_itemphrases(event, context, True, session)

        event = {"pathParameters": {"item_id": item.id}}
        context = {}
        s = time.perf_counter()
        response = get_online_factcheck.get_online_factcheck(
            event, context, True, session)
        elapsed = time.perf_counter() - s
        body = response['body']
        # Deserialize if body is string
        if isinstance(body, str):
            factcheck = json.loads(body)
        else:
            factcheck = body
        assert factcheck[
            'url'] == 'https://correctiv.org/faktencheck/2020/07/09/nein-rki-bestaetigt-nicht-eine-covid-19-sterblichkeitsrate-von-001-prozent-in-deutschland/'
        assert factcheck[
            'title'] == 'Falsch. Das Robert-Koch-Institut bestätigte nicht eine Covid-19- Sterblichkeitsrate von 0,01 Prozent in Deutschland.'
        assert elapsed < 3
def create_item_with_urls(session, urls) -> Item:
    item = Item()
    item.language = "de"
    item = item_handler.create_item(item, session)
    for url_str in urls:
        url = URL()
        url.id = str(uuid4())
        url.url = url_str
        itemUrl = ItemURL()
        itemUrl.item_id = item.id
        itemUrl.url_id = url.id
        itemUrl.id = str(uuid4())
        session.add(url)
        session.add(itemUrl)
        session.commit()
    return item
Exemple #7
0
def test_get_closed_items():

    # pre-stuff
    os.environ["STAGE"] = "dev"

    with Session() as session:

        context = None

        # create item
        item = Item()
        item.content = "Test content"
        item.language = "de"
        item.status = "closed"
        item = item_handler.create_item(item, session)

        # create events with tags
        event1 = {
            "pathParameters": {
                "item_id": item.id
            },
            "body": json.dumps({"tags": ['C', 'B', 'D']})
        }
        event2 = {
            "pathParameters": {
                "item_id": item.id
            },
            "body": json.dumps({"tags": ['B', 'C']})
        }
        event3 = {
            "pathParameters": {
                "item_id": item.id
            },
            "body": json.dumps({"tags": ['A', 'B']})
        }

        # post tags
        GetTags.post_tags_for_item(event1, context)
        GetTags.post_tags_for_item(event2, context)
        GetTags.post_tags_for_item(event3, context)

        # Check if tags are sorted by number of mentions
        response = get_closed_items(event1, context)
        body = response['body']
        tags = json.loads(body)[0]['tags']
        assert tags in [['B', 'C', 'A', 'D'], ['B', 'C', 'D', 'A']]
    def test_get_online_factcheck_by_itemid_5(self, monkeypatch):
        monkeypatch.setenv("DBNAME", "Test")
        os.environ["STAGE"] = "dev"

        session = get_db_session(True, None)

        # creating items
        item = Item()
        item.content = "Ein in sozialen Medien kursierendes Video soll angeblich große Schwächen der zum Corona-Test genutzten PCR-Methode offenbaren. 'Sensation! Naomi Seibt widerlegt den PCR Test von Prof Drosten! KEIN Virus EXISTENT!!!', heißt es etwa (hier archiviert, hier Video archiviert)"
        item.language = "de"
        item = item_handler.create_item(item, True, session)

        # store a fact check
        event = {
            "item": {
                "id": item.id,
                "content": item.content,
                "language": item.language,
            },
            "KeyPhrases": ["Drosten", "Test", "Corona", "PCR"],
            "Entities": []
        }
        context = ""
        EnrichItem.store_itementities(event, context, True, session)
        EnrichItem.store_itemphrases(event, context, True, session)

        event = {"pathParameters": {"item_id": item.id}}
        context = {}
        s = time.perf_counter()
        response = get_online_factcheck.get_online_factcheck(
            event, context, True, session)
        elapsed = time.perf_counter() - s
        body = response['body']
        # Deserialize if body is string
        if isinstance(body, str):
            factcheck = json.loads(body)
        else:
            factcheck = body
        assert factcheck[
            'url'] == 'https://correctiv.org/faktencheck/2020/11/23/nein-christian-drosten-hat-2014-nicht-gesagt-dass-er-pcr-tests-fuer-untauglich-halte/'
        assert factcheck[
            'title'] == 'Fehlender Kontext. Drosten sagte nicht, PCR-Tests seien „untauglich“ – er kritisierte die Teststrategie 2014 in der MERS-Epidemie. Seine Aussagen lassen sich nicht auf die heutige Coronavirus-Pandemie übertragen.'
        assert elapsed < 3
    def test_get_online_factcheck_by_itemid_4(self, monkeypatch):
        monkeypatch.setenv("DBNAME", "Test")
        os.environ["STAGE"] = "dev"

        session = get_db_session(True, None)

        # creating items
        item = Item()
        item.content = "https://kopp-report.de/helios-kliniken-veroeffentlichen-corona-fakten-keine-pandemie-von-nationaler-tragweite/?fbclid=IwAR1fMRjkKXXYQUiNxYrgYczcffvNZbW-F3z8Q4f4Ar00caSNO1KjFtyJrG4"
        item.language = None
        item = item_handler.create_item(item, True, session)

        # store a fact check
        event = {
            "item": {
                "id": item.id,
                "content": item.content,
                "language": item.language,
            },
            "KeyPhrases": [],
            "Entities": [],
            "Sentiment": "NEUTRAL"
        }
        context = ""
        EnrichItem.store_itementities(event, context, True, session)
        EnrichItem.store_itemphrases(event, context, True, session)

        event = {"pathParameters": {"item_id": item.id}}
        context = {}
        s = time.perf_counter()
        response = get_online_factcheck.get_online_factcheck(
            event, context, True, session)
        elapsed = time.perf_counter() - s
        body = response['body']
        # Deserialize if body is string
        assert body == 'No factcheck found. Exception: Language of Claim not recognized.'
        assert elapsed < 3
def test_post_tags_for_item():
    # pre-stuff
    os.environ["STAGE"] = "dev"

    with Session() as session:

        # create item
        item = Item()
        item.content = "https://corona-transition.org/rki-bestatigt-covid-19-sterblichkeitsrate-von-0-01-prozent-in" \
            "-deutschland?fbclid=IwAR2vLIkW_3EejFaeC5_wC_410uKhN_WMpWDMAcI-dF9TTsZ43MwaHeSl4n8%22 "
        item.language = "de"
        item = item_handler.create_item(item, session)

        # store a fact check
        event = {
            "item": {
                "id": item.id,
                "content": item.content,
                "language": item.language,
            },
            "Tags": ["RKI", "Covid", "Corona Transition"]
        }
        context = ""
        EnrichItem.store_itemtags(event, context)

        # create event
        event = {"pathParameters": {"item_id": item.id}}

        response = GetTags.get_tags_for_item(event, context)
        body = response['body']
        tags = json.loads(body)['Tags']
        assert tags == ['RKI', 'Covid', 'Corona Transition']

        # create event with 1 already existing tag and 1 new tag
        event = {
            "pathParameters": {
                "item_id": item.id
            },
            "body": json.dumps({"tags": ['RKI', 'Covid-19']})
        }

        response = GetTags.post_tags_for_item(event, context)
        body = response['body']
        tags_added = json.loads(body)['added new tags']
        tags_counter_increased = json.loads(body)['increased tag counter']
        assert tags_added == ['Covid-19']
        assert len(tags_counter_increased) == 1
        assert 'RKI' in tags_counter_increased
        assert 'Covid-19' not in tags_counter_increased

        response = GetTags.get_tags_for_item(event, context)
        body = response['body']
        tags = json.loads(body)['Tags']
        assert tags == ['RKI', 'Covid', 'Corona Transition', 'Covid-19']
        assert tags != ['Covid', 'Corona Transition', 'Covid-19', 'RKI']

        # Check counts: RKI posted twice, all other once
        assert session.query(ItemTag).join(Tag).filter(
            Tag.tag == 'RKI').count() == 2
        assert session.query(ItemTag).join(Tag).filter(
            Tag.tag == 'Covid').count() == 1
        assert session.query(ItemTag).join(Tag).filter(
            Tag.tag == 'Corona Transition').count() == 1
        assert session.query(ItemTag).join(Tag).filter(
            Tag.tag == 'Covid-19').count() == 1