def test_add_note_strict_false(): try: Chord(Note(name='C'), Note(name='E'), Note(name='G')).add_note(note='invalid', strict=False) Chord(strict=False).add_note(note='invalid', strict=False) except ChordException: pytest.fail('Unexpected ChordException')
def test_str(): s = Scale.create_from_root(root=Note(name='C')) assert 'C4,D4,E4,F4,G4,A4,B4' == str(s) s = Scale.create_from_root(root=Note(name='C'), order=Scale.DESCENDING_SCALE_TYPE) assert 'B4,A4,G4,F4,E4,D4,C4' == str(s)
def test_remove_note_none(): m = Mock(Note(name='C'), Note(name='C', octave=5)) m.remove_note() assert len(m.notes) == 2 m = Mock(Note(name='C'), Note(name='D'), Note(name='E')) m.remove_note() assert len(m.notes) == 3
def test_value(): n = Note(name='A') assert n.duration == 4 / 4 n.duration = 1 / 8 assert n.duration == 1 / 8 assert Note(name='A', duration=1 / 16).duration == 1 / 16
def test_is_valid(): assert Mock(strict=False).is_valid() is False assert Mock(Note(name='C'), strict=False).is_valid() is True assert Mock('a', 'b', strict=False).is_valid() is False assert Mock(Note(name='C'), Note(name='E')).is_valid() is True assert Mock(Note(name='C'), Note(name='E'), strict=False).is_valid() is True assert Mock(strict=False).is_valid() is False
def test_get_notes_from_root_with_none_note_list_type(): with pytest.raises(NoteListException) as exc: Mock.get_notes_from_root(root=Note(name='C')) assert 'Invalid note list type' == str(exc.value) with pytest.raises(NoteListException) as exc: Mock.get_notes_from_root(root=Note(name='C'), alt=Note.FLAT, octave=4) assert 'Invalid note list type' == str(exc.value)
def test_create_with_invalid_note_name(): with pytest.raises(NoteException) as exc: Note(name='S') assert 'Invalid note.' == str(exc.value) with pytest.raises(NoteException) as exc: Note(name=3) assert 'Invalid note.' == str(exc.value)
def test_get_notes_from_root_with_alt(): m = Mock(*Mock.get_notes_from_root( root=Note(name='C'), note_list_type=[3, 5, 7, 10], alt=Note.FLAT)) assert 'Eb4' in str(m).split(',') assert 'Bb4' in str(m).split(',') m = Mock(*Mock.get_notes_from_root( root=Note(name='C'), note_list_type=[3, 5, 7, 10], alt=Note.SHARP)) assert 'D#4' in str(m).split(',') assert 'A#4' in str(m).split(',')
def test_create_from_root_with_alt(): c = Chord.create_from_root(root=Note(name='C', alt=Note.SHARP), chord_type=Chord.MINOR_TYPE) assert 'D#4' in str(c).split(',') c = Chord.create_from_root(root=Note(name='C', alt=Note.FLAT), chord_type=Chord.MINOR_TYPE, alt=Note.FLAT) assert 'Eb4' in str(c).split(',')
def test_add_note_strict(): with pytest.raises(ChordException) as exc: Chord(Note(name='C'), Note(name='E'), Note(name='G')).add_note(note='invalid') assert 'Invalid note given.' == str(exc.value) with pytest.raises(ChordException) as exc: Chord(strict=False).add_note(note='invalid') assert 'Invalid note given.' == str(exc.value)
def test_str(): m = Mock(Note(name='C'), Note(name='E'), Note(name='G')) assert 'C4' in str(m).split(',') assert 'E4' in str(m).split(',') assert 'G4' in str(m).split(',') m.add_note(note=Note(name='C', octave=5)) assert 'C4' in str(m).split(',') assert 'E4' in str(m).split(',') assert 'G4' in str(m).split(',') assert 'C5' in str(m).split(',')
def test_repr(): n = eval(repr(Note(name='A'))) assert n.freq == 440 assert n.name == 'A' assert n.octave == 4 assert n.duration == 4 / 4 n = eval(repr(Note(freq=932.32, alt=Note.FLAT, duration=1 / 8))) assert n.freq == 932.32 assert n.name == 'Bb' assert n.octave == 5 assert n.duration == 1 / 8
def remove_note_by_note_not_strict(): m = Mock('a', 'b') m.remove_note(note='a', strict=False) assert len(m.notes) == 1 assert 'b' in m.notes assert 'a' not in m.notes m = Mock(Note(name='C'), Note(name='E')) m.remove_note(note=Note(name='C'), strict=False) assert len(m.notes) == 1 assert Note(name='E') in m.notes m.remove_note(name='G', strict=False) assert len(m.notes) == 1 assert Note(name='E') in m.notes try: m = Mock(Note(name='C')) m.remove_note(note=Note(name='C'), strict=False) except: pytest.fail('Unexpected NoteListException') assert len(m) == 0
def test_add_invalid_note(): m = Mock(Note(name='C'), Note(name='D'), Note(name='E')) with pytest.raises(NoteListException) as exc: m.add_note(note=1) assert 'Invalid note given.' == str(exc.value) with pytest.raises(NoteListException) as exc: m.add_note(note='invalid') assert 'Invalid note given.' == str(exc.value) m.add_note(note='invalid', strict=False) assert len(m.notes) == 4 with pytest.raises(AttributeError) as exc: assert "'str' object has no attribute 'freq'" in m.notes
def test_remove_note_by_name_not_strict(): m = Mock(Note(name='C'), Note(name='E')) m.remove_note(name='C', strict=False) assert len(m.notes) == 1 assert Note(name='E') in m.notes m.remove_note(name='G', strict=False) assert len(m.notes) == 1 assert Note(name='E') in m.notes try: m = Mock(Note(name='A')) m.remove_note(name='A', strict=False) except: pytest.fail('Unexpected NoteListException') assert len(m.notes) == 0
def test_create_from_root_with_int_octave(): c = Chord.create_from_root(root=Note(name='G', octave=3), octave=4) assert Note(name='G', octave=3) in c.notes assert Note(name='B', octave=4) in c.notes assert Note(name='D', octave=4) in c.notes c = Chord.create_from_root(root=Note(name='F', octave=4), octave=3) assert Note(name='F', octave=4) in c.notes assert Note(name='A', octave=3) in c.notes assert Note(name='C', octave=3) in c.notes
def test_add_note(): m = Mock(Note(name='C'), Note(name='D'), Note(name='E')) m.add_note(note=Note(name='G')) assert len(m.notes) == 4 assert Note(name='G') in m.notes m.add_note(note=Note(name='C')) assert len(m.notes) == 5 m.add_note(note=Note(name='A', octave=5)) assert len(m.notes) == 6 assert Note(name='A', octave=5) in m.notes
def test_create_with_name(): n = Note(name='A') assert n.name == 'A' assert n.freq == 440 assert n.octave == 4 n = Note(name='B') assert n.name == 'B' assert n.freq == 493.88 assert n.octave == 4 n = Note(name='B', octave=3) assert n.name == 'B' assert n.freq == 246.94 assert n.octave == 3 n = Note(name='D#', octave=5) assert n.name == 'D#' assert n.freq == 622.25 assert n.octave == 5
def test_create_not_strict(): try: assert len(Chord(strict=False).notes) == 0 assert len(Chord(Note(name='C'), strict=False).notes) == 1 c = Chord('a', 'b', strict=False) assert len(c.notes) == 2 assert 'a' in c.notes assert 'b' in c.notes except ChordException: pytest.fail("Unexpected ChordException")
def test_remove_note(): c = Chord.create_from_root(root=Note(name='C')) c.remove_note(note=Note(name='E')) assert len(c.notes) == 2 assert c.notes[0] == Note(name='C') assert c.notes[1] == Note(name='G') c = Chord.create_from_root(root=Note(name='C'), chord_type=Chord.MINOR_TYPE) c.remove_note(note=Note(name='Eb')) assert len(c.notes) == 2 assert c.notes[0] == Note(name='C') assert c.notes[1] == Note(name='G')
def test_create_with_freq(): n = Note(freq=440) assert n.name == 'A' assert n.freq == 440 assert n.octave == 4 n = Note(freq=246.94) assert n.name == 'B' assert n.freq == 246.94 assert n.octave == 3 n = Note(freq=466.16) assert n.name == 'A#' assert n.freq == 466.16 assert n.octave == 4 n = Note(freq=466.16, alt=Note.FLAT) assert n.name == 'Bb' assert n.freq == 466.16 assert n.octave == 4 n = Note(freq=466.16, alt=Note.SHARP) assert n.name == 'A#' assert n.freq == 466.16 assert n.octave == 4 n = Note(freq=440, name='B', octave=2) assert n.name == 'A' assert n.freq == 440 assert n.octave == 4
def test_create_from_root_with_callable_octave(): c = Chord.create_from_root(root=Note(name='G', octave=3), octave=lambda root_octave, i, distance: i + 1) assert Note(name='G', octave=3) in c.notes assert Note(name='B', octave=1) in c.notes assert Note(name='D', octave=2) in c.notes c = Chord.create_from_root( root=Note(name='F', octave=4), octave=lambda root_octave, i, distance: root_octave + i + 1) assert Note(name='F', octave=4) in c.notes assert Note(name='A', octave=5) in c.notes assert Note(name='C', octave=6) in c.notes with pytest.raises(TypeError): Chord.create_from_root(root=Note(name='F', octave=4), octave=lambda: 'invalid')
def test_change_freq(): n = Note(freq=440) n.freq = 220 assert n.name == 'A' assert n.freq == 220 assert n.octave == 3 n.freq = 622.25 assert n.name == 'D#' assert n.freq == 622.25 assert n.octave == 5 with pytest.raises(ValueError): n.freq = 'string'
def test_get_notes_from_root_with_invalid_octave(): notes = Mock.get_notes_from_root(root=Note(name='C'), note_list_type=[3, 5, 7, 10], octave='invalid') assert Note(name='C', octave=4) in notes assert Note(name='Eb', octave=4) in notes assert Note(name='F', octave=4) in notes assert Note(name='G', octave=4) in notes assert Note(name='Bb', octave=4) in notes
def test_change_alt(): n = Note(freq=466.16, alt=Note.SHARP) assert n.name == 'A#' assert n.freq == 466.16 assert n.octave == 4 n.alt = Note.FLAT assert n.name == 'Bb' assert n.freq == 466.16 assert n.octave == 4 n.alt = 'random string' assert n.name == 'A#' assert n.freq == 466.16 assert n.octave == 4
def get_notes_from_root(cls, root, note_list_type=None, octave=None, alt=Note.SHARP): """ :param root: root note :param note_list_type: a list of notes distance from root note :param octave: octave of notes in chord :param alt: note alteration 'sharp' or 'flat' :type root: Note :type note_list_type: list :type octave: Union[int, string, callable] :type alt: string :return: Scale """ if note_list_type is None: raise NoteListException('Invalid note list type') if octave is None: octave = cls.OCTAVE_TYPE_ROOT root_idx = root.get_note_index() def get_octave(i, distance): if callable(octave): return octave(root.octave, i, distance) elif isinstance(octave, int): return octave elif octave == cls.OCTAVE_TYPE_ROOT: return root.octave elif octave == cls.OCTAVE_TYPE_FROM_ROOT: return root.octave + int(distance / len(Note.NOTES)) else: return Note.A_DEFAULT_OCTAVE notes = [ Note(name=Note.get_note_name_by_index(root_idx + distance, alt=alt), octave=get_octave(i, root_idx + distance), alt=alt) for i, distance in enumerate(note_list_type) ] notes.insert(0, root) return notes
def test_is_valid(): assert Chord(strict=False).is_valid() is False assert Chord(Note(name='C'), strict=False).is_valid() is False assert Chord('a', 'b', strict=False).is_valid() is False assert Chord(Note(name='C'), Note(name='E')).is_valid() is True c = Chord(Note(name='C'), Note(name='E')) assert c.is_valid() is True assert Chord(Note(name='C'), strict=False).is_valid() is False
def test_get_notes_from_root_with_callable_octave(): notes = Mock.get_notes_from_root( root=Note(name='C', octave=3), note_list_type=[3, 7], octave=lambda root_octave, i, distance: root_octave + 1) assert Note(name='C', octave=3) in notes assert Note(name='Eb', octave=4) in notes assert Note(name='G', octave=4) in notes notes = Mock.get_notes_from_root( root=Note(name='C', octave=3), note_list_type=[3, 7], octave=lambda root_octave, i, distance: i + 1) assert Note(name='C', octave=3) in notes assert Note(name='Eb', octave=1) in notes assert Note(name='G', octave=2) in notes with pytest.raises(TypeError): notes = Mock.get_notes_from_root(root=Note(name='C', octave=3), note_list_type=[3, 7], octave=lambda: 'invalid')
def test_create(): s = Scale(Note(name='C'), Note(name='D'), Note(name='E')) assert s.notes[0] == Note(name='C') assert s.notes[1] == Note(name='D') assert s.notes[2] == Note(name='E') assert len(s.notes) == 3 s = Scale(Note(name='C'), Note(name='Bb', octave=3), Note(name='D'), Note(name='E')) assert s.notes[0] == Note(name='Bb', octave=3) assert s.notes[1] == Note(name='C') assert s.notes[2] == Note(name='D') assert s.notes[3] == Note(name='E') assert len(s.notes) == 4 s = Scale(Note(name='C'), Note(name='C'), Note(name='D'), Note(name='E')) assert s.notes[0] == Note(name='C') assert s.notes[1] == Note(name='D') assert s.notes[2] == Note(name='E') assert len(s.notes) == 3 s = Scale(Note(name='C'), Note(name='C', octave=5), Note(name='D'), Note(name='E')) assert s.notes[0] == Note(name='C') assert s.notes[1] == Note(name='D') assert s.notes[2] == Note(name='E') assert s.notes[3] == Note(name='C', octave=5) assert len(s.notes) == 4 s = Scale(Note(name='C', octave=5), Note(name='D', octave=3), Note(name='D')) assert s.notes[0] == Note(name='D', octave=3) assert s.notes[1] == Note(name='D') assert s.notes[2] == Note(name='C', octave=5) assert len(s.notes) == 3 s = Scale(Note(name='C'), Note(name='Bb', octave=3), Note(name='D'), order=Scale.ASCENDING_SCALE_TYPE) assert s.notes[0] == Note(name='Bb', octave=3) assert s.notes[1] == Note(name='C') assert s.notes[2] == Note(name='D') assert len(s.notes) == 3 s = Scale(Note(name='C', octave=5), Note(name='D', octave=3), Note(name='D'), Note(name='G'), order=Scale.ASCENDING_SCALE_TYPE) assert s.notes[0] == Note(name='D', octave=3) assert s.notes[1] == Note(name='D') assert s.notes[2] == Note(name='G') assert s.notes[3] == Note(name='C', octave=5) assert len(s.notes) == 4 s = Scale(Note(name='C'), Note(name='Bb', octave=3), Note(name='D'), Note(name='E'), order=Scale.DESCENDING_SCALE_TYPE) assert s.notes[0] == Note(name='E') assert s.notes[1] == Note(name='D') assert s.notes[2] == Note(name='C') assert s.notes[3] == Note(name='Bb', octave=3) assert len(s.notes) == 4 s = Scale(Note(name='C', octave=5), Note(name='D', octave=3), Note(name='D'), order=Scale.DESCENDING_SCALE_TYPE) assert s.notes[0] == Note(name='C', octave=5) assert s.notes[1] == Note(name='D') assert s.notes[2] == Note(name='D', octave=3) assert len(s.notes) == 3 with pytest.raises(ScaleException) as exc: Scale() assert 'Invalid Scale.' == str(exc.value) with pytest.raises(ScaleException) as exc: Scale('invalid') assert 'Invalid Scale.' == str(exc.value) with pytest.raises(ScaleException) as exc: Scale('invalid', 'invalid') assert 'Invalid Scale.' == str(exc.value) try: Scale(Note(name='C'), 'invalid', strict=False) Scale('invalid', strict=False) Scale(strict=False) except ScaleException: pytest.fail("Unexpected NoteListException")
def test_repr(): s = eval(repr(Scale.create_from_root(root=Note(name='C')))) assert s == Scale.create_from_root(root=Note(name='C')) s = eval(repr(Scale.create_from_root(root=Note(name='F')))) assert s == Scale.create_from_root(root=Note(name='F')) s = eval( repr( Scale(Note(name='C'), Note(name='D'), Note(name='E'), Note(name='F#'), Note(name='G#'), Note(name='A#')))) assert s == Scale(Note(name='C'), Note(name='D'), Note(name='E'), Note(name='F#'), Note(name='G#'), Note(name='A#')) s = eval(repr(Scale.create_from_root(root=Note(name='C'), strict=True))) assert s.strict is True s = eval(repr(Scale.create_from_root(root=Note(name='C'), strict=False))) assert s.strict is False