项目作者: nicwest

项目描述 :
not so unique ids
高级语言: Python
项目地址: git://github.com/nicwest/notsouid.git
创建时间: 2017-07-07T02:10:06Z
项目社区:https://github.com/nicwest/notsouid

开源协议:MIT License

下载


PyPI version
Build Status
codecov

notsouid

Quickly patch the dynamic/random uuid methods for testing.

Inspired by freezegun

Installation

  1. pip install notsouid

Usage

  1. >>> import uuid
  2. >>> from notsouid import freeze_uuid
  3. # uuid v1
  4. >>> with freeze_uuid():
  5. ... uuid.uuid1()
  6. ...
  7. UUID('00000001-0000-0000-0000-000000000000')
  8. >>> uuid.uuid1()
  9. UUID('d358c79a-62b7-11e7-b95d-001500e9d987')
  10. >>> uuid.uuid1()
  11. UUID('d417ca3c-62b7-11e7-b95d-001500e9d987')
  12. # uuid v4
  13. >>> with freeze_uuid():
  14. ... uuid.uuid4()
  15. ...
  16. UUID('00000000-0000-0000-0000-000000000001')
  17. >>> uuid.uuid4()
  18. UUID('e8b65efd-d8d3-4048-9619-036d9ea16887')
  19. >>> uuid.uuid4()
  20. UUID('c7b6d222-3149-4ac5-9673-61f913b40393')
  21. # auto increment
  22. >>> with freeze_uuid(auto_increment=True):
  23. ... uuid.uuid1()
  24. ... uuid.uuid4()
  25. ... uuid.uuid1()
  26. ... uuid.uuid4()
  27. ... uuid.uuid1()
  28. ... uuid.uuid4()
  29. ...
  30. UUID('00000001-0000-0000-0000-000000000000')
  31. UUID('00000000-0000-0000-0000-000000000001')
  32. UUID('00000002-0000-0000-0000-000000000000')
  33. UUID('00000000-0000-0000-0000-000000000002')
  34. UUID('00000003-0000-0000-0000-000000000000')
  35. UUID('00000000-0000-0000-0000-000000000003')
  36. # custom result
  37. >>> with freeze_uuid('abcd1234-acbd-1234-a1b2-a1b2c3d4e5f6'):
  38. ... uuid.uuid4()
  39. ... uuid.uuid1()
  40. ...
  41. UUID('abcd1234-acbd-1234-a1b2-a1b2c3d4e5f6')
  42. UUID('abcd1234-acbd-1234-a1b2-a1b2c3d4e5f6')
  43. # wrap auto increment
  44. >>> with freeze_uuid('ffffffff-ffff-ffff-ffff-fffffffffffe', auto_increment=True):
  45. ... uuid.uuid4()
  46. ... uuid.uuid4()
  47. ... uuid.uuid4()
  48. ... uuid.uuid4()
  49. ...
  50. UUID('ffffffff-ffff-ffff-ffff-fffffffffffe')
  51. UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
  52. UUID('ffffffff-ffff-ffff-ffff-000000000000')
  53. UUID('ffffffff-ffff-ffff-ffff-000000000001')
  54. # decorator
  55. >>> @freeze_uuid(auto_increment=True)
  56. ... def my_func():
  57. ... return uuid.uuid4()
  58. ...
  59. >>> my_func()
  60. UUID('00000000-0000-0000-0000-000000000001')
  61. >>> my_func()
  62. UUID('00000000-0000-0000-0000-000000000002')
  63. >>> my_func()
  64. UUID('00000000-0000-0000-0000-000000000003')