Skip to content

Commit a9677bc

Browse files
committed
chore: split lookup tests
1 parent 98280db commit a9677bc

File tree

5 files changed

+299
-284
lines changed

5 files changed

+299
-284
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { randomUUID } from 'node:crypto'
2+
import { rm } from 'node:fs/promises'
3+
import { resolve } from 'node:path'
4+
import { afterAll, beforeAll, describe, it } from 'vitest'
5+
import { clear, lookup, reload } from './index'
6+
7+
describe('lookup (all, smallMemory)', () => {
8+
const id = randomUUID()
9+
10+
beforeAll(async () => {
11+
await reload({
12+
dataDir: resolve(__dirname, '../data', id),
13+
tmpDataDir: resolve(__dirname, '../tmp', id),
14+
fields: 'all',
15+
smallMemory: true,
16+
addCountryInfo: true,
17+
})
18+
}, 15 * 60_000)
19+
20+
afterAll(async () => {
21+
clear()
22+
await rm(resolve(__dirname, '../data', id), { recursive: true, force: true })
23+
await rm(resolve(__dirname, '../tmp', id), { recursive: true, force: true })
24+
})
25+
26+
it('should return all fields for a valid IP address', async ({ expect }) => {
27+
//* Ipv4 (Metro code)
28+
const result1 = await lookup('170.171.1.0')
29+
expect.soft(result1).not.toBeNull()
30+
expect.soft(result1).toEqual({
31+
area: 10,
32+
capital: 'Washington D.C.',
33+
city: 'New York',
34+
continent: 'NA',
35+
continent_name: 'North America',
36+
country: 'US',
37+
country_name: 'United States',
38+
country_native: 'United States',
39+
currency: [
40+
'USD',
41+
'USN',
42+
'USS',
43+
],
44+
eu: undefined,
45+
languages: [
46+
'en',
47+
],
48+
latitude: 40.7621,
49+
longitude: -73.9517,
50+
metro: 501,
51+
phone: [
52+
1,
53+
],
54+
postcode: '10044',
55+
region1: 'NY',
56+
region1_name: 'New York',
57+
timezone: 'America/New_York',
58+
})
59+
60+
//* Ipv4 (Region2)
61+
const result2 = await lookup('213.189.170.238')
62+
expect.soft(result2).not.toBeNull()
63+
expect.soft(result2).toEqual({
64+
area: 50,
65+
capital: 'Brussels',
66+
city: 'Charleroi',
67+
continent: 'EU',
68+
continent_name: 'Europe',
69+
country: 'BE',
70+
country_name: 'Belgium',
71+
country_native: 'België',
72+
currency: [
73+
'EUR',
74+
],
75+
eu: true,
76+
languages: [
77+
'nl',
78+
'fr',
79+
'de',
80+
],
81+
latitude: 50.4102,
82+
longitude: 4.4472,
83+
phone: [
84+
32,
85+
],
86+
postcode: '6000',
87+
region1: 'WAL',
88+
region1_name: 'Wallonia',
89+
region2: 'WHT',
90+
region2_name: 'Hainaut Province',
91+
timezone: 'Europe/Brussels',
92+
})
93+
94+
const postalCodesTest = [
95+
{
96+
ip: '45.67.84.0',
97+
postcode: 'EC1A',
98+
},
99+
{
100+
ip: '106.160.170.0',
101+
postcode: '402-0051',
102+
},
103+
{
104+
ip: '172.94.24.0',
105+
postcode: 'LV-1063',
106+
},
107+
]
108+
109+
for (const { ip, postcode } of postalCodesTest) {
110+
const result = await lookup(ip)
111+
expect.soft(result).not.toBeNull()
112+
expect.soft(result).toMatchObject({ postcode })
113+
}
114+
115+
//* Ipv6
116+
const result4 = await lookup('2607:F8B0:4005:801::200E')
117+
expect.soft(result4).not.toBeNull()
118+
expect.soft(result4).toEqual({
119+
area: 1000,
120+
capital: 'Washington D.C.',
121+
continent: 'NA',
122+
continent_name: 'North America',
123+
country: 'US',
124+
country_name: 'United States',
125+
country_native: 'United States',
126+
currency: [
127+
'USD',
128+
'USN',
129+
'USS',
130+
],
131+
eu: undefined,
132+
languages: [
133+
'en',
134+
],
135+
latitude: 37.751,
136+
longitude: -97.822,
137+
phone: [
138+
1,
139+
],
140+
timezone: 'America/Chicago',
141+
})
142+
143+
//* Invalid IP address
144+
await expect.soft(lookup('invalid')).rejects.toThrowError('Invalid IPv4 address: invalid')
145+
146+
//* Too high IP address
147+
await expect.soft(lookup('256.256.256.256')).resolves.toBeNull()
148+
149+
//* Clear data
150+
clear()
151+
152+
//* Ips should return null after data is cleared
153+
await expect.soft(lookup('8.8.8.8')).resolves.toBeNull()
154+
155+
await expect.soft(lookup('2607:F8B0:4005:801::200E')).resolves.toBeNull()
156+
})
157+
})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { randomUUID } from 'node:crypto'
2+
import { rm } from 'node:fs/promises'
3+
import { resolve } from 'node:path'
4+
import { afterAll, beforeAll, describe, it } from 'vitest'
5+
import { clear, lookup, reload } from './index'
6+
7+
describe('lookup (city)', () => {
8+
const id = randomUUID()
9+
10+
beforeAll(async () => {
11+
await reload({
12+
dataDir: resolve(__dirname, '../data', id),
13+
tmpDataDir: resolve(__dirname, '../tmp', id),
14+
fields: ['city', 'country'],
15+
})
16+
}, 15 * 60_000)
17+
18+
afterAll(async () => {
19+
clear()
20+
await rm(resolve(__dirname, '../data', id), { recursive: true, force: true })
21+
await rm(resolve(__dirname, '../tmp', id), { recursive: true, force: true })
22+
})
23+
24+
it('should return the city and country for a valid IP address', async ({ expect }) => {
25+
//* Ipv4
26+
const result = await lookup('170.171.1.0')
27+
expect.soft(result).not.toBeNull()
28+
expect.soft(result).toEqual({ city: 'New York', country: 'US' })
29+
30+
//* Ipv6
31+
const result2 = await lookup('2606:2e00:8003::216:3eff:fe82:68ab')
32+
expect.soft(result2).not.toBeNull()
33+
expect.soft(result2).toEqual({ city: 'New York', country: 'US' })
34+
})
35+
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { randomUUID } from 'node:crypto'
2+
import { rm } from 'node:fs/promises'
3+
import { resolve } from 'node:path'
4+
import { afterAll, beforeAll, describe, it } from 'vitest'
5+
import { clear, lookup, reload } from './index'
6+
7+
describe('lookup (country, smallMemory)', () => {
8+
const id = randomUUID()
9+
10+
beforeAll(async () => {
11+
await reload({
12+
dataDir: resolve(__dirname, '../data', id),
13+
tmpDataDir: resolve(__dirname, '../tmp', id),
14+
fields: ['country'],
15+
smallMemory: true,
16+
})
17+
}, 5 * 60_000)
18+
19+
afterAll(async () => {
20+
clear()
21+
await rm(resolve(__dirname, '../data', id), { recursive: true, force: true })
22+
await rm(resolve(__dirname, '../tmp', id), { recursive: true, force: true })
23+
})
24+
25+
it('should return the country for a valid IP address', async ({ expect }) => {
26+
//* Ipv4
27+
const result = await lookup('8.8.8.8')
28+
expect.soft(result).not.toBeNull()
29+
expect.soft(result).toEqual({ country: 'US' })
30+
31+
const result2 = await lookup('207.97.227.239')
32+
expect.soft(result2).not.toBeNull()
33+
expect.soft(result2).toEqual({ country: 'US' })
34+
35+
//* Ipv6
36+
const result3 = await lookup('2607:F8B0:4005:801::200E')
37+
expect.soft(result3).not.toBeNull()
38+
expect.soft(result3).toEqual({ country: 'US' })
39+
40+
//* Invalid IP address
41+
await expect.soft(lookup('invalid')).rejects.toThrowError('Invalid IPv4 address: invalid')
42+
43+
//* Too high IP address
44+
await expect.soft(lookup('256.256.256.256')).resolves.toBeNull()
45+
46+
//* Clear data
47+
clear()
48+
49+
//* Ips should return null after data is cleared
50+
await expect.soft(lookup('8.8.8.8')).resolves.toBeNull()
51+
52+
await expect.soft(lookup('2607:F8B0:4005:801::200E')).resolves.toBeNull()
53+
})
54+
})
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { randomUUID } from 'node:crypto'
2+
import { rm } from 'node:fs/promises'
3+
import { resolve } from 'node:path'
4+
import { afterAll, beforeAll, describe, it } from 'vitest'
5+
import { clear, lookup, reload } from './index'
6+
7+
describe('lookup (country)', () => {
8+
const id = randomUUID()
9+
10+
beforeAll(async () => {
11+
await reload({
12+
dataDir: resolve(__dirname, '../data', id),
13+
tmpDataDir: resolve(__dirname, '../tmp', id),
14+
fields: ['country'],
15+
})
16+
}, 5 * 60_000)
17+
18+
afterAll(async () => {
19+
clear()
20+
await rm(resolve(__dirname, '../data', id), { recursive: true, force: true })
21+
await rm(resolve(__dirname, '../tmp', id), { recursive: true, force: true })
22+
})
23+
24+
it('should return the country for a valid IP address', async ({ expect }) => {
25+
//* Ipv4
26+
const result = await lookup('8.8.8.8')
27+
expect.soft(result).not.toBeNull()
28+
expect.soft(result).toEqual({ country: 'US' })
29+
30+
const result2 = await lookup('207.97.227.239')
31+
expect.soft(result2).not.toBeNull()
32+
expect.soft(result2).toEqual({ country: 'US' })
33+
34+
//* Ipv6
35+
const result3 = await lookup('2607:F8B0:4005:801::200E')
36+
expect.soft(result3).not.toBeNull()
37+
expect.soft(result3).toEqual({ country: 'US' })
38+
39+
//* Invalid IP address
40+
await expect.soft(lookup('invalid')).rejects.toThrowError('Invalid IPv4 address: invalid')
41+
42+
//* Too high IP address
43+
await expect.soft(lookup('256.256.256.256')).resolves.toBeNull()
44+
45+
//* Clear data
46+
clear()
47+
48+
//* Ips should return null after data is cleared
49+
await expect.soft(lookup('8.8.8.8')).resolves.toBeNull()
50+
51+
await expect.soft(lookup('2607:F8B0:4005:801::200E')).resolves.toBeNull()
52+
})
53+
})

0 commit comments

Comments
 (0)