<commit_before>import unittest
import json
from unittest import mock
import os

from opentrons.robot import Robot


class CalibrationTestCase(unittest.TestCase):
    def setUp(self):
        from main import app
        self.app = app.test_client()

        self.data_path = os.path.join(
            os.path.dirname(__file__) + '/data/'
        )

        self.robot = Robot.get_instance()
        self.robot.connect()

    def test_aspirate_dispense(self):

        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.assertEquals(self.robot._instruments['B'].max_volume, 10)

        self.robot._instruments['B'].calibrate_plunger(
            top=0, bottom=5, blow_out=6, drop_tip=7)

        # moving the head down (as the user would have already done)
        # so limit switch isn't hit during this test
        self.robot.move_head(z=0)

        arguments = {
            'axis': 'b'
        }
        response = self.app.post(
            '/aspirate',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        arguments = {
            'axis': 'b'
        }
        response = self.app.post(
            '/dispense',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        arguments = {
            'axis': 'b',
            'volume': 12
        }
        response = self.app.post(
            '/set_max_volume',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.assertEquals(self.robot._instruments['B'].max_volume, 12)

    def test_move_to_plunger_position(self):

        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.robot._instruments['B'].motor.move(12)
        self.robot._instruments['B'].calibrate('bottom')
        self.robot._instruments['B'].motor.move(2)
        current_pos = self.robot._driver.get_plunger_positions()['target']
        self.assertEquals(current_pos['b'], 2)

        arguments = {
            'position': 'bottom',
            'axis': 'b'
        }
        response = self.app.post(
            '/move_to_plunger_position',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        self.assertEqual(status, 'success')
        current_pos = self.robot._driver.get_plunger_positions()['target']
        self.assertEquals(current_pos['b'], 12)

    def test_move_to_container(self):
        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.robot.move_to = mock.Mock()

        arguments = {
            'label': 'test-plate',
            'slot': 'B2',
            'axis': 'b'
        }

        response = self.app.post(
            '/move_to_container',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        container = self.robot._deck['B2'].get_child_by_name(
            'test-plate')
        instrument = self.robot._instruments['B']
        expected = [
            mock.call(
                container[0].bottom(),
                instrument=instrument,
                strategy='arc',
                enqueue=False)
        ]
        self.assertEquals(self.robot.move_to.mock_calls, expected)

    def test_calibrate_placeable(self):
        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        arguments = {
            'label': 'test-plate',
            'axis': 'b'
        }

        response = self.app.post(
            '/calibrate_placeable',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        actual = json.loads(response.data.decode())
        name = actual['data']['name']
        axis = actual['data']['axis']
        step_list = actual['data']['calibrations']
        status = actual['status']

        self.assertEqual(name, 'test-plate')
        self.assertEqual(axis, 'b')
        self.assertTrue(bool(step_list))
        self.assertEqual(status, 'success')

    def test_calibrate_plunger(self):
        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.robot._instruments['B'].motor.move(2)
        arguments = {'position': 'top', 'axis': 'b'}
        response = self.app.post(
            '/calibrate_plunger',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')
        saved_pos = self.robot._instruments['B'].positions['top']
        self.assertEquals(saved_pos, 2)

        self.robot._instruments['B'].motor.move(3)
        arguments = {'position': 'bottom', 'axis': 'b'}
        response = self.app.post(
            '/calibrate_plunger',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')
        saved_pos = self.robot._instruments['B'].positions['bottom']
        self.assertEquals(saved_pos, 3)

        self.robot._instruments['B'].motor.move(4)
        arguments = {'position': 'blow_out', 'axis': 'b'}
        response = self.app.post(
            '/calibrate_plunger',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')
        saved_pos = self.robot._instruments['B'].positions['blow_out']
        self.assertEquals(saved_pos, 4)

        self.robot._instruments['B'].motor.move(5)
        arguments = {'position': 'drop_tip', 'axis': 'b'}
        response = self.app.post(
            '/calibrate_plunger',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')
        saved_pos = self.robot._instruments['B'].positions['drop_tip']
        self.assertEquals(saved_pos, 5)
<commit_msg>added test<commit_after>import unittest
import json
from unittest import mock
import os

from opentrons.robot import Robot


class CalibrationTestCase(unittest.TestCase):
    def setUp(self):
        from main import app
        self.app = app.test_client()

        self.data_path = os.path.join(
            os.path.dirname(__file__) + '/data/'
        )

        self.robot = Robot.get_instance()
        self.robot.connect()

    def test_move_to_slot(self):
        arguments = {
            'slot': 'A1'
        }
        response = self.app.post(
            '/move_to_slot',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        arguments = {
            'slot': 'A4'
        }
        response = self.app.post(
            '/move_to_slot',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'error')

    def test_aspirate_dispense(self):

        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.assertEquals(self.robot._instruments['B'].max_volume, 10)

        self.robot._instruments['B'].calibrate_plunger(
            top=0, bottom=5, blow_out=6, drop_tip=7)

        # moving the head down (as the user would have already done)
        # so limit switch isn't hit during this test
        self.robot.move_head(z=0)

        arguments = {
            'axis': 'b'
        }
        response = self.app.post(
            '/aspirate',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        arguments = {
            'axis': 'b'
        }
        response = self.app.post(
            '/dispense',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        arguments = {
            'axis': 'b',
            'volume': 12
        }
        response = self.app.post(
            '/set_max_volume',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.assertEquals(self.robot._instruments['B'].max_volume, 12)

    def test_move_to_plunger_position(self):

        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.robot._instruments['B'].motor.move(12)
        self.robot._instruments['B'].calibrate('bottom')
        self.robot._instruments['B'].motor.move(2)
        current_pos = self.robot._driver.get_plunger_positions()['target']
        self.assertEquals(current_pos['b'], 2)

        arguments = {
            'position': 'bottom',
            'axis': 'b'
        }
        response = self.app.post(
            '/move_to_plunger_position',
            data=json.dumps(dict(arguments)),
            content_type='application/json')
        self.assertEqual(status, 'success')
        current_pos = self.robot._driver.get_plunger_positions()['target']
        self.assertEquals(current_pos['b'], 12)

    def test_move_to_container(self):
        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.robot.move_to = mock.Mock()

        arguments = {
            'label': 'test-plate',
            'slot': 'B2',
            'axis': 'b'
        }

        response = self.app.post(
            '/move_to_container',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        container = self.robot._deck['B2'].get_child_by_name(
            'test-plate')
        instrument = self.robot._instruments['B']
        expected = [
            mock.call(
                container[0].bottom(),
                instrument=instrument,
                strategy='arc',
                enqueue=False)
        ]
        self.assertEquals(self.robot.move_to.mock_calls, expected)

    def test_calibrate_placeable(self):
        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        arguments = {
            'label': 'test-plate',
            'axis': 'b'
        }

        response = self.app.post(
            '/calibrate_placeable',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        actual = json.loads(response.data.decode())
        name = actual['data']['name']
        axis = actual['data']['axis']
        step_list = actual['data']['calibrations']
        status = actual['status']

        self.assertEqual(name, 'test-plate')
        self.assertEqual(axis, 'b')
        self.assertTrue(bool(step_list))
        self.assertEqual(status, 'success')

    def test_calibrate_plunger(self):
        response = self.app.post('/upload', data={
            'file': (open(self.data_path + 'protocol.py', 'rb'), 'protocol.py')
        })
        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')

        self.robot._instruments['B'].motor.move(2)
        arguments = {'position': 'top', 'axis': 'b'}
        response = self.app.post(
            '/calibrate_plunger',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')
        saved_pos = self.robot._instruments['B'].positions['top']
        self.assertEquals(saved_pos, 2)

        self.robot._instruments['B'].motor.move(3)
        arguments = {'position': 'bottom', 'axis': 'b'}
        response = self.app.post(
            '/calibrate_plunger',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')
        saved_pos = self.robot._instruments['B'].positions['bottom']
        self.assertEquals(saved_pos, 3)

        self.robot._instruments['B'].motor.move(4)
        arguments = {'position': 'blow_out', 'axis': 'b'}
        response = self.app.post(
            '/calibrate_plunger',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')
        saved_pos = self.robot._instruments['B'].positions['blow_out']
        self.assertEquals(saved_pos, 4)

        self.robot._instruments['B'].motor.move(5)
        arguments = {'position': 'drop_tip', 'axis': 'b'}
        response = self.app.post(
            '/calibrate_plunger',
            data=json.dumps(dict(arguments)),
            content_type='application/json')

        status = json.loads(response.data.decode())['status']
        self.assertEqual(status, 'success')
        saved_pos = self.robot._instruments['B'].positions['drop_tip']
        self.assertEquals(saved_pos, 5)

{'fraction_non_alphanumeric': 0.15149350649350649, 'fraction_numerical': 0.0037662337662337664, 'mean_word_length': 2.253274186734263, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7535858', 'n_tokens_mistral': 4581, 'n_tokens_neox': 4444, 'n_words': 677}