| 1 | import functools |
| 2 | import os.path |
| 3 | from collections import OrderedDict |
| 4 | |
| 5 | bucket_type_keys = OrderedDict() |
| 6 | |
| 7 | |
| 8 | def bucket_type_key(bucket_type): |
| 9 | """ |
| 10 | Registers a function that calculates test item key for the specified bucket type. |
| 11 | """ |
| 12 | |
| 13 | def decorator(f): |
| 14 | @functools.wraps(f) |
| 15 | def wrapped(item, session): |
| 16 | key = f(item) |
| 17 | |
| 18 | if session is not None: |
| 19 | for handler in session.random_order_bucket_type_key_handlers: |
| 20 | key = handler(item, key) |
| 21 | |
| 22 | return key |
| 23 | |
| 24 | bucket_type_keys[bucket_type] = wrapped |
| 25 | return wrapped |
| 26 | |
| 27 | return decorator |
| 28 | |
| 29 | |
| 30 | @bucket_type_key("global") |
| 31 | def get_global_key(item): |
| 32 | return None |
| 33 | |
| 34 | |
| 35 | @bucket_type_key("package") |
| 36 | def get_package_key(item): |
| 37 | if not hasattr(item, "module"): |
| 38 | return os.path.split(item.location[0])[0] |
| 39 | return item.module.__package__ |
| 40 | |
| 41 | |
| 42 | @bucket_type_key("module") |
| 43 | def get_module_key(item): |
| 44 | return item.location[0] |
| 45 | |
| 46 | |
| 47 | @bucket_type_key("class") |
| 48 | def get_class_key(item): |
| 49 | if not hasattr(item, "cls"): |
| 50 | return item.location[0] |
| 51 | if item.cls: |
| 52 | return item.module.__name__, item.cls.__name__ |
| 53 | else: |
| 54 | return item.module.__name__ |
| 55 | |
| 56 | |
| 57 | @bucket_type_key("parent") |
| 58 | def get_parent_key(item): |
| 59 | return item.parent |
| 60 | |
| 61 | |
| 62 | @bucket_type_key("grandparent") |
| 63 | def get_grandparent_key(item): |
| 64 | return item.parent.parent |
| 65 | |
| 66 | |
| 67 | @bucket_type_key("none") |
| 68 | def get_none_key(item): |
| 69 | raise RuntimeError("When shuffling is disabled (bucket_type=none), item key should not be calculated") |
| 70 | |
| 71 | |
| 72 | bucket_types = bucket_type_keys.keys() |