Best Python code snippet using green
tools.py
Source:tools.py
...94 y = self.interact.selected_y95 roi = RectangularROI(xmin=min(x), xmax=max(x), ymin=min(y), ymax=max(y))96 self.viewer.apply_roi(roi)97 if self.finalize_callback is not None:98 self.finalize_callback()99 def update_from_roi(self, roi):100 with self.viewer._output_widget:101 if isinstance(roi, RectangularROI):102 self.interact.selected_x = [roi.xmin, roi.xmax]103 self.interact.selected_y = [roi.ymin, roi.ymax]104 elif isinstance(roi, PolygonalROI):105 self.interact.selected_x = [np.min(roi.vx), np.max(roi.vx)]106 self.interact.selected_y = [np.min(roi.vy), np.max(roi.vy)]107 else:108 raise TypeError(f'Cannot initialize a BqplotRectangleMode from a {type(roi)}')109 # FIXME: the brush selector does not actually update unless the110 # widget is resized/refreshed, see111 # https://github.com/bloomberg/bqplot/issues/1067112 def on_selection_change(self, *args):113 if self.interact.selected_x is None or self.interact.selected_y is None:114 if self.finalize_callback is not None:115 self.finalize_callback()116 def activate(self):117 with self.viewer._output_widget:118 self.interact.selected_x = None119 self.interact.selected_y = None120 super().activate()121@viewer_tool122class BqplotCircleMode(BqplotSelectionTool):123 icon = 'glue_circle'124 tool_id = 'bqplot:circle'125 action_text = 'Circular ROI'126 tool_tip = 'Define a circular region of interest'127 def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):128 super().__init__(viewer, **kwargs)129 self.interact = BrushEllipseSelector(x_scale=self.viewer.scale_x,130 y_scale=self.viewer.scale_y,131 pixel_aspect=1)132 # Workaround for bug that causes the `color` trait to not be recognized133 style = self.interact.style.copy()134 style['fill'] = INTERACT_COLOR135 border_style = self.interact.border_style.copy()136 border_style['fill'] = INTERACT_COLOR137 border_style['stroke'] = INTERACT_COLOR138 self.interact.style = style139 self.interact.border_style = border_style140 if roi is not None:141 self.update_from_roi(roi)142 self.interact.observe(self.update_selection, "brushing")143 self.interact.observe(self.on_selection_change, "selected_x")144 self.interact.observe(self.on_selection_change, "selected_y")145 self.finalize_callback = finalize_callback146 def update_selection(self, *args):147 if self.interact.brushing:148 return149 with self.viewer._output_widget:150 if self.interact.selected_x is not None and self.interact.selected_y is not None:151 x = self.interact.selected_x152 y = self.interact.selected_y153 # similar to https://github.com/glue-viz/glue/blob/b14ccffac6a5154 # 271c2869ead9a562a2e66232e397/glue/core/roi.py#L1275-L1297155 # We should now check if the radius in data coordinates is the same156 # along x and y, as if so then we can return a circle, otherwise we157 # should return an ellipse.158 xc = x.mean()159 yc = y.mean()160 rx = abs(x[1] - x[0])/2161 ry = abs(y[1] - y[0])/2162 # We use a tolerance of 1e-2 below to match the tolerance set in glue-core163 # https://github.com/glue-viz/glue/blob/6b968b352bc5ad68b95ad5e3bb25550782a69ee8/glue/viewers/matplotlib/state.py#L198164 if np.allclose(rx, ry, rtol=1e-2):165 roi = CircularROI(xc=xc, yc=yc, radius=rx)166 else:167 roi = EllipticalROI(xc=xc, yc=yc, radius_x=rx, radius_y=ry)168 self.viewer.apply_roi(roi)169 if self.finalize_callback is not None:170 self.finalize_callback()171 def update_from_roi(self, roi):172 if isinstance(roi, CircularROI):173 rx = ry = roi.radius174 elif isinstance(roi, EllipticalROI):175 rx, ry = roi.radius_x, roi.radius_y176 else:177 raise TypeError(f'Cannot initialize a BqplotCircleMode from a {type(roi)}')178 self.interact.selected_x = [roi.xc - rx, roi.xc + rx]179 self.interact.selected_y = [roi.yc - ry, roi.yc + ry]180 def on_selection_change(self, *args):181 if self.interact.selected_x is None or self.interact.selected_y is None:182 if self.finalize_callback is not None:183 self.finalize_callback()184 def activate(self):185 with self.viewer._output_widget:186 self.interact.selected_x = None187 self.interact.selected_y = None188 super().activate()189@viewer_tool190class BqplotEllipseMode(BqplotCircleMode):191 icon = os.path.join(ICONS_DIR, 'glue_ellipse.svg')192 tool_id = 'bqplot:ellipse'193 action_text = 'Elliptical ROI'194 tool_tip = 'Define an elliptical region of interest'195 def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):196 super().__init__(viewer, **kwargs)197 self.interact = BrushEllipseSelector(x_scale=self.viewer.scale_x,...
test_ingredient_widget.py
Source:test_ingredient_widget.py
1""" Tests for the ingredient widget. """2from PySide2.QtWidgets import QLineEdit, QPushButton3from PySide2.QtCore import Qt4from PySide2.QtTest import QTest5from nutrition.recipe_builder_widget.widgets.ingredient import IngredientWidget6from tests.helpers import UsesQApplication, Callback, empty_callback7class TestIngredientWidget(UsesQApplication):8 """ Tests for the recipe name widget. """9 # This is a test, access to the private members is ok.10 # pylint: disable=protected-access11 PRODUCT_NAMES = ["test1", "test2", "test3"]12 def test_ingredient_layout(self):13 """ Tests the widget layout. """14 widget = IngredientWidget(self.PRODUCT_NAMES, empty_callback, empty_callback)15 self.assertTrue(hasattr(widget, "_ingredient_line_edit"))16 self.assertTrue(isinstance(widget._ingredient_line_edit, QLineEdit))17 self.assertTrue(hasattr(widget, "_ingredient_mass_line_edit"))18 self.assertTrue(isinstance(widget._ingredient_mass_line_edit, QLineEdit))19 self.assertTrue(hasattr(widget, "_ingredient_add_button"))20 self.assertTrue(isinstance(widget._ingredient_add_button, QPushButton))21 def test_ingredient_entered(self):22 """ Tests that after entering ingredient name the first callback is called. """23 input_data = self.PRODUCT_NAMES[0]24 default_mass = 10025 ingredient_entered_callback = Callback((input_data, default_mass))26 finalize_callback = Callback()27 widget = IngredientWidget(self.PRODUCT_NAMES, ingredient_entered_callback.callback, finalize_callback.callback)28 for char in input_data:29 QTest.keyPress(widget._ingredient_line_edit, char)30 QTest.keyRelease(widget._ingredient_line_edit, char)31 QTest.keyPress(widget._ingredient_line_edit, Qt.Key.Key_Enter)32 QTest.keyRelease(widget._ingredient_line_edit, Qt.Key.Key_Enter)33 self.assertTrue(ingredient_entered_callback.called)34 self.assertFalse(finalize_callback.called)35 def test_ingredient_mass_entered(self):36 """ Tests that after entering ingredient mass the first callback is called. """37 input_data = "259"38 ingredient_entered_callback = Callback(("", int(input_data)))39 finalize_callback = Callback()40 widget = IngredientWidget(self.PRODUCT_NAMES, ingredient_entered_callback.callback, finalize_callback.callback)41 for char in input_data:42 QTest.keyPress(widget._ingredient_mass_line_edit, char)43 QTest.keyRelease(widget._ingredient_mass_line_edit, char)44 QTest.keyPress(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter)45 QTest.keyRelease(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter)46 self.assertTrue(ingredient_entered_callback.called)47 self.assertFalse(finalize_callback.called)48 def test_ingredient_name_and_mass_entered(self):49 """ Tests that after entering ingredient name and mass the first callback is called. """50 input_data_name = self.PRODUCT_NAMES[1]51 input_data_mass = "331"52 default_mass = 10053 # After the first call (after product name was entered) the mass will be equal to default_mass.54 ingredient_entered_callback = Callback((input_data_name, default_mass))55 finalize_callback = Callback()56 widget = IngredientWidget(self.PRODUCT_NAMES, ingredient_entered_callback.callback, finalize_callback.callback)57 for char in input_data_name:58 QTest.keyPress(widget._ingredient_line_edit, char)59 QTest.keyRelease(widget._ingredient_line_edit, char)60 QTest.keyPress(widget._ingredient_line_edit, Qt.Key.Key_Enter)61 QTest.keyRelease(widget._ingredient_line_edit, Qt.Key.Key_Enter)62 # Refresh the callback.63 ingredient_entered_callback.called = False64 ingredient_entered_callback.expected_args = (input_data_name, int(input_data_mass))65 for char in input_data_mass:66 QTest.keyPress(widget._ingredient_mass_line_edit, char)67 QTest.keyRelease(widget._ingredient_mass_line_edit, char)68 QTest.keyPress(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter)69 QTest.keyRelease(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter)70 self.assertTrue(ingredient_entered_callback.called)71 self.assertFalse(finalize_callback.called)72 def test_ingredient_name_and_mass_finalize(self):73 """ Tests that after clicking the finalize button the second callback is called. """74 input_data_name = self.PRODUCT_NAMES[1]75 input_data_mass = "331"76 # After the first call (after product name was entered) the mass will be equal to 100.77 ingredient_entered_callback = Callback((input_data_name, 100))78 finalize_callback = Callback((input_data_name, int(input_data_mass)))79 widget = IngredientWidget(self.PRODUCT_NAMES, ingredient_entered_callback.callback, finalize_callback.callback)80 for char in input_data_name:81 QTest.keyPress(widget._ingredient_line_edit, char)82 QTest.keyRelease(widget._ingredient_line_edit, char)83 QTest.keyPress(widget._ingredient_line_edit, Qt.Key.Key_Enter)84 QTest.keyRelease(widget._ingredient_line_edit, Qt.Key.Key_Enter)85 # Refresh the callback.86 ingredient_entered_callback.called = False87 ingredient_entered_callback.expected_args = (input_data_name, int(input_data_mass))88 for char in input_data_mass:89 QTest.keyPress(widget._ingredient_mass_line_edit, char)90 QTest.keyRelease(widget._ingredient_mass_line_edit, char)91 QTest.keyPress(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter)92 QTest.keyRelease(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter)93 QTest.mouseClick(widget._ingredient_add_button, Qt.MouseButton.LeftButton)...
test_server.py
Source:test_server.py
1import sys2import unittest3from pymargo.core import Engine4class TestInitEngine(unittest.TestCase):5 def test_init_engine(self):6 engine = Engine('tcp')7 self.assertIsInstance(engine, Engine)8 engine.finalize()9 def test_init_engine_fail(self):10 with self.assertRaises(RuntimeError):11 engine = Engine('abc')12 class FinalizeCallback():13 def __init__(self):14 self.finalize_was_called = False15 def __call__(self):16 self.finalize_was_called = True17 def test_on_finalize(self):18 finalize_callback = TestInitEngine.FinalizeCallback()19 self.assertFalse(finalize_callback.finalize_was_called)20 engine = Engine('tcp')21 engine.on_finalize(finalize_callback)22 self.assertFalse(finalize_callback.finalize_was_called)23 engine.finalize()24 self.assertTrue(finalize_callback.finalize_was_called)25 26if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!