Package jptests :: Package data :: Module input
[hide private]
[frames] | no frames]

Source Code for Module jptests.data.input

  1  """Unit tests for jazzparser.data.input module 
  2   
  3  """ 
  4  """ 
  5  ============================== License ======================================== 
  6   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
  7    
  8   This file is part of The Jazz Parser. 
  9    
 10   The Jazz Parser is free software: you can redistribute it and/or modify 
 11   it under the terms of the GNU General Public License as published by 
 12   the Free Software Foundation, either version 3 of the License, or 
 13   (at your option) any later version. 
 14    
 15   The Jazz Parser is distributed in the hope that it will be useful, 
 16   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 18   GNU General Public License for more details. 
 19    
 20   You should have received a copy of the GNU General Public License 
 21   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 22   
 23  ============================ End license ====================================== 
 24   
 25  """ 
 26  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 27   
 28  import unittest, os 
 29  from jazzparser import settings 
 30   
 31  from jazzparser.data.input import DbInput, ChordInput, DbBulkInput, \ 
 32                                                                  ChordBulkInput, SegmentedMidiInput, \ 
 33                                                                  SegmentedMidiBulkInput, detect_input_type, \ 
 34                                                                  input_type_name, InputTypeError, INPUT_TYPES, \ 
 35                                                                  BULK_INPUT_TYPES, is_bulk_type 
 36  from jazzparser.data.db_mirrors import SequenceIndex 
 37   
 38  DB_SEQUENCES_FILE = os.path.join(settings.TEST_DATA_DIR, "dbsequences") 
 39  CHORDS_FILE = os.path.join(settings.TEST_DATA_DIR, "text_chords") 
 40  CHORD_SEQS_FILE = os.path.join(settings.TEST_DATA_DIR, "text_chord_list") 
 41  CHORDS = "IM7 IV7 IM7 I7 IV7" 
 42  MIDI_FILE = os.path.join(settings.TEST_DATA_DIR, "afine.mid") 
 43  SEGMENTED_MIDI = ( 
 44          os.path.join(settings.TEST_DATA_DIR, "afternoon.mid"), 
 45          2,  
 46          240 
 47  ) 
 48  SEGMENTED_MIDIS = os.path.join(settings.TEST_DATA_DIR, "segmidi.csv") 
 49   
50 -class TestDbInput(unittest.TestCase):
51 """ 52 Tests for loading a DbInput. 53 54 """
55 - def test_from_sequence(self):
56 # Load the sequence index file 57 index = SequenceIndex.from_file(DB_SEQUENCES_FILE) 58 # Pick out a sequence 59 seq = index.sequences[0] 60 # Construct a DbInput from this sequence 61 dbi = DbInput.from_sequence(seq)
62
63 - def test_from_file(self):
64 # Select a sequence out of the sequence index file using the "index" option 65 options = { 66 'index' : 0, 67 } 68 # Just load the sequence up from the file 69 dbi = DbInput.from_file(DB_SEQUENCES_FILE, options)
70
71 -class TestChordInput(unittest.TestCase):
72 """ 73 Test for loading a ChordInput. 74 75 """
76 - def test_from_file(self):
77 # Just load the file 78 ci = ChordInput.from_file(CHORDS_FILE, options={'roman':True})
79
80 - def test_from_string(self):
81 ci = ChordInput.from_string(CHORDS, roman=True)
82
83 -class TestSegmentedMidiInput(unittest.TestCase):
84 """ 85 Test for loading a SegmentedMidiInput from a MIDI file. 86 87 """
88 - def test_from_file(self):
89 """ Load a file with default options. """ 90 options = SegmentedMidiInput.process_option_dict({}) 91 mid = SegmentedMidiInput.from_file(MIDI_FILE, options=options)
92
94 """ Load a file with time unit and offset options. """ 95 options = SegmentedMidiInput.process_option_dict({ 96 'time_unit' : SEGMENTED_MIDI[1], 97 'tick_offset' : SEGMENTED_MIDI[2], 98 }) 99 mid = SegmentedMidiInput.from_file(SEGMENTED_MIDI[0], options=options)
100
101 -class TestDbBulkInput(unittest.TestCase):
102 """ 103 Test for loading a DbBulkInput. 104 105 """
106 - def test_from_file(self):
107 # Simply load a sequence index file 108 bulk = DbBulkInput.from_file(DB_SEQUENCES_FILE) 109 # We can get the sequences just but converting the iter to a list 110 seqs = list(bulk) 111 # There should be a non-zero number of sequences loaded 112 self.assertNotEqual(len(seqs), 0) 113 # Check the type of the first one 114 self.assertIsInstance(seqs[0], DbInput)
115
116 -class TestChordBulkInput(unittest.TestCase):
117 """ 118 Tests for loading a ChordBulkInput (text chord sequences). 119 120 """
121 - def test_from_file(self):
122 # Load up the chord sequence list from a file 123 bulk = ChordBulkInput.from_file(CHORD_SEQS_FILE, options={'roman':True}) 124 # Get a list of the sequences 125 seqs = list(bulk) 126 # Check some sequences were loaded 127 self.assertNotEqual(len(seqs), 0) 128 # Check the type of the first one 129 self.assertIsInstance(seqs[0], ChordInput) 130 131 # There should be some sequences with names (though not all) 132 self.assertTrue(any(seq.name is not None for seq in seqs))
133
134 -class TestSegmentedMidiBulkInput(unittest.TestCase):
135 """ 136 Tests for loading a list of MIDI files from a CSV. 137 138 """
139 - def test_from_file(self):
142
143 -class TestUtilities(unittest.TestCase):
144 """ 145 Tests the utility functions. 146 147 """
148 - def test_detect_input_type(self):
149 # Load some input: DbInput 150 dbi = DbInput.from_file(DB_SEQUENCES_FILE, {'index':0}) 151 # Run it through the preprocessor 152 datatype,obj = detect_input_type(dbi) 153 # Get the datatype from the type name lists 154 datatype2 = input_type_name(type(obj)) 155 self.assertEqual(datatype, datatype2) 156 157 # Do the same with ChordInput 158 ci = ChordInput.from_file(CHORDS_FILE, options={'roman':True}) 159 datatype,obj = detect_input_type(ci) 160 datatype2 = input_type_name(type(obj)) 161 self.assertEqual(datatype, datatype2) 162 163 # Try some bulk input 164 bulk = DbBulkInput.from_file(DB_SEQUENCES_FILE) 165 datatype,obj = detect_input_type(bulk, allow_bulk=True) 166 datatype2 = input_type_name(type(obj)) 167 self.assertEqual(datatype, datatype2) 168 169 # Try restricting the allowed type 170 datatype,obj = detect_input_type(ci, allowed=['chords']) 171 # And this one should get rejected 172 self.assertRaises(InputTypeError, detect_input_type, (ci,), {'allowed':'db'})
173
174 -class TestTypeLists(unittest.TestCase):
175 """ 176 Some sanity checks on the type lists. 177 178 """
179 - def test_input_types(self):
180 # Check INPUT_TYPES doesn't have bulk input types 181 for datatype,cls in INPUT_TYPES: 182 self.assertFalse(is_bulk_type(cls)) 183 # Check there are no duplicate names 184 names,__ = zip(*(INPUT_TYPES+BULK_INPUT_TYPES)) 185 self.assertEqual(len(names), len(set(names)), msg="Duplicate input type names")
186
187 - def test_bulk_input_types(self):
188 # Check BULK_INPUT_TYPES is only bulk types 189 for datatype,cls in BULK_INPUT_TYPES: 190 self.assertTrue(is_bulk_type(cls))
191