18
18
19
19
20
20
class Test_logger_name_from_path (unittest .TestCase ):
21
- def _call_fut (self , path ):
21
+ def _call_fut (self , path , project = None ):
22
22
from google .cloud .logging_v2 .entries import logger_name_from_path
23
23
24
- return logger_name_from_path (path )
24
+ return logger_name_from_path (path , project )
25
25
26
26
def test_w_simple_name (self ):
27
27
LOGGER_NAME = "LOGGER_NAME"
@@ -37,6 +37,30 @@ def test_w_name_w_all_extras(self):
37
37
logger_name = self ._call_fut (PATH )
38
38
self .assertEqual (logger_name , LOGGER_NAME )
39
39
40
+ def test_w_wrong_project (self ):
41
+ LOGGER_NAME = "LOGGER_NAME"
42
+ IN_PROJECT = "in-project"
43
+ PATH_PROJECT = "path-project"
44
+ PATH = "projects/%s/logs/%s" % (PATH_PROJECT , LOGGER_NAME )
45
+ with self .assertRaises (ValueError ):
46
+ self ._call_fut (PATH , IN_PROJECT )
47
+
48
+ def test_invalid_inputs (self ):
49
+ invalid_list = [
50
+ "" ,
51
+ "abc/123/logs/456" ,
52
+ "projects//logs/" ,
53
+ "projects/123/logs" ,
54
+ "projects/123logs/" ,
55
+ "projects123/logs" ,
56
+ "project/123" ,
57
+ "projects123logs456" ,
58
+ "/logs/123" ,
59
+ ]
60
+ for path in invalid_list :
61
+ with self .assertRaises (ValueError ):
62
+ self ._call_fut (path )
63
+
40
64
41
65
class Test__int_or_none (unittest .TestCase ):
42
66
def _call_fut (self , value ):
@@ -315,6 +339,62 @@ def test_from_api_repr_w_loggers_w_logger_match(self):
315
339
self .assertEqual (entry .operation , OPERATION )
316
340
self .assertIsNone (entry .payload )
317
341
342
+ def test_from_api_repr_w_folder_path (self ):
343
+ from datetime import datetime
344
+ from datetime import timedelta
345
+ from google .cloud ._helpers import UTC
346
+
347
+ client = _Client (self .PROJECT )
348
+ IID = "IID"
349
+ NOW = datetime .utcnow ().replace (tzinfo = UTC )
350
+ LATER = NOW + timedelta (seconds = 1 )
351
+ TIMESTAMP = _datetime_to_rfc3339_w_nanos (NOW )
352
+ RECEIVED = _datetime_to_rfc3339_w_nanos (LATER )
353
+ LOG_NAME = "folders/%s/logs/%s" % (self .PROJECT , self .LOGGER_NAME )
354
+ LABELS = {"foo" : "bar" , "baz" : "qux" }
355
+ TRACE = "12345678-1234-5678-1234-567812345678"
356
+ SPANID = "000000000000004a"
357
+ FILE = "my_file.py"
358
+ LINE_NO = 123
359
+ FUNCTION = "my_function"
360
+ SOURCE_LOCATION = {"file" : FILE , "line" : str (LINE_NO ), "function" : FUNCTION }
361
+ OP_ID = "OP_ID"
362
+ PRODUCER = "PRODUCER"
363
+ OPERATION = {"id" : OP_ID , "producer" : PRODUCER , "first" : True , "last" : False }
364
+ API_REPR = {
365
+ "logName" : LOG_NAME ,
366
+ "insertId" : IID ,
367
+ "timestamp" : TIMESTAMP ,
368
+ "receiveTimestamp" : RECEIVED ,
369
+ "labels" : LABELS ,
370
+ "trace" : TRACE ,
371
+ "spanId" : SPANID ,
372
+ "traceSampled" : True ,
373
+ "sourceLocation" : SOURCE_LOCATION ,
374
+ "operation" : OPERATION ,
375
+ }
376
+ klass = self ._get_target_class ()
377
+
378
+ entry = klass .from_api_repr (API_REPR , client )
379
+
380
+ self .assertEqual (entry .log_name , LOG_NAME )
381
+ self .assertIsNone (entry .logger )
382
+ self .assertEqual (entry .insert_id , IID )
383
+ self .assertEqual (entry .timestamp , NOW )
384
+ self .assertEqual (entry .received_timestamp , LATER )
385
+ self .assertEqual (entry .labels , LABELS )
386
+ self .assertEqual (entry .trace , TRACE )
387
+ self .assertEqual (entry .span_id , SPANID )
388
+ self .assertTrue (entry .trace_sampled )
389
+
390
+ source_location = entry .source_location
391
+ self .assertEqual (source_location ["file" ], FILE )
392
+ self .assertEqual (source_location ["line" ], LINE_NO )
393
+ self .assertEqual (source_location ["function" ], FUNCTION )
394
+
395
+ self .assertEqual (entry .operation , OPERATION )
396
+ self .assertIsNone (entry .payload )
397
+
318
398
def test_to_api_repr_w_source_location_no_line (self ):
319
399
from google .cloud .logging_v2 .logger import _GLOBAL_RESOURCE
320
400
0 commit comments