def test_beatmapset(): beatmapset = Beatmapset(41823, beatmapset_json=mock_old_beatmap.JSON) assert beatmapset.id == 41823 assert beatmapset.artist == "The Quick Brown Fox" assert beatmapset.title == "The Big Black" assert beatmapset.creator.id == 19048 assert beatmapset.creator.name == "Blue Dragon" assert beatmapset.modes == ["osu", "taiko"] assert beatmapset.genre == "Electronic" assert beatmapset.language == "English" assert beatmapset.tags == ["onosakihito", "speedcore", "renard", "lapfox"] assert beatmapset.mode_str() == "[osu][taiko]" assert str( beatmapset ) == "The Quick Brown Fox - The Big Black (mapped by Blue Dragon) [osu][taiko]" assert beatmapset.beatmaps[0].id == 131891 assert beatmapset.beatmaps[0].mode == "osu" assert beatmapset.beatmaps[0].bpm == 360.3 assert beatmapset.beatmaps[0].submit_date.month == 12 assert beatmapset.beatmaps[0].submit_date.day == 24 assert beatmapset.beatmaps[0].version == "WHO'S AFRAID OF THE BIG BLACK" assert beatmapset.beatmaps[1].mode == "taiko" assert beatmapset.beatmaps[1].version == "Ono's Taiko Oni" assert beatmapset.beatmaps[1].max_combo == None assert str( beatmapset.beatmaps[1] ) == "The Quick Brown Fox - The Big Black (mapped by Blue Dragon) [taiko] \"Ono's Taiko Oni\""
def retrieve_beatmapsets( self, where: str, where_values: tuple = None, group_by: str = None, order_by: str = None, limit: int = None) -> Generator[Beatmapset, None, None]: """Returns a generator of all beatmapsets from the database matching the given WHERE clause.""" fetched_rows = self.retrieve_table_data( table="beatmapsets", where=where, where_values=where_values, selection="id, title, artist, creator_id, genre, language, tags", group_by=group_by, order_by=order_by, limit=limit) for row in (fetched_rows or []): _id = row[0] title = row[1] artist = row[2] creator = self.retrieve_user("id=%s", (row[3], )) modes = self.retrieve_beatmapset_modes(_id) genre = row[4] language = row[5] tags = row[6].split(" ") if row[6] else None beatmaps = list( self.retrieve_beatmaps("beatmapset_id=%s", (_id, ), allow_api=False)) beatmapset = Beatmapset(_id, artist, title, creator, modes, genre, language, tags, beatmaps, allow_api=False) if beatmapset.is_incomplete(): # The retrieved beatmapset is incomplete, and should be updated. api_beatmapset = Beatmapset(_id, allow_api=True) self.insert_beatmapset(api_beatmapset) for beatmap in beatmaps: # Ensure any deleted beatmap gets cleared out before we repopulate it. self.delete_beatmap(beatmap) for api_beatmap in api_beatmapset.beatmaps: self.insert_beatmap(api_beatmap) beatmapset = api_beatmapset yield beatmapset
def test_beatmapset(): beatmapset = Beatmapset(41823, beatmapset_json=mock_old_beatmap.JSON) assert beatmapset.id == 41823 assert beatmapset.artist == "The Quick Brown Fox" assert beatmapset.title == "The Big Black" assert beatmapset.creator.id == 19048 assert beatmapset.creator.name == "Blue Dragon" assert beatmapset.modes == ["osu", "taiko"] assert beatmapset.genre == "Electronic" assert beatmapset.language == "English" assert beatmapset.mode_str() == "[osu][taiko]" assert str(beatmapset) == "The Quick Brown Fox - The Big Black (mapped by Blue Dragon) [osu][taiko]"
def test_insert_retrieve_beatmapset_modes(test_database): modes = ["osu", "taiko"] beatmapset = Beatmapset(1, modes=modes, allow_api=False) test_database.insert_beatmapset_modes(beatmapset) retrieved_modes = test_database.retrieve_beatmapset_modes(1) assert retrieved_modes == modes
async def test_insert_retrieve_event_digit_properties(test_database): user = User(1, "497") beatmapset = Beatmapset(3, artist="5", title="2", creator=user, allow_api=False) discussion = Discussion(2, beatmapset, user, content="8", tab="tab", difficulty="diff") event = Event(_type="test", time=datetime.utcnow(), user=user, beatmapset=beatmapset, discussion=discussion, content="4") test_database.insert_event(event) retrieved_event = await test_database.retrieve_event( where="type=%s", where_values=("test", )) # Ensures the database field retrieval retains the `str` type, rather than reinterpreting as `int`. assert retrieved_event.content == "4" assert retrieved_event.user.name == "497" assert retrieved_event.beatmapset.artist == "5" assert retrieved_event.beatmapset.title == "2" assert retrieved_event.discussion.content == "8"
def test_update_beatmapset_status_nom_qual_dq(test_database): creator = User(3, name="test") user1 = User(1, name="someone") user2 = User(2, name="sometwo") beatmapset = Beatmapset(1, creator=creator, allow_api=False) event1 = Event("nominate", from_string("2020-01-01 00:00:00"), beatmapset, user=user1) event2 = Event("qualify", from_string("2020-01-01 00:01:00"), beatmapset, user=user2) event3 = Event("disqualify", from_string("2020-01-02 00:00:00"), beatmapset, user=user1) test_database.insert_event(event1) test_database.insert_event(event2) test_database.insert_event(event3) retrieved_status = test_database.retrieve_beatmapset_status( "beatmapset_id=%s ORDER BY time DESC", (beatmapset.id, )) assert retrieved_status.status == "pending" assert retrieved_status.nominators == [] retrieved_status = test_database.retrieve_beatmapset_status( "beatmapset_id=%s AND time=%s", (beatmapset.id, from_string("2020-01-01 00:01:00"))) assert retrieved_status.status == "qualified" assert retrieved_status.nominators == [user1, user2]
async def test_insert_retrieve_multiple_events(test_database): time = datetime.utcnow() user = User(1, name="test") beatmapset = Beatmapset(1, creator=user, allow_api=False) discussion = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") event1 = Event(_type="test", time=time, beatmapset=beatmapset, discussion=discussion, user=user) event2 = Event(_type="123", time=time, beatmapset=beatmapset, discussion=discussion, user=user) test_database.insert_event(event1) test_database.insert_event(event2) retrieved_events = test_database.retrieve_events( where="beatmapset_id=%s", where_values=(beatmapset.id, )) assert await anext(retrieved_events, None) == event1 assert await anext(retrieved_events, None) == event2
async def test_insert_retrieve_event(test_database): time = datetime.utcnow() user = User(1, name="test") beatmapset = Beatmapset(1, artist="123", title="456", creator=user, modes=["osu", "taiko"], genre="genre", language="language") discussion = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") event = Event(_type="test", time=time, beatmapset=beatmapset, discussion=discussion, user=user) test_database.insert_event(event) retrieved_event = await test_database.retrieve_event("type=%s", ("test", )) assert retrieved_event.type == event.type assert retrieved_event.time == event.time assert retrieved_event.beatmapset == event.beatmapset assert retrieved_event.discussion == event.discussion assert retrieved_event.user == event.user assert retrieved_event.content == event.content assert retrieved_event == event
async def test_insert_retrieve_multiple_events(test_database): time = datetime.utcnow() user = User(1, name="test") beatmapset = Beatmapset(1, artist="123", title="456", creator=user, modes=["osu", "taiko"], genre="genre", language="language") discussion = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") event1 = Event(_type="test", time=time, beatmapset=beatmapset, discussion=discussion, user=user) event2 = Event(_type="123", time=time, beatmapset=beatmapset, discussion=discussion, user=user) test_database.insert_event(event1) test_database.insert_event(event2) retrieved_events = test_database.retrieve_events( where="beatmapset_id=%s", where_values=(beatmapset.id, )) assert await anext(retrieved_events, None) == event1 assert await anext(retrieved_events, None) == event2
def test_insert_retrieve_multiple_discussions(test_database): user = User(1, name="test") beatmapset = Beatmapset(1, artist="123", title="456", creator=user, modes=["osu", "taiko"], genre="genre", language="language") discussion1 = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") discussion2 = Discussion(2, beatmapset=beatmapset, user=user, content="real testing", tab="tab", difficulty="diff") test_database.insert_discussion(discussion1) test_database.insert_discussion(discussion2) retrieved_discussions = test_database.retrieve_discussions( where="beatmapset_id=%s", where_values=(beatmapset.id, )) assert next(retrieved_discussions, None) == discussion1 assert next(retrieved_discussions, None) == discussion2
async def test_insert_retrieve_event(test_database): time = datetime.utcnow() user = User(1, allow_api=False) beatmapset = Beatmapset(1, creator=user, allow_api=False) discussion = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") event = Event(_type="test", time=time, beatmapset=beatmapset, discussion=discussion, user=user) test_database.insert_event(event) retrieved_event = await test_database.retrieve_event("type=%s", ("test", )) assert retrieved_event.type == event.type assert retrieved_event.time == event.time assert retrieved_event.beatmapset == event.beatmapset assert retrieved_event.discussion == event.discussion assert retrieved_event.user == event.user assert retrieved_event.content == event.content assert retrieved_event == event
def test_insert_retrieve_discussion(test_database): user = User(1, name="test") beatmapset = Beatmapset(1, artist="123", title="456", creator=user, modes=["osu", "taiko"], genre="genre", language="language") discussion = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") test_database.insert_discussion(discussion) retrieved_discussion = test_database.retrieve_discussion( where="id=%s", where_values=(1, )) assert retrieved_discussion.id == discussion.id assert retrieved_discussion.beatmapset == discussion.beatmapset assert retrieved_discussion.user == discussion.user assert retrieved_discussion.content == discussion.content assert retrieved_discussion.tab == discussion.tab assert retrieved_discussion.difficulty == discussion.difficulty assert retrieved_discussion == discussion
def test_old_discussion(): beatmapset = Beatmapset(41823, beatmapset_json=mock_old_beatmap.JSON) discussion = Discussion(1234956, beatmapset) # No such discussion exists, but this should still work. assert discussion.id == 1234956 assert discussion.beatmapset == beatmapset
def test_discussion_int_content(): beatmapset = Beatmapset(1001546, beatmapset_json=mock_beatmap.JSON) discussion = Discussion(1234956, beatmapset, content=4) assert discussion.id == 1234956 assert discussion.content == "4" assert discussion.beatmapset == beatmapset
def test_insert_incomplete_discussion(test_database): user = User(1, name="test") beatmapset = Beatmapset(1, creator=user, allow_api=False) discussion = Discussion(1, beatmapset=beatmapset) with pytest.raises(ValueError) as error: test_database.insert_discussion(discussion) assert "missing from discussion" in str(error.value)
def parse_event_json(self, event_json: object, user_jsons: object = None) -> Event: """Returns a BeatmapsetEvent reflecting the given event json object. Ignores any event with an incomplete context (e.g. deleted beatmaps). Requests user names from the api unless supplied with the corresponding user json from the discussion page.""" if not event_json: # Seems to occur when the respective beatmapset has been deleted. However, it's there when # viewing the page source manually for some reason, regardless of login status. log_err( "WARNING | An event is missing; the beatmapset was probably deleted." ) return None try: # Scrape object data _type = event_json["message_type"] time = timestamp.from_string(event_json["created_at"]) beatmapset_id = event_json["beatmapset_id"] discussion_id = event_json["starting_post"][ "beatmap_discussion_id"] user_id = event_json["user_id"] # The user name is either provided by a user json from the discussion page, or queried through the api. user_json = self.__lookup_user_json(user_id, user_jsons) user_name = user_json["username"] if user_json else None content = event_json["starting_post"]["message"] difficulty = event_json["beatmap"][ "version"] if "beatmap" in event_json and "version" in event_json[ "beatmap"] else None tab = None if event_json["timestamp"] is not None: tab = "timeline" elif difficulty: tab = "general" else: tab = "generalAll" # Reconstruct objects beatmapset = Beatmapset(beatmapset_id) user = User(user_id, user_name) if user_id is not None else None # TODO: This portion is missing handling for replies, see the other method. # Still unclear which message_type replies use; will need to find out if/when replies get json formats. discussion = Discussion( discussion_id, beatmapset, user, content, tab, difficulty) if discussion_id is not None else None except DeletedContextError as err: log_err(err) else: return Event(_type=_type, time=time, beatmapset=beatmapset, discussion=discussion, user=user, content=content) return None
def test_beatmapset_without_api(): beatmapset = Beatmapset(4, allow_api=False) assert beatmapset.artist is not None assert beatmapset.title is not None assert beatmapset.modes is not None assert beatmapset.language is not None assert beatmapset.genre is not None assert beatmapset.tags is not None
def test_insert_retrieve_discussion_and_replies(test_database): time = datetime.utcnow() author = User(1, name="one") replier = User(2, name="two") beatmapset = Beatmapset(1, artist="123", title="456", creator=replier, modes=["osu", "taiko"], genre="genre", language="language") discussion = Discussion(1, beatmapset=beatmapset, user=author, content="ping", tab="tab", difficulty="diff") problem = Event(_type="problem", time=time, beatmapset=beatmapset, discussion=discussion, user=author, content="ping") reply1 = Event(_type="reply", time=time, beatmapset=beatmapset, discussion=discussion, user=replier, content="pong") reply2 = Event(_type="reply", time=time, beatmapset=beatmapset, discussion=discussion, user=author, content="miss") test_database.insert_event(problem) test_database.insert_event(reply1) test_database.insert_event(reply2) retrieved_problem = test_database.retrieve_event( where="type=%s", where_values=("problem", )) retrieved_reply1 = test_database.retrieve_event( where="type=%s AND user_id=%s", where_values=("reply", replier.id)) retrieved_reply2 = test_database.retrieve_event( where="type=%s AND user_id=%s", where_values=("reply", author.id)) assert retrieved_problem assert retrieved_reply1 assert retrieved_reply2
def test_insert_retrieve_multiple_beatmapsets(test_database): user = User(1, name="test") beatmapset1 = Beatmapset(1, artist="123", title="456", creator=user, modes=["osu", "taiko"], allow_api=False) beatmapset2 = Beatmapset(2, artist="456", title="789", creator=user, modes=["osu"], allow_api=False) test_database.insert_beatmapset(beatmapset1) test_database.insert_beatmapset(beatmapset2) retrieved_beatmapsets = test_database.retrieve_beatmapsets( where="creator_id=%s", where_values=(user.id, )) assert next(retrieved_beatmapsets, None) == beatmapset1 assert next(retrieved_beatmapsets, None) == beatmapset2
def test_insert_retrieve_beatmapset_modes(test_database): modes = ["osu", "taiko"] user = User(1, name="test") beatmapset = Beatmapset(1, artist="123", title="456", creator=user, modes=modes, genre="genre", language="language") test_database.insert_beatmapset_modes(beatmapset) retrieved_modes = test_database.retrieve_beatmapset_modes(1) assert retrieved_modes == modes
def test_insert_incomplete_discussion(test_database): user = User(1, name="test") beatmapset = Beatmapset(1, artist="123", title="456", creator=user, modes=["osu", "taiko"], genre="genre", language="language") discussion = Discussion(1, beatmapset=beatmapset) with pytest.raises(ValueError) as error: test_database.insert_discussion(discussion) assert "missing from discussion" in str(error.value)
def test_insert_retrieve_obv_sev(test_database): user = User(1, name="test") beatmapset = Beatmapset(1, creator=user, allow_api=False) discussion = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") test_database.insert_obv_sev(discussion, obv=2, sev=0) obv, sev = test_database.retrieve_obv_sev(discussion_id=1) assert obv == 2 assert sev == 0
def test_insert_retrieve_beatmapset(test_database): beatmapset = Beatmapset(1, allow_api=False) test_database.insert_beatmapset(beatmapset) retrieved_beatmapset = test_database.retrieve_beatmapset( where="id=%s", where_values=(1, )) assert retrieved_beatmapset.id == beatmapset.id assert retrieved_beatmapset.artist == beatmapset.artist assert retrieved_beatmapset.title == beatmapset.title assert retrieved_beatmapset.creator == beatmapset.creator assert retrieved_beatmapset.modes == beatmapset.modes assert retrieved_beatmapset.genre == beatmapset.genre assert retrieved_beatmapset.language == beatmapset.language assert retrieved_beatmapset.beatmaps == beatmapset.beatmaps assert retrieved_beatmapset == beatmapset
def test_update_beatmapset_status_nom(test_database): creator = User(3, name="test") user = User(1, name="someone") beatmapset = Beatmapset(1, creator=creator, allow_api=False) event = Event("nominate", from_string("2020-01-01 00:00:00"), beatmapset, user=user) test_database.insert_event( event) # Should call `update_beatmapset_status`. retrieved_status = test_database.retrieve_beatmapset_status( "beatmapset_id=%s", (beatmapset.id, )) assert retrieved_status.status == "nominated" assert retrieved_status.nominators == [user]
def test_insert_retrieve_beatmapset_with_beatmaps(test_database): # Major difference between `allow_api=False` and from json is # that latter actually makes the `beatmaps` field populated, and # so we test whether that's inserted and retrieved properly here. beatmapset = Beatmapset(41823, beatmapset_json=mock_old_beatmap.JSON) test_database.insert_beatmapset(beatmapset) retrieved_beatmapset = test_database.retrieve_beatmapset( where="id=%s", where_values=(41823, )) assert retrieved_beatmapset.id == beatmapset.id assert retrieved_beatmapset.artist == beatmapset.artist assert retrieved_beatmapset.title == beatmapset.title assert retrieved_beatmapset.creator == beatmapset.creator assert retrieved_beatmapset.modes == beatmapset.modes assert retrieved_beatmapset.genre == beatmapset.genre assert retrieved_beatmapset.language == beatmapset.language assert retrieved_beatmapset.beatmaps == beatmapset.beatmaps assert retrieved_beatmapset == beatmapset
def parse_event(self, event: Tag) -> Event: """Returns a BeatmapsetEvent reflecting the given event html Tag object. Ignores any event with an incomplete context (e.g. deleted beatmaps).""" try: # Scrape object data _type = self.parse_event_type(event) time = self.parse_event_time(event) link = self.parse_event_link(event) beatmapset_id = self.parse_id_from_beatmapset_link(link) discussion_id = self.parse_id_from_discussion_link(link) user_id = self.parse_event_author_id(event) user_name = self.parse_event_author_name(event) content = self.parse_discussion_message(event) # Reconstruct objects beatmapset = Beatmapset(beatmapset_id) user = User(user_id, user_name) if user_id is not None else None if _type == "reply": # Replies should look up the discussion they are posted on. discussion = Discussion( discussion_id, beatmapset) if discussion_id is not None else None else: tab = self.parse_discussion_tab(event) difficulty = self.parse_discussion_diff(event) discussion = Discussion( discussion_id, beatmapset, user, content, tab, difficulty) if discussion_id is not None else None except DeletedContextError as err: log_err(err) else: return Event(_type=_type, time=time, beatmapset=beatmapset, discussion=discussion, user=user, content=content) return None
async def test_insert_retrieve_event_cached(cached_database): time = datetime.utcnow() user = User(1, name="test") beatmapset = Beatmapset(1, artist="123", title="456", creator=user, modes=["osu", "taiko"], genre="genre", language="language") discussion = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") for i in range(100): event = Event(_type=f"{i}", time=time, beatmapset=beatmapset, discussion=discussion, user=user) cached_database.insert_event(event) start_time = datetime.utcnow() retrieved_events_uncached = cached_database.retrieve_events( where="beatmapset_id=%s", where_values=(beatmapset.id, )) async for event in retrieved_events_uncached: assert event.beatmapset == beatmapset delta_time_uncached = datetime.utcnow() - start_time start_time = datetime.utcnow() retrieved_events_cached = cached_database.retrieve_events( where="beatmapset_id=%s", where_values=(beatmapset.id, )) async for event in retrieved_events_cached: assert event.beatmapset == beatmapset delta_time_cached = datetime.utcnow() - start_time assert await anext(retrieved_events_uncached, None) == await anext(retrieved_events_cached, None) assert delta_time_uncached > delta_time_cached
def test_insert_retrieve_obv_sev_event(test_database): user = User(1, name="test") beatmapset = Beatmapset(1, creator=user, allow_api=False) discussion = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") event = Event(_type=types.SEV, time=from_string("2020-01-01 00:00:00"), beatmapset=beatmapset, discussion=discussion, content="2/0") test_database.insert_obv_sev_event(event) obv, sev = test_database.retrieve_obv_sev(discussion_id=1) assert obv == 2 assert sev == 0
def test_insert_retrieve_beatmapset_status(test_database): creator = User(3, name="test") user1 = User(1, name="someone") user2 = User(2, name="sometwo") beatmapset = Beatmapset(1, creator=creator, allow_api=False) status = BeatmapsetStatus(_id=1, beatmapset=beatmapset, status="qualified", time=from_string("2020-01-01 00:00:00"), nominators=[user1, user2]) test_database.insert_beatmapset_status(status) retrieved_status = test_database.retrieve_beatmapset_status( "beatmapset_id=%s", (beatmapset.id, )) assert retrieved_status.beatmapset == status.beatmapset assert retrieved_status.status == status.status assert retrieved_status.time == status.time assert retrieved_status.nominators == status.nominators assert retrieved_status == status
def test_insert_retrieve_discussion(test_database): user = User(1, name="test") beatmapset = Beatmapset(1, creator=user, allow_api=False) discussion = Discussion(1, beatmapset=beatmapset, user=user, content="testing", tab="tab", difficulty="diff") test_database.insert_discussion(discussion) retrieved_discussion = test_database.retrieve_discussion( where="id=%s", where_values=(1, )) assert retrieved_discussion.id == discussion.id assert retrieved_discussion.beatmapset == discussion.beatmapset assert retrieved_discussion.user == discussion.user assert retrieved_discussion.content == discussion.content assert retrieved_discussion.tab == discussion.tab assert retrieved_discussion.difficulty == discussion.difficulty assert retrieved_discussion == discussion