Best Python code snippet using lisa_python
test_testsuite.py
Source:test_testsuite.py  
...125def generate_cases_result() -> List[TestResult]:126    case_metadata = generate_cases_metadata()127    case_results = [TestResult("0", TestCaseRuntimeData(x)) for x in case_metadata]128    return case_results129def select_and_check(130    ut: TestCase, case_runbook: List[Any], expected_descriptions: List[str]131) -> List[TestCaseRuntimeData]:132    runbook = RunbookBuilder._validate_and_load({constants.TESTCASE: case_runbook})133    case_metadata = generate_cases_metadata()134    runbook.testcase = parse_testcase_filters(runbook.testcase_raw)135    filters = cast(List[schema.TestCase], runbook.testcase)136    selected = select_testcases(filters, case_metadata)137    ut.assertListEqual(expected_descriptions, [case.description for case in selected])138    return selected139class TestSuiteTestCase(TestCase):140    def generate_suite_instance(self) -> MockTestSuite:141        case_results = generate_cases_result()142        self.case_results = case_results[:2]143        suite_metadata = case_results[0].runtime_data.metadata.suite144        runbook = generate_runbook(is_single_env=True, local=True, remote=True)145        envs = load_environments(runbook)146        self.default_env = list(envs.values())[0]147        assert self.default_env148        test_suite = MockTestSuite(149            metadata=suite_metadata,150        )151        return test_suite152    def setUp(self) -> None:153        cleanup_cases_metadata()154    def test_expanded_nodespace(self) -> None:155        cases = generate_cases_metadata()156        for case in cases:157            self.assertIsNotNone(case.requirement)158            assert case.requirement.environment159            for node in case.requirement.environment.nodes:160                self.assertEqual(161                    1, node.node_count, "node count should be expanded to 1"162                )163    def test_case_override_suite(self) -> None:164        cases = generate_cases_metadata()165        case1_found = False166        case2_found = False167        for case in cases:168            assert case.requirement.environment169            assert case.suite.requirement.environment170            if case.name == "mock_ut1":171                self.assertEqual(2, len(case.requirement.environment.nodes))172                self.assertEqual(1, len(case.suite.requirement.environment.nodes))173                case1_found = True174            if case.name == "mock_ut2":175                self.assertEqual(1, len(case.requirement.environment.nodes))176                self.assertEqual(1, len(case.suite.requirement.environment.nodes))177                case2_found = True178        self.assertEqual(True, case1_found)179        self.assertEqual(True, case2_found)180    def test_test_result_canrun(self) -> None:181        runbook = [{constants.TESTCASE_CRITERIA: {"priority": [0, 1, 2]}}]182        cases = select_and_check(self, runbook, ["ut1", "ut2", "ut3"])183        case = cases[0]184        for status in TestStatus:185            result = TestResult("0", case)186            result.set_status(status, f"set_{status}")187            self.assertEqual(f"set_{status}", result.message)188            self.assertEqual(status, result.status)189            if status == TestStatus.QUEUED:190                self.assertEqual(True, result.is_queued)191            else:192                self.assertEqual(False, result.is_queued)193    def test_skip_before_suite_failed(self) -> None:194        test_suite = self.generate_suite_instance()195        test_suite.set_fail_phase(fail_on_before_suite=True)196        test_suite.start(...layout.py
Source:layout.py  
1from dash import html, dcc2import dash_bootstrap_components as dbc3import datetime4import tzlocal5import zoneinfo6from dotenv import load_dotenv7import os8from nightscout_dash.distribution_table import distribution_table_column_contents9load_dotenv()10def generate_ns_layout():11    default_spacing_class = "mb-3"12    select_and_check = [13        html.H2(children="Select data"),14        dbc.InputGroup(15            [16                dbc.InputGroupText("Time zone"),17                dbc.Select(18                    id="timezone-name",19                    options=[20                        {"label": zone_name, "value": zone_name}21                        for zone_name in zoneinfo.available_timezones()22                    ],23                    value=os.getenv(24                        "LOCALZONE_NAME",25                        default=tzlocal.get_localzone_name(),26                    ),27                ),28                dbc.Tooltip(29                    "Set a LOCALZONE_NAME environment variable to control the default value.",30                    target="timezone-name",31                ),32            ],33            className="mb-2",34        ),35        dbc.InputGroup(36            [37                dbc.InputGroupText("Date range"),38                dcc.DatePickerRange(39                    id="data-date-range",40                    max_date_allowed=datetime.date.today(),41                    end_date=datetime.date.today(),42                    start_date=datetime.date.today() - datetime.timedelta(days=7),43                    className="form-control",44                ),45            ],46            className="mb-2",47        ),48        dbc.InputGroup(49            [50                dbc.InputGroupText("Nightscout URL"),51                dcc.Input(52                    id="nightscout-url",53                    type="text",54                    value=os.getenv("NIGHTSCOUT_URL"),55                    className="form-control",56                ),57            ],58            className="mb-2",59            id="nightscout-url-input-group",60        ),61        dbc.Alert(62            children="Error loading data from Nightscout",63            color="danger",64            id="nightscout-error",65            is_open=False,66        ),67        dbc.Tooltip(68            "Set a NIGHTSCOUT_URL environment variable to control the default value.",69            target="nightscout-url-input-group",70        ),71        dbc.Row(72            [73                dbc.Button(74                    "Submit",75                    outline=True,76                    color="primary",77                    className="ms-auto w-auto",78                    id="submit-button",79                ),80            ]81        ),82        dbc.Spinner(83            dcc.Graph(84                id="loaded-data-graph",85                style={"height": "300px"},86            )87        ),88    ]89    ns_layout = html.Div(90        children=[91            html.H1(92                children="Nightscout data analysis", className=default_spacing_class93            ),94            dbc.Row(95                [96                    dbc.Col(97                        select_and_check,98                        width=5,99                        xs={"width": 6, "offset": 0},100                        lg={"width": 5, "offset": 0},101                        class_name="text-right",102                    ),103                    dbc.Col(104                        [105                            html.H2(children="About"),106                            html.Div(107                                children="""This Dash app displays custom plots to help understand Nightscout data about108                                blood sugar and treatments. It is currently a skeleton with ongoing work on109                                expanding plot types. Coming soon: """110                            ),111                            html.Ul(112                                children=[113                                    html.Li(114                                        "plots of the number of distinct lows over time"115                                    ),116                                    html.Li("BG percentiles by day"),117                                    html.Li(118                                        "annotations showing profile change timing"119                                    ),120                                ],121                                className=default_spacing_class,122                            ),123                            html.Div(124                                children="Data is loaded via the Nightscout API, not directly from the MongoDB. When "125                                "data for a given range is loaded, it is then stored so that if the range is "126                                "changed only data not previously requested is loaded.",127                                className=default_spacing_class,128                            ),129                            html.Div(130                                children="The next priority is to implement a simple tool for easily adding "131                                "annotations - e.g. 'forgot to dose' or 'probably underestimated carbs' or "132                                "'pressure low' - as well as special event types like 'exclude this range' "133                                "to more flexibly focus on 'good' data in analysis.",134                                className=default_spacing_class,135                            ),136                        ],137                        width=3,138                        xs={"width": 5, "offset": 0},139                        lg={"width": 3, "offset": 3},140                        class_name="mr-3",141                    ),142                ],143                class_name=default_spacing_class,144            ),145            dbc.Row(146                [147                    html.H2(148                        id="subset-data-header",149                        style={"text-align": "center"},150                    ),151                ]152            ),153            dbc.Row(154                [155                    dbc.Col(156                        distribution_table_column_contents,157                        width=6,158                    ),159                    dbc.Col(160                        [161                            html.H3(children="Time in range per day"),162                            dbc.Spinner(163                                dcc.Graph(164                                    id="range-fraction-by-day-graph",165                                )166                            ),167                        ],168                        width=6,169                    ),170                ],171            ),172            dbc.Row(173                [174                    dbc.Col(175                        [176                            html.H3(children="Basal rates"),177                            dbc.Switch(178                                id="basal-rate-includes-scheduled",179                                label="Include regularly-scheduled basals (when Control IQ was not active or we don't have data)",180                                value=False,181                            ),182                            dbc.Spinner(183                                dcc.Graph(184                                    id="basal-rate-graph",185                                )186                            ),187                        ],188                        width=6,189                    ),190                    dbc.Col(191                        [192                            html.H3(children="Site change impact"),193                            dbc.InputGroup(194                                [195                                    dbc.InputGroupText("Hours to bin together: "),196                                    dbc.Input(197                                        type="number",198                                        min=1,199                                        max=24,200                                        step=1,201                                        value=6,202                                        id="site-change-graph-bin-hours",203                                    ),204                                ],205                            ),206                            dbc.RadioItems(207                                options=[208                                    {209                                        "label": "Plot over entire site change",210                                        "value": 1,211                                    },212                                    {"label": "Plot over time of day", "value": 2},213                                ],214                                value=1,215                                id="site-change-graph-style",216                                inline=True,217                            ),218                            dbc.Spinner(219                                dcc.Graph(220                                    id="site-change-graph",221                                )222                            ),223                        ],224                        width=6,225                    ),226                ],227            ),228            dcc.Store(id="all-bg-data"),229            dcc.Store(id="subset-bg-data"),230            dcc.Store(id="first-load-dummy"),231            dcc.Store(id="already-loaded-dates"),232            dcc.Store(id="profile-data"),233            dcc.Store(id="loaded-nightscout-url"),234        ]235    )...test_testselector.py
Source:test_testselector.py  
...7    def setUp(self) -> None:8        cleanup_cases_metadata()9    def test_no_case_selected(self) -> None:10        runbook = [{constants.TESTCASE_CRITERIA: {"area": "demo"}}]11        select_and_check(self, runbook, [])12    def test_select_by_priority(self) -> None:13        runbook = [{constants.TESTCASE_CRITERIA: {"priority": 0}}]14        select_and_check(self, runbook, ["ut1"])15    def test_select_by_tag(self) -> None:16        runbook = [{constants.TESTCASE_CRITERIA: {"tags": "t1"}}]17        select_and_check(self, runbook, ["ut1", "ut2"])18    def test_select_by_one_of_tag(self) -> None:19        runbook = [{constants.TESTCASE_CRITERIA: {"tags": ["t1", "t3"]}}]20        select_and_check(self, runbook, ["ut1", "ut2", "ut3"])21    def test_select_by_two_rules(self) -> None:22        runbook = [{constants.TESTCASE_CRITERIA: {"tags": ["t1", "t3"], "area": "a1"}}]23        select_and_check(self, runbook, ["ut1", "ut2"])24    def test_select_by_two_criteria(self) -> None:25        runbook = [26            {constants.TESTCASE_CRITERIA: {"name": "mock_ut1"}},27            {constants.TESTCASE_CRITERIA: {"name": "mock_ut2"}},28        ]29        select_and_check(self, runbook, ["ut1", "ut2"])30    def test_select_then_drop(self) -> None:31        runbook = [32            {constants.TESTCASE_CRITERIA: {"tags": "t1"}},33            {34                constants.TESTCASE_CRITERIA: {"name": "mock_ut2"},35                constants.TESTCASE_SELECT_ACTION: "exclude",36            },37        ]38        select_and_check(self, runbook, ["ut1"])39    def test_select_drop_select(self) -> None:40        runbook = [41            {constants.TESTCASE_CRITERIA: {"tags": "t1"}},42            {43                constants.TESTCASE_CRITERIA: {"name": "mock_ut2"},44                constants.TESTCASE_SELECT_ACTION: "exclude",45            },46            {constants.TESTCASE_CRITERIA: {"tags": "t1"}},47        ]48        select_and_check(self, runbook, ["ut1", "ut2"])49    def test_select_force_include(self) -> None:50        runbook = [51            {52                constants.TESTCASE_CRITERIA: {"tags": "t1"},53                constants.TESTCASE_SELECT_ACTION: "forceInclude",54            },55            {56                constants.TESTCASE_CRITERIA: {"name": "mock_ut2"},57                constants.TESTCASE_SELECT_ACTION: "exclude",58            },59        ]60        select_and_check(self, runbook, ["ut1", "ut2"])61    def test_select_force_conflict(self) -> None:62        runbook = [63            {64                constants.TESTCASE_CRITERIA: {"tags": "t1"},65                constants.TESTCASE_SELECT_ACTION: "forceInclude",66            },67            {68                constants.TESTCASE_CRITERIA: {"name": "mock_ut2"},69                constants.TESTCASE_SELECT_ACTION: "forceExclude",70            },71        ]72        with self.assertRaises(LisaException) as cm:73            select_and_check(self, runbook, ["ut1", "ut2"])74        self.assertIsInstance(cm.exception, LisaException)75        self.assertIn("force", str(cm.exception))76    def test_select_force_conflict_exclude(self) -> None:77        runbook = [78            {79                constants.TESTCASE_CRITERIA: {"tags": "t1"},80                constants.TESTCASE_SELECT_ACTION: "include",81            },82            {83                constants.TESTCASE_CRITERIA: {"name": "mock_ut2"},84                constants.TESTCASE_SELECT_ACTION: "forceExclude",85            },86            {87                constants.TESTCASE_CRITERIA: {"tags": "t1"},88                constants.TESTCASE_SELECT_ACTION: "forceInclude",89            },90        ]91        with self.assertRaises(LisaException) as cm:92            select_and_check(self, runbook, [])93            self.assertIsInstance(cm.exception, LisaException)94            self.assertIn("force", str(cm.exception))95    def test_select_with_setting(self) -> None:96        runbook = [97            {constants.TESTCASE_CRITERIA: {"tags": "t1"}, "retry": 2},98        ]99        selected = select_and_check(self, runbook, ["ut1", "ut2"])100        self.assertListEqual([2, 2], [case.retry for case in selected])101    def test_select_with_times(self) -> None:102        runbook = [103            {constants.TESTCASE_CRITERIA: {"tags": "t1"}},104            {105                constants.TESTCASE_CRITERIA: {"name": "mock_ut2"},106                "times": 2,107                constants.TESTCASE_SELECT_ACTION: "none",108            },109        ]110        selected = select_and_check(self, runbook, ["ut1", "ut2", "ut2"])111        self.assertListEqual([1, 2, 2], [case.times for case in selected])112        self.assertListEqual([0, 0, 0], [case.retry for case in selected])113    def test_select_with_setting_none(self) -> None:114        runbook = [115            {constants.TESTCASE_CRITERIA: {"tags": "t1"}},116            {117                constants.TESTCASE_CRITERIA: {"name": "mock_ut2"},118                "retry": 2,119                constants.TESTCASE_SELECT_ACTION: "none",120            },121        ]122        selected = select_and_check(self, runbook, ["ut1", "ut2"])123        self.assertListEqual([0, 2], [case.retry for case in selected])124    def test_select_with_diff_setting(self) -> None:125        runbook = [126            {constants.TESTCASE_CRITERIA: {"tags": "t1"}, "retry": 2},127            {constants.TESTCASE_CRITERIA: {"name": "mock_ut2"}, "retry": 3},128        ]129        selected = select_and_check(self, runbook, ["ut1", "ut2"])...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!!
