def test_export_artists_to_csv(tmpdir): """Ensure we can export a list of artists to a .csv file.""" path = os.path.join(tmpdir, "artists.csv") artists = [Artist("Artist1"), Artist("Artist2")] _io.export_artists_to_csv(artists, path) with open(path) as stream: actual = stream.read() assert actual == "Artist1,100,{}\nArtist2,100,{}\n"
def test_import_artists_csv_1_columns(tmpdir): """Ensure we can import csv containing artists with two columns.""" path = os.path.join(tmpdir, "artists.csv") with open(path, "w") as stream: stream.write("Artist1\nArtist2") actual = _io.import_artists_from_csv(path) assert actual == [ Artist("Artist1"), Artist("Artist2"), ]
def context(): """A context instance.""" return Context( artists=[ Artist("artist1"), Artist("artist2"), ], tasks=[ Task("0010", 1), Task("0020", 2), Task("0030", 3), ], )
def add_artist(self): """Add a new artist to the current context.""" names = {artist.name for artist in self.context.artists} name = _get_unique_name(names, "Artist") artist = Artist(name=name, availability=100, tags={}) self.context.artists.append(artist) self.set_dirty()
def test_import_assignments_to_csv(tmpdir): """Ensure we can import a list of assignments to a .csv file.""" path = os.path.join(tmpdir, "artists.csv") with open(path, "w") as stream: stream.write("0010,Artist1\n0020,Artist2\n") artist1 = Artist("Artist1") artist2 = Artist("Artist2") task1 = Task("0010", 1) task2 = Task("0020", 2) context = Context( artists=[artist1, artist2], tasks=[task1, task2], ) actual = _io.import_assignments_from_csv(context, path) assert actual == [Assignment(artist1, task1), Assignment(artist2, task2)]
def _set_artist_name(artist: Artist, value: str) -> bool: """Set an artist name from a string value. :param artist: An artist :param value: The artist new name :return: Was the artist name changed? """ if artist.name == value: return False artist.name = value return True
def test_export_assignments_to_csv(tmpdir): """Ensure we can export a list of assignments to a .csv file.""" path = os.path.join(tmpdir, "artists.csv") artist1 = Artist("Artist1") artist2 = Artist("Artist2") task1 = Task("0010", 1) task2 = Task("0020", 2) context = Context( artists=[artist1, artist2], tasks=[task1, task2], assignments=[Assignment(artist1, task1), Assignment(artist2, task2)], ) _io.export_assignments_to_csv(context.assignments, path) with open(path) as stream: actual = stream.read() assert actual == "0010,Artist1\n0020,Artist2\n"
def _set_artist_availability(artist: Artist, value: str) -> bool: """Set an artist availability from a string value. :param artist: An artist :param value: The artist availability as a string :return: Was the artist availability changed? """ availability = int(value) if artist.availability == availability: return False artist.availability = availability return True
def _set_artist_tags(artist: Artist, value: str) -> bool: """Set an artist tags from a string value. :param artist: An artist :param value: The artist tags as a string :return: Was the artist tags changed? """ try: tags = { key: int(val) for key, val in (token.split(":", 1) for token in value.split(",") if token) } except ValueError: return False if artist.tags == tags: return False artist.tags = tags return True
def test_serialization(tmp_path): """Validate we can serialize/deserialize a context. """ path = tmp_path / "context.yml" artist1 = Artist("Artist1", tags={"0010": 1}) artist2 = Artist("Artist1", tags={"0010": 1}) task1 = Task("0010", 1, tags=["acting"]) task2 = Task("0020", 1) context = Context( artists=[artist1], tasks=[task1, task2], assignments=[Assignment(artist1, task1), Assignment(artist2, task2)], combinations=[TaskGroup([task1], 1)], ) # Test serialization _io.export_context_to_yml(context, path) actual = yaml.load(path.read_text()) expected = { "artists": [{ "name": "Artist1", "availability": 100, "tags": [{ "name": "0010", "weight": 1 }], }], "tasks": [ { "name": "0010", "duration": 1.0, "tags": ["acting"] }, { "name": "0020", "duration": 1.0 }, ], "settings": { "TAGS": 100, "EQUAL_TASKS_BY_USER": 10, "EQUAL_TASKS_COUNT_BY_USER": 10, }, "assignments": [ { "artist": "Artist1", "task": "0010" }, { "artist": "Artist1", "task": "0020" }, ], "combinations": [{ "tasks": ["0010"], "weight": 1 }], } assert actual == expected # Test deserialization round-trip deserialized = _io.import_context_from_yml(path) assert context == deserialized