Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): add script statistics to job resource #9428

Merged
merged 2 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add explicit unit test coverage for the ScriptStackFrame and ScriptSt…
…atistics classes
  • Loading branch information
tswast committed Oct 9, 2019
commit 8f00cc4617bcc599ce9cadc72007398809c4194a
5 changes: 3 additions & 2 deletions bigquery/google/cloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -3481,8 +3481,9 @@ def __init__(self, resource):

@property
def procedure_id(self):
"""str: Name of the active procedure, empty if in a top-level
script.
"""Optional[str]: Name of the active procedure.

Omitted if in a top-level script.
"""
return self._properties.get("procedureId")

Expand Down
101 changes: 89 additions & 12 deletions bigquery/tests/unit/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,35 +286,26 @@ def test_script_statistics(self):
"evaluationKind": "EXPRESSION",
"stackFrames": [
{
"procedureId": "some-procedure",
"startLine": 5,
"startColumn": 29,
"endLine": 9,
"endColumn": 14,
"text": "QUERY TEXT",
},
{},
}
],
}
}
script_stats = job.script_statistics
self.assertEqual(script_stats.evaluation_kind, "EXPRESSION")
stack_frames = script_stats.stack_frames
self.assertEqual(len(stack_frames), 2)
self.assertEqual(len(stack_frames), 1)
stack_frame = stack_frames[0]
self.assertEqual(stack_frame.procedure_id, "some-procedure")
self.assertIsNone(stack_frame.procedure_id)
self.assertEqual(stack_frame.start_line, 5)
self.assertEqual(stack_frame.start_column, 29)
self.assertEqual(stack_frame.end_line, 9)
self.assertEqual(stack_frame.end_column, 14)
self.assertEqual(stack_frame.text, "QUERY TEXT")
stack_frame = stack_frames[1]
self.assertIsNone(stack_frame.procedure_id)
self.assertIsNone(stack_frame.start_line)
self.assertIsNone(stack_frame.start_column)
self.assertIsNone(stack_frame.end_line)
self.assertIsNone(stack_frame.end_column)
self.assertIsNone(stack_frame.text)

def test_num_child_jobs(self):
client = _make_client(project=self.PROJECT)
Expand Down Expand Up @@ -5379,6 +5370,92 @@ def test_end(self):
self.assertEqual(entry.end.strftime(_RFC3339_MICROS), self.END_RFC3339_MICROS)


class TestScriptStackFrame(unittest.TestCase, _Base):
def _make_one(self, resource):
from google.cloud.bigquery.job import ScriptStackFrame

return ScriptStackFrame(resource)

def test_procedure_id(self):
frame = self._make_one({"procedureId": "some-procedure"})
self.assertEqual(frame.procedure_id, "some-procedure")
del frame._properties["procedureId"]
self.assertIsNone(frame.procedure_id)

def test_start_line(self):
frame = self._make_one({"startLine": 5})
self.assertEqual(frame.start_line, 5)
frame._properties["startLine"] = "5"
self.assertEqual(frame.start_line, 5)

def test_start_column(self):
frame = self._make_one({"startColumn": 29})
self.assertEqual(frame.start_column, 29)
frame._properties["startColumn"] = "29"
self.assertEqual(frame.start_column, 29)

def test_end_line(self):
frame = self._make_one({"endLine": 9})
self.assertEqual(frame.end_line, 9)
frame._properties["endLine"] = "9"
self.assertEqual(frame.end_line, 9)

def test_end_column(self):
frame = self._make_one({"endColumn": 14})
self.assertEqual(frame.end_column, 14)
frame._properties["endColumn"] = "14"
self.assertEqual(frame.end_column, 14)

def test_text(self):
frame = self._make_one({"text": "QUERY TEXT"})
self.assertEqual(frame.text, "QUERY TEXT")


class TestScriptStatistics(unittest.TestCase, _Base):
def _make_one(self, resource):
from google.cloud.bigquery.job import ScriptStatistics

return ScriptStatistics(resource)

def test_evalutation_kind(self):
stats = self._make_one({"evaluationKind": "EXPRESSION"})
self.assertEqual(stats.evaluation_kind, "EXPRESSION")
self.assertEqual(stats.stack_frames, [])

def test_stack_frames(self):
stats = self._make_one(
{
"stackFrames": [
{
"procedureId": "some-procedure",
"startLine": 5,
"startColumn": 29,
"endLine": 9,
"endColumn": 14,
"text": "QUERY TEXT",
},
{},
]
}
)
stack_frames = stats.stack_frames
self.assertEqual(len(stack_frames), 2)
stack_frame = stack_frames[0]
self.assertEqual(stack_frame.procedure_id, "some-procedure")
self.assertEqual(stack_frame.start_line, 5)
self.assertEqual(stack_frame.start_column, 29)
self.assertEqual(stack_frame.end_line, 9)
self.assertEqual(stack_frame.end_column, 14)
self.assertEqual(stack_frame.text, "QUERY TEXT")
stack_frame = stack_frames[1]
self.assertIsNone(stack_frame.procedure_id)
self.assertIsNone(stack_frame.start_line)
self.assertIsNone(stack_frame.start_column)
self.assertIsNone(stack_frame.end_line)
self.assertIsNone(stack_frame.end_column)
self.assertIsNone(stack_frame.text)


class TestTimelineEntry(unittest.TestCase, _Base):
ELAPSED_MS = 101
ACTIVE_UNITS = 50
Expand Down