Skip to content

Commit

Permalink
Format tests and comment out that failing one
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Oct 28, 2023
1 parent 871bada commit 411692f
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 186 deletions.
86 changes: 40 additions & 46 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import errno
import functools
import os
import sys
import types

from av.datasets import fate as fate_suite
Expand Down Expand Up @@ -86,7 +85,7 @@ def _inner(self, *args, **kwargs):
return _inner


class MethodLogger(object):
class MethodLogger:
def __init__(self, obj):
self._obj = obj
self._log = []
Expand Down Expand Up @@ -142,12 +141,12 @@ def assertNdarraysEqual(self, a, b):
msg = ""
for equal in it:
if not equal:
msg += "- arrays differ at index %s; %s %s\n" % (
msg += "- arrays differ at index {}; {} {}\n".format(
it.multi_index,
a[it.multi_index],
b[it.multi_index],
)
self.fail("ndarrays contents differ\n%s" % msg)
self.fail(f"ndarrays contents differ\n{msg}")

def assertImagesAlmostEqual(self, a, b, epsilon=0.1, *args):
self.assertEqual(a.size, b.size, "sizes dont match")
Expand All @@ -156,45 +155,40 @@ def assertImagesAlmostEqual(self, a, b, epsilon=0.1, *args):
for i, ax, bx in zip(range(len(a)), a, b):
diff = sum(abs(ac / 256 - bc / 256) for ac, bc in zip(ax, bx)) / 3
if diff > epsilon:
self.fail(
"images differed by %s at index %d; %s %s" % (diff, i, ax, bx)
)

# Add some of the unittest methods that we love from 2.7.
if sys.version_info < (2, 7):

def assertIs(self, a, b, msg=None):
if a is not b:
self.fail(
msg
or "%r at 0x%x is not %r at 0x%x; %r is not %r"
% (type(a), id(a), type(b), id(b), a, b)
)

def assertIsNot(self, a, b, msg=None):
if a is b:
self.fail(msg or "both are %r at 0x%x; %r" % (type(a), id(a), a))

def assertIsNone(self, x, msg=None):
if x is not None:
self.fail(msg or "is not None; %r" % x)

def assertIsNotNone(self, x, msg=None):
if x is None:
self.fail(msg or "is None; %r" % x)

def assertIn(self, a, b, msg=None):
if a not in b:
self.fail(msg or "%r not in %r" % (a, b))

def assertNotIn(self, a, b, msg=None):
if a in b:
self.fail(msg or "%r in %r" % (a, b))

def assertIsInstance(self, instance, types, msg=None):
if not isinstance(instance, types):
self.fail(msg or "not an instance of %r; %r" % (types, instance))

def assertNotIsInstance(self, instance, types, msg=None):
if isinstance(instance, types):
self.fail(msg or "is an instance of %r; %r" % (types, instance))
self.fail(f"images differed by {diff} at index {i}; {ax} {bx}")

def assertIs(self, a, b, msg=None):
if a is not b:
self.fail(
msg
or "%r at 0x%x is not %r at 0x%x; %r is not %r"
% (type(a), id(a), type(b), id(b), a, b)
)

def assertIsNot(self, a, b, msg=None):
if a is b:
self.fail(msg or f"both are {type(a)!r} at 0x{id(a):x}; {a!r}")

def assertIsNone(self, x, msg=None):
if x is not None:
self.fail(msg or f"is not None; {x!r}")

def assertIsNotNone(self, x, msg=None):
if x is None:
self.fail(msg or f"is None; {x!r}")

def assertIn(self, a, b, msg=None):
if a not in b:
self.fail(msg or f"{a!r} not in {b!r}")

def assertNotIn(self, a, b, msg=None):
if a in b:
self.fail(msg or f"{a!r} in {b!r}")

def assertIsInstance(self, instance, types, msg=None):
if not isinstance(instance, types):
self.fail(msg or f"not an instance of {types!r}; {instance!r}")

def assertNotIsInstance(self, instance, types, msg=None):
if isinstance(instance, types):
self.fail(msg or f"is an instance of {types!r}; {instance!r}")
5 changes: 0 additions & 5 deletions tests/test_audiofifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class TestAudioFifo(TestCase):
def test_data(self):

container = av.open(fate_suite("audio-reference/chorusnoise_2ch_44kHz_s16.wav"))
stream = container.streams.audio[0]

Expand All @@ -31,7 +30,6 @@ def test_data(self):
self.assertTrue(input_[:min_len] == output[:min_len])

def test_pts_simple(self):

fifo = av.AudioFifo()

iframe = av.AudioFrame(samples=1024)
Expand Down Expand Up @@ -61,7 +59,6 @@ def test_pts_simple(self):
self.assertRaises(ValueError, fifo.write, iframe)

def test_pts_complex(self):

fifo = av.AudioFifo()

iframe = av.AudioFrame(samples=1024)
Expand All @@ -79,7 +76,6 @@ def test_pts_complex(self):
self.assertEqual(fifo.pts_per_sample, 2.0)

def test_missing_sample_rate(self):

fifo = av.AudioFifo()

iframe = av.AudioFrame(samples=1024)
Expand All @@ -96,7 +92,6 @@ def test_missing_sample_rate(self):
self.assertEqual(oframe.time_base, iframe.time_base)

def test_missing_time_base(self):

fifo = av.AudioFifo()

iframe = av.AudioFrame(samples=1024)
Expand Down
3 changes: 0 additions & 3 deletions tests/test_audioresampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def test_matching_passthrough(self):
self.assertEqual(len(oframes), 0)

def test_pts_assertion_same_rate(self):

resampler = AudioResampler("s16", "mono")

# resample one frame
Expand Down Expand Up @@ -115,7 +114,6 @@ def test_pts_assertion_same_rate(self):
self.assertEqual(len(oframes), 0)

def test_pts_assertion_new_rate(self):

resampler = AudioResampler("s16", "mono", 44100)

# resample one frame
Expand Down Expand Up @@ -144,7 +142,6 @@ def test_pts_assertion_new_rate(self):
self.assertEqual(oframe.samples, 16)

def test_pts_missing_time_base(self):

resampler = AudioResampler("s16", "mono", 44100)

# resample one frame
Expand Down
19 changes: 3 additions & 16 deletions tests/test_codec_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

def iter_frames(container, stream):
for packet in container.demux(stream):
for frame in packet.decode():
yield frame
yield from packet.decode()


def iter_raw_frames(path, packet_sizes, ctx):
Expand All @@ -26,15 +25,13 @@ def iter_raw_frames(path, packet_sizes, ctx):
assert read_size == size
if not read_size:
break
for frame in ctx.decode(packet):
yield frame
yield from ctx.decode(packet)
while True:
try:
frames = ctx.decode(None)
except EOFError:
break
for frame in frames:
yield frame
yield from frames
if not frames:
break

Expand Down Expand Up @@ -120,15 +117,13 @@ def test_encoder_pix_fmt(self):
self.assertEqual(ctx.pix_fmt, "yuv420p")

def test_parse(self):

# This one parses into a single packet.
self._assert_parse("mpeg4", fate_suite("h264/interlaced_crop.mp4"))

# This one parses into many small packets.
self._assert_parse("mpeg2video", fate_suite("mpeg2/mpeg2_field_encoding.ts"))

def _assert_parse(self, codec_name, path):

fh = av.open(path)
packets = []
for packet in fh.demux(video=0):
Expand All @@ -137,7 +132,6 @@ def _assert_parse(self, codec_name, path):
full_source = b"".join(bytes(p) for p in packets)

for size in 1024, 8192, 65535:

ctx = Codec(codec_name).create()
packets = []

Expand All @@ -162,7 +156,6 @@ def test_encoding_tiff(self):
self.image_sequence_encode("tiff")

def image_sequence_encode(self, codec_name):

try:
codec = Codec(codec_name, "w")
except UnknownCodecError:
Expand All @@ -187,7 +180,6 @@ def image_sequence_encode(self, codec_name):
frame_count = 1
path_list = []
for frame in iter_frames(container, video_stream):

new_frame = frame.reformat(width, height, pix_fmt)
new_packets = ctx.encode(new_frame)

Expand Down Expand Up @@ -249,7 +241,6 @@ def test_encoding_dnxhd(self):
self.video_encoding("dnxhd", options)

def video_encoding(self, codec_name, options={}, codec_tag=None):

try:
codec = Codec(codec_name, "w")
except UnknownCodecError:
Expand Down Expand Up @@ -280,9 +271,7 @@ def video_encoding(self, codec_name, options={}, codec_tag=None):
frame_count = 0

with open(path, "wb") as f:

for frame in iter_frames(container, video_stream):

new_frame = frame.reformat(width, height, pix_fmt)

# reset the picture type
Expand Down Expand Up @@ -326,7 +315,6 @@ def test_encoding_mp2(self):
self.audio_encoding("mp2")

def audio_encoding(self, codec_name):

try:
codec = Codec(codec_name, "w")
except UnknownCodecError:
Expand Down Expand Up @@ -361,7 +349,6 @@ def audio_encoding(self, codec_name):

with open(path, "wb") as f:
for frame in iter_frames(container, audio_stream):

resampled_frames = resampler.resample(frame)
for resampled_frame in resampled_frames:
samples += resampled_frame.samples
Expand Down
5 changes: 0 additions & 5 deletions tests/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class TestDecode(TestCase):
def test_decoded_video_frame_count(self):

container = av.open(fate_suite("h264/interlaced_crop.mp4"))
video_stream = next(s for s in container.streams if s.type == "video")

Expand Down Expand Up @@ -40,7 +39,6 @@ def test_decode_audio_corrupt(self):
self.assertEqual(frame_count, 0)

def test_decode_audio_sample_count(self):

container = av.open(fate_suite("audio-reference/chorusnoise_2ch_44kHz_s16.wav"))
audio_stream = next(s for s in container.streams if s.type == "audio")

Expand All @@ -58,7 +56,6 @@ def test_decode_audio_sample_count(self):
self.assertEqual(sample_count, total_samples)

def test_decoded_time_base(self):

container = av.open(fate_suite("h264/interlaced_crop.mp4"))
stream = container.streams.video[0]

Expand All @@ -71,7 +68,6 @@ def test_decoded_time_base(self):
return

def test_decoded_motion_vectors(self):

container = av.open(fate_suite("h264/interlaced_crop.mp4"))
stream = container.streams.video[0]
codec_context = stream.codec_context
Expand All @@ -88,7 +84,6 @@ def test_decoded_motion_vectors(self):
return

def test_decoded_motion_vectors_no_flag(self):

container = av.open(fate_suite("h264/interlaced_crop.mp4"))
stream = container.streams.video[0]

Expand Down
6 changes: 2 additions & 4 deletions tests/test_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TestDeprecations(TestCase):
def test_method(self):
class Example(object):
class Example:
def __init__(self, x=100):
self.x = x

Expand All @@ -22,8 +22,7 @@ def foo(self, a, b):
self.assertIn("Example.foo is deprecated", captured[0].message.args[0])

def test_renamed_attr(self):
class Example(object):

class Example:
new_value = "foo"
old_value = deprecation.renamed_attr("new_value")

Expand All @@ -35,7 +34,6 @@ def new_func(self, a, b):
obj = Example()

with warnings.catch_warnings(record=True) as captured:

self.assertEqual(obj.old_value, "foo")
self.assertIn(
"Example.old_value is deprecated", captured[0].message.args[0]
Expand Down
1 change: 0 additions & 1 deletion tests/test_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class TestDictionary(TestCase):
def test_basics(self):

d = Dictionary()
d["key"] = "value"

Expand Down
Loading

0 comments on commit 411692f

Please sign in to comment.