Skip to content

Commit 8f96883

Browse files
committed
churn(test): handle Redis 8 coordinate precision in GEOPOS
- Update GEOPOS tests to handle increased precision in Redis 8 (17 decimal places vs 14) - Add precision-aware coordinate comparison helper - Add comprehensive test suite for coordinate comparison function
1 parent c93114a commit 8f96883

File tree

1 file changed

+119
-5
lines changed

1 file changed

+119
-5
lines changed

packages/client/lib/commands/GEOPOS.spec.ts

Lines changed: 119 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { strict as assert } from 'node:assert';
1+
import { strict as assert, fail } from 'node:assert';
22
import testUtils, { GLOBAL } from '../test-utils';
33
import GEOPOS from './GEOPOS';
44
import { parseArgs } from './generic-transformers';
@@ -41,12 +41,126 @@ describe('GEOPOS', () => {
4141
...coordinates
4242
});
4343

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+
4871
}, {
4972
client: GLOBAL.SERVERS.OPEN,
5073
cluster: GLOBAL.CLUSTERS.OPEN
5174
});
5275
});
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

Comments
 (0)