def get_venue_from_response(response: dict, event: Event) -> Event: """ parse json response and add venue to given event Arguments: response {dict} -- meetup api response in a dict event {Event} -- The Event where the venue will be added Returns: Event -- Given Event with added venue """ if "address_1" in response: event.venue_address_1 = response["address_1"] if "address_2" in response: event.venue_address_2 = response["address_2"] if "address_3" in response: event.venue_address_3 = response["address_3"] if "city" in response: event.venue_city = response["city"] if "country" in response: event.venue_country = response["country"] if "lat" in response and "lon" in response: event.venue_location = {"lat": response["lat"], "lon": response["lon"]} if "localized_country_name" in response: event.venue_localized_country_name = response["localized_country_name"] if "name" in response: event.venue_name = response["name"] if "phone" in response: event.venue_phone = response["phone"] if "zip_code" in response: event.venue_zip_code = response["zip_code"] return event
def get_event_host_from_response(response: dict, event: Event) -> Event: """ parse json response and add event hosts to given event Arguments: response {dict} -- meetup api response in a dict event {Event} -- The Event where the event_host will be added Returns: Event -- Given Event with added event_host """ # add optional fields if "host_count" in response: event.event_host_host_count = response["host_count"] if "id" in response: event.event_host_id = response["id"] if "intro" in response: event.event_host_intro = response["intro"] if "join_date" in response: event.event_host_join_date = datetime.fromtimestamp( response["join_date"] / 1000) if "name" in response: event.event_host_name = response["name"] return event
def test_group_last_event_time(group_1: Group): events: dict = { "first": { "meetup_id": "first", "time": datetime(year=2000, month=1, day=1), }, "middle": { "meetup_id": "middle", "time": datetime(year=2010, month=1, day=1), }, "last": { "meetup_id": "last", "time": datetime(year=2020, month=1, day=1), }, } # init group models group_1.save() # test with no event assert group_1.last_event_time is None # test with one event event_first: Event = Event( meetup_id=events["first"]["meetup_id"], time=events["first"]["time"], name="", link="", date_in_series_pattern=False, ) group_1.add_event(event_first) group_1.save() assert group_1.last_event_time == events["first"]["time"] # test with 2 events event_last: Event = Event( meetup_id=events["last"]["meetup_id"], time=events["last"]["time"], name="", link="", date_in_series_pattern=False, ) group_1.add_event(event_last) group_1.save() assert group_1.last_event_time == events["last"]["time"] # test with 3 events event_middle: Event = Event( meetup_id=events["middle"]["meetup_id"], time=events["middle"]["time"], name="", link="", date_in_series_pattern=False, ) group_1.add_event(event_middle) group_1.save() assert group_1.last_event_time == events["last"]["time"]
def test_group_add_events(group_1: Group): # init group model group_1.save() # create 10 events events: List[Event] = [] for i in range(0, 10): event: Event = Event( meetup_id=str(i), created=datetime.now(), time=datetime.now(), name="", link="", date_in_series_pattern=False, ) events.append(event) # add events to group group_1.add_events(events) group_1.save() sleep(1) # check if events was added group_2: Group = Group.get_group(urlname=group_1.urlname) group_events: List[Event] = group_2.events assert len(group_events) == 10 for event in group_events: assert isinstance(group_events[i], Event) assert group_events[i].meetup_id == str(i)
def test_add_event_venue_to_list(): event: Event = Event( meetup_id=0, created=datetime.now(), time=datetime.now(), name="", link="", date_in_series_pattern=False, ) # check with event without venue venue_list_1: List[dict] = Group.add_event_venue_to_list(venue_list=[], event=event) assert len(venue_list_1) == 0 # add venue to event event.venue_name = "Café" event.venue_location = [52.520008, 13.404954] # check with any previous event venue_list_2: List[dict] = Group.add_event_venue_to_list( venue_list=venue_list_1, event=event) assert len(venue_list_2) == 1 # add again the same event venue_list_3: List[dict] = Group.add_event_venue_to_list( venue_list=venue_list_2, event=event) assert len(venue_list_3) == 1 # add a different event event.venue_location = [51.050407, 13.737262] venue_list_4: List[dict] = Group.add_event_venue_to_list( venue_list=venue_list_3, event=event) assert len(venue_list_4) == 2
def test_group_event_exists(group_1: Group, group_2: Group): # init group models group_1.save() group_2.save() # init event search_event: Event = Event( meetup_id=0, created=datetime.now(), time=datetime.now(), name="", link="", date_in_series_pattern=False, ) # test when event does not exists assert group_1.event_exists( event_meetup_id=search_event.meetup_id) is False # test with existing event group_1.add_event(search_event) group_1.save() assert group_1.event_exists(event_meetup_id=search_event.meetup_id) is True # test with saved event in wrong group assert group_2.event_exists( event_meetup_id=search_event.meetup_id) is False
def test_to_json_dict(group_1: Group): # add datetime element group_1.created = datetime.now() # test with no event assert isinstance(group_1.to_json_dict(load_events=True), dict) # check if an empty venue array was added assert len(group_1.to_json_dict(load_events=True)["venues"]) == 0 # check if venue_location_average is not set assert "venue_location_average" not in group_1.to_json_dict( load_events=True) # add event to group event: Event = Event( meetup_id=0, created=datetime.now(), time=datetime.now(), name="", link="", date_in_series_pattern=False, venue_name="Café", venue_location={ "lat": 52.520008, "lon": 13.404954 }, ) group_1.add_event(event=event) # test with event assert isinstance(group_1.to_json_dict(load_events=True), dict) # check if the venue array was added with loaded groups assert len(group_1.to_json_dict(load_events=True)["venues"]) == 1 # check if events are in dict assert len(group_1.to_json_dict(load_events=True)["events"]) == 1 # check if events not included in dict assert len(group_1.to_json_dict(load_events=False)["events"]) == 0 # check if the venue array was added without loaded groups assert len(group_1.to_json_dict(load_events=True)["venues"]) == 1 # check if venue_location_average is set assert (group_1.to_json_dict(load_events=True)["venue_location_average"] ["lat"] == event.venue_location["lat"]) assert (group_1.to_json_dict(load_events=True)["venue_location_average"] ["lon"] == event.venue_location["lon"])
def create_events_to_group( search_query: str, valid_events: bool, group: Group, amount: int = 1, venue: bool = False, ) -> List[Event]: """ Create random test events and save them to a group Arguments: search_query {str} -- use query param for the search request valid_events {bool} -- should the groups searchable by the the query term group {Group} -- group to at the events Keyword Arguments: amount {int} -- how many events should be created (default: {1}) venue {bool} -- if venue should be added to eventa (default: {False}) Returns: List[Event] -- created & saved events """ created_events: List[Event] = [] for i in range(0, amount): event_name: str = random_string(search_query=search_query, valid=valid_events) event: Event = Event( meetup_id=event_name, time=datetime.now(), name=event_name, link="http://none", date_in_series_pattern=False, ) if venue: event.venue_name = event_name event.venue_location = {"lat": i + 1, "lon": i + 1} created_events.append(event) group.add_events(events=created_events) group.save() sleep(1) return created_events
def test_group_add_event(group_1: Group): # init group model group_1.save() # add 10 events & check if there was added for i in range(0, 10): event: Event = Event( meetup_id=str(i), created=datetime.now(), time=datetime.now(), name="", link="", date_in_series_pattern=False, ) group_1.add_event(event=event) group_1.save() sleep(1) group_2: Group = Group.get_group(urlname=group_1.urlname) group_events: List[Event] = group_2.events assert len(group_events) == i + 1 assert isinstance(group_events[i], Event) assert group_events[i].meetup_id == str(i)
def test_search_geo_distance(client: FlaskClient, group_1: Group): """ Test geo_distance filter Arguments: client {FlaskClient} -- client to access flask web ressource """ # init group with no location group_1.save() sleep(1) # test no location in groups, search for potsdam response_1: JSONResponse = client.put( url_for("meetupsearchapi"), data=generate_search_dict(query="*", geo_distance="100km", geo_lat=52.396149, geo_lon=13.058540), ) assert response_1.status_code == 200 assert len(response_1.json["results"]) == 0 assert response_1.json["hits"] == 0 assert isinstance(response_1, JSONResponse) # add berlin as location for group event_berlin: Event = Event( meetup_id="berlin", time=datetime.now(), name="berlin", link="http://none", date_in_series_pattern=False, venue_name="Café", venue_location={ "lat": 52.520008, "lon": 13.404954 }, ) group_1.add_event(event=event_berlin) group_1.save() sleep(1) # check if potsdam is in 100km from berlin center response_2: JSONResponse = client.put( url_for("meetupsearchapi"), data=generate_search_dict(query="*", geo_distance="100km", geo_lat=52.396149, geo_lon=13.058540), ) assert response_2.status_code == 200 assert len(response_2.json["results"]) == 1 assert response_2.json["hits"] == 1 assert isinstance(response_2, JSONResponse) # check if potsdam is in 1km from berlin center response_3: JSONResponse = client.put( url_for("meetupsearchapi"), data=generate_search_dict(query="*", geo_distance="0.5km", geo_lat=52.396149, geo_lon=13.058540), ) assert response_3.status_code == 200 assert len(response_3.json["results"]) == 0 assert response_3.json["hits"] == 0 assert isinstance(response_3, JSONResponse)
def get_event_from_response(response: dict, group: Group) -> Event: """ parse json response and return an Event Arguments: response {dict} -- meetup api response in a dict group {Group} -- Group where the event should be added later Raises: EventAlreadyExists: Raise when a event with the same meetup_id already in the group exists Returns: Event -- New Event wich can be added to the group """ # if event already exists, raise try: if group.event_exists(event_meetup_id=response["id"]): raise EventAlreadyExists("Event {} already exists on {}!".format( response["id"], group.name)) except KeyError: raise InvalidResponse("No Event in Response!") date_in_series_pattern: bool = False if "date_in_series_pattern" in response: date_in_series_pattern = response["date_in_series_pattern"] event: Event = Event( meetup_id=response["id"], time=datetime.fromtimestamp(response["time"] / 1000), name=response["name"], link=response["link"], date_in_series_pattern=date_in_series_pattern, ) # add optional fields if "attendance_count" in response: event.attendance_count = response["attendance_count"] if "attendance_sample" in response: event.attendance_sample = response["attendance_sample"] if "attendee_sample" in response: event.attendee_sample = response["attendee_sample"] if "created" in response: event.created = datetime.fromtimestamp(response["created"] / 1000) if "description" in response: event.description = response["description"] if "duration" in response: event.duration = response["duration"] if "fee" in response: event.fee_accepts = response["fee"]["accepts"] event.fee_amount = response["fee"]["amount"] event.fee_currency = response["fee"]["currency"] event.fee_description = response["fee"]["description"] event.fee_label = response["fee"]["label"] if "how_to_find_us" in response: event.how_to_find_us = response["how_to_find_us"] if "status" in response: event.status = response["status"] if "utc_offset" in response: event.utc_offset = response["utc_offset"] / 1000 if "updated" in response: event.updated = datetime.fromtimestamp(response["updated"] / 1000) if "venue" in response: event = get_venue_from_response(response=response["venue"], event=event) if "venue_visibility" in response: event.venue_visibility = response["venue_visibility"] if "visibility" in response: event.visibility = response["visibility"] return event