File: test_utils.py

package info (click to toggle)
jupyter-core 5.7.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 740 kB
  • sloc: python: 2,595; makefile: 175; sh: 77; javascript: 1
file content (75 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Tests for utils"""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations

import asyncio
import os
import tempfile

import pytest

from jupyter_core.utils import (
    deprecation,
    ensure_async,
    ensure_dir_exists,
    ensure_event_loop,
    run_sync,
)


def test_ensure_dir_exists():
    with tempfile.TemporaryDirectory() as td:
        ensure_dir_exists(td)
        ensure_dir_exists(os.path.join(str(td), "foo"), 0o777)


def test_deprecation():
    with pytest.deprecated_call():
        deprecation("foo")


async def afunc():
    return "afunc"


def func():
    return "func"


sync_afunc = run_sync(afunc)


def test_run_sync():
    async def foo():
        return 1

    foo_sync = run_sync(foo)
    assert foo_sync() == 1
    assert foo_sync() == 1
    ensure_event_loop().close()

    asyncio.set_event_loop(None)
    assert foo_sync() == 1
    ensure_event_loop().close()

    asyncio.run(foo())


def test_ensure_async():
    async def main():
        assert await ensure_async(afunc()) == "afunc"
        assert await ensure_async(func()) == "func"

    asyncio.run(main())


def test_ensure_event_loop():
    loop = ensure_event_loop()

    async def inner():
        return asyncio.get_running_loop()

    inner_sync = run_sync(inner)
    assert inner_sync() == loop