#922 Insert method in class MetadataList results in an extra None Value in its metadata list#923
#922 Insert method in class MetadataList results in an extra None Value in its metadata list#923Ozy-Viking wants to merge 2 commits into
Conversation
Insert method in class MetadataList results in an extra None Value in its metadata listThe Bug found while writing 1,000 test cases for a Masters course I'm undertaking, let me know if you want all the tests. :) Steps to Reproduce (for bugs)
Expected Behavior
Current Behavior
InitializationThis section is setting up testing that shows the bug. # test_commom.py
from xl.common import MetadataList
def mock_metadata(title, **kwargs):
return {"title": title } | kwargs
class MockSong:
def __init__(self, title, **kwargs) -> None:
self.__dict__ = mock_metadata(title, **kwargs)
@property
def metadata(self):
return self.__dict__
def __eq__(self, other):
if isinstance(other, MockSong):
return self.metadata == other.metadata
else:
return NotImplemented
def __str__(self) -> str:
return f'{self.title} by {self.artist}'
def __repr__(self) -> str:
return f'MockSong("{self.title}")'
def mock_songs_with_metadata(num_songs=10):
song_list = [MockSong(f"Song {x}") for x in range(num_songs)]
meta_list = [x.metadata for x in song_list]
return (song_list, meta_list)
def mock_song():
return MockSong("Insert Song")
NUM_OF_SONGS = 5
song_list, meta_list = mock_songs_with_metadata(NUM_OF_SONGS)
mdl = MetadataList(song_list, meta_list)
def reset_mdl():
song_list, meta_list = mock_songs_with_metadata(NUM_OF_SONGS)
global mdl
mdl = MetadataList(song_list, meta_list) Insert at BeginningWhen the index is 0 ( metadata = [
{'title': 'Insert Song'},
None,
{'title': 'Song 0'},
{'title': 'Song 1'},
{'title': 'Song 2'},
{'title': 'Song 3'},
{'title': 'Song 4'}
]For reference: class MetadataList:
def __setitem__(self, i, value):
self.__list.__setitem__(i, value)
if isinstance(value, MetadataList):
metadata = list(value.metadata)
else:
metadata = [None] * len(value)
self.metadata.__setitem__(i, metadata)
def insert(self, i, item, metadata=None):
if i >= len(self):
i = len(self)
e = len(self) + 1
else:
e = i
self[i:e] = [item]
self.metadata[i:e] = [metadata]ExplanationThe bug is in
Example Code for Begginingreset_mdl()
insert_song = mock_song()
mdl.insert(0, insert_song, insert_song.metadata)
print(f"{len(mdl)} != {len(mdl.metadata)}")
print(mdl)
print(mdl.metadata)Insert in the MiddleWhen the index is 2 ( metadata = [
{'title': 'Song 0'},
{'title': 'Song 1'},
{'title': 'Insert Song'},
None,
{'title': 'Song 2'},
{'title': 'Song 3'},
{'title': 'Song 4'}
]Example Code for Middlereset_mdl()
insert_song = mock_song()
mdl.insert(2, insert_song, insert_song.metadata)
print(f"{len(mdl)} != {len(mdl.metadata)}")
print(mdl)
print(mdl.metadata)Insert at the EndWhen the object is inserted at the end i.e. appended, the list is correct. [
{'title': 'Song 0'},
{'title': 'Song 1'},
{'title': 'Song 2'},
{'title': 'Song 3'},
{'title': 'Song 4'},
{'title': 'Insert Song'}
]reset_mdl()
insert_song = mock_song()
mdl.insert(5, insert_song, insert_song.metadata)
print(f"{len(mdl)} == {len(mdl.metadata)}")
print(mdl)
print(mdl.metadata)Possible SolutionAs the metadata list has increased in size by the Originalclass MetadataList:
def insert(self, i, item, metadata=None):
if i >= len(self):
i = len(self)
e = len(self) + 1
else:
e = i
self[i:e] = [item]
self.metadata[i:e] = [metadata]Proposed changeclass MetadataList:
def insert(self, i, item, metadata=None):
if i >= len(self):
i = len(self)
e = len(self) + 1
else:
e = i
self[i:e] = [item]
self.metadata[i] = metadataEnvironment
|
#922