formatMongoDBQuery.spec.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {Fragment} from 'react';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import {formatMongoDBQuery} from 'sentry/views/insights/database/utils/formatMongoDBQuery';
  4. describe('formatMongoDBQuery', function () {
  5. it('correctly formats MongoDB JSON strings and can handle all primitive types', function () {
  6. const query =
  7. '{"stringKey":"test","insert":"my_collection","numericKey":7,"booleanKey":true,"nullKey":null}';
  8. const tokenizedQuery = formatMongoDBQuery(query, 'insert');
  9. render(<Fragment>{tokenizedQuery}</Fragment>);
  10. const boldedText = screen.getByText(/"insert": "my_collection"/i);
  11. expect(boldedText).toBeInTheDocument();
  12. // It should be bolded and correctly spaced
  13. expect(boldedText).toContainHTML('<b>"insert": "my_collection"</b>');
  14. // Get the other tokens and confirm they are not bolded
  15. const stringToken = screen.getByText(/"stringkey": "test"/i);
  16. const numericToken = screen.getByText(/"numerickey": 7/i);
  17. const booleanToken = screen.getByText(/"booleankey": true/i);
  18. const nullToken = screen.getByText(/"nullkey": null/i);
  19. expect(stringToken).toContainHTML('<span>"stringKey": "test"</span>');
  20. expect(numericToken).toContainHTML('<span>"numericKey": 7</span>');
  21. expect(booleanToken).toContainHTML('<span>"booleanKey": true</span>');
  22. expect(nullToken).toContainHTML('<span>"nullKey": null</span>');
  23. });
  24. it('correctly formats MongoDB JSON strings that includes nested objects', function () {
  25. const query =
  26. '{"objectKey":{"nestedObject":{"deeplyNested":{}}},"somethingElse":100,"find":"my_collection"}';
  27. const tokenizedQuery = formatMongoDBQuery(query, 'find');
  28. render(<Fragment>{tokenizedQuery}</Fragment>);
  29. const boldedText = screen.getByText(/"find": "my_collection"/i);
  30. expect(boldedText).toBeInTheDocument();
  31. // It should be bolded and correctly spaced
  32. expect(boldedText).toContainHTML('<b>"find": "my_collection"</b>');
  33. const objToken = screen.getByText(/"objectkey": \{/i);
  34. expect(objToken).toContainHTML(
  35. '<span>"objectKey": { "nestedObject": { "deeplyNested": {} } }</span>'
  36. );
  37. });
  38. it('correctly formats MongoDB JSON strings that include arrays', function () {
  39. const query =
  40. '{"arrayKey":[1,2,{"objInArray":{"objInObjInArray":{}}},3,4],"somethingElse":100,"delete":"my_collection"}';
  41. const tokenizedQuery = formatMongoDBQuery(query, 'delete');
  42. render(<Fragment>{tokenizedQuery}</Fragment>);
  43. const boldedText = screen.getByText(/"delete": "my_collection"/i);
  44. expect(boldedText).toBeInTheDocument();
  45. // It should be bolded and correctly spaced
  46. expect(boldedText).toContainHTML('<b>"delete": "my_collection"</b>');
  47. const arrayToken = screen.getByText(/"arraykey": \[/i);
  48. expect(arrayToken).toContainHTML(
  49. '<span>"arrayKey": [1, 2, { "objInArray": { "objInObjInArray": {} } }, 3, 4]</span>'
  50. );
  51. });
  52. it('correctly formats MongoDB JSON strings that include nested arrays', function () {
  53. const query =
  54. '{"deeplyNestedArrayWithObjects":[1,2,3,[1,2,[null,true,[{},{},{"test":[[1,2],[3,4]]},{}]]],4],"findOneAndDelete":"my_collection"}';
  55. const tokenizedQuery = formatMongoDBQuery(query, 'findOneAndDelete');
  56. render(<Fragment>{tokenizedQuery}</Fragment>);
  57. const boldedText = screen.getByText(/"findOneAndDelete": "my_collection"/i);
  58. expect(boldedText).toBeInTheDocument();
  59. // It should be bolded and correctly spaced
  60. expect(boldedText).toContainHTML('<b>"findOneAndDelete": "my_collection"</b>');
  61. const clusterFudgeToken = screen.getByText(
  62. /"deeplynestedarraywithobjects": \[1, 2, 3, \[1, 2, \[null, true, \[\{\}, \{\}, \{ "test": \[\[1, 2\], \[3, 4\]\] \}, \{\}\]\]\], 4\]/i
  63. );
  64. expect(clusterFudgeToken).toContainHTML(
  65. '<span>"deeplyNestedArrayWithObjects": [1, 2, 3, [1, 2, [null, true, [{}, {}, { "test": [[1, 2], [3, 4]] }, {}]]], 4]</span>'
  66. );
  67. });
  68. it('handles truncated MongoDB query strings by repairing the JSON', function () {
  69. const query = `{"_id":{},"test":"?","insert":"some_collection","address":"?","details":{"email":"?","nam*`;
  70. const tokenizedQuery = formatMongoDBQuery(query, 'insert');
  71. render(<Fragment>{tokenizedQuery}</Fragment>);
  72. const boldedText = screen.getByText(/"insert": "some_collection"/i);
  73. expect(boldedText).toContainHTML('<b>"insert": "some_collection"</b>');
  74. // The last entry in this case will be repaired by assigning a null value to the incomplete key
  75. const truncatedEntry = screen.getByText(
  76. /"details": \{ "email": "\?", "nam\*": null \}/i
  77. );
  78. expect(truncatedEntry).toBeInTheDocument();
  79. });
  80. it('properly handles formatting MongoDB queries when the operation entry is the last entry', function () {
  81. const query = `{"first_key":"first_value","second_key":"second_value","findOne":"my_collection"}`;
  82. const tokenizedQuery = formatMongoDBQuery(query, 'findOne');
  83. render(<Fragment>{tokenizedQuery}</Fragment>);
  84. const boldedText = screen.getByText(/"findOne": "my_collection"/i);
  85. expect(boldedText).toContainHTML('<b>"findOne": "my_collection"</b>');
  86. const commaTokens = screen.getAllByText(',');
  87. expect(commaTokens).toHaveLength(2);
  88. });
  89. });