Example #1
0
async def test_remove_errors():
    with run_server():
        async with make_connection() as tree:
            for exc_type, chars in InvalidChars.items():
                for c in chars:
                    with pytest.raises(exc_type):
                        await tree.remove_node("/", "a" + c + "b")
                    if c != '/':
                        with pytest.raises(exc_type):
                            await tree.remove_node("/a" + c + "b", "foo")
            for exc_type, names in InvalidNames.items():
                for name in names:
                    with pytest.raises(exc_type):
                        await tree.remove_node("/", name)
                    with pytest.raises(exc_type):
                        await tree.remove_node("/" + name, "foo")
            for exc_type, names in EmptyComponentPaths.items():
                for name in names:
                    with pytest.raises(exc_type):
                        await tree.remove_node(name, "foo")

            with pytest.raises(db.NoSuchNode):
                await tree.remove_node("/", "a")

            await tree.create_directory("/", "a")
            await tree.create_directory("/a", "b")
            with pytest.raises(db.DirectoryNotEmpty):
                await tree.remove_node("/", "a")
            await tree.remove_node("/a", "b")
Example #2
0
async def test_initial_tree():
    """
    Ensure that the tree starts empty.
    """
    with run_server():
        async with make_connection() as tree:
            assert await tree.list_directory("/") == []
Example #3
0
async def test_create_errors():
    with run_server():
        async with make_connection() as tree:
            await tree.create_directory("/", "dir")
            await tree.create_file("/", "file")

            for path in ("/", "/dir"):
                for ty in NodeTypes:
                    for exc_type, chars in InvalidChars.items():
                        for c in chars:
                            with pytest.raises(exc_type):
                                await create_node(tree, ty, path, "a" + c + "b")
                            if c != '/':
                                with pytest.raises(exc_type):
                                    await create_node(tree, ty, "/a" + c + "b", "foo")
                    for exc_type, names in InvalidNames.items():
                        for name in names:
                            with pytest.raises(exc_type):
                                await create_node(tree, ty, path, name)
                            with pytest.raises(exc_type):
                                await create_node(tree, ty, "/" + name, "foo")
                    for exc_type, names in EmptyComponentPaths.items():
                        for name in names:
                            with pytest.raises(exc_type):
                                await create_node(tree, ty, name, "foo")

                with pytest.raises(db.NoSuchNode):
                    await create_node(tree, ty, "/b", "a")
                with pytest.raises(db.NodeAlreadyExists):
                    await create_node(tree, ty, "/", "dir")
                with pytest.raises(db.NotDirectory):
                    await create_node(tree, ty, "/file", 'foo')
Example #4
0
async def test_subscribe_glob_filter():
    """
    Ensure that globs only match matching files.
    """
    with run_server():
        async with make_connection() as tree:
            expect = None
            count = 0
            async def on_changed(changes: {str: [str]}):
                # Sort all files in the changes list for stability.
                changes = {k: list(sorted(v)) for k, v in changes.items()}
                nonlocal count
                assert expect is not None
                assert expect == (count, changes)
                count += 1

            for name in ['a', 'b', 'c', 'aa']:
                await tree.create_file("/", name)
            await tree.watch_matching_files("/?", on_changed)

            expect = (0, {"foo": ["/a"]})
            await tree.set_file("/a", "foo")
            await tree.set_file("/aa", "foo")
            expect = (1, {"bar": ["/b"]})
            await tree.set_file("/b", "bar")
            await tree.set_file("/aa", "bar")
            expect = (2, {"baz": ["/c"]})
            await tree.set_file("/c", "baz")
            await tree.set_file("/aa", "baz")
Example #5
0
async def test_subscribe_glob_multi():
    """
    Ensure that globs return all matching path events in a single message.
    """
    with run_server():
        async with make_connection() as tree:
            expect = None
            count = 0
            async def on_changed(changes):
                # Sort all files in the changes list for stability.
                changes = {k: list(sorted(v)) for k, v in changes.items()}
                nonlocal count
                assert expect is not None
                assert expect == (count, changes)
                count += 1

            for a in ['a', 'b', 'c']:
                await tree.create_directory("/", a)
                for b in ['a', 'b', 'c']:
                    await tree.create_directory("/{}".format(a), b)
                    for c in ['foo', 'bar', 'baz']:
                        await tree.create_file("/{}/{}".format(a, b), c)
            await tree.watch_matching_files("/a/**/foo", on_changed)

            expect = (0, {"a": ["/a/a/foo"]})
            await tree.set_file("/a/a/foo", "a")
            expect = (1, {"b": ["/a/a/foo"]})
            await tree.set_matching_files("/a/a/*", "b")
            await tree.set_matching_files("/**/bar", "c")
            expect = (2, {"c": ["/a/a/foo", "/a/b/foo", "/a/c/foo"]})
            await tree.set_matching_files("/**/foo", "c")
Example #6
0
async def test_watch_errors():
    with run_server():
        async with make_connection() as tree:
            async def target(**_):
                pass

            with pytest.raises(db.Dotfile):
                await tree.watch_matching_files("/../../usr/lib/libGL.so", target)
Example #7
0
async def test_basic_formula_input():
    with run_server():
        async with make_connection() as tree:
            await tree.create_file("/", "a0")
            await tree.set_file("/a0", "Hello, World!")
            await tree.create_formula("/", "result", {'a0': '/a0'}, 'a0')
            data = await tree.get_file("/result")
            assert data == "Hello, World!"
Example #8
0
def before_request():
    g.db = util.make_connection()

    g.asp = g.db.asus.asp
    g.part = g.db.asus.part
    g.order = g.db.asus.order

    for collection in util.collections:
        util.collections[collection].index()
Example #9
0
 def pos(self, word):
     ''' get the pos of the word '''
     self.single_word_check(word)
     soup = make_connection('thesaurus', word)
     try:
         pos_word = soup.find('em', class_='css-siah4c e9i53te8').text
     except:
         pos_word = ''
     return pos_word
Example #10
0
async def test_data():
    """
    Check that basic create/get/set/remove works as expected.
    """
    with run_server():
        async with make_connection() as tree:
            await tree.create_file("/", "a")
            await tree.set_file("/a", "flinfniffle")
            data = await tree.get_file("/a")
            assert data == "flinfniffle"
            await tree.remove_node("/", "a")
Example #11
0
async def test_data_errors():
    with run_server():
        async with make_connection() as tree:
            with pytest.raises(db.Dotfile):
                await tree.set_file("/.", "")
            with pytest.raises(db.Dotfile):
                await tree.get_file("/.")
            with pytest.raises(db.NonAbsolutePath):
                await tree.set_file("a/b", "")
            with pytest.raises(db.NonAbsolutePath):
                await tree.get_file("a/b")
Example #12
0
async def test_set_glob_basic():
    """
    Ensure that globs only match matching files.
    """
    with run_server():
        async with make_connection() as tree:
            for name in "abcd":
                await tree.create_file("/", name)
            await tree.set_matching_files("/*", "hello")
            data = await tree.get_matching_files("/*")
            for name in "abcd":
                assert data["/" + name] == "hello"
Example #13
0
async def test_subscribe_same_client_data():
    """
    Ensure that subscriptions to data work and that we can:
      * make multiple subscriptions to the same path on a single client.
      * touch a subpath without being notified in the parent
      * remove one subscription of multiple
    """
    with run_server():
        async with make_connection() as tree:
            count1 = 0
            notify1 = asyncio.Future()

            async def on_child_changed1(changes):
                assert changes == {"foo": ["/a"]}
                nonlocal count1, notify1
                count1 += 1
                notify1.set_result(...)

            count2 = 0
            notify2 = asyncio.Future()

            async def on_child_changed2(changes):
                assert changes == {"foo": ["/a"]}
                nonlocal count2, notify2
                count2 += 1
                notify2.set_result(...)

            # Create and subscribe to data node.
            await tree.create_file("/", "a")
            await tree.create_file("/", "b")
            subid1 = await tree.watch_matching_files("/a", on_child_changed1)
            subid2 = await tree.watch_matching_files("/a", on_child_changed2)

            # Check that we get messages when we change the data, but not when we set siblings, or query it.
            await tree.set_file("/a", "foo")
            await tree.set_file("/b", "foo")
            data = await tree.get_file("/a")
            assert data == "foo"
            await asyncio.gather(notify1, notify2)
            assert count1 == 1
            assert count2 == 1

            # Reset notifications; unsubscribe #1, then check that we only get the notice on 2.
            notify1 = asyncio.Future()
            notify2 = asyncio.Future()
            await tree.unwatch(subid1)
            await tree.set_file("/a", "foo")
            await asyncio.sleep(0.1)  # we don't expect a response from 1, but give it some time to be more sure.
            await notify2
            assert count1 == 1
            assert count2 == 2
Example #14
0
async def test_subscribe_multiple_clients():
    """
    Ensure that causing an event on one client reports that event on a different client.
    """
    with run_server():
        async with make_connection() as treeA:
            async with make_connection() as treeB:
                count = 0
                notify = asyncio.Future()

                async def on_child_changed1(changes):
                    nonlocal count
                    count += 1
                    if 'bar' in changes:
                        notify.set_result(...)

                await treeA.create_file("/", "a")
                await treeA.watch_matching_files("/a", on_child_changed1)

                await treeB.set_file("/a", "foo")
                await treeB.set_file("/a", "bar")

                await notify
                assert count == 2
Example #15
0
async def test_subscribe_glob_basic_file():
    """
    Ensure that subscribing to a glob works properly.
    """
    with run_server():
        async with make_connection() as tree:
            expect = None
            count = 0
            async def on_foo_changed(changes: {str: [str]}):
                # Sort all files in the changes list for stability.
                changes = {k: list(sorted(v)) for k, v in changes.items()}
                nonlocal count
                assert expect is not None
                assert expect == (count, changes)
                count += 1

            sid = await tree.watch_matching_files("/{a,b}-foo", on_foo_changed)

            # We should be able to create a new path and have the glob pick it up.
            await tree.create_file("/", "a-foo")

            # Try setting file contents.
            expect = (0, {"0": ["/a-foo"]})
            await tree.set_file("/a-foo", "0")
            expect = (1, {"": ["/a-foo"]})
            await tree.set_file("/a-foo", "")
            expect = None

            # We should not get a notification on removing the watched node.
            await tree.remove_node("/", "a-foo")

            # We should not get a notification for inserting into a non-matching directory.
            await tree.create_file("/", "a-bar")
            await tree.set_file("/a-bar", "test")
            await tree.remove_node("/", "a-bar")

            # Check for multiple matches with overlapping.
            await tree.create_file("/", "a-foo")
            await tree.create_file("/", "b-foo")
            await tree.create_file("/", "c-foo")

            expect = (2, {"2": ["/a-foo"]})
            await tree.set_matching_files("/{a,c}-foo", "2")
            expect = (3, {"3": ["/b-foo"]})
            await tree.set_matching_files("/{b,c}-foo", "3")
            expect = (4, {"4": ["/a-foo", "/b-foo"]})
            await tree.set_matching_files("/{a,b,c}-foo", "4")
            expect = None
Example #16
0
 def antonyms(self, word):
     ''' get antonyms '''
     self.single_word_check(word)
     antonyms_list = []
     soup = make_connection('thesaurus', word)
     try:
         antonym_tags = soup.find_all('ul',
                                      class_='css-1lc0dpe et6tpn80')[1]
         for ant in antonym_tags:
             try:
                 antonyms_list.append(ant.span.a.text)
             except:
                 continue
     except:
         pass
     return antonyms_list
Example #17
0
 def synonyms(self, word):
     ''' get synonyms '''
     self.single_word_check(word)
     synonyms_list = []
     soup = make_connection('thesaurus', word)
     try:
         synonym_tags = soup.find(
             'ul', class_='css-1lc0dpe et6tpn80').find_all('li')
         for syn in synonym_tags:
             try:
                 synonyms_list.append(syn.span.a.text)
             except:
                 continue
     except:
         pass
     return synonyms_list
Example #18
0
async def test_formula_subscription_all():
    with run_server():
        async with make_connection() as tree:
            await tree.create_file("/", "a0")
            await tree.set_file("/a0", "Hello, World!")
            await tree.create_formula("/", "result", {'a0': '/a0'}, 'a0')

            count = 0
            expect = None

            async def on_result_changed(changes):
                nonlocal count
                assert expect == (count, changes)
                count += 1
            await tree.watch_matching_files("/*", on_result_changed)

            expect = (0, {"foobar": ["/a0", "/result"]})
            await tree.set_file("/a0", "foobar")
            assert count == 1
Example #19
0
 def meanings(self, word):
     ''' get the meaning of the word '''
     self.single_word_check(word)
     definitions = []
     soup = make_connection('dictionary', word)
     try:
         description_tags = soup.find_all('div', class_='e1q3nk1v3')
         #first clear the examples from definiton tag
         #Handle example not found error
         for desc in description_tags:
             try:
                 desc.find('span', class_='luna-example').clear()
             except:
                 continue
             finally:
                 definitions.append(desc.text)
     except:
         pass
     return definitions
Example #20
0
async def test_formula_nested():
    with run_server():
        async with make_connection() as tree:
            expect = None
            count = 0

            async def on_result_changed(changes):
                nonlocal count
                assert expect == (count, changes)
                count += 1

            await tree.create_file("/", "a")
            await tree.create_formula("/", "b", {"a": "/a"}, "a")
            await tree.create_formula("/", "c", {"b": "/b"}, "b")
            await tree.watch_matching_files("/{a,c}", on_result_changed)

            expect = (0, {"foobar": ["/a", "/c"]})
            await tree.set_file("/a", "foobar")
            assert count == 1
            assert await tree.get_file("/c") == "foobar"
Example #21
0
async def test_tree_async():
    """
    Do a large amount of work using async calls.
    """
    with run_server():
        async with make_connection() as tree:
            futures = []
            children = "abcdefghijklmnopqrstuvwxyz"
            for i, a in enumerate(children):
                if i % 2 == 0:
                    futures.append(await tree.create_directory_async("/", a))
                else:
                    # Note that this is safe because we're using TCP under the hood. If we ever do anything non-serial,
                    # this test will need to change pretty dramatically.
                    futures.append(await tree.create_file_async("/", a))
                    futures.append(await tree.set_file_async("/" + a, a))
            await asyncio.gather(*futures)

            future = await tree.list_directory_async("/")
            result = await future
            assert "".join(sorted(result.children)) == children

            futures = []
            for i, a in enumerate(children):
                if i % 2 == 1:
                    futures.append(await tree.get_file_async("/" + a))
            results = await asyncio.gather(*futures)
            assert "".join(sorted([rv.data for rv in results])) == children[1::2]

            futures = []
            vowels = "aeiou"
            for a in vowels:
                futures.append(await tree.remove_node_async("/", a))
            await asyncio.gather(*futures)

            future = await tree.list_directory_async("/")
            result = await future
            assert "".join(sorted(result.children)) == \
                   "bcdfghjklmnpqrstvwxyz"
Example #22
0
async def test_formula_multi_input():
    """
    Use a formula to ensure that the basic use works.
    """
    with run_server():
        async with make_connection() as tree:
            expect = None
            count = 0

            async def on_result_changed(changes):
                nonlocal count
                assert expect == (count, changes)
                count += 1

            await tree.create_formula("/", "result", {'a0': "/arg0", 'a1': "/arg1"},
                                      '(join "" a0 a1)')
            with pytest.raises(db.FormulaInputNotFound):
                await tree.get_file("/result")

            await tree.watch_matching_files("/*", on_result_changed)

            await tree.create_file("/", "arg0")
            with pytest.raises(db.FormulaInputNotFound):
                await tree.get_file("/result")

            await tree.create_file("/", "arg1")
            await tree.get_file("/result")

            expect = (0, {"foo": ["/arg0", "/result"]})
            await tree.set_file("/arg0", "foo")
            assert await tree.get_file("/result") == "foo"

            expect = (1, {"bar": ["/arg1"], "foobar": ["/result"]})
            await tree.set_file("/arg1", "bar")
            assert await tree.get_file("/result") == "foobar"

            expect = (2, {"baz": ["/arg0"], "bazbar": ["/result"]})
            await tree.set_file("/arg0", "baz")
            assert await tree.get_file("/result") == "bazbar"
Example #23
0
async def test_tree_sync():
    """
    Create a large tree using the sync API and verify the content and layout.
    """
    with run_server():
        async with make_connection() as tree:
            for a in "abcd":
                await tree.create_directory("/", a)
                for b in "efgh":
                    await tree.create_directory("/{}".format(a), b)
                    for c in "ijkl":
                        await tree.create_directory("/{}/{}".format(a, b), c)
                        for d in "mnop":
                            await tree.create_directory("/{}/{}/{}".format(a, b, c), d)

            path = "/"
            assert "".join(sorted(await tree.list_directory(path))) == "abcd"
            for a in await tree.list_directory(path):
                a_path = path + a
                assert "".join(sorted(await tree.list_directory(a_path))) == "efgh"
                for b in await tree.list_directory(a_path):
                    b_path = a_path + "/" + b
                    assert "".join(sorted(await tree.list_directory(b_path))) == "ijkl"
                    for c in await tree.list_directory(b_path):
                        c_path = b_path + "/" + c
                        assert "".join(sorted(await tree.list_directory(c_path))) == "mnop"
                        for d in await tree.list_directory(c_path):
                            d_path = c_path + "/" + d
                            assert "".join(sorted(await tree.list_directory(d_path))) == ""

            path = "/"
            for a in await tree.list_directory(path):
                for b in await tree.list_directory("/{}".format(a)):
                    for c in await tree.list_directory("/{}/{}".format(a, b)):
                        for d in await tree.list_directory("/{}/{}/{}".format(a, b, c)):
                            await tree.remove_node("/{}/{}/{}".format(a, b, c), d)
                        await tree.remove_node("/{}/{}".format(a, b), c)
                    await tree.remove_node("/{}".format(a), b)
                await tree.remove_node("/", a)
Example #24
0
def make_carla_client(host, world_port, timeout=15):
    """Context manager for creating and connecting a CarlaClient."""
    with util.make_connection(CarlaClient, host, world_port, timeout) as client:
        yield client
Example #25
0
async def test_unwatch_errors():
    with run_server():
        async with make_connection() as tree:
            with pytest.raises(db.NoSuchSubscription):
                await tree.unwatch(10)
Example #26
0
async def test_basic_formula_types():
    with run_server():
        async with make_connection() as tree:
            await tree.create_formula("/", "result", {}, '42')
            with pytest.raises(db.FormulaTypeError):
                await tree.get_file("/result")
Example #27
0
async def test_basic_formula_get():
    with run_server():
        async with make_connection() as tree:
            await tree.create_formula("/", "result", {}, '"Hello, World!"')
            data = await tree.get_file("/result")
            assert data == 'Hello, World!'
Example #28
0
async def test_basic_formula_no_assign():
    with run_server():
        async with make_connection() as tree:
            await tree.create_formula("/", "result", {}, '"foo"')
            with pytest.raises(db.NotFile):
                await tree.set_file("/result", "anything")
Example #29
0
async def test_basic_formula_missing_input():
    with run_server():
        async with make_connection() as tree:
            await tree.create_formula("/", "result", {'a0': '/a0'}, 'a0')
            with pytest.raises(db.FormulaInputNotFound):
                await tree.get_file("/result")
Example #30
0
async def test_basic_formula_stmt():
    with run_server():
        async with make_connection() as tree:
            await tree.create_formula("/", "result", {}, '(format "~s" 42)')
            data = await tree.get_file("/result")
            assert data == '42'