Skip to content

Commit b39a810

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 b39a810

File tree

1 file changed

+109
-3
lines changed

1 file changed

+109
-3
lines changed

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

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,118 @@ describe('GEOPOS', () => {
4141
...coordinates
4242
});
4343

44-
assert.deepEqual(
45-
await client.geoPos('key', 'member'),
46-
[coordinates]
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+
assert.ok(result.length === 1, 'Expected one result');
54+
55+
assert.ok(
56+
Math.abs(Number(result[0].longitude) - Number(coordinates.longitude)) < Math.pow(10, -PRECISION),
57+
`Longitude mismatch: ${result[0].longitude} vs ${coordinates.longitude}`
58+
);
59+
assert.ok(
60+
Math.abs(Number(result[0].latitude) - Number(coordinates.latitude)) < Math.pow(10, -PRECISION),
61+
`Latitude mismatch: ${result[0].latitude} vs ${coordinates.latitude}`
4762
);
4863
}, {
4964
client: GLOBAL.SERVERS.OPEN,
5065
cluster: GLOBAL.CLUSTERS.OPEN
5166
});
5267
});
68+
69+
describe('compareWithPrecision', () => {
70+
it('should match exact same numbers', () => {
71+
assert.strictEqual(
72+
compareWithPrecision('123.456789', '123.456789', 6),
73+
true
74+
);
75+
});
76+
77+
it('should match when actual has more precision than needed', () => {
78+
assert.strictEqual(
79+
compareWithPrecision('123.456789123456', '123.456789', 6),
80+
true
81+
);
82+
});
83+
84+
it('should match when expected has more precision than needed', () => {
85+
assert.strictEqual(
86+
compareWithPrecision('123.456789', '123.456789123456', 6),
87+
true
88+
);
89+
});
90+
91+
it('should fail when decimals differ within precision', () => {
92+
assert.strictEqual(
93+
compareWithPrecision('123.456689', '123.456789', 6),
94+
false
95+
);
96+
});
97+
98+
it('should handle negative numbers', () => {
99+
assert.strictEqual(
100+
compareWithPrecision('-122.06429868936538', '-122.06429868936539', 13),
101+
true
102+
);
103+
});
104+
105+
it('should fail when integer parts differ', () => {
106+
assert.strictEqual(
107+
compareWithPrecision('124.456789', '123.456789', 6),
108+
false
109+
);
110+
});
111+
112+
it('should handle zero decimal places', () => {
113+
assert.strictEqual(
114+
compareWithPrecision('123.456789', '123.456789', 0),
115+
true
116+
);
117+
});
118+
119+
it('should handle numbers without decimal points', () => {
120+
assert.strictEqual(
121+
compareWithPrecision('123', '123', 6),
122+
true
123+
);
124+
});
125+
126+
it('should handle one number without decimal point', () => {
127+
assert.strictEqual(
128+
compareWithPrecision('123', '123.000', 3),
129+
true
130+
);
131+
});
132+
133+
it('should match Redis coordinates with different precision', () => {
134+
assert.strictEqual(
135+
compareWithPrecision(
136+
'-122.06429868936538696',
137+
'-122.06429868936539',
138+
13
139+
),
140+
true
141+
);
142+
});
143+
144+
it('should match Redis latitude with different precision', () => {
145+
assert.strictEqual(
146+
compareWithPrecision(
147+
'37.37749628831998194',
148+
'37.37749628831998',
149+
14
150+
),
151+
true
152+
);
153+
});
154+
});
155+
156+
const compareWithPrecision = (actual: string, expected: string, decimals: number): boolean => {
157+
return Math.abs(Number(actual) - Number(expected)) < Math.pow(10, -decimals);
158+
};

0 commit comments

Comments
 (0)