def test_get_speed_at_height_values_must_be_positive_non_0_numbers_for_all_but_base(self, mock_load):
        mock_load.return_value = self.default_config
        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height('a',10,10,1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,'a',10,1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,10,'a',1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,10,10,'a',1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,10,10,1,'a',1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(-1,10,10,1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,-10,10,1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,10,-1,1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,10,10,-1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,10,10,1,-1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,0,10,1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,10,0,1,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,10,10,0,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,10,10,1,0,1)
 def test_get_speed_at_height_returns_a_correct_height(self, mock_load):
     mock_load.return_value = self.default_config
     configuration_API = ConfigurationAPI(ConfigurationManager())
     configuration_API.load_printer("test")
     
     speed = configuration_API.get_speed_at_height(0,1,10,20,0.5)
     self.assertEquals(15, speed)
    def test_get_serial_options_loads_correctly(self, mock_save, mock_load):
        expected_enabled = True
        expected_port = 'com54'
        expected_on = 'GOGOGO'
        expected_off = 'STOPSTOP'

        mock_load.return_value =  self.default_config
        expected = self.default_config
        expected.serial.on = expected_enabled
        expected.serial.port      = expected_port
        expected.serial.on_command        = expected_on
        expected.serial.off_command       = expected_off

        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        configuration_API.set_serial_enabled(expected_enabled)
        configuration_API.set_serial_port(expected_port)
        configuration_API.set_serial_on_command(expected_on)
        configuration_API.set_serial_off_command(expected_off)

        self.assertEquals( expected_enabled ,configuration_API.get_serial_enabled())
        self.assertEquals( expected_port ,configuration_API.get_serial_port())
        self.assertEquals( expected_on ,configuration_API.get_serial_on_command())
        self.assertEquals( expected_off ,configuration_API.get_serial_off_command())

        self.assertConfigurationEqual(expected, mock_save.mock_calls[0][1][0])
    def test_get_available_audio_options_sets_currently_selected(self, mock_load, mock_get_valid_sampling_options):
        audio_options = { 
            "input" : [{'sample_rate' : 48000, 'depth': '16 bit' },{'sample_rate' : 44100, 'depth': '16 bit' }], 
            "output": [{'sample_rate' : 48000, 'depth': '16 bit' },{'sample_rate' : 44100, 'depth': '16 bit' }]
            }

        mock_get_valid_sampling_options.return_value = audio_options
        config = self.default_config
        config.audio.input.bit_depth = '16 bit'
        config.audio.input.sample_rate = 44100
        config.audio.output.bit_depth = '16 bit'
        config.audio.output.sample_rate = 44100

        mock_load.return_value = config

        capi = ConfigurationAPI(ConfigurationManager())
        capi.load_printer("Printer")

        actual = capi.get_available_audio_options()

        in1 = AudioSetting(44100,'16 bit', current = True)
        in2 = AudioSetting(48000,'16 bit', recommended = True)
        out1 = AudioSetting(44100,'16 bit', current = True)
        out2 = AudioSetting(48000,'16 bit', recommended = True)
        

        self.assertListContentsEqual([in1, in2],actual['inputs'])
        self.assertListContentsEqual([out1, out2],actual['outputs'])
 def test_get_cure_test_returns_a_layer_generator(self, mock_load):
     mock_load.return_value = self.default_config
     configuration_API = ConfigurationAPI(ConfigurationManager())
     configuration_API.load_printer("test")
     
     cure_test = configuration_API.get_cure_test(0,1,1,2)
     cure_test.next()
    def test_get_available_audio_options_should_include_recomended_options(self, mock_load, mock_get_valid_sampling_options):
        audio_options = { 
            "input" : [{'sample_rate' : 48000, 'depth': '16 bit' }], 
            "output": [{'sample_rate' : 48000, 'depth': '16 bit' }]
            }
        
        expected_in  = AudioSetting(48000,'16 bit', current = True)
        expected_in.set_recommended()
        expected_out = AudioSetting(48000,'16 bit', current = True)
        expected_out.set_recommended()
        

        expected = {
                    "inputs" : [expected_in], 
                    "outputs": [expected_out] 
                   }

        mock_get_valid_sampling_options.return_value = audio_options
        mock_load.return_value = self.default_config
        capi = ConfigurationAPI(ConfigurationManager())
        capi.load_printer("Printer")
        
        actual = capi.get_available_audio_options()

        self.assertListContentsEqual(expected['inputs'],actual['inputs'])
        self.assertListContentsEqual(expected['outputs'],actual['outputs'])
    def initialize(self):
        self.grid()
        self._current_printer = self.kwargs['printer']
        self._configuration_api = ConfigurationAPI(self._configuration_manager)
        self._configuration_api.load_printer(self._current_printer)

        self._base_height = IntVar()
        self._base_height.set(3)
        self._total_height = IntVar()
        self._total_height.set(23)
        self._start_speed = IntVar()
        self._start_speed.set(50)
        self._stop_speed = IntVar()
        self._stop_speed.set(250)
        self._best_height = IntVar()
        self._best_height.set(0)

        Label(self, text='Printer: ').grid(column=0, row=10)
        Label(self,
              text=self._configuration_api.current_printer()).grid(column=1,
                                                                   row=10)
        Button(self, text='?', command=self._help).grid(column=2,
                                                        row=10,
                                                        stick=N + E)

        Label(self).grid(column=1, row=15)

        Label(self, text="Base Height (mm)").grid(column=0, row=20)
        Entry(self, textvariable=self._base_height).grid(column=1, row=20)

        Label(self, text="Total Height (mm)").grid(column=0, row=30)
        Entry(self, textvariable=self._total_height).grid(column=1, row=30)

        Label(self, text="Start Speed (mm)").grid(column=0, row=40)
        Entry(self, textvariable=self._start_speed).grid(column=1, row=40)

        Label(self, text="Finish Speed (mm)").grid(column=0, row=50)
        Entry(self, textvariable=self._stop_speed).grid(column=1, row=50)
        Label(self).grid(column=1, row=60)

        Button(self, text="Run Test",
               command=self._start).grid(column=2, row=70, sticky=N + S + E)

        Label(self).grid(column=1, row=80)

        Label(self, text="Best height above base (mm)").grid(column=0, row=90)
        self._best_height_field = Entry(self, textvariable=self._best_height)
        self._best_height_field.grid(column=1, row=90)

        Label(self).grid(column=1, row=100)

        Button(self, text="Save", command=self._save).grid(column=2,
                                                           row=110,
                                                           sticky=N + S + W)
        Button(self, text="Back", command=self._back).grid(column=0,
                                                           row=110,
                                                           sticky=N + S + W)

        self.update()
    def test_get_dripper_type_should_return_current_type(self, mock_load):
        configuration_API = ConfigurationAPI(ConfigurationManager())
        mock_load.return_value = self.default_config
        configuration_API.load_printer('Printer')

        actual = configuration_API.get_dripper_type()

        self.assertEquals(self.default_config.dripper.dripper_type, actual)
    def test_start_counting_drips_should_start_getting_drips(self, mock_start,mock_load,mock_save):
        configuration_API = ConfigurationAPI(ConfigurationManager())
        mock_load.return_value = self.default_config
        configuration_API.load_printer('printer')

        configuration_API.start_counting_drips()

        mock_start.assert_called_with()
    def test_add_printer_should_save_itself(self, mock_save, mock_new):
        capi = ConfigurationAPI(ConfigurationManager())
        mock_new.return_value = "Some Printer Config"

        capi.add_printer("NewName")

        mock_new.assert_called_with("NewName")
        mock_save.assert_called_with("Some Printer Config")
    def test_get_emulated_drips_per_second_should_return(self, mock_load):
        configuration_API = ConfigurationAPI(ConfigurationManager())
        mock_load.return_value = self.default_config
        configuration_API.load_printer('Printer')

        actual = configuration_API.get_emulated_drips_per_second()

        self.assertEquals(self.default_config.dripper.emulated_drips_per_second, actual)
    def test_load_printer_calls_load(self, mock_load):
        printer_name = 'MegaPrint'
        mock_load.return_value = { 'name':printer_name}
        capi = ConfigurationAPI(ConfigurationManager())
        
        capi.load_printer(printer_name)

        mock_load.assert_called_with(printer_name)
    def test_get_drips_per_mm_should_return_current_setting_if_unmarked(self, mock_load):
        configuration_API = ConfigurationAPI(ConfigurationManager())
        mock_load.return_value = self.default_config

        configuration_API.load_printer('Printer')

        actual = configuration_API.get_drips_per_mm()

        self.assertEquals(self.default_config.dripper.drips_per_mm, actual)
    def test_get_available_printers_lists_printers(self, mock_list):
        printers = ['Tom','Dick','Harry']
        capi = ConfigurationAPI(ConfigurationManager())
        mock_list.return_value = printers

        actual = capi.get_available_printers()

        mock_list.assert_called_with()
        self.assertEqual(printers,actual)
    def test_set_dripper_type_should_return_current_type(self, mock_load):
        configuration_API = ConfigurationAPI(ConfigurationManager())
        mock_load.return_value = self.default_config
        configuration_API.load_printer('Printer')
        expected = 'emulated'
        configuration_API.set_dripper_type(expected)
        actual = configuration_API.get_dripper_type()

        self.assertEquals(expected, actual)
    def test_drip_calibration_should_call_reset_when_reset_requested(self, mock_reset,mock_start,mock_load,mock_save):
        configuration_API = ConfigurationAPI(ConfigurationManager())
        mock_load.return_value = self.default_config
        configuration_API.load_printer('printer')

        configuration_API.start_counting_drips()
        configuration_API.reset_drips()

        mock_reset.assert_called_with(0)
    def test_get_max_lead_distance_mm_returns_max_lead_distance(self, mock_load):
        expected = 0.4
        config = self.default_config
        config.dripper.max_lead_distance_mm = expected
        mock_load.return_value =  config
        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        self.assertEquals(expected,configuration_API.get_max_lead_distance_mm())
    def test_get_laser_thickness_mm_returns_thickness(self, mock_load):
        expected = 7.0
        config = self.default_config
        config.options.laser_thickness_mm = expected
        mock_load.return_value =  config
        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        self.assertEquals(expected,configuration_API.get_laser_thickness_mm())
    def test_get_speed_at_height_must_exceed_base_height(self, mock_load):
        mock_load.return_value = self.default_config
        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(10,1,1,2,1)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(1,1,1,2,1)
    def test_get_config_returns_current_config(self, mock_load):
        expected = { 'name':'MegaPrint' }
        mock_load.return_value = expected
        capi = ConfigurationAPI(ConfigurationManager())
        capi.load_printer('printer')

        actual = capi.get_current_config()
        
        self.assertEquals(expected, actual)
    def test_get_speed_at_height_must_have_height_between_total_and_base(self, mock_load):
        mock_load.return_value = self.default_config
        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(0,10,1,2,11)
        with self.assertRaises(Exception):
            configuration_API.get_speed_at_height(2,10,1,2,0)
 def test_set_speed(self, mock_save, mock_load):
     mock_load.return_value = self.default_config
     expected = self.default_config
     expected.options.draw_speed = 121.0
     configuration_API = ConfigurationAPI(ConfigurationManager())
     configuration_API.load_printer("test")
     
     configuration_API.set_speed(121)
     
     self.assertConfigurationEqual(expected, mock_save.mock_calls[0][1][0])
    def test_get_cure_test_final_speed_exceeds_start_speed(self, mock_load):
        mock_load.return_value = self.default_config
        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        with self.assertRaises(Exception):
            configuration_API.get_cure_test(1,10,10,1)

        with self.assertRaises(Exception):
            configuration_API.get_cure_test(1,10,1,1)
    def test_sublayer_height_mm_returns_theight(self, mock_load):
        expected = 7.0
        expected_config = self.default_config
        expected_config.options.sublayer_height_mm = expected
        mock_load.return_value =  expected_config

        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        self.assertEquals(expected,configuration_API.get_sublayer_height_mm())
 def test_set_speed_should_throw_exception_if_less_then_or_0(self, mock_save, mock_load):
     mock_load.return_value = self.default_config
     expected_config = self.default_config
     expected_config.options.draw_speed = 121.0
     configuration_API = ConfigurationAPI(ConfigurationManager())
     configuration_API.load_printer("test")
     
     with self.assertRaises(Exception):
         configuration_API.set_speed(-1)
     with self.assertRaises(Exception):
         configuration_API.set_speed(0)
    def test_current_printer_returns_printer_name(self, mock_new, mock_save):
        capi = ConfigurationAPI(ConfigurationManager())
        name = "Spam"
        config = self.default_config
        config.name = name
        mock_new.return_value = config
        capi.add_printer('Spam')

        actual = capi.current_printer()

        self.assertEqual('Spam', actual)
    def initialize(self):
        self.grid()

        self._current_printer = self.kwargs['printer']
        self._configuration_api = ConfigurationAPI(self._configuration_manager)

        Label(self, text=self._current_printer).grid(column=1, row=10)
        Button(self, text="Do not delete", command=self._back).grid(column=1,
                                                                    row=110)
        Button(self, text="Yes, delete",
               command=self._remove_printer).grid(column=1, row=120)
    def test_set_max_lead_distance_mm_sets_max_lead_distance_mm(self, mock_save, mock_load):
        expected = 0.4
        expected_config = self.default_config
        expected_config.dripper.max_lead_distance_mm = expected
        mock_load.return_value =  self.default_config
        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        configuration_API.set_max_lead_distance_mm(expected)

        self.assertEquals(expected,configuration_API.get_max_lead_distance_mm())
        self.assertConfigurationEqual(expected_config, mock_save.mock_calls[0][1][0])
    def test_set_laser_thickness_mm_sets_thickness(self, mock_save, mock_load):
        expected_thickness = 7.0
        config =  self.default_config
        expected = config
        expected.options.laser_thickness_mm = expected_thickness
        mock_load.return_value =  config 
        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        configuration_API.set_laser_thickness_mm(expected_thickness)

        self.assertEquals(expected_thickness,configuration_API.get_laser_thickness_mm())
        mock_save.assert_called_with(expected)
    def test_set_sublayer_height_mm_returns_height(self, mock_save, mock_load):
        expected_height = 7.0
        config =  self.default_config
        expected = config
        expected.options.sublayer_height_mm = expected_height
        mock_load.return_value =  config 
        configuration_API = ConfigurationAPI(ConfigurationManager())
        configuration_API.load_printer("test")

        configuration_API.set_sublayer_height_mm(expected_height)

        self.assertEquals(expected_height,configuration_API.get_sublayer_height_mm())
        mock_save.assert_called_with(expected)