Skip to content

Commit

Permalink
Test types.Flag without doctest
Browse files Browse the repository at this point in the history
Since Python 3.10, repr() of Enum shows members in definition order so
it's hard to rely on this in doctests while testing with other Python
versions. Move those tests as unit tests and compare values instead of
repr().
  • Loading branch information
dlax committed Mar 12, 2021
1 parent e2b816e commit 17f9de2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
38 changes: 2 additions & 36 deletions pgactivity/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,7 @@ def enum_next(e: E) -> E:

@enum.unique
class Flag(enum.IntFlag):
"""Column flag.
>>> f = Flag(3)
>>> f
<Flag.APPNAME|DATABASE: 3>
>>> f | Flag.CLIENT
<Flag.CLIENT|APPNAME|DATABASE: 7>
>>> f ^= Flag.APPNAME
>>> f
<Flag.DATABASE: 1>
"""
"""Column flag."""

DATABASE = 1
APPNAME = 2
Expand Down Expand Up @@ -99,31 +89,7 @@ def from_options(
nowrite: bool,
**kwargs: Any,
) -> "Flag":
"""Build a Flag value from command line options.
>>> options = {
... 'noappname': False,
... 'noclient': False,
... 'nocpu': False,
... 'nodb': False,
... 'nomem': False,
... 'nopid': False,
... 'noread': False,
... 'notime': False,
... 'nouser': False,
... 'nowait': False,
... 'nowrite': False,
... }
>>> Flag.from_options(is_local=True, **options)
<Flag.PID|IOWAIT|MODE|TYPE|RELATION|WAIT|TIME|WRITE|READ|MEM|CPU|USER|CLIENT|APPNAME|DATABASE: 32767>
>>> Flag.from_options(is_local=False, **options)
<Flag.PID|MODE|TYPE|RELATION|WAIT|TIME|USER|CLIENT|APPNAME|DATABASE: 24335>
>>> options['nodb'] = True
>>> options['notime'] = True
>>> options['nopid'] = True
>>> Flag.from_options(is_local=False, **options)
<Flag.MODE|TYPE|RELATION|WAIT|USER|CLIENT|APPNAME: 7694>
"""
"""Build a Flag value from command line options."""
flag = cls.all()
if nodb:
flag ^= cls.DATABASE
Expand Down
72 changes: 72 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from pgactivity.types import Flag


def test_flag():
f = Flag(3)
assert f == Flag.APPNAME | Flag.DATABASE
assert f | Flag.CLIENT == Flag.CLIENT | Flag.APPNAME | Flag.DATABASE
f ^= Flag.APPNAME
assert f == Flag.DATABASE


def test_flag_from_options():
options = {
"noappname": False,
"noclient": False,
"nocpu": False,
"nodb": False,
"nomem": False,
"nopid": False,
"noread": False,
"notime": False,
"nouser": False,
"nowait": False,
"nowrite": False,
}
flag = Flag.from_options(is_local=True, **options)
assert (
flag
== Flag.PID
| Flag.IOWAIT
| Flag.MODE
| Flag.TYPE
| Flag.RELATION
| Flag.WAIT
| Flag.TIME
| Flag.WRITE
| Flag.READ
| Flag.MEM
| Flag.CPU
| Flag.USER
| Flag.CLIENT
| Flag.APPNAME
| Flag.DATABASE
)
flag = Flag.from_options(is_local=False, **options)
assert (
flag
== Flag.PID
| Flag.MODE
| Flag.TYPE
| Flag.RELATION
| Flag.WAIT
| Flag.TIME
| Flag.USER
| Flag.CLIENT
| Flag.APPNAME
| Flag.DATABASE
)
options["nodb"] = True
options["notime"] = True
options["nopid"] = True
flag = Flag.from_options(is_local=False, **options)
assert (
flag
== Flag.MODE
| Flag.TYPE
| Flag.RELATION
| Flag.WAIT
| Flag.USER
| Flag.CLIENT
| Flag.APPNAME
)

0 comments on commit 17f9de2

Please sign in to comment.