Skip to content

Commit

Permalink
fix: add_tracks_to_collection ignores tracks that are already in the …
Browse files Browse the repository at this point in the history
…collection
  • Loading branch information
faradox committed Mar 5, 2024
1 parent 93f4460 commit 7aacdca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/nendo/library/sqlalchemy_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,23 @@ def add_tracks_to_collection(
"Tracks do not exist: ",
missing_ids,
)

# only add relationships that do not already exist
rc_rel_db = model.TrackCollectionRelationshipDB
existing_relationships = (
session.query(rc_rel_db)
.filter(
rc_rel_db.source_id.in_(track_ids),
rc_rel_db.target_id == collection_id,
)
.all()
)
existing_relationship_track_ids = [
rel.source_id for rel in existing_relationships
]
track_ids = [
tid for tid in track_ids if tid not in existing_relationship_track_ids
]

# append all tracks to the end of the collection
last_postition = (
Expand Down
9 changes: 7 additions & 2 deletions tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,15 @@ def test_add_tracks_to_collection(self):
track_ids=[test_track_1.id, test_track_2.id],
collection_id=test_collection.id,
)
retrieved_test_collection = nd.library.get_collection(
test_collection.refresh()
self.assertEqual(len(test_collection), 2)
nd.library.add_tracks_to_collection(
track_ids=[test_track_1.id, test_track_2.id],
collection_id=test_collection.id,
)
self.assertEqual(len(retrieved_test_collection), 2)
test_collection.refresh()
self.assertEqual(len(test_collection), 2)


def test_find_collections_by_empty_value(self):
"""Test finding collections by empty search value."""
Expand Down

0 comments on commit 7aacdca

Please sign in to comment.