diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-12-20 12:49:13 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-12-20 12:49:13 -0500 |
| commit | c54b27ab123a4bf29ba7bc76924e188e2bc88e9f (patch) | |
| tree | fa2cb4af6c8783f169a2925c510dbc4dd0de986f | |
| parent | e6afc0a8cf7a8fb18855cab9da488a0d48c42386 (diff) | |
| download | sqlalchemy-c54b27ab123a4bf29ba7bc76924e188e2bc88e9f.tar.gz | |
Implement random_choices for Python 2
Apparently py2k has no random.choices, so make a quick
one for the tests that use it.
Change-Id: Iadc3442b35f400b5bab0f711b7d3ede5dbc28f52
| -rw-r--r-- | lib/sqlalchemy/testing/util.py | 15 | ||||
| -rw-r--r-- | test/sql/test_compare.py | 5 |
2 files changed, 18 insertions, 2 deletions
diff --git a/lib/sqlalchemy/testing/util.py b/lib/sqlalchemy/testing/util.py index dbe6a383d..5244d0d37 100644 --- a/lib/sqlalchemy/testing/util.py +++ b/lib/sqlalchemy/testing/util.py @@ -66,6 +66,21 @@ def picklers(): yield pickle_.loads, lambda d: pickle_.dumps(d, protocol) +if py2k: + + def random_choices(population, k=1): + pop = list(population) + # lame but works :) + random.shuffle(pop) + return pop[0:k] + + +else: + + def random_choices(population, k=1): + return random.choices(population, k=k) + + def round_decimal(value, prec): if isinstance(value, float): return round(value, prec) diff --git a/test/sql/test_compare.py b/test/sql/test_compare.py index f8fc43ba5..6a2d47c20 100644 --- a/test/sql/test_compare.py +++ b/test/sql/test_compare.py @@ -59,6 +59,7 @@ from sqlalchemy.testing import is_false from sqlalchemy.testing import is_not_ from sqlalchemy.testing import is_true from sqlalchemy.testing import ne_ +from sqlalchemy.testing.util import random_choices from sqlalchemy.util import class_hierarchy @@ -392,13 +393,13 @@ class CoreFixtures(object): lambda: ( # same number of params each time, so compare for IN # with legacy behavior of bind for each value works - column("x").in_(random.choices(range(10), k=3)), + column("x").in_(random_choices(range(10), k=3)), # expanding IN places the whole list into a single parameter # so it can be of arbitrary length as well column("x").in_( bindparam( "q", - random.choices(range(10), k=random.randint(0, 7)), + random_choices(range(10), k=random.randint(0, 7)), expanding=True, ) ), |
