示例#1
0
def test_record_no_content(path2yaml_touched):
    """If to append the specified yaml file when there is no content.

    Args:
        path2yaml_touched (str): initated path, and the file has been
            touched.
    """
    p = path2yaml_touched
    record("apple", p)
    assert _read_yaml(p) == ["apple"]
示例#2
0
def test_record_written(path2yaml_written):
    """If to append the specified yaml file when there is content.

    Args:
        path2yaml_written (str): initated path, and the file has been
            touched and written.
    """
    p = path2yaml_written
    record("apple", p)
    words = _read_yaml(p)
    assert "apple" in words and "banana" in words
示例#3
0
def test_record_no_file(path2yaml):
    """If to touch and write the specified yaml file.

    Args:
        path2yaml (str): path to a yaml file storing searching history. The
            file doesn't exist.
    """
    p = path2yaml
    assert not pathlib.Path(p).is_file(), "The yaml file is not touched."

    record("apple", p)
    assert pathlib.Path(p).is_file() and _read_yaml(p) == [
        "apple"
    ], "A yaml file should have been touched and written."
示例#4
0
def path2yaml_written(tmp_path_factory):
    """Initiate another path to a yaml file, touch and write it.

    Args:
        tmp_path_factory (TempPathFactory): pytest tool to initiate
            temporary a path.

    Returns:
        str: initated path. The yaml has been touched and written.
    """
    p = str(tmp_path_factory.mktemp("tmp2", numbered=False) / ".history.yaml")
    pathlib.Path(p).touch()
    record("banana", p)

    assert pathlib.Path(p).is_file() and _read_yaml(p) == [
        "banana"
    ], "A yaml file should have been touched and written."

    return p
示例#5
0
    def query(self, word):
        """Query word from the database.

        Args:
            word (str): the word to be queried.

        Returns:
            dict: Query result with format:
                {
                    id: (int)
                    word: (str)
                    sw: (str)
                    phonetic: (str)
                    definition: (str)
                    trans: (str)
                    "pos": (str)
                    "collins": (int)
                    "oxford": (int)
                    "tag": (str)
                    "bnc": (int)
                    "frq": (int)
                    "exchange": (str)
                }
        """
        try:
            query = "select * from stardict where word = ?"
            cursor = self._conn.cursor()
            cursor.execute(query, (word, ))
            record(word)
            res = cursor.fetchone()

            return (dict([(x, y)
                          for x, y in zip(_key_names, res)]) if res else None)

        except sqlite3.Error:
            logger.exception("SQLite DB search failed.")