Ejemplo n.º 1
0
 def setUp(self):
     self.service = Mock()
     self.client = ManagementClient(self.service)
Ejemplo n.º 2
0
 def setUp(self):
     self.service = Mock()
     self.client = ManagementClient(self.service)
Ejemplo n.º 3
0
class ManagementClientTest(unittest.TestCase):
    def setUp(self):
        self.service = Mock()
        self.client = ManagementClient(self.service)

    def mock_list(self, name):
        data = fixture("%s.json" % name)
        getattr(self.service.management(),
                name)().list.return_value.execute.return_value = data

    def assert_list_called(self, name, **kwargs):
        getattr(self.service.management(),
                name)().list.assert_called_once_with(**kwargs)

    def test_accounts(self):
        self.mock_list("accounts")

        accounts = self.client.accounts()

        self.assert_list_called("accounts")
        self.assertEqual(accounts.username,
                         "*****@*****.**")
        self.assertEqual(accounts.kind, "analytics#accounts")

        account = iter(accounts).next()
        self.assertEqual(account["id"], "26179049")

    def test_account(self):
        self.mock_list("accounts")

        account = self.client.account("26179049")

        self.assert_list_called("accounts")
        self.assertEqual(account["id"], "26179049")

    def test_account_fails_with_invalid_account_id(self):
        self.mock_list("accounts")
        self.assertRaises(
            GapyError,
            self.client.account,
            "26179040")

    def test_webproperties(self):
        self.mock_list("webproperties")

        webproperties = self.client.webproperties("26179049")

        self.assert_list_called("webproperties", accountId="26179049")
        self.assertEqual(webproperties.username,
                         "*****@*****.**")
        self.assertEqual(webproperties.kind, "analytics#webproperties")

        webproperty = iter(webproperties).next()
        self.assertEqual(webproperty["id"], "UA-26179049-1")

    def test_webproperty(self):
        self.mock_list("webproperties")

        webproperty = self.client.webproperty("26179049", "UA-26179049-1")

        self.assert_list_called("webproperties", accountId="26179049")
        self.assertEqual(webproperty["id"], "UA-26179049-1")

    def test_webproperty_fails_with_invalid_id(self):
        self.mock_list("webproperties")
        self.assertRaises(
            GapyError,
            self.client.webproperty,
            "26179049", "UA-26179049-2")

    def test_profiles(self):
        self.mock_list("profiles")

        profiles = self.client.profiles("26179049", "UA-26179049-1")

        self.assert_list_called("profiles", accountId="26179049",
                                webPropertyId="UA-26179049-1")
        self.assertEqual(profiles.username,
                         "*****@*****.**")
        self.assertEqual(profiles.kind, "analytics#profiles")

    def test_profile(self):
        self.mock_list("profiles")

        profile = self.client.profile("26179049", "UA-26179049-1", "53872948")

        self.assert_list_called("profiles", accountId="26179049",
                                webPropertyId="UA-26179049-1")
        self.assertEqual(profile["id"], "53872948")

    def test_profile_fails_with_invalid_id(self):
        self.mock_list("profiles")
        self.assertRaises(
            GapyError,
            self.client.profile,
            "26179049", "UA-26179049-2", "53872949")
Ejemplo n.º 4
0
class ManagementClientTest(unittest.TestCase):
    def setUp(self):
        self.service = Mock()
        self.client = ManagementClient(self.service)

    def mock_list(self, name):
        data = fixture("%s.json" % name)
        getattr(self.service.management(),
                name)().list.return_value.execute.return_value = data

    def assert_list_called(self, name, **kwargs):
        getattr(self.service.management(),
                name)().list.assert_called_once_with(**kwargs)

    def test_accounts(self):
        self.mock_list("accounts")

        accounts = self.client.accounts()

        self.assert_list_called("accounts")
        self.assertEqual(accounts.username,
                         "*****@*****.**")
        self.assertEqual(accounts.kind, "analytics#accounts")

        account = iter(accounts).next()
        self.assertEqual(account["id"], "26179049")

    def test_account(self):
        self.mock_list("accounts")

        account = self.client.account("26179049")

        self.assert_list_called("accounts")
        self.assertEqual(account["id"], "26179049")

    def test_account_fails_with_invalid_account_id(self):
        self.mock_list("accounts")
        self.assertRaises(GapyError, self.client.account, "26179040")

    def test_webproperties(self):
        self.mock_list("webproperties")

        webproperties = self.client.webproperties("26179049")

        self.assert_list_called("webproperties", accountId="26179049")
        self.assertEqual(webproperties.username,
                         "*****@*****.**")
        self.assertEqual(webproperties.kind, "analytics#webproperties")

        webproperty = iter(webproperties).next()
        self.assertEqual(webproperty["id"], "UA-26179049-1")

    def test_webproperty(self):
        self.mock_list("webproperties")

        webproperty = self.client.webproperty("26179049", "UA-26179049-1")

        self.assert_list_called("webproperties", accountId="26179049")
        self.assertEqual(webproperty["id"], "UA-26179049-1")

    def test_webproperty_fails_with_invalid_id(self):
        self.mock_list("webproperties")
        self.assertRaises(GapyError, self.client.webproperty, "26179049",
                          "UA-26179049-2")

    def test_profiles(self):
        self.mock_list("profiles")

        profiles = self.client.profiles("26179049", "UA-26179049-1")

        self.assert_list_called("profiles",
                                accountId="26179049",
                                webPropertyId="UA-26179049-1")
        self.assertEqual(profiles.username,
                         "*****@*****.**")
        self.assertEqual(profiles.kind, "analytics#profiles")

    def test_profile(self):
        self.mock_list("profiles")

        profile = self.client.profile("26179049", "UA-26179049-1", "53872948")

        self.assert_list_called("profiles",
                                accountId="26179049",
                                webPropertyId="UA-26179049-1")
        self.assertEqual(profile["id"], "53872948")

    def test_profile_fails_with_invalid_id(self):
        self.mock_list("profiles")
        self.assertRaises(GapyError, self.client.profile, "26179049",
                          "UA-26179049-2", "53872949")

    def test_segments(self):
        self.mock_list("segments")

        segments = self.client.segments()

        self.assert_list_called("segments")
        self.assertEqual(segments.username,
                         "*****@*****.**")
        self.assertEqual(segments.kind, "analytics#segments")

        segment = iter(segments).next()
        self.assertEqual(segment["id"], "-2")