|
1 |
| -import { strict as assert } from 'node:assert'; |
| 1 | +import { strict as assert, fail } from 'node:assert'; |
2 | 2 | import testUtils, { GLOBAL } from '../test-utils';
|
3 | 3 | import GEOPOS from './GEOPOS';
|
4 | 4 | import { parseArgs } from './generic-transformers';
|
@@ -41,12 +41,126 @@ describe('GEOPOS', () => {
|
41 | 41 | ...coordinates
|
42 | 42 | });
|
43 | 43 |
|
44 |
| - assert.deepEqual( |
45 |
| - await client.geoPos('key', 'member'), |
46 |
| - [coordinates] |
47 |
| - ); |
| 44 | + const result = await client.geoPos('key', 'member'); |
| 45 | + |
| 46 | + /** |
| 47 | + * - Redis < 8: Returns coordinates with 14 decimal places (e.g., "-122.06429868936539") |
| 48 | + * - Redis 8+: Returns coordinates with 17 decimal places (e.g., "-122.06429868936538696") |
| 49 | + * |
| 50 | + */ |
| 51 | + const PRECISION = 13; // Number of decimal places to compare |
| 52 | + |
| 53 | + if (result && result.length === 1 && result[0] != null) { |
| 54 | + const { longitude, latitude } = result[0]; |
| 55 | + |
| 56 | + assert.ok( |
| 57 | + compareWithPrecision(longitude, coordinates.longitude, PRECISION), |
| 58 | + `Longitude mismatch: ${longitude} vs ${coordinates.longitude}` |
| 59 | + ); |
| 60 | + assert.ok( |
| 61 | + compareWithPrecision(latitude, coordinates.latitude, PRECISION), |
| 62 | + `Latitude mismatch: ${latitude} vs ${coordinates.latitude}` |
| 63 | + ); |
| 64 | + |
| 65 | + } else { |
| 66 | + assert.fail('Expected a valid result'); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + |
48 | 71 | }, {
|
49 | 72 | client: GLOBAL.SERVERS.OPEN,
|
50 | 73 | cluster: GLOBAL.CLUSTERS.OPEN
|
51 | 74 | });
|
52 | 75 | });
|
| 76 | + |
| 77 | +describe('compareWithPrecision', () => { |
| 78 | + it('should match exact same numbers', () => { |
| 79 | + assert.strictEqual( |
| 80 | + compareWithPrecision('123.456789', '123.456789', 6), |
| 81 | + true |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should match when actual has more precision than needed', () => { |
| 86 | + assert.strictEqual( |
| 87 | + compareWithPrecision('123.456789123456', '123.456789', 6), |
| 88 | + true |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should match when expected has more precision than needed', () => { |
| 93 | + assert.strictEqual( |
| 94 | + compareWithPrecision('123.456789', '123.456789123456', 6), |
| 95 | + true |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should fail when decimals differ within precision', () => { |
| 100 | + assert.strictEqual( |
| 101 | + compareWithPrecision('123.456689', '123.456789', 6), |
| 102 | + false |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should handle negative numbers', () => { |
| 107 | + assert.strictEqual( |
| 108 | + compareWithPrecision('-122.06429868936538', '-122.06429868936539', 13), |
| 109 | + true |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should fail when integer parts differ', () => { |
| 114 | + assert.strictEqual( |
| 115 | + compareWithPrecision('124.456789', '123.456789', 6), |
| 116 | + false |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should handle zero decimal places', () => { |
| 121 | + assert.strictEqual( |
| 122 | + compareWithPrecision('123.456789', '123.456789', 0), |
| 123 | + true |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should handle numbers without decimal points', () => { |
| 128 | + assert.strictEqual( |
| 129 | + compareWithPrecision('123', '123', 6), |
| 130 | + true |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle one number without decimal point', () => { |
| 135 | + assert.strictEqual( |
| 136 | + compareWithPrecision('123', '123.000', 3), |
| 137 | + true |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should match Redis coordinates with different precision', () => { |
| 142 | + assert.strictEqual( |
| 143 | + compareWithPrecision( |
| 144 | + '-122.06429868936538696', |
| 145 | + '-122.06429868936539', |
| 146 | + 13 |
| 147 | + ), |
| 148 | + true |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should match Redis latitude with different precision', () => { |
| 153 | + assert.strictEqual( |
| 154 | + compareWithPrecision( |
| 155 | + '37.37749628831998194', |
| 156 | + '37.37749628831998', |
| 157 | + 14 |
| 158 | + ), |
| 159 | + true |
| 160 | + ); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +export const compareWithPrecision = (actual: string, expected: string, decimals: number): boolean => { |
| 165 | + return Math.abs(Number(actual) - Number(expected)) < Math.pow(10, -decimals); |
| 166 | +}; |
0 commit comments