def test_append_delete_queries_from_event(self, monkeypatch, db_connection,
                                              db_events):
        event_queries = EventQueries(db_connection, [])
        event = db_events[0]
        event_queries.append_delete_queries_from_event(event)

        real = event_queries.get_sql_queries()
        test = [
            "DELETE FROM EventRatingType WHERE event_id = '3mbuq0b4pciqef8qghu4ouc715';"
        ]

        assert real == test
    def test_append_single_delete_query_from_event(self, db_connection,
                                                   db_events, google_events):
        db_event = db_events[0]

        event_queries = EventQueries(db_connection, [])

        event_queries.append_single_delete_query_from_event(db_event)

        real = event_queries.get_sql_queries()
        test = [
            "DELETE FROM EventRatingType WHERE id = '3mbuq0b4pciqef8qghu4ouc715';"
        ]

        assert real == test
示例#3
0
            def execute_sql_queries_stub(s: insert_event_util.EventQueries):
                real_list_of_queries = s.get_sql_queries()
                test_list_of_queries = [
                    "DELETE FROM EventRatingType WHERE event_id = '3mbuq0b4pciqef8qghu4ouc715';"
                ]

                for (real, test) in zip(real_list_of_queries,
                                        test_list_of_queries):
                    assert real == test
    def test_is_event_button_still_in_google_event(self, db_events,
                                                   google_events):
        db_event = db_events[0]
        db_event_with_button_name = deepcopy(db_event)
        db_event_with_button_name['event_button_name'] = 'alfa'

        google_event = google_events["3mbuq0b4pciqef8qghu4ouc715"]
        google_event['event_button_names'] = ['alfa']

        real = EventQueries.is_event_button_assigned_google_event(
            db_event_with_button_name, google_event)
        test = True
        assert real == test

        db_event_with_button_name['event_button_name'] = 'beta'
        real = EventQueries.is_event_button_assigned_google_event(
            db_event, google_event)
        test = False
        assert real == test
    def test_append_insert_or_update_queries_from_event(
            self, monkeypatch, db_connection, google_events):
        event_queries = EventQueries(db_connection, [])
        event = google_events["3mbuq0b4pciqef8qghu4ouc715"]
        event['event_code'] = '12345'
        event['event_id'] = 'fake_event_id'
        event_queries.append_insert_or_update_queries_from_event(event)

        real = event_queries.get_sql_queries()
        test = [
            'INSERT IGNORE INTO EventRatingType (id, event_button_id, event_button_name, calendar_id, '
            'timestamp_from, timestamp_to, event_summary, creator, event_code, event_id) VALUES ("fake_event_id", '
            '0, "NULL", "*****@*****.**", 1575381600, 1575396000, '
            '"Rust kodekveld", "*****@*****.**", "12345", "fake_event_id") '
            'ON DUPLICATE KEY UPDATE event_button_id=0, event_button_name="NULL", '
            'calendar_id="*****@*****.**", timestamp_from=1575381600, '
            'timestamp_to=1575396000, event_summary="Rust kodekveld", creator="*****@*****.**", '
            'event_code="12345", event_id="fake_event_id";'
        ]

        assert real == test
示例#6
0
            def execute_sql_queries_stub(s: insert_event_util.EventQueries):
                real_list_of_queries = s.get_sql_queries()
                test_list_of_queries = [
                    'INSERT IGNORE INTO EventRatingType (id, event_button_id, event_button_name, '
                    'calendar_id, timestamp_from, timestamp_to, event_summary, creator, event_id, event_code) '
                    'VALUES ("3mbuq0b4pciqef8qghu4ouc715", 0, "NULL", '
                    '"*****@*****.**", 1575381600, 1575396000, '
                    '"Rust kodekveld", "*****@*****.**", "3mbuq0b4pciqef8qghu4ouc715", "01096") '
                    'ON DUPLICATE KEY UPDATE event_button_id=0, event_button_name="NULL", '
                    'calendar_id="*****@*****.**", '
                    'timestamp_from=1575381600, timestamp_to=1575396000, event_summary="Rust kodekveld", '
                    'creator="*****@*****.**", event_id="3mbuq0b4pciqef8qghu4ouc715", event_code="01096";'
                ]

                for (real, test) in zip(real_list_of_queries,
                                        test_list_of_queries):
                    assert real == test
示例#7
0
            def execute_sql_queries_stub(s: insert_event_util.EventQueries):
                real_list_of_queries = s.get_sql_queries()
                test_list_of_queries = [(
                    'INSERT IGNORE INTO EventRatingType (id, event_button_id, event_button_name, '
                    'calendar_id, timestamp_from, timestamp_to, event_summary, creator, event_id, event_code) '
                    'VALUES ("3mbuq0b4pciqef8qghu4ouc715", 0, "NULL", '
                    '"*****@*****.**", '
                    '1575381600, 1575396000, "Rust kodekveld", "*****@*****.**", '
                    '"3mbuq0b4pciqef8qghu4ouc715", "41026") ON DUPLICATE KEY UPDATE event_button_id=0, '
                    'event_button_name="NULL", '
                    'calendar_id="*****@*****.**", '
                    'timestamp_from=1575381600, timestamp_to=1575396000, event_summary="Rust kodekveld", '
                    'creator="*****@*****.**", event_id="3mbuq0b4pciqef8qghu4ouc715", event_code="41026";'
                )]

                for (real, test) in zip(real_list_of_queries,
                                        test_list_of_queries):
                    # Test that they are equal without event code since this is a random code
                    assert self.remove_occurences_of_event_code_from_query(real) == \
                           self.remove_occurences_of_event_code_from_query(test)
 def test_generate_event_code(self, monkeypatch, db_connection):
     event_queries = EventQueries(db_connection, [])
     generated_event_code = event_queries.generate_event_code()
     assert len(generated_event_code) == 5
示例#9
0
def handler(event: Dict, context) -> Dict[str, Union[str, int]]:
    global db_connection
    if db_connection is None:
        db_connection = get_db_connection()

    google_events = get_google_events(event)
    db_events = get_events_from_db_which_are_active_the_next_24_hours(
        db_connection)

    queries = EventQueries(db_connection, db_events)

    # Construct DELETE queries for deleted google events from db or single DELETE queries if an event button is removed
    google_event_ids = list(
        google_events.keys()) if len(google_events) > 0 else []
    for db_event in db_events:
        db_event_id = db_event['event_id']
        if db_event_id not in google_event_ids:
            queries.append_delete_queries_from_event(db_event)
        elif db_event_id in google_event_ids and \
                db_event['event_button_name'] != 'NULL' and not \
                EventQueries.is_event_button_assigned_google_event(db_event, google_events[db_event_id]):
            queries.append_single_delete_query_from_event(db_event)

    # Construct INSERT and UPDATE queries for added or updated google events in db
    db_event_ids = list([db_event['event_id'] for db_event in db_events])
    for (google_event_id, google_event) in google_events.items():
        google_event['event_id'] = str(google_event_id)

        # get event_code from db_events or generate a new event_code
        if google_event_id in db_event_ids:
            db_event_code = [
                e['event_code'] for e in db_events
                if e['event_id'] == google_event_id
            ].pop(0)
            google_event['event_code'] = db_event_code
        else:
            google_event['event_code'] = queries.generate_event_code()

        queries.append_insert_or_update_queries_from_event(google_event)

    # Execute sql queries so that db is in sync with google calendar
    queries.execute_sql_queries()
    for q in queries.get_sql_queries():
        print(q)
    db_connection.commit()

    return {'statusCode': 200, 'body': 'Done'}