Best Python code snippet using lemoncheesecake
test_adapter.py
Source:test_adapter.py  
1from typing import Generator2from .conftest import *3@pytest.fixture()4def subject_adapter(mock_api_client, stats_fixture):5    subject = TestRailAPIAdapter(api_client=mock_api_client, stats=stats_fixture)6    return subject7def test_get_project_returns_correct_instance_type(subject_adapter, mock_api_client):8    project_id = FIELD("integer_number", start=1, end=1000)9    actual = subject_adapter.get_project(project_id=project_id)10    mock_api_client.projects.get_project.assert_called_once_with(project_id=project_id)11    assert isinstance(actual, TestRailProject)12    assert actual.announcement is not None13    assert subject_adapter.stats.get_project == 114    assert subject_adapter.stats.total == 115def test_get_projects_returns_generator_of_project_instances(subject_adapter, mock_api_client):16    actual = subject_adapter.get_projects(limit=1)17    assert isinstance(actual, Generator)18    for project in actual:19        assert isinstance(project, TestRailProject)20        assert project.announcement is not None21    expected_calls = [call(limit=1, offset=i) for i in range(4)]22    mock_api_client.projects.get_projects.assert_has_calls(calls=expected_calls, any_order=False)23    assert subject_adapter.stats.get_projects == 424    assert subject_adapter.stats.total == 425def test_get_suite_returns_correct_instance_type(subject_adapter, mock_api_client):26    suite_id = FIELD("integer_number", start=1, end=1000)27    actual = subject_adapter.get_suite(suite_id=suite_id)28    mock_api_client.suites.get_suite.assert_called_once_with(suite_id=suite_id)29    assert isinstance(actual, TestRailSuite)30    assert actual.description is not None31    assert subject_adapter.stats.get_suite == 132    assert subject_adapter.stats.total == 133def test_get_suites_returns_list_of_suite_instances(subject_adapter, mock_api_client):34    project_id = FIELD("integer_number", start=1, end=1000)35    actual = subject_adapter.get_suites(project_id=project_id)36    assert len(actual) > 037    for suite in actual:38        assert isinstance(suite, TestRailSuite)39        assert suite.description is not None40    mock_api_client.suites.get_suites.assert_called_once_with(project_id=project_id)41    assert subject_adapter.stats.get_suites == 142    assert subject_adapter.stats.total == 143def test_get_sections_returns_correct_instance_type(subject_adapter, mock_api_client):44    section_id = FIELD("integer_number", start=1, end=1000)45    actual = subject_adapter.get_section(section_id=section_id)46    mock_api_client.sections.get_section.assert_called_once_with(section_id=section_id)47    assert isinstance(actual, TestRailSection)48    assert actual.description is not None49    assert subject_adapter.stats.get_section == 150    assert subject_adapter.stats.total == 151def test_get_section_returns_generator_of_sections(subject_adapter, mock_api_client):52    project_id = FIELD("integer_number", start=1, end=1000)53    suite_id = FIELD("integer_number", start=1, end=1000)54    actual = subject_adapter.get_sections(project_id=project_id, suite_id=suite_id, limit=1, offset=0)55    for section in actual:56        assert isinstance(section, TestRailSection)57        assert section.description is not None58    expected_calls = [call(limit=1, offset=i, project_id=project_id, suite_id=suite_id) for i in range(4)]59    mock_api_client.sections.get_sections.assert_has_calls(calls=expected_calls, any_order=False)60    assert subject_adapter.stats.total == 461    assert subject_adapter.stats.get_sections == 462def test_add_section_returns_correct_instance_type(subject_adapter, mock_api_client):63    project_id = FIELD("integer_number", start=1, end=1000)64    suite_id = FIELD("integer_number", start=1, end=1000)65    name = FIELD("word")66    description = FIELD("text")67    parent_id = FIELD("integer_number", start=1, end=1000)68    actual = subject_adapter.add_section(69        project_id=project_id,70        suite_id=suite_id,71        name=name,72        description=description,73        parent_id=parent_id74    )75    mock_api_client.sections.add_section.assert_called_once_with(76        project_id=project_id,77        suite_id=suite_id,78        name=name,79        description=description,80        parent_id=parent_id81    )82    assert isinstance(actual, TestRailSection)83    assert actual.description is not None84    assert subject_adapter.stats.add_section == 185    assert subject_adapter.stats.total == 186def test_get_case_returns_correct_instance_type(subject_adapter, mock_api_client):87    case_id = FIELD("integer_number", start=1, end=1000)88    actual = subject_adapter.get_case(case_id=case_id)89    assert isinstance(actual, TestRailCase)90    assert len(actual.steps_separated) == 191    assert actual.steps_separated[0]["content"] is not None92    assert subject_adapter.stats.get_case == 193    assert subject_adapter.stats.total == 194    mock_api_client.cases.get_case.assert_called_once_with(case_id=case_id)95def test_add_case_returns_correct_instance_type(subject_adapter, mock_api_client):96    section_id = FIELD("integer_number", start=1, end=1000)97    title = FIELD("title")98    actual = subject_adapter.add_case(section_id=section_id, title=title)99    assert isinstance(actual, TestRailCase)100    assert len(actual.steps_separated) == 1101    assert actual.steps_separated[0]["content"] is not None102    assert subject_adapter.stats.add_case == 1103    assert subject_adapter.stats.total == 1104    mock_api_client.cases.add_case.assert_called_once_with(section_id=section_id, title=title)105def test_update_case_returns_correct_instance_type(subject_adapter, mock_api_client):106    case_id = FIELD("integer_number", start=1, end=1000)107    section_id = FIELD("integer_number", start=1, end=1000)108    title = FIELD("title")109    start_case = subject_adapter.get_case(case_id=case_id)110    actual = subject_adapter.update_case(case=start_case, title=title, section_id=section_id)111    assert isinstance(actual, TestRailCase)112    assert len(actual.steps_separated) == 1113    assert actual.steps_separated[0]["content"] is not None114    assert subject_adapter.stats.update_case == 1115    assert subject_adapter.stats.total == 2116    mock_api_client.cases.update_case.assert_called_once_with(117        case_id=start_case.case_id,118        title=title,119        section_id=section_id120    )121def test_get_cases_returns_generator_of_cases(subject_adapter, mock_api_client):122    project_id = FIELD("integer_number", start=1, end=1000)123    actual = subject_adapter.get_cases(project_id=project_id, limit=2, offset=0)124    assert isinstance(actual, Generator)125    for case in actual:126        assert isinstance(case, TestRailCase)127        assert len(case.steps_separated) == 1128        assert case.steps_separated[0]["content"] is not None129    expected_calls = [call(project_id=project_id, limit=2, offset=i) for i in range(0, 4, 2)]130    mock_api_client.cases.get_cases.assert_has_calls(expected_calls, any_order=False)131    assert subject_adapter.stats.get_cases == 2132    assert subject_adapter.stats.total == 2133def test_get_cases_with_suite_returns_generator_of_cases(subject_adapter, mock_api_client):134    project_id = FIELD("integer_number", start=1, end=1000)135    suite_id = FIELD("integer_number", start=1, end=1000)136    actual = subject_adapter.get_cases(project_id=project_id, suite_id=suite_id, limit=2, offset=0)137    assert isinstance(actual, Generator)138    for case in actual:139        assert isinstance(case, TestRailCase)140        assert len(case.steps_separated) == 1141        assert case.steps_separated[0]["content"] is not None142    expected_calls = [call(project_id=project_id, suite_id=suite_id, limit=2, offset=i) for i in range(0, 4, 2)]143    mock_api_client.cases.get_cases.assert_has_calls(expected_calls, any_order=False)144    assert subject_adapter.stats.get_cases == 2145    assert subject_adapter.stats.total == 2146def test_get_case_fields_returns_correct_instance_type(subject_adapter, mock_api_client):147    actual = subject_adapter.get_case_fields()148    assert len(actual) > 0149    for field_item in actual:150        assert isinstance(field_item, TestRailCaseField)151        assert field_item.description is not None152    assert subject_adapter.stats.get_case_fields == 1153    assert subject_adapter.stats.total == 1154def test_get_case_types_returns_correct_instance_type(subject_adapter, mock_api_client):155    actual = subject_adapter.get_case_types()156    assert len(actual) == 2157    assert isinstance(actual, list)158    for case_type in actual:159        assert isinstance(case_type, TestRailCaseType)160    assert subject_adapter.stats.get_case_types == 1161    assert subject_adapter.stats.total == 1162@pytest.mark.parametrize("soft", [True, False])163def test_delete_section_deletes_a_single_section(subject_adapter, mock_api_client, suite_fixture, soft):164    section = list(suite_fixture.sections.values())[0]165    subject_adapter.delete_section(section=section, soft=soft)166    mock_api_client.sections.delete_section.assert_called_once_with(section_id=section.section_id, soft=int(soft))167    assert subject_adapter.stats.delete_section == 1168    assert subject_adapter.stats.total == 1169    if soft:170        assert section.section_id in suite_fixture.sections171    else:172        assert section.section_id not in suite_fixture.sections173@pytest.mark.parametrize("soft", [True, False])174def test_delete_case_deletes_a_single_case(subject_adapter, mock_api_client, suite_fixture, soft):175    case = list(suite_fixture.cases.values())[0]176    subject_adapter.delete_case(case=case, soft=soft)177    mock_api_client.cases.delete_case.assert_called_once_with(case_id=case.case_id, soft=int(soft))178    assert subject_adapter.stats.delete_case == 1179    assert subject_adapter.stats.total == 1180    if soft:181        assert case.case_id in suite_fixture.cases182    else:183        assert case.case_id not in suite_fixture.cases184def test_move_section_moves_a_single_section_to_top(subject_adapter, mock_api_client, suite_fixture):185    new_parent = None186    top_section = list(suite_fixture.direct_sections.values())[0]187    bottom_section = list(top_section.sections.values())[0]188    assert bottom_section.parent is not None189    subject_adapter.move_section(section=bottom_section, new_parent=new_parent)190    assert bottom_section.parent is None191    mock_api_client.sections.move_section.assert_called_once_with(section_id=bottom_section.section_id, parent_id=None)192def test_move_section_moves_a_single_section_to_new_section(subject_adapter, mock_api_client, suite_fixture):193    top_section = list(suite_fixture.direct_sections.values())[0]194    bottom_section = list(top_section.sections.values())[0]195    new_parent = TestRailSection.from_data(get_section_response())196    new_parent.suite_id = suite_fixture.suite_id197    new_parent.link(suite_fixture)198    assert bottom_section.parent is top_section199    subject_adapter.move_section(section=bottom_section, new_parent=new_parent)200    assert bottom_section.parent is not top_section201    assert bottom_section.parent is new_parent202    mock_api_client.sections.move_section.assert_called_once_with(section_id=bottom_section.section_id,203                                                                  parent_id=new_parent.section_id)204def test_create_new_section_for_path(subject_adapter):205    suite = TestRailSuite.from_data(get_suite_response())206    new_section = TestRailSection.from_data(get_section_response())207    parent_section = TestRailSection.from_data(get_section_response())208    # Set up ID (foreign key) links without explicitly linking objects209    new_section.suite_id = suite.suite_id210    parent_section.suite_id = suite.suite_id211    new_section.parent_id = parent_section.section_id212    with patch.object(suite, 'section_for_path', return_value=parent_section) as section_for_path_mock:213        with patch.object(subject_adapter, 'add_section', return_value=new_section) as add_section_patch:214            actual = subject_adapter.create_new_section_for_path(suite=suite, path='path/to/new/section')215    assert isinstance(actual, TestRailSection)216    assert actual is new_section217    assert actual.suite is suite218    assert actual.parent is not None219    assert actual.parent is parent_section220    add_section_patch.assert_called_once_with(221        name="section",222        project_id=suite.project_id,223        suite_id=suite.suite_id,224        parent_id=parent_section.section_id225    )...test_deletion.py
Source:test_deletion.py  
1from typing import Generator2from testrail_data_model.deletion import DeletionHandler, MarkForDeletionHandler, deletion_handler_factory3from .conftest import *4@pytest.fixture()5def mock_adapter():6    return MagicMock()7@pytest.fixture8def suite_fixture_with_deletion_marking(suite_fixture):9    to_be_deleted = TestRailSection.from_data(get_section_response())10    to_be_deleted.name = "__to_be_deleted__"11    to_be_deleted.suite_id = suite_fixture.suite_id12    to_be_deleted.parent_id = None13    to_be_deleted_sections = TestRailSection.from_data(get_section_response())14    to_be_deleted_sections.name = "sections"15    to_be_deleted_sections.suite_id = suite_fixture.suite_id16    to_be_deleted_sections.parent_id = to_be_deleted.section_id17    to_be_deleted.link(suite=suite_fixture, parent=None)18    to_be_deleted_sections.link(suite=suite_fixture, parent=to_be_deleted)19    return suite_fixture20@pytest.mark.parametrize("soft", [True, False])21def test_handler_deletes_a_single_case(mock_adapter, suite_fixture, soft):22    subject = DeletionHandler(suite=suite_fixture, adapter=mock_adapter, soft=soft)23    case = list(suite_fixture.cases.values())[0]24    subject.delete_case(case=case)25    mock_adapter.delete_case.assert_called_once_with(case=case, soft=soft)26@pytest.mark.parametrize("soft", [True, False])27def test_handler_deletes_a_single_section(mock_adapter, suite_fixture, soft):28    subject = DeletionHandler(suite=suite_fixture, adapter=mock_adapter, soft=soft)29    section = list(suite_fixture.sections.values())[0]30    subject.delete_section(section=section)31    mock_adapter.delete_section.assert_called_once_with(section=section, soft=soft)32@pytest.mark.parametrize("soft", [True, False])33def test_handler_deletes_multiple_cases(mock_adapter, suite_fixture, soft):34    subject = DeletionHandler(suite=suite_fixture, adapter=mock_adapter, soft=soft)35    cases_to_delete = list(suite_fixture.cases.values())36    actual = subject.delete_cases(cases_to_delete)37    assert isinstance(actual, Generator)38    for _ in actual:39        continue40    expected_calls = [call(case=case, soft=soft) for case in cases_to_delete]41    mock_adapter.delete_case.assert_has_calls(calls=expected_calls, any_order=False)42@pytest.mark.parametrize("soft", [True, False])43def test_handler_deletes_multiple_sections(mock_adapter, suite_fixture, soft):44    subject = DeletionHandler(suite=suite_fixture, adapter=mock_adapter, soft=soft)45    sections_to_delete = list(suite_fixture.sections.values())46    actual = subject.delete_sections(sections_to_delete)47    assert isinstance(actual, Generator)48    for _ in actual:49        continue50    expected_calls = [call(section=section, soft=soft) for section in sections_to_delete]51    mock_adapter.delete_section.assert_has_calls(calls=expected_calls, any_order=False)52def test_mark_case_for_deletion_handles_single_case(mock_adapter, suite_fixture_with_deletion_marking):53    suite = suite_fixture_with_deletion_marking54    new_case = TestRailCase.from_data(get_case_response())55    new_case.suite_id = suite.suite_id56    subject = MarkForDeletionHandler(suite=suite, adapter=mock_adapter)57    new_section_id = subject.to_be_deleted_sections[0].section_id58    new_case.section_id = new_section_id59    case = list(suite.cases.values())[0]60    mock_adapter.update_case = MagicMock(return_value=new_case)61    actual = subject.delete_case(case=case)62    assert actual is new_case63    mock_adapter.update_case.assert_called_once_with(case=case, section_id=new_section_id)64def test_mark_section_for_deletion_handles_single_section(mock_adapter, suite_fixture_with_deletion_marking):65    suite = suite_fixture_with_deletion_marking66    subject = MarkForDeletionHandler(suite=suite, adapter=mock_adapter)67    _, new_parent = subject.to_be_deleted_sections68    section = None69    for _section in suite.direct_sections.values():70        if not _section.path.startswith('__to_be_deleted__'):71            section = _section72            break73    assert section is not None74    mock_adapter.move_section = MagicMock(return_value=section)75    actual = subject.delete_section(section=section)76    assert actual is section77    mock_adapter.move_section.assert_called_once_with(section=section, new_parent=new_parent)78def test_mark_cases_for_deletion_handles_multiple_cases(mock_adapter, suite_fixture_with_deletion_marking):79    suite = suite_fixture_with_deletion_marking80    already_marked = TestRailCase.from_data(get_case_response())81    already_marked.suite_id = suite.suite_id82    new_case = TestRailCase.from_data(get_case_response())83    new_case.suite_id = suite.suite_id84    subject = MarkForDeletionHandler(suite=suite, adapter=mock_adapter)85    to_be_deleted_section = subject.to_be_deleted_sections[0]86    new_section_id = to_be_deleted_section.section_id87    already_marked.section_id = new_section_id88    new_case.section_id = new_section_id89    already_marked.link(suite=suite, section=to_be_deleted_section)90    cases_to_delete = [list(suite.cases.values())[0], already_marked]91    mock_adapter.update_case = MagicMock(return_value=new_case)92    actual = subject.delete_cases(cases_to_delete)93    assert isinstance(actual, Generator)94    results = [res for res in actual]95    mock_adapter.update_case.assert_called_once_with(case=cases_to_delete[0], section_id=new_section_id)96    assert len(results) == 297    assert results == [new_case, already_marked]98def test_mark_sections_for_deletion_handles_multiple_sections(mock_adapter, suite_fixture_with_deletion_marking):99    suite = suite_fixture_with_deletion_marking100    subject = MarkForDeletionHandler(suite=suite, adapter=mock_adapter)101    _, new_parent = subject.to_be_deleted_sections102    all_sections = list(suite.sections.values())103    sections_to_mark = sorted(104        [section for section in all_sections if not section.path.startswith('__to_be_deleted__')],105        key=lambda x: x.path,106        reverse=True107    )108    expected_calls = [call(section=section, new_parent=new_parent) for section in sections_to_mark]109    actual = subject.delete_sections(sections=(sections_to_mark + list(subject.to_be_deleted_sections)))110    assert isinstance(actual, Generator)111    results = [res for res in actual]112    assert len(results) > 2113    mock_adapter.move_section.assert_has_calls(calls=expected_calls, any_order=False)114@pytest.mark.parametrize("settings", [{"mark_for_deletion": True}, {"mark_for_deletion": False}, {}])115def test_handler_factory_returns_correct_instance(suite_fixture, mock_adapter, settings):116    actual = deletion_handler_factory(suite=suite_fixture, adapter=mock_adapter, **settings)117    expected_type = MarkForDeletionHandler if settings.get("mark_for_deletion", True) else DeletionHandler118    assert isinstance(actual, expected_type)119def test_mark_for_deletion_special_sections_are_created_if_not_found(mock_adapter, suite_fixture):120    suite = suite_fixture121    to_be_deleted_section_name = FIELD('word')122    to_be_deleted_section = TestRailSection.from_data(get_section_response())123    to_be_deleted_section.suite_id = suite_fixture.suite_id124    to_be_deleted_section.parent_id = None125    to_be_deleted_section.name = to_be_deleted_section_name126    to_be_deleted_section_section = TestRailSection.from_data(get_section_response())127    to_be_deleted_section_section.suite_id = suite_fixture.suite_id128    to_be_deleted_section_section.parent_id = to_be_deleted_section.section_id129    to_be_deleted_section_section.name = "sections"130    mock_adapter.create_new_section_for_path = MagicMock(131        side_effect=[to_be_deleted_section, to_be_deleted_section_section]132    )133    subject = MarkForDeletionHandler(suite=suite, adapter=mock_adapter,134                                     to_be_deleted_section_name=to_be_deleted_section_name)135    expected_calls = [call(suite, to_be_deleted_section_name), call(suite, to_be_deleted_section_name + "/sections")]136    assert subject.to_be_deleted_sections == (to_be_deleted_section, to_be_deleted_section_section)...test_runsuite_resource.py
Source:test_runsuite_resource.py  
1"""2Tests for RunRunResource api.3"""4from tests.case.api.crud import ApiCrudCases5import logging6mozlogger = logging.getLogger('moztrap.test')7class RunSuiteResourceTest(ApiCrudCases):8    """Please see the test suites implemented in tests.suite.api.ApiCrudSuites.9    The following abstract methods must be implemented:10      - factory(self)                           (property)11      - resource_name(self)                     (property)12      - permission(self)                        (property)13      - new_object_data(self)                   (property)14      - backend_object(self, id)                (method)15      - backend_data(self, backend_object)      (method)16      - backend_meta_data(self, backend_object) (method)17    """18    # implementations for abstract methods and properties19    @property20    def factory(self):21        """The factory to use to create fixtures of the object under test.22        """23        return self.F.RunSuiteFactory()24    @property25    def resource_name(self):26        """String defining the resource name.27        """28        return "runsuite"29    @property30    def permission(self):31        """String defining the permission required for32        Create, Update, and Delete.33        """34        return "execution.manage_runs"35    def order_generator(self):36        """give an incrementing number for order."""37        self.__dict__.setdefault("order", 0)38        self.order += 139        return self.order40    @property41    def new_object_data(self):42        """Generates a dictionary containing the field names and auto-generated43        values needed to create a unique object.44        The output of this method can be sent in the payload parameter of a45        POST message.46        """47        self.productversion_fixture = self.F.ProductVersionFactory.create()48        self.suite_fixture = self.F.SuiteFactory.create(49            product=self.productversion_fixture.product)50        self.run_fixture = self.F.RunFactory.create(51            productversion=self.productversion_fixture)52        fields = {53            u"suite": unicode(54                self.get_detail_url("suite", str(self.suite_fixture.id))),55            u"run": unicode(56                self.get_detail_url("run", str(self.run_fixture.id))57            ),58            u"order": self.order_generator(),59            }60        return fields61    def backend_object(self, id):62        """Returns the object from the backend, so you can query it's values in63        the database for validation.64        """65        return self.model.RunSuite.everything.get(id=id)66    def backend_data(self, backend_obj):67        """Query's the database for the object's current values. Output is a68        dictionary that should match the result of getting the object's detail69        via the API, and can be used to verify API output.70        Note: both keys and data should be in unicode71        """72        return {73            u"id": backend_obj.id,74            u"resource_uri": unicode(75                self.get_detail_url(self.resource_name, str(backend_obj.id))),76            u"suite": unicode(77                self.get_detail_url("suite", str(backend_obj.suite.id))),78            u"run": unicode(79                self.get_detail_url("run", str(backend_obj.run.id))),80            u"order": backend_obj.order,81            }82    @property83    def read_create_fields(self):84        """List of fields that are required for create but read-only for update."""85        return ["run", "suite"]86    # overrides from crud.py87    # additional test suites, if any88    # validation suites89    def test_create_mismatched_product_error(self):90        """error if run.product does not match suite.product"""91        mozlogger.info("test_create_mismatched_product_error")92        fields = self.new_object_data93        product = self.F.ProductFactory()94        self.suite_fixture.product = product95        self.suite_fixture.save()96        # do post97        res = self.post(98            self.get_list_url(self.resource_name),99            params=self.credentials,100            payload=fields,101            status=400,102            )103        error_message = str(104            "suite's product must match run's product."105        )...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!!
