|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { VIEW_PIPELINE_UTILS } from './views'; |
| 3 | +import type { Document } from 'bson'; |
| 4 | + |
| 5 | +describe('views', function () { |
| 6 | + describe('isPipelineSearchQueryable', function () { |
| 7 | + it('should return true for a valid pipeline with $addFields stage', function () { |
| 8 | + const pipeline: Document[] = [{ $addFields: { testField: 'testValue' } }]; |
| 9 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 10 | + true, |
| 11 | + ); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should return true for a valid pipeline with $set stage', function () { |
| 15 | + const pipeline: Document[] = [{ $set: { testField: 'testValue' } }]; |
| 16 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 17 | + true, |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should return true for a valid pipeline with $match stage using $expr', function () { |
| 22 | + const pipeline: Document[] = [ |
| 23 | + { $match: { $expr: { $eq: ['$field', 'value'] } } }, |
| 24 | + ]; |
| 25 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 26 | + true, |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return false for a pipeline with an unsupported stage', function () { |
| 31 | + const pipeline: Document[] = [{ $group: { _id: '$field' } }]; |
| 32 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 33 | + false, |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return false for a $match stage without $expr', function () { |
| 38 | + const pipeline: Document[] = [{ $match: { nonExprKey: 'someValue' } }]; |
| 39 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 40 | + false, |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return false for a $match stage with $expr and additional fields', function () { |
| 45 | + const pipeline: Document[] = [ |
| 46 | + { |
| 47 | + $match: { |
| 48 | + $expr: { $eq: ['$field', 'value'] }, |
| 49 | + anotherField: 'value', |
| 50 | + }, |
| 51 | + }, |
| 52 | + ]; |
| 53 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 54 | + false, |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return true for an empty pipeline', function () { |
| 59 | + const pipeline: Document[] = []; |
| 60 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 61 | + true, |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return false if any stage in the pipeline is invalid', function () { |
| 66 | + const pipeline: Document[] = [ |
| 67 | + { $addFields: { testField: 'testValue' } }, |
| 68 | + { $match: { $expr: { $eq: ['$field', 'value'] } } }, |
| 69 | + { $group: { _id: '$field' } }, |
| 70 | + ]; |
| 71 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 72 | + false, |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle a pipeline with multiple valid stages', function () { |
| 77 | + const pipeline: Document[] = [ |
| 78 | + { $addFields: { field1: 'value1' } }, |
| 79 | + { $match: { $expr: { $eq: ['$field', 'value'] } } }, |
| 80 | + { $set: { field2: 'value2' } }, |
| 81 | + ]; |
| 82 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 83 | + true, |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should return false for a $match stage with no conditions', function () { |
| 88 | + const pipeline: Document[] = [{ $match: {} }]; |
| 89 | + expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( |
| 90 | + false, |
| 91 | + ); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('isVersionSearchCompatibleForViewsDataExplorer', function () { |
| 96 | + it('should return true for a version greater than or equal to 8.0.0', function () { |
| 97 | + expect( |
| 98 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( |
| 99 | + '8.0.0', |
| 100 | + ), |
| 101 | + ).to.equal(true); |
| 102 | + expect( |
| 103 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( |
| 104 | + '8.0.1', |
| 105 | + ), |
| 106 | + ).to.equal(true); |
| 107 | + expect( |
| 108 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( |
| 109 | + '8.1.0', |
| 110 | + ), |
| 111 | + ).to.equal(true); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should return false for a version less than 8.0.0', function () { |
| 115 | + expect( |
| 116 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( |
| 117 | + '7.9.9', |
| 118 | + ), |
| 119 | + ).to.equal(false); |
| 120 | + expect( |
| 121 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( |
| 122 | + '7.0.0', |
| 123 | + ), |
| 124 | + ).to.equal(false); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should handle invalid version format by returning false', function () { |
| 128 | + expect( |
| 129 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( |
| 130 | + 'invalid-version', |
| 131 | + ), |
| 132 | + ).to.equal(false); |
| 133 | + expect( |
| 134 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer(''), |
| 135 | + ).to.equal(false); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('isVersionSearchCompatibleForViewsCompass', function () { |
| 140 | + it('should return true for a version greater than or equal to 8.1.0', function () { |
| 141 | + expect( |
| 142 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.1.0'), |
| 143 | + ).to.equal(true); |
| 144 | + expect( |
| 145 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.1.1'), |
| 146 | + ).to.equal(true); |
| 147 | + expect( |
| 148 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.2.0'), |
| 149 | + ).to.equal(true); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should return false for a version less than 8.1.0', function () { |
| 153 | + expect( |
| 154 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.0.9'), |
| 155 | + ).to.equal(false); |
| 156 | + expect( |
| 157 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.0.0'), |
| 158 | + ).to.equal(false); |
| 159 | + expect( |
| 160 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('7.9.9'), |
| 161 | + ).to.equal(false); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should handle invalid version format by returning false', function () { |
| 165 | + expect( |
| 166 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass( |
| 167 | + 'invalid-version', |
| 168 | + ), |
| 169 | + ).to.equal(false); |
| 170 | + expect( |
| 171 | + VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass(''), |
| 172 | + ).to.equal(false); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments