def test_create_track_slug_trailing_spaces(): assert create_track_slug(" some track title ", 0, 0) == "some-track-title" assert create_track_slug("some ( original mix )", 0, 0) == "some-original-mix" assert create_track_slug("( some track title )", 0, 0) == "some-track-title"
def test_create_track_slug_long_characters(): title = "#$*$(Strip\x00\x00\x00 !!@#*)&$(&#$%*Weird + Characters" assert create_track_slug(title, 1, 0) == "strip-weird-characters" assert create_track_slug(title, 1, 3) == "strip-weird-characters-3"
def test_create_track_slug_unicode(): title = "ñóนมนุษşoföre" assert create_track_slug(title, 15633, 0) == "ñóนมนุษşoföre" assert create_track_slug(title, 15633, 3) == "ñóนมนุษşoföre-3"
def test_create_track_slug_periods_and_punctuation(): title = "Some: track; that has, like, punctuation!? everywhere." assert (create_track_slug( title, 0, 0) == "some-track-that-has-like-punctuation-everywhere")
def test_create_track_slug_chinese(): title = "听妈妈的话" assert create_track_slug(title, 15633, 0) == "听妈妈的话" assert create_track_slug(title, 15633, 3) == "听妈妈的话-3"
def test_create_track_slug_normal_title(): title = "Karma Police" assert create_track_slug(title, 1, 0) == "karma-police" assert create_track_slug(title, 1, 3) == "karma-police-3"
def test_create_track_slug_good_and_bad_characters(): title = "f???" assert create_track_slug(title, 15633, 0) == "f" assert create_track_slug(title, 15633, 3) == "f-3"
def test_create_track_slug_only_bad_characters(): title = "???" assert create_track_slug(title, 15633, 0) == "LjEvV" assert create_track_slug(title, 15633, 3) == "LjEvV-3"
def update_track_routes_table(session, track_record, track_metadata, pending_track_routes): """Creates the route for the given track""" # Check if the title is staying the same, and if so, return early if track_record.title == track_metadata["title"]: return # Get the title slug, and set the new slug to that # (will check for conflicts later) new_track_slug_title = helpers.create_track_slug(track_metadata["title"], track_record.track_id) new_track_slug = new_track_slug_title # Find the current route for the track # Check the pending track route updates first prev_track_route_record = next( (route for route in pending_track_routes if route.is_current and route.track_id == track_record.track_id), None, ) # Then query the DB if necessary if prev_track_route_record is None: prev_track_route_record = ( session.query(TrackRoute).filter( TrackRoute.track_id == track_record.track_id, TrackRoute.is_current == True, ) # noqa: E712 .one_or_none()) if prev_track_route_record is not None: if prev_track_route_record.title_slug == new_track_slug_title: # If the title slug hasn't changed, we have no work to do return # The new route will be current prev_track_route_record.is_current = False # Check for collisions by slug titles, and get the max collision_id max_collision_id: Optional[int] = None # Check pending updates first for route in pending_track_routes: if (route.title_slug == new_track_slug_title and route.owner_id == track_record.owner_id): max_collision_id = (route.collision_id if max_collision_id is None else max(max_collision_id, route.collision_id)) # Check DB if necessary if max_collision_id is None: max_collision_id = (session.query( functions.max(TrackRoute.collision_id)).filter( TrackRoute.title_slug == new_track_slug_title, TrackRoute.owner_id == track_record.owner_id, ).one_or_none())[0] existing_track_route: Optional[TrackRoute] = None # If the new track_slug ends in a digit, there's a possibility it collides # with an existing route when the collision_id is appended to its title_slug if new_track_slug[-1].isdigit(): existing_track_route = next( (route for route in pending_track_routes if route.slug == new_track_slug and route.owner_id == track_record.owner_id), None, ) if existing_track_route is None: existing_track_route = (session.query(TrackRoute).filter( TrackRoute.slug == new_track_slug, TrackRoute.owner_id == track_record.owner_id, ).one_or_none()) new_collision_id = 0 has_collisions = existing_track_route is not None if max_collision_id is not None: has_collisions = True new_collision_id = max_collision_id while has_collisions: # If there is an existing track by the user with that slug, # then we need to append the collision number to the slug new_collision_id += 1 new_track_slug = helpers.create_track_slug(track_metadata["title"], track_record.track_id, new_collision_id) # Check for new collisions after making the new slug # In rare cases the user may have track names that end in numbers that # conflict with this track name when the collision id is appended, # for example they could be trying to create a route that conflicts # with the old routing (of appending -{track_id}) This is a fail safe # to increment the collision ID until no such collisions are present. # # Example scenario: # - User uploads track titled "Track" (title_slug: 'track') # - User uploads track titled "Track 1" (title_slug: 'track-1') # - User uploads track titled "Track" (title_slug: 'track') # - Try collision_id: 1, slug: 'track-1' and find new collision # - Use collision_id: 2, slug: 'track-2' # - User uploads track titled "Track" (title_slug: 'track') # - Use collision_id: 3, slug: 'track-3' # - User uploads track titled "Track 1" (title_slug: 'track-1') # - Use collision_id: 1, slug: 'track-1-1' # # This may be expensive with many collisions, but should be rare. existing_track_route = next( (route for route in pending_track_routes if route.slug == new_track_slug and route.owner_id == track_record.owner_id), None, ) if existing_track_route is None: existing_track_route = (session.query(TrackRoute).filter( TrackRoute.slug == new_track_slug, TrackRoute.owner_id == track_record.owner_id, ).one_or_none()) has_collisions = existing_track_route is not None # Add the new track route new_track_route = TrackRoute() new_track_route.slug = new_track_slug new_track_route.title_slug = new_track_slug_title new_track_route.collision_id = new_collision_id new_track_route.owner_id = track_record.owner_id new_track_route.track_id = track_record.track_id new_track_route.is_current = True new_track_route.blockhash = track_record.blockhash new_track_route.blocknumber = track_record.blocknumber new_track_route.txhash = track_record.txhash session.add(new_track_route) # Add to pending track routes so we don't add the same route twice pending_track_routes.append(new_track_route)