<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import pytest
import mock

from soco import SoCo
from soco.groups import ZoneGroup
from soco.xml import XML

IP_ADDR = '192.168.1.101'


@pytest.yield_fixture()
def moco():
    """ A mock soco with fake services.

    Allows calls to services to be tracked. Should not cause any network access
    """
    services = (
        'AVTransport', 'RenderingControl', 'DeviceProperties',
        'ContentDirectory', 'ZoneGroupTopology')
    patchers = [mock.patch('soco.core.{0}'.format(service))
                for service in services]
    for patch in patchers:
        patch.start()
    yield SoCo(IP_ADDR)
    for patch in reversed(patchers):
        patch.stop()


@pytest.yield_fixture
def moco_zgs(moco):
    """A mock soco with zone group state"""
    ZGS = """<ZoneGroups>
          <ZoneGroup Coordinator="RINCON_000XXX1400" ID="RINCON_000XXXX1400:0">
            <ZoneGroupMember
                BootSeq="33"
                Configuration="1"
                Icon="x-rincon-roomicon:zoneextender"
                Invisible="1"
                IsZoneBridge="1"
                Location="http://192.168.1.100:1400/xml/device_description.xml"
                MinCompatibleVersion="22.0-00000"
                SoftwareVersion="24.1-74200"
                UUID="RINCON_000ZZZ1400"
                ZoneName="BRIDGE"/>
          </ZoneGroup>
          <ZoneGroup Coordinator="RINCON_000XXX1400" ID="RINCON_000XXX1400:46">
            <ZoneGroupMember
                BootSeq="44"
                Configuration="1"
                Icon="x-rincon-roomicon:living"
                Location="http://192.168.1.101:1400/xml/device_description.xml"
                MinCompatibleVersion="22.0-00000"
                SoftwareVersion="24.1-74200"
                UUID="RINCON_000XXX1400"
                ZoneName="Living Room"/>
            <ZoneGroupMember
                BootSeq="52"
                Configuration="1"
                Icon="x-rincon-roomicon:kitchen"
                Location="http://192.168.1.102:1400/xml/device_description.xml"
                MinCompatibleVersion="22.0-00000"
                SoftwareVersion="24.1-74200"
                UUID="RINCON_000YYY1400"
                ZoneName="Kitchen"/>
          </ZoneGroup>
        </ZoneGroups>"""
    moco.zoneGroupTopology.GetZoneGroupState.return_value = {
        'ZoneGroupState': ZGS
    }
    yield moco


@pytest.mark.parametrize('bad_ip_addr', [
    'not_ip', '555.555.555.555'
])
def test_soco_bad_ip(bad_ip_addr):
    with pytest.raises(ValueError):
            speaker = SoCo(bad_ip_addr)


def test_soco_init(moco):
    assert moco.ip_address == IP_ADDR
    assert moco.speaker_info == {}


def test_soco_str(moco):
    assert str(moco) == "<SoCo object at ip {0}>".format(IP_ADDR)


def test_soco_repr(moco):
    assert repr(moco) == 'SoCo("{0}")'.format(IP_ADDR)


class TestAVTransport:

    @pytest.mark.parametrize('playmode', [
        "NORMAL", "REPEAT_ALL", "SHUFFLE", "SHUFFLE_NOREPEAT"
    ])
    def test_soco_play_mode_values(self, moco, playmode):
        moco.avTransport.GetTransportSettings.return_value = {
            'PlayMode': playmode}
        assert moco.play_mode == playmode
        moco.avTransport.GetTransportSettings.assert_called_once_with(
            [('InstanceID', 0)]
        )
        moco.avTransport.reset_mock()

    def test_soco_play_mode_bad_value(self, moco):
        with pytest.raises(KeyError):
            moco.play_mode = "BAD_VALUE"
        assert not moco.avTransport.SetPlayMode.called

    def test_soco_play_mode_lowercase(self, moco):
        moco.play_mode = "normal"
        moco.avTransport.SetPlayMode.assert_called_once_with(
            [('InstanceID', 0), ('NewPlayMode', 'NORMAL')]
        )

    def test_soco_cross_fade(self, moco):
        moco.avTransport.GetCrossfadeMode.return_value = {'CrossfadeMode': '1'}
        assert moco.cross_fade
        moco.avTransport.GetCrossfadeMode.return_value = {'CrossfadeMode': '0'}
        assert not moco.cross_fade
        moco.avTransport.GetCrossfadeMode.assert_called_with(
            [('InstanceID', 0)]
        )
        moco.cross_fade = True
        moco.avTransport.SetCrossfadeMode.assert_called_once_with(
            [('InstanceID', 0), ('CrossfadeMode', '1')]
        )

    def test_soco_play(self, moco):
        moco.play()
        moco.avTransport.Play.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    def test_soco_pause(self, moco):
        moco.pause()
        moco.avTransport.Pause.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    def test_soco_stop(self, moco):
        moco.stop()
        moco.avTransport.Stop.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    def test_soco_next(self, moco):
        moco.next()
        moco.avTransport.Next.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    def test_soco_previous(self, moco):
        moco.previous()
        moco.avTransport.Previous.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    @pytest.mark.parametrize('bad_timestamp', [
        'NOT_VALID',
        '12:34:56:78',
        '99',
        '12:3:4'
    ])
    def test_soco_seek_invalid(self, moco, bad_timestamp):<commit_msg>Added simple test for play_uri called with uri.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import pytest
import mock

from soco import SoCo
from soco.groups import ZoneGroup
from soco.xml import XML

IP_ADDR = '192.168.1.101'


@pytest.yield_fixture()
def moco():
    """ A mock soco with fake services.

    Allows calls to services to be tracked. Should not cause any network access
    """
    services = (
        'AVTransport', 'RenderingControl', 'DeviceProperties',
        'ContentDirectory', 'ZoneGroupTopology')
    patchers = [mock.patch('soco.core.{0}'.format(service))
                for service in services]
    for patch in patchers:
        patch.start()
    yield SoCo(IP_ADDR)
    for patch in reversed(patchers):
        patch.stop()


@pytest.yield_fixture
def moco_zgs(moco):
    """A mock soco with zone group state"""
    ZGS = """<ZoneGroups>
          <ZoneGroup Coordinator="RINCON_000XXX1400" ID="RINCON_000XXXX1400:0">
            <ZoneGroupMember
                BootSeq="33"
                Configuration="1"
                Icon="x-rincon-roomicon:zoneextender"
                Invisible="1"
                IsZoneBridge="1"
                Location="http://192.168.1.100:1400/xml/device_description.xml"
                MinCompatibleVersion="22.0-00000"
                SoftwareVersion="24.1-74200"
                UUID="RINCON_000ZZZ1400"
                ZoneName="BRIDGE"/>
          </ZoneGroup>
          <ZoneGroup Coordinator="RINCON_000XXX1400" ID="RINCON_000XXX1400:46">
            <ZoneGroupMember
                BootSeq="44"
                Configuration="1"
                Icon="x-rincon-roomicon:living"
                Location="http://192.168.1.101:1400/xml/device_description.xml"
                MinCompatibleVersion="22.0-00000"
                SoftwareVersion="24.1-74200"
                UUID="RINCON_000XXX1400"
                ZoneName="Living Room"/>
            <ZoneGroupMember
                BootSeq="52"
                Configuration="1"
                Icon="x-rincon-roomicon:kitchen"
                Location="http://192.168.1.102:1400/xml/device_description.xml"
                MinCompatibleVersion="22.0-00000"
                SoftwareVersion="24.1-74200"
                UUID="RINCON_000YYY1400"
                ZoneName="Kitchen"/>
          </ZoneGroup>
        </ZoneGroups>"""
    moco.zoneGroupTopology.GetZoneGroupState.return_value = {
        'ZoneGroupState': ZGS
    }
    yield moco


@pytest.mark.parametrize('bad_ip_addr', [
    'not_ip', '555.555.555.555'
])
def test_soco_bad_ip(bad_ip_addr):
    with pytest.raises(ValueError):
            speaker = SoCo(bad_ip_addr)


def test_soco_init(moco):
    assert moco.ip_address == IP_ADDR
    assert moco.speaker_info == {}


def test_soco_str(moco):
    assert str(moco) == "<SoCo object at ip {0}>".format(IP_ADDR)


def test_soco_repr(moco):
    assert repr(moco) == 'SoCo("{0}")'.format(IP_ADDR)


class TestAVTransport:

    @pytest.mark.parametrize('playmode', [
        "NORMAL", "REPEAT_ALL", "SHUFFLE", "SHUFFLE_NOREPEAT"
    ])
    def test_soco_play_mode_values(self, moco, playmode):
        moco.avTransport.GetTransportSettings.return_value = {
            'PlayMode': playmode}
        assert moco.play_mode == playmode
        moco.avTransport.GetTransportSettings.assert_called_once_with(
            [('InstanceID', 0)]
        )
        moco.avTransport.reset_mock()

    def test_soco_play_mode_bad_value(self, moco):
        with pytest.raises(KeyError):
            moco.play_mode = "BAD_VALUE"
        assert not moco.avTransport.SetPlayMode.called

    def test_soco_play_mode_lowercase(self, moco):
        moco.play_mode = "normal"
        moco.avTransport.SetPlayMode.assert_called_once_with(
            [('InstanceID', 0), ('NewPlayMode', 'NORMAL')]
        )

    def test_soco_cross_fade(self, moco):
        moco.avTransport.GetCrossfadeMode.return_value = {'CrossfadeMode': '1'}
        assert moco.cross_fade
        moco.avTransport.GetCrossfadeMode.return_value = {'CrossfadeMode': '0'}
        assert not moco.cross_fade
        moco.avTransport.GetCrossfadeMode.assert_called_with(
            [('InstanceID', 0)]
        )
        moco.cross_fade = True
        moco.avTransport.SetCrossfadeMode.assert_called_once_with(
            [('InstanceID', 0), ('CrossfadeMode', '1')]
        )

    def test_soco_play(self, moco):
        moco.play()
        moco.avTransport.Play.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    def test_soco_play_uri(self, moco):
        uri = 'http://archive.org/download/TenD2005-07-16.flac16/TenD2005-07-16t10Wonderboy_64kb.mp3'
        moco.play_uri(uri)
        moco.avTransport.SetAVTransportURI.assert_called_once_with([
            ('InstanceID', 0),
            ('CurrentURI', uri),
            ('CurrentURIMetaData', '')
        ])

    def test_soco_pause(self, moco):
        moco.pause()
        moco.avTransport.Pause.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    def test_soco_stop(self, moco):
        moco.stop()
        moco.avTransport.Stop.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    def test_soco_next(self, moco):
        moco.next()
        moco.avTransport.Next.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    def test_soco_previous(self, moco):
        moco.previous()
        moco.avTransport.Previous.assert_called_once_with(
            [('InstanceID', 0), ('Speed', 1)]
        )

    @pytest.mark.parametrize('bad_timestamp', [
        'NOT_VALID',
        '12:34:56:78',
        '99',
        '12:3:4'
    ])
    def test_soco_seek_invalid(self, moco, bad_timestamp):
{'fraction_non_alphanumeric': 0.12277723170404789, 'fraction_numerical': 0.03967473862925565, 'mean_word_length': 2.3260029717682023, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2060112', 'n_tokens_mistral': 3987, 'n_tokens_neox': 3625, 'n_words': 570}