<commit_before>import unittest
from airpnp.device import *
from xml.etree import ElementTree


class TestDevice(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        f = open('test/device_root.xml', 'r')
        elem = ElementTree.parse(f)
        self.device = Device(elem, 'http://www.base.com')

    def test_device_attributes(self):
        device = self.device

        self.assertEqual(device.friendlyName, 'WDTVLIVE')
        self.assertEqual(device.deviceType,
                         'urn:schemas-upnp-org:device:MediaRenderer:1')
        self.assertEqual(device.manufacturer, 'Western Digital Corporation')
        self.assertEqual(device.modelName, 'WD TV HD Live')

    def test_service_count(self):
        device = self.device
        services = [s for s in device]

        self.assertEqual(len(services), 3)

    def test_getting_service_by_id(self):
        device = self.device
        service = device['urn:upnp-org:serviceId:AVTransport']

        self.assertEqual(service.__class__, Service)


class TestService(unittest.TestCase):

    def setUp(self):
        f = open('test/device_root.xml', 'r')
        elem = ElementTree.parse(f)
        self.device = Device(elem, 'http://www.base.com')

    def test_service_attributes(self):
        service = self.device['urn:upnp-org:serviceId:AVTransport']

        self.assertEqual(service.serviceType, 'urn:schemas-upnp-org:service:AVTransport:1')
        self.assertEqual(service.serviceId, 'urn:upnp-org:serviceId:AVTransport')

        # URLs are resolved using the base URL
        self.assertEqual(service.SCPDURL, 'http://www.base.com/MediaRenderer_AVTransport/scpd.xml')
        self.assertEqual(service.controlURL, 'http://www.base.com/MediaRenderer_AVTransport/control')
        self.assertEqual(service.eventSubURL, 'http://www.base.com/MediaRenderer_AVTransport/event')

    def test_service_actions(self):
        service = self.device['urn:upnp-org:serviceId:AVTransport']
        f = open('test/service_scpd.xml', 'r')
        elem = ElementTree.parse(f)
        service.initialize(elem, lambda url, msg: None)
        
        self.assertTrue(hasattr(service, 'GetCurrentTransportActions'))

    #TODO: 
    # - calling method
    # - not passing IN argument
    # - getting result<commit_msg>Added test cases for the device module.<commit_after>import unittest
import mock
import airpnp.upnp as upnp
from airpnp.device import *
from xml.etree import ElementTree


class TestDevice(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        f = open('test/device_root.xml', 'r')
        elem = ElementTree.parse(f)
        self.device = Device(elem, 'http://www.base.com')

    def test_device_base_url(self):
        self.assertEqual(self.device.get_base_url(), "http://www.base.com")

    def test_device_attributes(self):
        device = self.device

        self.assertEqual(device.friendlyName, 'WDTVLIVE')
        self.assertEqual(device.deviceType,
                         'urn:schemas-upnp-org:device:MediaRenderer:1')
        self.assertEqual(device.manufacturer, 'Western Digital Corporation')
        self.assertEqual(device.modelName, 'WD TV HD Live')

    def test_tostring(self):
        self.assertEqual(str(self.device), "WDTVLIVE [UDN=uuid:67ff722f-0090-a976-17db-e9396986c234]")

    def test_error_on_unknown_attribute(self):
        device = self.device
        with self.assertRaises(NameError):
            device.modelBlob

    def test_service_count(self):
        device = self.device
        services = [s for s in device]

        self.assertEqual(len(services), 3)

    def test_getting_service_by_id(self):
        device = self.device
        service = device['urn:upnp-org:serviceId:AVTransport']

        self.assertEqual(service.__class__, Service)


class TestService(unittest.TestCase):

    def setUp(self):
        f = open('test/device_root.xml', 'r')
        elem = ElementTree.parse(f)
        self.device = Device(elem, 'http://www.base.com')

        self.soap_sender = mock.Mock()

        self.service = self.device['urn:upnp-org:serviceId:AVTransport']
        f = open('test/service_scpd.xml', 'r')
        elem = ElementTree.parse(f)
        self.service.initialize(elem, self.soap_sender)

    def test_service_attributes(self):
        service = self.service

        self.assertEqual(service.serviceType, 'urn:schemas-upnp-org:service:AVTransport:1')
        self.assertEqual(service.serviceId, 'urn:upnp-org:serviceId:AVTransport')

    def test_resolution_of_urls(self):
        service = self.service

        # URLs are resolved using the base URL
        self.assertEqual(service.SCPDURL, 'http://www.base.com/MediaRenderer_AVTransport/scpd.xml')
        self.assertEqual(service.controlURL, 'http://www.base.com/MediaRenderer_AVTransport/control')
        self.assertEqual(service.eventSubURL, 'http://www.base.com/MediaRenderer_AVTransport/event')

    def test_service_action_existence(self):
        self.assertTrue(hasattr(self.service, 'GetCurrentTransportActions'))

    def test_service_action_calls_soap_sender(self):
        self.service.GetCurrentTransportActions(InstanceID="0")
        self.assertTrue(self.soap_sender.called)

    def test_service_action_throws_for_missing_argument(self):
        self.assertRaises(KeyError, self.service.GetCurrentTransportActions)

    def test_service_action_soap_sender_args(self):
        self.service.GetCurrentTransportActions(InstanceID="0")
        args, _ = self.soap_sender.call_args

        self.assertEqual(args[0], self.device)
        self.assertEqual(args[1], self.service.controlURL)
        self.assertEqual(args[2].__class__, upnp.SoapMessage)


    #TODO: 
    # - async + deferred
    # - getting result
{'fraction_non_alphanumeric': 0.11557876414273281, 'fraction_numerical': 0.006092254134029591, 'mean_word_length': 3.2626112759643915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 11, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3700060', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1689, 'n_words': 279}