Download s3pathlib/modified_testcases/test_validate.py from Dorothydu/gitrepo_top_sample_50: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/datasets/Dorothydu/gitrepo_top_sample_50/resolve/main/s3pathlib/modified_testcases/test_validate.py
- Command line
-
hf download hf://datasets/Dorothydu/gitrepo_top_sample_50/s3pathlib/modified_testcases/test_validate.py
-
curl -L -o test_validate.py https://huggingface.co/datasets/Dorothydu/gitrepo_top_sample_50/resolve/main/s3pathlib/modified_testcases/test_validate.py
2.81 kB
| # -*- coding: utf-8 -*- | |
| import unittest | |
| from s3pathlib.validate import validate_s3_bucket, validate_s3_key, validate_s3_uri, validate_s3_arn | |
| class TestS3Validation(unittest.TestCase): | |
| def test_validate_s3_bucket(self): | |
| test_cases = [ | |
| # bad case | |
| ("", False), | |
| ("a", False), | |
| ("a" * 100, False), | |
| ("bucket@example.com", False), | |
| ("my_bucket", False), | |
| ("-my-bucket", False), | |
| ("my-bucket-", False), | |
| ("my-bucket-", False), | |
| ("192.168.0.1", False), | |
| ("xn--my-bucket", False), | |
| ("my-bucket-s3alias", False), | |
| # good case | |
| ("my-bucket", True), | |
| ] | |
| for bucket, flag in test_cases: | |
| if flag: | |
| validate_s3_bucket(bucket) | |
| else: | |
| with self.assertRaises(Exception): | |
| validate_s3_bucket(bucket) | |
| def test_validate_s3_key(self): | |
| test_cases = [ | |
| # bad cases | |
| ("a" * 2000, False), | |
| ("%20", False), | |
| # good cases | |
| ("", True), | |
| ("abcd", True), | |
| ] | |
| for key, flag in test_cases: | |
| if flag: | |
| validate_s3_key(key) | |
| else: | |
| with self.assertRaises(Exception): | |
| validate_s3_key(key) | |
| def test_validate_s3_uri(self): | |
| test_cases = [ | |
| # bad cases | |
| ("bucket/key", False), | |
| ("s3://bucket", False), | |
| ("s3://ab/%20", False), | |
| # good cases | |
| ("s3://bucket/key", True), | |
| ("s3://bucket/folder/file.txt", True), | |
| ("s3://bucket/", True), | |
| ("s3://bucket/folder/", True), | |
| ] | |
| for uri, flag in test_cases: | |
| if flag: | |
| validate_s3_uri(uri) | |
| else: | |
| with self.assertRaises(Exception): | |
| validate_s3_uri(uri) | |
| def test_validate_s3_arn(self): | |
| test_cases = [ | |
| # bad cases | |
| ("bucket/key", False), | |
| ("s3://bucket", False), | |
| ("arn:aws:s3:::b", False), | |
| ("arn:aws:s3:::{}".format("b" * 100), False), | |
| ("arn:aws:s3:::bucket/%20", False), | |
| # good cases | |
| ("arn:aws:s3:::bucket/key", True), | |
| ("arn:aws:s3:::bucket/folder/file.txt", True), | |
| ("arn:aws:s3:::bucket/", True), | |
| ("arn:aws:s3:::bucket/folder", True), | |
| ("arn:aws:s3:::bucket", True), | |
| ] | |
| for arn, flag in test_cases: | |
| if flag: | |
| validate_s3_arn(arn) | |
| else: | |
| with self.assertRaises(Exception): | |
| validate_s3_arn(arn) | |
| if __name__ == "__main__": | |
| unittest.main() |