| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,6 +60,7 @@ | |||
| 60 | 60 | import datetime | |
| 61 | 61 | ||
| 62 | 62 | _AUTO_ID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | |
| 63 | + system_random = random.SystemRandom() | ||
| 63 | 64 | ||
| 64 | 65 | ||
| 65 | 66 | class BaseCollectionReference(Generic[QueryType]): | |
@@ -623,8 +624,11 @@ def _auto_id() -> str: | |||
| 623 | 624 | str: A 20 character string composed of digits, uppercase and | |
| 624 | 625 | lowercase and letters. | |
| 625 | 626 | """ | |
| 626 | - | ||
| 627 | - return "".join(random.choice(_AUTO_ID_CHARS) for _ in range(20)) | ||
| 627 | + try: | ||
| 628 | + return "".join(system_random.choice(_AUTO_ID_CHARS) for _ in range(20)) | ||
| 629 | + # Very old Unix systems don't have os.urandom (/dev/urandom), in which case use random.choice | ||
| 630 | + except NotImplementedError: | ||
| 631 | + return "".join(random.choice(_AUTO_ID_CHARS) for _ in range(20)) | ||
| 628 | 632 | ||
| 629 | 633 | ||
| 630 | 634 | def _item_to_document_ref(collection_reference, item): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -437,7 +437,7 @@ def test_basecollectionreference_pipeline(mock_query): | |||
| 437 | 437 | assert pipeline == mock_query._build_pipeline.return_value | |
| 438 | 438 | ||
| 439 | 439 | ||
| 440 | - @mock.patch("random.choice") | ||
| 440 | + @mock.patch("random.SystemRandom.choice") | ||
| 441 | 441 | def test__auto_id(mock_rand_choice): | |
| 442 | 442 | from google.cloud.firestore_v1.base_collection import _AUTO_ID_CHARS, _auto_id | |
| 443 | 443 | ||
@@ -450,6 +450,21 @@ def test__auto_id(mock_rand_choice): | |||
| 450 | 450 | assert mock_rand_choice.mock_calls == mock_calls | |
| 451 | 451 | ||
| 452 | 452 | ||
| 453 | + @mock.patch("random.choice") | ||
| 454 | + @mock.patch("random.SystemRandom.choice") | ||
| 455 | + def test__auto_id_fallback_to_random(mock_system_rand_choice, mock_rand_choice): | ||
| 456 | + from google.cloud.firestore_v1.base_collection import _AUTO_ID_CHARS, _auto_id | ||
| 457 | + | ||
| 458 | + mock_system_rand_choice.side_effect = NotImplementedError | ||
| 459 | + mock_result = "0123456789abcdefghij" | ||
| 460 | + mock_rand_choice.side_effect = list(mock_result) | ||
| 461 | + result = _auto_id() | ||
| 462 | + assert result == mock_result | ||
| 463 | + | ||
| 464 | + mock_calls = [mock.call(_AUTO_ID_CHARS)] * 20 | ||
| 465 | + assert mock_rand_choice.mock_calls == mock_calls | ||
| 466 | + | ||
| 467 | + | ||
| 453 | 468 | def _make_credentials(): | |
| 454 | 469 | import google.auth.credentials | |
| 455 | 470 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments