baker_recipes.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import random
  2. from model_bakery.random_gen import gen_string
  3. from model_bakery.recipe import Recipe
  4. from events.models import Event
  5. from .models import Issue
  6. title_choices = [
  7. "OperationalError: string is too long for tsvector",
  8. "HttpErrorResponse: Http failure response for https://app.glitchtip.com/api/0/organizations/: 403 OK",
  9. 'Error: Uncaught (in promise): at: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":403,"statusText":"OK","url":"(/api/0/api-tokens/","ok":false,"name":"HttpErrorResponse"',
  10. ]
  11. culprit_chopices = [
  12. "style-src cdn.example.com",
  13. "/message/",
  14. "http://localhost",
  15. "?(src/index)",
  16. ]
  17. metadata_choices = [
  18. {
  19. "type": "InvalidCursorName",
  20. "value": 'cursor "_django_curs_140385757349504_sync_1" does not exist\n',
  21. "filename": "django/db/models/sql/compiler.py",
  22. "function": "cursor_iter",
  23. },
  24. {
  25. "type": "SyntaxError",
  26. "value": "invalid syntax (views.py, line 165)",
  27. "filename": "organizations_ext/urls.py",
  28. "function": "<module>",
  29. },
  30. {
  31. "type": "TransactionGroup.MultipleObjectsReturned",
  32. "value": "get() returned more than one TransactionGroup -- it returned 2!",
  33. "filename": "django/db/models/query.py",
  34. "function": "get",
  35. },
  36. ]
  37. tag_choices = [
  38. {
  39. "browser": ["Chrome 109.0.0"],
  40. "os.name": ["Windows"],
  41. "release": ["glitchtip@3.0.4"],
  42. "environment": ["prod"],
  43. "browser.name": ["Chrome"],
  44. },
  45. {"release": ["glitchtip@test1"], "environment": ["staging"]},
  46. {
  47. "release": ["glitchtip@3.0.3", "glitchtip@3.0.4"],
  48. "environment": ["prod"],
  49. },
  50. ]
  51. browser_choices = ["Chrome", "Firefox", "Edge", "Opera"]
  52. os_choices = ["Linux", "Windows", "FreeBSD", "Android"]
  53. environment_choices = ["prod", "staging", "testing", "local"]
  54. def gen_string_50():
  55. return gen_string(50)
  56. def choice_or_random(choices, generator=gen_string_50):
  57. if random.getrandbits(1):
  58. return random.choice(choices)
  59. return generator()
  60. def gen_title(*args, **kwargs):
  61. # Add 8 random chars to end, to make unique
  62. return choice_or_random(title_choices) + gen_string(8)
  63. def gen_culprit(*args, **kwargs):
  64. return choice_or_random(culprit_chopices)
  65. def gen_random_metadata():
  66. if random.getrandbits(1):
  67. return {}
  68. return {
  69. "type": gen_string(20),
  70. "value": gen_string(30),
  71. "filename": gen_string(10),
  72. "function": gen_string(6),
  73. }
  74. def gen_metadata():
  75. return choice_or_random(metadata_choices, gen_random_metadata)
  76. def gen_version():
  77. return f"{random.randint(1, 300)}.{random.randint(0, 9)}"
  78. def gen_tags():
  79. tags = {}
  80. if random.getrandbits(1):
  81. browser = random.choice(browser_choices)
  82. tags["browser.name"] = browser
  83. if random.getrandbits(1):
  84. tags["browser"] = f"browser {gen_version()}"
  85. if random.getrandbits(1):
  86. tags["release"] = gen_version()
  87. if random.getrandbits(1):
  88. tags["environment"] = random.choices(environment_choices)
  89. if random.getrandbits(1):
  90. tags["os.name"] = random.choice(os_choices)
  91. return tags
  92. issue_recipe = Recipe(
  93. Issue, title=gen_title, culprit=gen_culprit, metadata=gen_metadata, tags=gen_tags
  94. )
  95. event_recipe = Recipe(Event)