def testSimple(self): with temp.AutoTempFilePath() as filepath: with io.open(filepath, mode="w", encoding="utf-8") as filedesc: filedesc.write("""{ "foo": "bar", "quux": "norf", "thud": "blargh" }""") expected = { "foo": "bar", "quux": "norf", "thud": "blargh", } self.assertEqual(json.ReadFromPath(filepath), expected)
def CompareSchemaToKnownGood(self, schema): expected_schema_path = os.path.join(config.CONFIG["Test.data_dir"], "bigquery", "ExportedFile.schema") expected_schema_data = json.ReadFromPath(expected_schema_path) # It's easier to just compare the two dicts but even a change to the proto # description requires you to fix the json so we just compare field names # and types. schema_fields = [(x["name"], x["type"]) for x in schema] schema_metadata_fields = [ (x["name"], x["type"]) for x in schema[0]["fields"] ] expected_fields = [(x["name"], x["type"]) for x in expected_schema_data] expected_metadata_fields = [ (x["name"], x["type"]) for x in expected_schema_data[0]["fields"] ] self.assertEqual(schema_fields, expected_fields) self.assertEqual(schema_metadata_fields, expected_metadata_fields)
def _testForRegression(self): # pylint: disable=invalid-name """Checks whether there's a regression. This method is intentionally protected so that the test runner doesn't detect any tests in base regression classes. ApiRegressionTestMetaclass creates a public testForRegression method for generated regression classes. """ prev_data = json.ReadFromPath(self.output_file_name) checks = prev_data[self.__class__.handler.__name__] relevant_checks = [] for check in checks: if check["test_class"] == self.golden_file_class_name: relevant_checks.append(check) self.Run() # Make sure that this test has generated some checks. self.assertTrue(self.checks) self.assertEqual(self.checks, relevant_checks)
def testInsertData(self, mock_http, mock_build, mock_creds): bq_client = bigquery.GetBigQueryClient( service_account_json=self.SERVICE_ACCOUNT_JSON, project_id=self.PROJECT_ID) schema_path = os.path.join(config.CONFIG["Test.data_dir"], "bigquery", "ExportedFile.schema") schema_data = json.ReadFromPath(schema_path) data_fd = open( os.path.join(config.CONFIG["Test.data_dir"], "bigquery", "ExportedFile.json.gz"), "rb") now = rdfvalue.RDFDatetime.Now().AsSecondsSinceEpoch() job_id = "hunts_HFFE1D044_Results_%s" % now bq_client.InsertData("ExportedFile", data_fd, schema_data, job_id) # We should have called insert once insert = mock_build.return_value.jobs.return_value.insert self.assertEqual(insert.call_count, 1) self.assertEqual( job_id, insert.call_args_list[0][1]["body"]["jobReference"]["jobId"])
def testUnicode(self): with temp.AutoTempFilePath() as filepath: with io.open(filepath, mode="w", encoding="utf-8") as filedesc: filedesc.write("""["🐋", "🐬", "🐟"]""") self.assertEqual(json.ReadFromPath(filepath), ["🐋", "🐬", "🐟"])