Exemplo n.º 1
0
def test_basic_md5_sha():
    obj = {
        "product": "Coffee",
        "company": "Gobias Industries",
        "price_in_cents": 4000,
    }

    serialized_obj = (
        '{"company": "Gobias Industries", "price_in_cents": 4000, "product": "Coffee"}'
    )

    assert md5_sha_from_str(serialized_obj) == md5_sha_from_dict(obj)
    assert md5_sha_from_str(serialized_obj) == "35f22273cd6a6798b04f8ddef51135e3"
Exemplo n.º 2
0
def test_custom_default_md5_sha():
    def custom_datetime_serializer(obj: Any):
        if isinstance(obj, datetime.datetime):
            return "<datetime>"

    obj = {
        "product": "Coffee",
        "company": "Gobias Industries",
        "datetime": datetime.datetime.now(),
    }

    serialized_obj = '{"company": "Gobias Industries", "datetime": "<datetime>", "product": "Coffee"}'

    assert md5_sha_from_str(serialized_obj) == md5_sha_from_dict(
        obj, default=custom_datetime_serializer)
    assert md5_sha_from_str(
        serialized_obj) == "dc280121213aabcaeb8087aef268fd0d"
Exemplo n.º 3
0
    def _truncate_label(cls, label: str) -> str:
        """BigQuery requires column names start with either a letter or
        underscore. To make sure this is always the case, an underscore is prefixed
        to the md5 hash of the original label.

        :param label: expected expression label
        :return: truncated label
        """
        return "_" + md5_sha_from_str(label)
Exemplo n.º 4
0
def test_ignore_nan_md5_sha():
    obj = {
        "product": "Coffee",
        "company": "Gobias Industries",
        "price": math.nan,
    }

    serialized_obj = (
        '{"company": "Gobias Industries", "price": NaN, "product": "Coffee"}'
    )

    assert md5_sha_from_str(serialized_obj) == md5_sha_from_dict(obj)
    assert md5_sha_from_str(serialized_obj) == "5d129d1dffebc0bacc734366476d586d"

    serialized_obj = (
        '{"company": "Gobias Industries", "price": null, "product": "Coffee"}'
    )

    assert md5_sha_from_str(serialized_obj) == md5_sha_from_dict(obj, ignore_nan=True)
    assert md5_sha_from_str(serialized_obj) == "40e87d61f6add03816bccdeac5713b9f"
Exemplo n.º 5
0
    def _mutate_label(label: str) -> str:
        """
        BigQuery field_name should start with a letter or underscore and contain only
        alphanumeric characters. Labels that start with a number are prefixed with an
        underscore. Any unsupported characters are replaced with underscores and an
        md5 hash is added to the end of the label to avoid possible collisions.

        :param label: Expected expression label
        :return: Conditionally mutated label
        """
        label_hashed = "_" + md5_sha_from_str(label)

        # if label starts with number, add underscore as first character
        label_mutated = "_" + label if re.match(r"^\d", label) else label

        # replace non-alphanumeric characters with underscores
        label_mutated = re.sub(r"[^\w]+", "_", label_mutated)
        if label_mutated != label:
            # add first 5 chars from md5 hash to label to avoid possible collisions
            label_mutated += label_hashed[:6]

        return label_mutated
Exemplo n.º 6
0
 def digest(self) -> str:
     """
     Returns a MD5 HEX digest that makes this dashboard unique
     """
     return md5_sha_from_str(self.params or "")
Exemplo n.º 7
0
 def test_ssl_certificate_file_creation(self):
     path = create_ssl_cert_file(ssl_certificate)
     expected_filename = md5_sha_from_str(ssl_certificate)
     self.assertIn(expected_filename, path)
     self.assertTrue(os.path.exists(path))
Exemplo n.º 8
0
 def digest(self) -> str:
     """
     Returns a MD5 HEX digest that makes this dashboard unique
     """
     unique_string = f"{self.position_json}.{self.css}.{self.json_metadata}"
     return md5_sha_from_str(unique_string)