Best Python code snippet using lemoncheesecake
test_cacheprovider.py
Source:test_cacheprovider.py  
...166        p = testdir.makepyfile(167            """168            def test_1():169                assert 0170            def test_2():171                assert 0172            def test_3():173                assert 1174        """175        )176        result = testdir.runpytest()177        result.stdout.fnmatch_lines(["*2 failed*"])178        p.write(179            _pytest._code.Source(180                """181            def test_1():182                assert 1183            def test_2():184                assert 1185            def test_3():186                assert 0187        """188            )189        )190        result = testdir.runpytest("--lf")191        result.stdout.fnmatch_lines(["*2 passed*1 desel*"])192        result = testdir.runpytest("--lf")193        result.stdout.fnmatch_lines(["*1 failed*2 passed*"])194        result = testdir.runpytest("--lf", "--cache-clear")195        result.stdout.fnmatch_lines(["*1 failed*2 passed*"])196        # Run this again to make sure clear-cache is robust197        if os.path.isdir(".pytest_cache"):198            shutil.rmtree(".pytest_cache")199        result = testdir.runpytest("--lf", "--cache-clear")200        result.stdout.fnmatch_lines(["*1 failed*2 passed*"])201    def test_failedfirst_order(self, testdir):202        testdir.tmpdir.join("test_a.py").write(203            _pytest._code.Source(204                """205            def test_always_passes():206                assert 1207        """208            )209        )210        testdir.tmpdir.join("test_b.py").write(211            _pytest._code.Source(212                """213            def test_always_fails():214                assert 0215        """216            )217        )218        result = testdir.runpytest()219        # Test order will be collection order; alphabetical220        result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"])221        result = testdir.runpytest("--ff")222        # Test order will be failing tests firs223        result.stdout.fnmatch_lines(["test_b.py*", "test_a.py*"])224    def test_lastfailed_failedfirst_order(self, testdir):225        testdir.makepyfile(226            **{227                "test_a.py": """228                def test_always_passes():229                    assert 1230            """,231                "test_b.py": """232                def test_always_fails():233                    assert 0234            """,235            }236        )237        result = testdir.runpytest()238        # Test order will be collection order; alphabetical239        result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"])240        result = testdir.runpytest("--lf", "--ff")241        # Test order will be failing tests firs242        result.stdout.fnmatch_lines(["test_b.py*"])243        assert "test_a.py" not in result.stdout.str()244    def test_lastfailed_difference_invocations(self, testdir, monkeypatch):245        monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1)246        testdir.makepyfile(247            test_a="""248            def test_a1():249                assert 0250            def test_a2():251                assert 1252        """,253            test_b="""254            def test_b1():255                assert 0256        """,257        )258        p = testdir.tmpdir.join("test_a.py")259        p2 = testdir.tmpdir.join("test_b.py")260        result = testdir.runpytest()261        result.stdout.fnmatch_lines(["*2 failed*"])262        result = testdir.runpytest("--lf", p2)263        result.stdout.fnmatch_lines(["*1 failed*"])264        p2.write(265            _pytest._code.Source(266                """267            def test_b1():268                assert 1269        """270            )271        )272        result = testdir.runpytest("--lf", p2)273        result.stdout.fnmatch_lines(["*1 passed*"])274        result = testdir.runpytest("--lf", p)275        result.stdout.fnmatch_lines(["*1 failed*1 desel*"])276    def test_lastfailed_usecase_splice(self, testdir, monkeypatch):277        monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1)278        testdir.makepyfile(279            """280            def test_1():281                assert 0282        """283        )284        p2 = testdir.tmpdir.join("test_something.py")285        p2.write(286            _pytest._code.Source(287                """288            def test_2():289                assert 0290        """291            )292        )293        result = testdir.runpytest()294        result.stdout.fnmatch_lines(["*2 failed*"])295        result = testdir.runpytest("--lf", p2)296        result.stdout.fnmatch_lines(["*1 failed*"])297        result = testdir.runpytest("--lf")298        result.stdout.fnmatch_lines(["*2 failed*"])299    def test_lastfailed_xpass(self, testdir):300        testdir.inline_runsource(301            """302            import pytest303            @pytest.mark.xfail304            def test_hello():305                assert 1306        """307        )308        config = testdir.parseconfigure()309        lastfailed = config.cache.get("cache/lastfailed", -1)310        assert lastfailed == -1311    def test_non_serializable_parametrize(self, testdir):312        """Test that failed parametrized tests with unmarshable parameters313        don't break pytest-cache.314        """315        testdir.makepyfile(316            r"""317            import pytest318            @pytest.mark.parametrize('val', [319                b'\xac\x10\x02G',320            ])321            def test_fail(val):322                assert False323        """324        )325        result = testdir.runpytest()326        result.stdout.fnmatch_lines("*1 failed in*")327    def test_terminal_report_lastfailed(self, testdir):328        test_a = testdir.makepyfile(329            test_a="""330            def test_a1():331                pass332            def test_a2():333                pass334        """335        )336        test_b = testdir.makepyfile(337            test_b="""338            def test_b1():339                assert 0340            def test_b2():341                assert 0342        """343        )344        result = testdir.runpytest()345        result.stdout.fnmatch_lines(["collected 4 items", "*2 failed, 2 passed in*"])346        result = testdir.runpytest("--lf")347        result.stdout.fnmatch_lines(348            [349                "collected 4 items / 2 deselected",350                "run-last-failure: rerun previous 2 failures",351                "*2 failed, 2 deselected in*",352            ]353        )354        result = testdir.runpytest(test_a, "--lf")355        result.stdout.fnmatch_lines(356            [357                "collected 2 items",358                "run-last-failure: run all (no recorded failures)",359                "*2 passed in*",360            ]361        )362        result = testdir.runpytest(test_b, "--lf")363        result.stdout.fnmatch_lines(364            [365                "collected 2 items",366                "run-last-failure: rerun previous 2 failures",367                "*2 failed in*",368            ]369        )370        result = testdir.runpytest("test_b.py::test_b1", "--lf")371        result.stdout.fnmatch_lines(372            [373                "collected 1 item",374                "run-last-failure: rerun previous 1 failure",375                "*1 failed in*",376            ]377        )378    def test_terminal_report_failedfirst(self, testdir):379        testdir.makepyfile(380            test_a="""381            def test_a1():382                assert 0383            def test_a2():384                pass385        """386        )387        result = testdir.runpytest()388        result.stdout.fnmatch_lines(["collected 2 items", "*1 failed, 1 passed in*"])389        result = testdir.runpytest("--ff")390        result.stdout.fnmatch_lines(391            [392                "collected 2 items",393                "run-last-failure: rerun previous 1 failure first",394                "*1 failed, 1 passed in*",395            ]396        )397    def test_lastfailed_collectfailure(self, testdir, monkeypatch):398        testdir.makepyfile(399            test_maybe="""400            import os401            env = os.environ402            if '1' == env['FAILIMPORT']:403                raise ImportError('fail')404            def test_hello():405                assert '0' == env['FAILTEST']406        """407        )408        def rlf(fail_import, fail_run):409            monkeypatch.setenv("FAILIMPORT", fail_import)410            monkeypatch.setenv("FAILTEST", fail_run)411            testdir.runpytest("-q")412            config = testdir.parseconfigure()413            lastfailed = config.cache.get("cache/lastfailed", -1)414            return lastfailed415        lastfailed = rlf(fail_import=0, fail_run=0)416        assert lastfailed == -1417        lastfailed = rlf(fail_import=1, fail_run=0)418        assert list(lastfailed) == ["test_maybe.py"]419        lastfailed = rlf(fail_import=0, fail_run=1)420        assert list(lastfailed) == ["test_maybe.py::test_hello"]421    def test_lastfailed_failure_subset(self, testdir, monkeypatch):422        testdir.makepyfile(423            test_maybe="""424            import os425            env = os.environ426            if '1' == env['FAILIMPORT']:427                raise ImportError('fail')428            def test_hello():429                assert '0' == env['FAILTEST']430        """431        )432        testdir.makepyfile(433            test_maybe2="""434            import os435            env = os.environ436            if '1' == env['FAILIMPORT']:437                raise ImportError('fail')438            def test_hello():439                assert '0' == env['FAILTEST']440            def test_pass():441                pass442        """443        )444        def rlf(fail_import, fail_run, args=()):445            monkeypatch.setenv("FAILIMPORT", fail_import)446            monkeypatch.setenv("FAILTEST", fail_run)447            result = testdir.runpytest("-q", "--lf", *args)448            config = testdir.parseconfigure()449            lastfailed = config.cache.get("cache/lastfailed", -1)450            return result, lastfailed451        result, lastfailed = rlf(fail_import=0, fail_run=0)452        assert lastfailed == -1453        result.stdout.fnmatch_lines(["*3 passed*"])454        result, lastfailed = rlf(fail_import=1, fail_run=0)455        assert sorted(list(lastfailed)) == ["test_maybe.py", "test_maybe2.py"]456        result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",))457        assert list(lastfailed) == ["test_maybe.py"]458        # edge case of test selection - even if we remember failures459        # from other tests we still need to run all tests if no test460        # matches the failures461        result, lastfailed = rlf(fail_import=0, fail_run=0, args=("test_maybe2.py",))462        assert list(lastfailed) == ["test_maybe.py"]463        result.stdout.fnmatch_lines(["*2 passed*"])464    def test_lastfailed_creates_cache_when_needed(self, testdir):465        # Issue #1342466        testdir.makepyfile(test_empty="")467        testdir.runpytest("-q", "--lf")468        assert not os.path.exists(".pytest_cache/v/cache/lastfailed")469        testdir.makepyfile(test_successful="def test_success():\n    assert True")470        testdir.runpytest("-q", "--lf")471        assert not os.path.exists(".pytest_cache/v/cache/lastfailed")472        testdir.makepyfile(test_errored="def test_error():\n    assert False")473        testdir.runpytest("-q", "--lf")474        assert os.path.exists(".pytest_cache/v/cache/lastfailed")475    def test_xfail_not_considered_failure(self, testdir):476        testdir.makepyfile(477            """478            import pytest479            @pytest.mark.xfail480            def test():481                assert 0482        """483        )484        result = testdir.runpytest()485        result.stdout.fnmatch_lines("*1 xfailed*")486        assert self.get_cached_last_failed(testdir) == []487    def test_xfail_strict_considered_failure(self, testdir):488        testdir.makepyfile(489            """490            import pytest491            @pytest.mark.xfail(strict=True)492            def test():493                pass494        """495        )496        result = testdir.runpytest()497        result.stdout.fnmatch_lines("*1 failed*")498        assert (499            self.get_cached_last_failed(testdir)500            == ["test_xfail_strict_considered_failure.py::test"]501        )502    @pytest.mark.parametrize("mark", ["mark.xfail", "mark.skip"])503    def test_failed_changed_to_xfail_or_skip(self, testdir, mark):504        testdir.makepyfile(505            """506            import pytest507            def test():508                assert 0509        """510        )511        result = testdir.runpytest()512        assert (513            self.get_cached_last_failed(testdir)514            == ["test_failed_changed_to_xfail_or_skip.py::test"]515        )516        assert result.ret == 1517        testdir.makepyfile(518            """519            import pytest520            @pytest.{mark}521            def test():522                assert 0523        """.format(524                mark=mark525            )526        )527        result = testdir.runpytest()528        assert result.ret == 0529        assert self.get_cached_last_failed(testdir) == []530        assert result.ret == 0531    def get_cached_last_failed(self, testdir):532        config = testdir.parseconfigure()533        return sorted(config.cache.get("cache/lastfailed", {}))534    def test_cache_cumulative(self, testdir):535        """536        Test workflow where user fixes errors gradually file by file using --lf.537        """538        # 1. initial run539        test_bar = testdir.makepyfile(540            test_bar="""541            def test_bar_1():542                pass543            def test_bar_2():544                assert 0545        """546        )547        test_foo = testdir.makepyfile(548            test_foo="""549            def test_foo_3():550                pass551            def test_foo_4():552                assert 0553        """554        )555        testdir.runpytest()556        assert (557            self.get_cached_last_failed(testdir)558            == ["test_bar.py::test_bar_2", "test_foo.py::test_foo_4"]559        )560        # 2. fix test_bar_2, run only test_bar.py561        testdir.makepyfile(562            test_bar="""563            def test_bar_1():564                pass565            def test_bar_2():566                pass567        """568        )569        result = testdir.runpytest(test_bar)570        result.stdout.fnmatch_lines("*2 passed*")571        # ensure cache does not forget that test_foo_4 failed once before572        assert self.get_cached_last_failed(testdir) == ["test_foo.py::test_foo_4"]573        result = testdir.runpytest("--last-failed")574        result.stdout.fnmatch_lines("*1 failed, 3 deselected*")575        assert self.get_cached_last_failed(testdir) == ["test_foo.py::test_foo_4"]576        # 3. fix test_foo_4, run only test_foo.py577        test_foo = testdir.makepyfile(578            test_foo="""579            def test_foo_3():580                pass581            def test_foo_4():582                pass583        """584        )585        result = testdir.runpytest(test_foo, "--last-failed")586        result.stdout.fnmatch_lines("*1 passed, 1 deselected*")587        assert self.get_cached_last_failed(testdir) == []588        result = testdir.runpytest("--last-failed")589        result.stdout.fnmatch_lines("*4 passed*")590        assert self.get_cached_last_failed(testdir) == []591    def test_lastfailed_no_failures_behavior_all_passed(self, testdir):592        testdir.makepyfile(593            """594            def test_1():595                assert True596            def test_2():597                assert True598        """599        )600        result = testdir.runpytest()601        result.stdout.fnmatch_lines(["*2 passed*"])602        result = testdir.runpytest("--lf")603        result.stdout.fnmatch_lines(["*2 passed*"])604        result = testdir.runpytest("--lf", "--lfnf", "all")605        result.stdout.fnmatch_lines(["*2 passed*"])606        result = testdir.runpytest("--lf", "--lfnf", "none")607        result.stdout.fnmatch_lines(["*2 desel*"])608    def test_lastfailed_no_failures_behavior_empty_cache(self, testdir):609        testdir.makepyfile(610            """611            def test_1():612                assert True613            def test_2():614                assert False615        """616        )617        result = testdir.runpytest("--lf", "--cache-clear")618        result.stdout.fnmatch_lines(["*1 failed*1 passed*"])619        result = testdir.runpytest("--lf", "--cache-clear", "--lfnf", "all")620        result.stdout.fnmatch_lines(["*1 failed*1 passed*"])621        result = testdir.runpytest("--lf", "--cache-clear", "--lfnf", "none")622        result.stdout.fnmatch_lines(["*2 desel*"])623class TestNewFirst(object):624    def test_newfirst_usecase(self, testdir):625        testdir.makepyfile(626            **{627                "test_1/test_1.py": """628                def test_1(): assert 1629                def test_2(): assert 1630                def test_3(): assert 1631            """,632                "test_2/test_2.py": """633                def test_1(): assert 1634                def test_2(): assert 1635                def test_3(): assert 1636            """,637            }638        )639        testdir.tmpdir.join("test_1/test_1.py").setmtime(1)640        result = testdir.runpytest("-v")641        result.stdout.fnmatch_lines(642            [643                "*test_1/test_1.py::test_1 PASSED*",644                "*test_1/test_1.py::test_2 PASSED*",645                "*test_1/test_1.py::test_3 PASSED*",646                "*test_2/test_2.py::test_1 PASSED*",647                "*test_2/test_2.py::test_2 PASSED*",648                "*test_2/test_2.py::test_3 PASSED*",649            ]650        )651        result = testdir.runpytest("-v", "--nf")652        result.stdout.fnmatch_lines(653            [654                "*test_2/test_2.py::test_1 PASSED*",655                "*test_2/test_2.py::test_2 PASSED*",656                "*test_2/test_2.py::test_3 PASSED*",657                "*test_1/test_1.py::test_1 PASSED*",658                "*test_1/test_1.py::test_2 PASSED*",659                "*test_1/test_1.py::test_3 PASSED*",660            ]661        )662        testdir.tmpdir.join("test_1/test_1.py").write(663            "def test_1(): assert 1\n"664            "def test_2(): assert 1\n"665            "def test_3(): assert 1\n"666            "def test_4(): assert 1\n"667        )668        testdir.tmpdir.join("test_1/test_1.py").setmtime(1)669        result = testdir.runpytest("-v", "--nf")670        result.stdout.fnmatch_lines(671            [672                "*test_1/test_1.py::test_4 PASSED*",673                "*test_2/test_2.py::test_1 PASSED*",674                "*test_2/test_2.py::test_2 PASSED*",675                "*test_2/test_2.py::test_3 PASSED*",676                "*test_1/test_1.py::test_1 PASSED*",677                "*test_1/test_1.py::test_2 PASSED*",678                "*test_1/test_1.py::test_3 PASSED*",...test_properties.py
Source:test_properties.py  
1import smart_imports2smart_imports.all()3class TestClient(properties.Client):4    pass5class TEST_PROPERTIES(properties.PROPERTIES):6    records = (('test_1', 0, 'ÑеÑÑ 1', lambda x: x, lambda x: x, None, tt_api_properties.TYPE.REPLACE),7               ('test_2', 1, 'ÑеÑÑ 2', str, int, list, tt_api_properties.TYPE.APPEND))8properties_client = TestClient(entry_point=accounts_conf.settings.TT_PLAYERS_PROPERTIES_ENTRY_POINT,9                               properties=TEST_PROPERTIES)10class SetGetTests(utils_testcase.TestCase):11    def setUp(self):12        properties_client.cmd_debug_clear_service()13    def test_simple(self):14        properties_client.cmd_set_properties([(666, 'test_1', 'x.1')])15        properties = properties_client.cmd_get_properties({666: ['test_1']})16        self.assertEqual(properties[666].test_1, 'x.1')17        self.assertEqual(properties[666].test_2, [])18    def test_multiple(self):19        properties_client.cmd_set_properties([(666, 'test_1', 'x.1'),20                                              (777, 'test_2', 13),21                                              (888, 'test_1', 'x.2'),22                                              (888, 'test_2', 14)])23        properties = properties_client.cmd_get_properties({666: ['test_1'],24                                                           777: ['test_1', 'test_2'],25                                                           888: ['test_1', 'test_2']})26        self.assertEqual(properties[666].test_1, 'x.1')27        self.assertEqual(properties[666].test_2, [])28        self.assertEqual(properties[777].test_1, None)29        self.assertEqual(properties[777].test_2, [13])30        self.assertEqual(properties[888].test_1, 'x.2')31        self.assertEqual(properties[888].test_2, [14])32    def test_types(self):33        properties_client.cmd_set_properties([(666, 'test_1', 'x.1'),34                                              (777, 'test_2', 13),35                                              (666, 'test_1', 'x.2'),36                                              (777, 'test_2', 14)])37        properties = properties_client.cmd_get_properties({666: ['test_1', 'test_2'],38                                                           777: ['test_1', 'test_2']})39        self.assertEqual(properties[666].test_1, 'x.2')40        self.assertEqual(properties[666].test_2, [])41        self.assertEqual(properties[777].test_1, None)42        self.assertEqual(properties[777].test_2, [13, 14])43    def test_unknown_property(self):44        with self.assertRaises(exceptions.TTPropertiesError):45            properties_client.cmd_set_properties([(666, 'unknown', 'x.1')])46        with self.assertRaises(exceptions.TTPropertiesError):47            properties_client.cmd_get_properties({666: ['unknown']})48class SetGetSimplifiedTests(utils_testcase.TestCase):49    def setUp(self):50        properties_client.cmd_debug_clear_service()51    def test_simple(self):52        properties_client.cmd_set_property(object_id=666, name='test_1', value='x.1')53        value = properties_client.cmd_get_object_property(object_id=666, name='test_1')54        self.assertEqual(value, 'x.1')55    def test_default(self):56        value = properties_client.cmd_get_object_property(object_id=666, name='test_2')57        self.assertEqual(value, [])58    def test_wrong_properties(self):59        with self.assertRaises(exceptions.TTPropertiesError):60            properties_client.cmd_set_property(object_id=666, name='unknown', value='x.1')61        with self.assertRaises(exceptions.TTPropertiesError):62            properties_client.cmd_get_object_property(object_id=666, name='unknown')63class GetAllObjectProperties(utils_testcase.TestCase):64    def setUp(self):65        properties_client.cmd_debug_clear_service()66    def test_simple(self):67        properties_client.cmd_set_properties([(666, 'test_1', 'x.1'),68                                              (666, 'test_2', 13),69                                              (777, 'test_2', 14)])70        properties = properties_client.cmd_get_all_object_properties(object_id=666)71        self.assertEqual(properties.test_1, 'x.1')72        self.assertEqual(properties.test_2, [13])73        properties = properties_client.cmd_get_all_object_properties(object_id=777)74        self.assertEqual(properties.test_1, None)...models.py
Source:models.py  
1# -*- coding: utf-8 -*-2# This file is auto-generated, don't edit it. Thanks.3from Tea.model import TeaModel4class Test1(TeaModel):5    """6    TestModel7    """8    def __init__(self, test=None, test_2=None, test_3=None, test_5=None, sts=None, a_sts=None):9        # test desc10        self.test = test  # type: str11        # modelçtest back comment12        # test2 desc13        self.test_2 = test_2  # type: str14        # modelçtest2 back comment15        # test3 desc16        self.test_3 = test_3  # type: list[list[str]]17        # test desc18        self.test_5 = test_5  # type: str19        # sts desc20        self.sts = sts  # type: str21        # asts desc22        self.a_sts = a_sts  # type: str23    def validate(self):24        self.validate_required(self.test, 'test')25        self.validate_required(self.test_2, 'test_2')26        self.validate_required(self.test_3, 'test_3')27        self.validate_required(self.test_5, 'test_5')28        self.validate_required(self.sts, 'sts')29        self.validate_required(self.a_sts, 'a_sts')30    def to_map(self):31        _map = super(Test1, self).to_map()32        if _map is not None:33            return _map34        result = dict()35        if self.test is not None:36            result['test'] = self.test37        if self.test_2 is not None:38            result['test2'] = self.test_239        if self.test_3 is not None:40            result['test3'] = self.test_341        if self.test_5 is not None:42            result['test5'] = self.test_543        if self.sts is not None:44            result['sts'] = self.sts45        if self.a_sts is not None:46            result['asts'] = self.a_sts47        return result48    def from_map(self, m=None):49        m = m or dict()50        if m.get('test') is not None:51            self.test = m.get('test')52        if m.get('test2') is not None:53            self.test_2 = m.get('test2')54        if m.get('test3') is not None:55            self.test_3 = m.get('test3')56        if m.get('test5') is not None:57            self.test_5 = m.get('test5')58        if m.get('sts') is not None:59            self.sts = m.get('sts')60        if m.get('asts') is not None:61            self.a_sts = m.get('asts')62        return self63class Test2(TeaModel):64    """65    TestModel266    """67    def __init__(self, test=None, test_2=None):68        # modelçtest front comment69        # test desc70        self.test = test  # type: str71        # modelçtest front comment72        # test2 desc73        self.test_2 = test_2  # type: str74    def validate(self):75        self.validate_required(self.test, 'test')76        self.validate_required(self.test_2, 'test_2')77    def to_map(self):78        _map = super(Test2, self).to_map()79        if _map is not None:80            return _map81        result = dict()82        if self.test is not None:83            result['test'] = self.test84        if self.test_2 is not None:85            result['test2'] = self.test_286        return result87    def from_map(self, m=None):88        m = m or dict()89        if m.get('test') is not None:90            self.test = m.get('test')91        if m.get('test2') is not None:92            self.test_2 = m.get('test2')93        return self94class Test3(TeaModel):95    """96    TestModel397    """98    def __init__(self, test=None, test_1=None):99        # modelçtest front comment100        # test desc101        self.test = test  # type: str102        # empty comment1103        # empy comment2104        # test desc105        self.test_1 = test_1  # type: str106        # modelçtest back comment107    def validate(self):108        self.validate_required(self.test, 'test')109        self.validate_required(self.test_1, 'test_1')110    def to_map(self):111        _map = super(Test3, self).to_map()112        if _map is not None:113            return _map114        result = dict()115        if self.test is not None:116            result['test'] = self.test117        if self.test_1 is not None:118            result['test1'] = self.test_1119        return result120    def from_map(self, m=None):121        m = m or dict()122        if m.get('test') is not None:123            self.test = m.get('test')124        if m.get('test1') is not None:125            self.test_1 = m.get('test1')...test_component_graph.py
Source:test_component_graph.py  
1#!/usr/bin/env python32# Copyright 2019 The Fuchsia Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5import unittest6from server.graph.component_graph import *7from server.graph.component_link import *8from server.graph.component_node import *9class TestComponentGraph(unittest.TestCase):10    def test_add_node(self):11        component_graph = ComponentGraph()12        self.assertEqual(len(component_graph.nodes), 0)13        component_graph.add_node(14            ComponentNode(15                "fuchsia-pkg://fuchsia.com/test_1#/meta/test_1.cmx", {}))16        self.assertEqual(len(component_graph.nodes), 1)17        component_graph.add_node(18            ComponentNode(19                "fuchsia-pkg://fuchsia.com/test_2#/meta/test_2.cmx", {}))20        self.assertEqual(len(component_graph.nodes), 2)21    def test_add_link(self):22        component_graph = ComponentGraph()23        component_graph.add_node(24            ComponentNode(25                "fuchsia-pkg://fuchsia.com/test_1#/meta/test_1.cmx", {}))26        component_graph.add_node(27            ComponentNode(28                "fuchsia-pkg://fuchsia.com/test_2#/meta/test_2.cmx", {}))29        self.assertEqual(len(component_graph.links), 0)30        component_graph.add_link(31            ComponentLink(32                "fuchsia-pkg://fuchsia.com/test_1#/meta/test_1.cmx",33                "fuchsia-pkg://fuchsia.com/test_2#/meta/test_2.cmx", "use",34                "fidl.Service"))35        self.assertEqual(len(component_graph.links), 1)36        component_graph.add_link(37            ComponentLink(38                "fuchsia-pkg://fuchsia.com/test_2#/meta/test_2.cmx",39                "fuchsia-pkg://fuchsia.com/test_1#/meta/test_1.cmx", "use",40                "fidl.AnotherService"))41        self.assertEqual(len(component_graph.links), 2)42    def test_get_item(self):43        component_graph = ComponentGraph()44        component_graph.add_node(45            ComponentNode(46                "fuchsia-pkg://fuchsia.com/test_1#/meta/test_1.cmx", {}))47        component_graph.add_node(48            ComponentNode(49                "fuchsia-pkg://fuchsia.com/test_2#/meta/test_2.cmx", {}))50        self.assertIsNotNone(51            component_graph["fuchsia-pkg://fuchsia.com/test_1#/meta/test_1.cmx"]52        )53        self.assertIsNotNone(54            component_graph["fuchsia-pkg://fuchsia.com/test_2#/meta/test_2.cmx"]55        )56        with self.assertRaises(KeyError):57            self.assertIsNotNone(component_graph["fake_url3"])58    def test_export_empty(self):59        component_graph = ComponentGraph()60        empty_export = component_graph.export()61        self.assertEqual(empty_export["nodes"], [])62        self.assertEqual(empty_export["links"], [])63    def test_export(self):64        component_graph = ComponentGraph()65        component_graph.add_node(66            ComponentNode(67                "fuchsia-pkg://fuchsia.com/test_1#/meta/test_1.cmx", {}))68        component_graph.add_node(69            ComponentNode(70                "fuchsia-pkg://fuchsia.com/test_2#/meta/test_2.cmx", {}))71        self.assertEqual(len(component_graph.links), 0)72        component_graph.add_link(73            ComponentLink(74                "fuchsia-pkg://fuchsia.com/test_1#/meta/test_1.cmx",75                "fuchsia-pkg://fuchsia.com/test_2#/meta/test_2.cmx", "use",76                "fidl.Service"))77        self.assertEqual(len(component_graph.links), 1)78        component_graph.add_link(79            ComponentLink(80                "fuchsia-pkg://fuchsia.com/test_2#/meta/test_2.cmx",81                "fuchsia-pkg://fuchsia.com/test_1#/meta/test_1.cmx", "use",82                "fidl.AnotherService"))83        self.assertEqual(len(component_graph.links), 2)84        exported = component_graph.export()85        self.assertEqual(len(exported["nodes"]), 2)86        self.assertEqual(len(exported["links"]), 2)87if __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!!
