Skip to content

Commit 7eaa585

Browse files
test: use logging API in unit tests when possible (#118)
1 parent 6843a3a commit 7eaa585

13 files changed

+95
-99
lines changed

tests/unit/handlers/test_app_engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TestAppEngineHandler(unittest.TestCase):
2222
PROJECT = "PROJECT"
2323

2424
def _get_target_class(self):
25-
from google.cloud.logging_v2.handlers.app_engine import AppEngineHandler
25+
from google.cloud.logging.handlers import AppEngineHandler
2626

2727
return AppEngineHandler
2828

tests/unit/handlers/test_container_engine.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ class TestContainerEngineHandler(unittest.TestCase):
1919
PROJECT = "PROJECT"
2020

2121
def _get_target_class(self):
22-
from google.cloud.logging_v2.handlers.container_engine import (
23-
ContainerEngineHandler,
24-
)
22+
from google.cloud.logging.handlers import ContainerEngineHandler
2523

2624
return ContainerEngineHandler
2725

tests/unit/handlers/test_handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TestCloudLoggingHandler(unittest.TestCase):
2222

2323
@staticmethod
2424
def _get_target_class():
25-
from google.cloud.logging_v2.handlers.handlers import CloudLoggingHandler
25+
from google.cloud.logging.handlers import CloudLoggingHandler
2626

2727
return CloudLoggingHandler
2828

@@ -47,7 +47,7 @@ def test_ctor_defaults(self):
4747

4848
def test_ctor_explicit(self):
4949
import io
50-
from google.cloud.logging_v2.resource import Resource
50+
from google.cloud.logging import Resource
5151

5252
resource = Resource("resource_type", {"resource_label": "value"})
5353
labels = {"handler_lable": "value"}
@@ -91,7 +91,7 @@ def test_emit(self):
9191

9292
class TestSetupLogging(unittest.TestCase):
9393
def _call_fut(self, handler, excludes=None):
94-
from google.cloud.logging_v2.handlers.handlers import setup_logging
94+
from google.cloud.logging.handlers import setup_logging
9595

9696
if excludes:
9797
return setup_logging(handler, excluded_loggers=excludes)

tests/unit/handlers/transports/test_background_thread.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class TestBackgroundThreadHandler(unittest.TestCase):
2525

2626
@staticmethod
2727
def _get_target_class():
28-
from google.cloud.logging_v2.handlers.transports import (
29-
BackgroundThreadTransport,
30-
)
28+
from google.cloud.logging.handlers.transports import BackgroundThreadTransport
3129

3230
return BackgroundThreadTransport
3331

tests/unit/handlers/transports/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TestBaseHandler(unittest.TestCase):
2121

2222
@staticmethod
2323
def _get_target_class():
24-
from google.cloud.logging_v2.handlers.transports import Transport
24+
from google.cloud.logging.handlers.transports import Transport
2525

2626
return Transport
2727

tests/unit/handlers/transports/test_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TestSyncHandler(unittest.TestCase):
2222

2323
@staticmethod
2424
def _get_target_class():
25-
from google.cloud.logging_v2.handlers.transports import SyncTransport
25+
from google.cloud.logging.handlers.transports import SyncTransport
2626

2727
return SyncTransport
2828

tests/unit/test__gapic.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import google.auth.credentials
1818
import mock
1919

20-
import google.cloud.logging_v2
20+
import google.cloud.logging
2121
from google.cloud import logging_v2
2222
from google.cloud.logging_v2 import _gapic
2323
from google.cloud.logging_v2.services.config_service_v2 import ConfigServiceV2Client
@@ -91,7 +91,7 @@ def test_list_entries_with_options(self):
9191
result = client.list_entries(
9292
[PROJECT_PATH],
9393
filter_=FILTER,
94-
order_by=google.cloud.logging_v2.ASCENDING,
94+
order_by=google.cloud.logging.ASCENDING,
9595
page_size=42,
9696
page_token="token",
9797
)
@@ -103,7 +103,7 @@ def test_list_entries_with_options(self):
103103
request = call.call_args.args[0]
104104
assert request.resource_names == [PROJECT_PATH]
105105
assert request.filter == FILTER
106-
assert request.order_by == google.cloud.logging_v2.ASCENDING
106+
assert request.order_by == google.cloud.logging.ASCENDING
107107
assert request.page_size == 42
108108
assert request.page_token == "token"
109109

@@ -179,7 +179,7 @@ def test_list_sinks(self):
179179
# Check the response
180180
assert len(sinks) == 1
181181
sink = sinks[0]
182-
assert isinstance(sink, google.cloud.logging_v2.sink.Sink)
182+
assert isinstance(sink, google.cloud.logging.Sink)
183183
assert sink.name == self.SINK_NAME
184184
assert sink.destination == self.DESTINATION_URI
185185
assert sink.filter_ == FILTER
@@ -351,7 +351,7 @@ def test_list_metrics(self):
351351
# Check the response
352352
assert len(metrics) == 1
353353
metric = metrics[0]
354-
assert isinstance(metric, google.cloud.logging_v2.metric.Metric)
354+
assert isinstance(metric, google.cloud.logging.Metric)
355355
assert metric.name == self.METRIC_PATH
356356
assert metric.description == self.DESCRIPTION
357357
assert metric.filter_ == FILTER

tests/unit/test__http.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ def _make_timestamp():
130130
return NOW, _datetime_to_rfc3339_w_nanos(NOW)
131131

132132
def test_list_entries_no_paging(self):
133-
from google.cloud.logging_v2.client import Client
134-
from google.cloud.logging_v2.entries import TextEntry
135-
from google.cloud.logging_v2.logger import Logger
133+
from google.cloud.logging import Client
134+
from google.cloud.logging import TextEntry
135+
from google.cloud.logging import Logger
136136

137137
NOW, TIMESTAMP = self._make_timestamp()
138138
IID = "IID"
@@ -184,11 +184,11 @@ def test_list_entries_no_paging(self):
184184
)
185185

186186
def test_list_entries_w_paging(self):
187-
from google.cloud.logging_v2 import DESCENDING
188-
from google.cloud.logging_v2.client import Client
189-
from google.cloud.logging_v2.logger import Logger
190-
from google.cloud.logging_v2.entries import ProtobufEntry
191-
from google.cloud.logging_v2.entries import StructEntry
187+
from google.cloud.logging import DESCENDING
188+
from google.cloud.logging import Client
189+
from google.cloud.logging import Logger
190+
from google.cloud.logging import ProtobufEntry
191+
from google.cloud.logging import StructEntry
192192

193193
PROJECT1 = "PROJECT1"
194194
PROJECT1_PATH = f"projects/{PROJECT1}"
@@ -362,7 +362,7 @@ def test_ctor(self):
362362
self.assertEqual(api.api_request, connection.api_request)
363363

364364
def test_list_sinks_no_paging(self):
365-
from google.cloud.logging_v2.sink import Sink
365+
from google.cloud.logging import Sink
366366

367367
TOKEN = "TOKEN"
368368
RETURNED = {
@@ -402,7 +402,7 @@ def test_list_sinks_no_paging(self):
402402
)
403403

404404
def test_list_sinks_w_paging(self):
405-
from google.cloud.logging_v2.sink import Sink
405+
from google.cloud.logging import Sink
406406

407407
TOKEN = "TOKEN"
408408
PAGE_SIZE = 42
@@ -633,7 +633,7 @@ def _make_one(self, *args, **kw):
633633
return self._get_target_class()(*args, **kw)
634634

635635
def test_list_metrics_no_paging(self):
636-
from google.cloud.logging_v2.metric import Metric
636+
from google.cloud.logging import Metric
637637

638638
TOKEN = "TOKEN"
639639
RETURNED = {
@@ -667,7 +667,7 @@ def test_list_metrics_no_paging(self):
667667
)
668668

669669
def test_list_metrics_w_paging(self):
670-
from google.cloud.logging_v2.metric import Metric
670+
from google.cloud.logging import Metric
671671

672672
TOKEN = "TOKEN"
673673
PAGE_SIZE = 42

tests/unit/test_client.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TestClient(unittest.TestCase):
4343

4444
@staticmethod
4545
def _get_target_class():
46-
from google.cloud.logging_v2.client import Client
46+
from google.cloud.logging import Client
4747

4848
return Client
4949

@@ -238,7 +238,7 @@ def make_api(client_obj):
238238
self.assertIs(again, api)
239239

240240
def test_logger(self):
241-
from google.cloud.logging_v2.logger import Logger
241+
from google.cloud.logging import Logger
242242

243243
creds = _make_credentials()
244244
client = self._make_one(project=self.PROJECT, credentials=creds)
@@ -249,7 +249,7 @@ def test_logger(self):
249249
self.assertEqual(logger.project, self.PROJECT)
250250

251251
def test_list_entries_defaults(self):
252-
from google.cloud.logging_v2.entries import TextEntry
252+
from google.cloud.logging import TextEntry
253253

254254
IID = "IID"
255255
TEXT = "TEXT"
@@ -308,10 +308,10 @@ def test_list_entries_defaults(self):
308308
self.assertLess(yesterday - timestamp, timedelta(minutes=1))
309309

310310
def test_list_entries_explicit(self):
311-
from google.cloud.logging_v2 import DESCENDING
312-
from google.cloud.logging_v2.entries import ProtobufEntry
313-
from google.cloud.logging_v2.entries import StructEntry
314-
from google.cloud.logging_v2.logger import Logger
311+
from google.cloud.logging import DESCENDING
312+
from google.cloud.logging import ProtobufEntry
313+
from google.cloud.logging import StructEntry
314+
from google.cloud.logging import Logger
315315

316316
PROJECT1 = "PROJECT1"
317317
PROJECT2 = "PROJECT2"
@@ -404,10 +404,10 @@ def test_list_entries_explicit(self):
404404
self.assertLess(yesterday - timestamp, timedelta(minutes=1))
405405

406406
def test_list_entries_explicit_timestamp(self):
407-
from google.cloud.logging_v2 import DESCENDING
408-
from google.cloud.logging_v2.entries import ProtobufEntry
409-
from google.cloud.logging_v2.entries import StructEntry
410-
from google.cloud.logging_v2.logger import Logger
407+
from google.cloud.logging import DESCENDING
408+
from google.cloud.logging import ProtobufEntry
409+
from google.cloud.logging import StructEntry
410+
from google.cloud.logging import Logger
411411

412412
PROJECT1 = "PROJECT1"
413413
PROJECT2 = "PROJECT2"
@@ -492,7 +492,7 @@ def test_list_entries_explicit_timestamp(self):
492492
)
493493

494494
def test_sink_defaults(self):
495-
from google.cloud.logging_v2.sink import Sink
495+
from google.cloud.logging import Sink
496496

497497
creds = _make_credentials()
498498
client = self._make_one(project=self.PROJECT, credentials=creds)
@@ -505,7 +505,7 @@ def test_sink_defaults(self):
505505
self.assertEqual(sink.parent, self.PROJECT_PATH)
506506

507507
def test_sink_explicit(self):
508-
from google.cloud.logging_v2.sink import Sink
508+
from google.cloud.logging import Sink
509509

510510
creds = _make_credentials()
511511
client = self._make_one(project=self.PROJECT, credentials=creds)
@@ -520,7 +520,7 @@ def test_sink_explicit(self):
520520
self.assertEqual(sink.parent, self.PROJECT_PATH)
521521

522522
def test_list_sinks_no_paging(self):
523-
from google.cloud.logging_v2.sink import Sink
523+
from google.cloud.logging import Sink
524524

525525
PROJECT = "PROJECT"
526526
TOKEN = "TOKEN"
@@ -559,7 +559,7 @@ def test_list_sinks_no_paging(self):
559559
)
560560

561561
def test_list_sinks_with_paging(self):
562-
from google.cloud.logging_v2.sink import Sink
562+
from google.cloud.logging import Sink
563563

564564
PROJECT = "PROJECT"
565565
SINK_NAME = "sink_name"
@@ -603,7 +603,7 @@ def test_list_sinks_with_paging(self):
603603
)
604604

605605
def test_metric_defaults(self):
606-
from google.cloud.logging_v2.metric import Metric
606+
from google.cloud.logging import Metric
607607

608608
creds = _make_credentials()
609609

@@ -617,7 +617,7 @@ def test_metric_defaults(self):
617617
self.assertEqual(metric.project, self.PROJECT)
618618

619619
def test_metric_explicit(self):
620-
from google.cloud.logging_v2.metric import Metric
620+
from google.cloud.logging import Metric
621621

622622
creds = _make_credentials()
623623

@@ -633,7 +633,7 @@ def test_metric_explicit(self):
633633
self.assertEqual(metric.project, self.PROJECT)
634634

635635
def test_list_metrics_no_paging(self):
636-
from google.cloud.logging_v2.metric import Metric
636+
from google.cloud.logging import Metric
637637

638638
metrics = [
639639
{
@@ -669,7 +669,7 @@ def test_list_metrics_no_paging(self):
669669
)
670670

671671
def test_list_metrics_with_paging(self):
672-
from google.cloud.logging_v2.metric import Metric
672+
from google.cloud.logging import Metric
673673

674674
token = "TOKEN"
675675
next_token = "T00KEN"
@@ -719,7 +719,7 @@ def test_get_default_handler_app_engine(self):
719719
import os
720720
from google.cloud._testing import _Monkey
721721
from google.cloud.logging_v2.client import _APPENGINE_FLEXIBLE_ENV_VM
722-
from google.cloud.logging_v2.handlers import AppEngineHandler
722+
from google.cloud.logging.handlers import AppEngineHandler
723723

724724
credentials = _make_credentials()
725725
client = self._make_one(
@@ -734,7 +734,7 @@ def test_get_default_handler_app_engine(self):
734734
self.assertIsInstance(handler, AppEngineHandler)
735735

736736
def test_get_default_handler_container_engine(self):
737-
from google.cloud.logging_v2.handlers import ContainerEngineHandler
737+
from google.cloud.logging.handlers import ContainerEngineHandler
738738

739739
credentials = _make_credentials()
740740
client = self._make_one(
@@ -753,8 +753,8 @@ def test_get_default_handler_container_engine(self):
753753

754754
def test_get_default_handler_general(self):
755755
import io
756-
from google.cloud.logging_v2.handlers import CloudLoggingHandler
757-
from google.cloud.logging_v2.resource import Resource
756+
from google.cloud.logging.handlers import CloudLoggingHandler
757+
from google.cloud.logging import Resource
758758

759759
name = "test-logger"
760760
resource = Resource("resource_type", {"resource_label": "value"})
@@ -778,7 +778,7 @@ def test_get_default_handler_general(self):
778778
self.assertEqual(handler.labels, labels)
779779

780780
def test_setup_logging(self):
781-
from google.cloud.logging_v2.handlers import CloudLoggingHandler
781+
from google.cloud.logging.handlers import CloudLoggingHandler
782782

783783
credentials = _make_credentials()
784784
client = self._make_one(
@@ -804,8 +804,8 @@ def test_setup_logging(self):
804804

805805
def test_setup_logging_w_extra_kwargs(self):
806806
import io
807-
from google.cloud.logging_v2.handlers import CloudLoggingHandler
808-
from google.cloud.logging_v2.resource import Resource
807+
from google.cloud.logging.handlers import CloudLoggingHandler
808+
from google.cloud.logging import Resource
809809

810810
name = "test-logger"
811811
resource = Resource("resource_type", {"resource_label": "value"})

0 commit comments

Comments
 (0)