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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
"""WebRTC models tests."""
from __future__ import annotations
from typing import TYPE_CHECKING
import orjson
import pytest
from tests import load_fixture
from webrtc_models import (
RTCConfiguration,
RTCIceCandidate,
RTCIceCandidateInit,
RTCIceServer,
)
if TYPE_CHECKING:
from mashumaro.mixins.orjson import DataClassORJSONMixin
from syrupy import SnapshotAssertion
@pytest.mark.parametrize(
("clazz", "filename"),
[
# RTCIceServer
(RTCIceServer, "RTCIceServer_only_urls_string.json"),
(RTCIceServer, "RTCIceServer_only_urls_list.json"),
(RTCIceServer, "RTCIceServer_urls_string.json"),
(RTCIceServer, "RTCIceServer_urls_list.json"),
# RTCConfiguration
(RTCConfiguration, "RTCConfiguration_empty.json"),
(RTCConfiguration, "RTCConfiguration_one_iceServer.json"),
(RTCConfiguration, "RTCConfiguration_multiple_iceServers.json"),
# RTCIceCandidateInit
(RTCIceCandidateInit, "RTCIceCandidateInit_end.json"),
(RTCIceCandidateInit, "RTCIceCandidateInit_candidate.json"),
],
)
def test_decoding_and_encoding(
snapshot: SnapshotAssertion,
clazz: type[DataClassORJSONMixin],
filename: str,
) -> None:
"""Test decoding/encoding."""
file_content = load_fixture(filename)
instance = clazz.from_json(file_content)
assert instance == snapshot(name="dataclass")
file_content_dict = orjson.loads(file_content)
instance_dict = instance.to_dict()
# Verify json
assert instance.to_json() == orjson.dumps(file_content_dict).decode()
# Verify dict
assert instance_dict == file_content_dict
assert instance == clazz.from_dict(instance_dict)
@pytest.mark.parametrize(
("clazz", "filename"),
[
# RTCIceCandidate
(RTCIceCandidate, "RTCIceCandidate_end.json"),
(RTCIceCandidate, "RTCIceCandidate_candidate.json"),
],
)
def test_decoding_and_encoding_deprecated(
snapshot: SnapshotAssertion,
clazz: type[DataClassORJSONMixin],
filename: str,
) -> None:
"""Test decoding/encoding."""
file_content = load_fixture(filename)
with pytest.deprecated_call():
instance = clazz.from_json(file_content)
assert instance == snapshot(name="dataclass")
file_content_dict = orjson.loads(file_content)
instance_dict = instance.to_dict()
# Verify json
assert instance.to_json() == orjson.dumps(file_content_dict).decode()
# Verify dict
assert instance_dict == file_content_dict
with pytest.deprecated_call():
assert instance == clazz.from_dict(instance_dict)
def test_no_mid_and_mlineindex() -> None:
"""Test spd_mid and sdp_multilineindex raises TypeError."""
file_content = load_fixture("RTCIceCandidate_candidate.json")
cand = RTCIceCandidateInit.from_json(file_content)
assert cand.sdp_m_line_index == 0
assert cand.sdp_mid is None
def test_invalid_mlineindex() -> None:
"""Test spd_mid and sdp_multilineindex raises TypeError."""
file_content = load_fixture("RTCIceCandidateInit_invalid.json")
msg = "sdpMLineIndex must be greater than or equal to 0"
with pytest.raises(ValueError, match=msg):
RTCIceCandidateInit.from_json(file_content)
|