Example #1
0
    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)

        # Using an empty list if the handler class name is not present in the
        # golden file. This way it's easy to debug new tests: we get a proper
        # regression test failure instead of a KeyError.
        checks = prev_data.get(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)

        # Always show the full diff, even when it's a bit larger.
        self.maxDiff = 100000  # pylint: disable=invalid-name

        self.assertEqual(relevant_checks, self.checks)
Example #2
0
    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)
Example #3
0
    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 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"])
Example #5
0
  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)
Example #6
0
    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), ["🐋", "🐬", "🐟"])