def load_json(json_byte_stream): json_str = convert_byte_stream_to_str(json_byte_stream) api_response = {'errors': None, 'warnings': []} robot = Robot.get_instance() robot.reset() jpp = None errors, warnings = [], [] try: jpp = JSONProtocolProcessor(json_str) jpp.process() robot.simulate() except JSON_ERROR: errors.append('Cannot parse invalid JSON') except Exception as e: errors.append(str(e)) if jpp: errors.extend(jpp.errors) warnings.extend(jpp.warnings) if robot.get_warnings(): warnings.extend(robot.get_warnings()) api_response['errors'] = errors api_response['warnings'] = warnings return api_response
def test_process_head(self): protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) jpp.process_deck() jpp.process_head() expected_settings = { "tip-racks": [ self.robot._deck.containers()['p200-rack'] ], "trash-container": self.robot._deck.containers()['trash'], "down-plunger-speed": 200, "up-plunger-speed": 500, "tip-plunge": 6, "extra-pull-volume": 0, "extra-pull-delay": 200, "distribute-percentage": 0.1, "points": [ { "f1": 1, "f2": 1 }, { "f1": 5, "f2": 5 }, { "f1": 7, "f2": 7 }, { "f1": 10, "f2": 10 } ] } instrument = self.robot._instruments["B"] head_expected = { 'p200': { 'instance': instrument, 'settings': expected_settings } } self.assertDictEqual(head_expected, jpp.head) self.assertEqual(1, instrument.channels) self.assertEqual(0, instrument.min_volume) self.assertEqual(200, instrument.max_volume) self.assertEqual('p200', instrument.name)
def test_process_instructions(self): protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) jpp.process_deck() jpp.process_head() jpp.process_instructions() api_calls = [ get_name_from_closure(cmd.do) for cmd in self.robot._commands ] api_calls_expected = [ 'pick_up_tip', 'aspirate', 'dispense', 'delay', 'aspirate', 'dispense', 'delay', 'drop_tip' ] self.assertEqual(api_calls, api_calls_expected) # self.robot.run() instrument = self.robot._instruments["B"] wells_referenced = [(i.get_parent().get_name(), i.get_name()) for i in instrument.placeables] wells_referenced_expected = [ ('p200-rack', 'A1'), # Location of first tip in tiprack ('.75 mL Tube Rack', 'A1'), # 1st transfer ('.75 mL Tube Rack', 'C1'), # 1st transfer ('.75 mL Tube Rack', 'A1'), # 2nd transfer ('.75 mL Tube Rack', 'C1'), # 2nd transfer ('trash', 'A1') # Location of tiprack in trash ] self.assertEqual(wells_referenced, wells_referenced_expected)
def test_all(self): failures = [] for protocol_path, protocol_dict in self.get_protocols(): Robot.reset_for_tests() try: jpp = JSONProtocolProcessor(protocol_dict) jpp.process() except Exception as e: failures.append((protocol_path, e, jpp.errors)) if failures: print('The following protocols failed to parse') for path, exc, reason in failures: print("[{}]. Reason: {}".format(path, exc)) assert False
def test_process_command(self): jpp = JSONProtocolProcessor(self.get_protocol()) jpp.handle_consolidate = mock.Mock() jpp.handle_distribute = mock.Mock() jpp.handle_transfer = mock.Mock() jpp.handle_mix = mock.Mock() for cmd in ['consolidate', 'distribute', 'mix', 'transfer']: jpp.process_command(None, cmd, None) for cmd in ['consolidate', 'distribute', 'mix', 'transfer']: getattr(jpp, "handle_{}".format(cmd), mock.Mock()).called with self.assertRaises(JSONProcessorRuntimeError): jpp.process_command(None, 'foo', None)
def test_deck(self): protocol = self.get_protocol() deck_dict = protocol['deck'] jpp = JSONProtocolProcessor(protocol) jpp.process_deck() robot_deck = self.robot._deck robot_containers = robot_deck.containers() deck_expected = {k: {'instance': v} for k, v in robot_containers.items()} self.assertDictEqual(jpp.deck, deck_expected) for name, container_instance in robot_containers.items(): self.assertEqual( robot_deck[deck_dict[name]['slot']][0], container_instance)
def test_all(self): failures = [] for protocol_path, protocol_dict in self.get_protocols(): Robot.reset_for_tests() try: jpp = JSONProtocolProcessor(protocol_dict) jpp.process() except Exception as e: failures.append( (protocol_path, e, jpp.errors) ) if failures: print('The following protocols failed to parse') for path, exc, reason in failures: print("[{}]. Reason: {}".format(path, exc)) assert False
def test_process_command(self): jpp = JSONProtocolProcessor(self.get_protocol()) jpp.handle_consolidate = mock.Mock() jpp.handle_distribute = mock.Mock() jpp.handle_transfer = mock.Mock() jpp.handle_mix = mock.Mock() for cmd in ['consolidate', 'distribute', 'mix', 'transfer']: jpp.process_command(None, cmd, None) for cmd in ['consolidate', 'distribute', 'mix', 'transfer']: getattr(jpp, "handle_{}".format(cmd), mock.Mock()).called with self.assertRaises(JSONProcessorRuntimeError): jpp.process_command(None, 'foo', None)
def test_deck(self): protocol = self.get_protocol() deck_dict = protocol['deck'] jpp = JSONProtocolProcessor(protocol) jpp.process_deck() robot_deck = self.robot._deck robot_containers = robot_deck.containers() deck_expected = { k: { 'instance': v } for k, v in robot_containers.items() } self.assertDictEqual(jpp.deck, deck_expected) for name, container_instance in robot_containers.items(): self.assertEqual(robot_deck[deck_dict[name]['slot']][0], container_instance)
def test_validate_protocol(self): protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) jpp.validate() self.assertEqual(jpp.errors, []) self.assertEqual( jpp.warnings, ['JSON Protocol section "Ingredients" will not be used']) protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) del jpp.protocol['head'] self.assertRaises(JSONProcessorValidationError, jpp.validate) protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) del jpp.protocol['deck'] self.assertRaises(JSONProcessorValidationError, jpp.validate) protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) del jpp.protocol['instructions'] self.assertRaises(JSONProcessorValidationError, jpp.validate)
def test_process_head(self): protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) jpp.process_deck() jpp.process_head() expected_settings = { "tip-racks": [self.robot._deck.containers()['p200-rack']], "trash-container": self.robot._deck.containers()['trash'], "down-plunger-speed": 200, "up-plunger-speed": 500, "tip-plunge": 6, "extra-pull-volume": 0, "extra-pull-delay": 200, "distribute-percentage": 0.1, "points": [{ "f1": 1, "f2": 1 }, { "f1": 5, "f2": 5 }, { "f1": 7, "f2": 7 }, { "f1": 10, "f2": 10 }] } instrument = self.robot._instruments["B"] head_expected = { 'p200': { 'instance': instrument, 'settings': expected_settings } } self.assertDictEqual(head_expected, jpp.head) self.assertEqual(1, instrument.channels) self.assertEqual(0, instrument.min_volume) self.assertEqual(200, instrument.max_volume) self.assertEqual('p200', instrument.name)
def test_process_instructions(self): protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) jpp.process_deck() jpp.process_head() jpp.process_instructions() api_calls = [ get_name_from_closure(cmd.do) for cmd in self.robot._commands ] api_calls_expected = [ 'pick_up_tip', 'aspirate', 'dispense', 'delay', 'aspirate', 'dispense', 'delay', 'drop_tip' ] self.assertEqual(api_calls, api_calls_expected) # self.robot.run() instrument = self.robot._instruments["B"] wells_referenced = [ (i.get_parent().get_name(), i.get_name()) for i in instrument.placeables ] wells_referenced_expected = [ ('p200-rack', 'A1'), # Location of first tip in tiprack ('.75 mL Tube Rack', 'A1'), # 1st transfer ('.75 mL Tube Rack', 'C1'), # 1st transfer ('.75 mL Tube Rack', 'A1'), # 2nd transfer ('.75 mL Tube Rack', 'C1'), # 2nd transfer ('B2', 'trash') # Location of tiprack in trash ] self.assertEqual(wells_referenced, wells_referenced_expected)
def test_validate_protocol(self): protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) jpp.validate() self.assertEqual(jpp.errors, []) self.assertEqual( jpp.warnings, ['JSON Protocol section "Ingredients" will not be used'] ) protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) del jpp.protocol['head'] self.assertRaises(JSONProcessorValidationError, jpp.validate) protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) del jpp.protocol['deck'] self.assertRaises(JSONProcessorValidationError, jpp.validate) protocol = self.get_protocol() jpp = JSONProtocolProcessor(protocol) del jpp.protocol['instructions'] self.assertRaises(JSONProcessorValidationError, jpp.validate)