Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sql/2_2021_09_22_4_countries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ INSERT INTO t_country (iso, iso3, "numeric", latitude, longitude, native_name) V
('GA', 'GAB', 266, -0.80368900000000000000, 11.60944400000000000000, 'Gabon'),
('GB', 'GBR', 826, 55.37805100000000000000, -3.43597300000000000000, 'United Kingdom'),
('GD', 'GRD', 308, 12.26277600000000000000, -61.60417100000000000000, 'Grenada'),
('GE', 'GEO', 268, 32.15743510000000000000, -82.90712300000000000000, 'ბაαƒ₯αƒαƒ αƒ—αƒ•αƒ”αƒšαƒ'),
('GE', 'GEO', 268, 42.31540700000000000000, 43.35689200000000000000, 'ბაαƒ₯αƒαƒ αƒ—αƒ•αƒ”αƒšαƒ'),
('GF', 'GUF', 254, 4.00000000000000000000, -53.00000000000000000000, 'French Guiana'),
('GG', 'GGY', 831, 49.50000000000000000000, -2.56000000000000000000, 'Guernsey'),
('GH', 'GHA', 288, 7.94652700000000000000, -1.02319400000000000000, 'Ghana'),
Expand Down Expand Up @@ -207,7 +207,7 @@ INSERT INTO t_country (iso, iso3, "numeric", latitude, longitude, native_name) V
('SL', 'SLE', 694, 8.46055500000000000000, -11.77988900000000000000, 'Serra Leoa'),
('SM', 'SMR', 674, 43.94236000000000000000, 12.45777700000000000000, 'San Marino'),
('SN', 'SEN', 686, 14.49740100000000000000, -14.45236200000000000000, 'SΓ©nΓ©gal'),
('SO', 'SOM', 706, 9.98698670000000000000, 45.29938620000000000000, 'Somalia'),
('SO', 'SOM', 706, 5.15214900000000000000, 46.19961600000000000000, 'Somalia'),
('SR', 'SUR', 740, 3.91930500000000000000, -56.02778300000000000000, 'Surinam'),
('SS', 'SSD', 728, NULL, NULL, 'South Sudan'),
('ST', 'STP', 678, 0.18636000000000000000, 6.61308100000000000000, 'SΓ£o TomΓ© e PrΓ­ncipe'),
Expand Down Expand Up @@ -236,7 +236,7 @@ INSERT INTO t_country (iso, iso3, "numeric", latitude, longitude, native_name) V
('US', 'USA', 840, 37.09024000000000000000, -95.71289100000000000000, 'United States'),
('UY', 'URY', 858, -32.52277900000000000000, -55.76583500000000000000, 'Uruguay'),
('UZ', 'UZB', 860, 41.37749100000000000000, 64.58526200000000000000, 'O''zbekiston'),
('VA', 'VAT', 336, 30.03374270000000000000, -95.81166770000000000000, 'Sancta Sedes'),
('VA', 'VAT', 336, 41.90291600000000000000, 12.45338900000000000000, 'Sancta Sedes'),
('VC', 'VCT', 670, 12.98430500000000000000, -61.28722800000000000000, 'Saint Vincent and the Grenadines'),
('VE', 'VEN', 862, 6.42375000000000000000, -66.58973000000000000000, 'Venezuela'),
('VG', 'VGB', 92, 18.50000000000000000000, -64.50000000000000000000, 'Virgin Islands (British)'),
Expand Down
35 changes: 35 additions & 0 deletions sql/9_41_2026_11_11_country_coordinates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
\c grottoce;

-- =============================================================================
-- #1699: three countries were placed on a homonym
-- =============================================================================
--
-- t_country.latitude/longitude centre the map of a country page. Three rows of
-- sql/2_2021_09_22_4_countries.sql had been geocoded from the English name and
-- landed on another place of that name:
--
-- GE Georgia 32.157, -82.907 the US state of Georgia
-- VA Holy See 30.034, -95.812 a place in Texas
-- SO Somalia 9.987, 45.299 Somaliland, the north of the country
--
-- They get the centre of the country, from the same source as the other rows
-- (FR 46.227638, 2.213749; US 37.09024, -95.712891). The seed is fixed too, for
-- fresh databases.
--
-- Only a row still holding the wrong value is changed: a coordinate corrected
-- by hand in the meantime is kept. Idempotent.
--
-- Production does not run migrations on deploy: apply this file by hand.
-- =============================================================================

UPDATE t_country
SET latitude = 42.315407, longitude = 43.356892
WHERE iso = 'GE' AND latitude = 32.1574351 AND longitude = -82.907123;

UPDATE t_country
SET latitude = 41.902916, longitude = 12.453389
WHERE iso = 'VA' AND latitude = 30.0337427 AND longitude = -95.8116677;

UPDATE t_country
SET latitude = 5.152149, longitude = 46.199616
WHERE iso = 'SO' AND latitude = 9.9869867 AND longitude = 45.2993862;
118 changes: 118 additions & 0 deletions test/integration/0_models/TCountry.coordinates.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const fs = require('fs');
const path = require('path');
const should = require('should');
const supertest = require('supertest');
const { forTestDatabase } = require('../../customSQL');

// #1699: GE, VA and SO were placed on a homonym (the US state of Georgia, a
// place in Texas, Somaliland), so the map of their country page showed it.
const MIGRATION = forTestDatabase(
fs.readFileSync(
path.join(
__dirname,
'../../../sql/9_41_2026_11_11_country_coordinates.sql'
),
'utf8'
)
);
const SEED = fs.readFileSync(
path.join(__dirname, '../../../sql/2_2021_09_22_4_countries.sql'),
'utf8'
);

const COUNTRIES = [
{
id: 'GE',
iso3: 'GEO',
numeric: 268,
nativeName: 'ბაαƒ₯αƒαƒ αƒ—αƒ•αƒ”αƒšαƒ',
wrong: [32.1574351, -82.907123],
// Georgia lies between 41-44 N and 40-47 E
box: { lat: [41, 44], lon: [40, 47] },
},
{
id: 'VA',
iso3: 'VAT',
numeric: 336,
nativeName: 'Sancta Sedes',
wrong: [30.0337427, -95.8116677],
box: { lat: [41.89, 41.91], lon: [12.44, 12.46] },
},
{
id: 'SO',
iso3: 'SOM',
numeric: 706,
nativeName: 'Somalia',
wrong: [9.9869867, 45.2993862],
// The centre of Somalia, not of Somaliland (north of 8 N)
box: { lat: [2, 7], lon: [43, 49] },
},
];

const IDS = COUNTRIES.map((c) => c.id);

const runMigration = () => sails.getDatastore().sendNativeQuery(MIGRATION);

const shouldBeIn = (lat, lon, box) => {
should(lat).be.within(box.lat[0], box.lat[1]);
should(lon).be.within(box.lon[0], box.lon[1]);
};

describe('Country coordinates (#1699)', () => {
before(async () => {
await TCountry.destroy({ id: IDS });
await TCountry.createEach(
COUNTRIES.map(({ id, iso3, numeric, nativeName, wrong }) => ({
id,
iso3,
numeric,
nativeName,
latitude: String(wrong[0]),
longitude: String(wrong[1]),
}))
);
});

after(async () => {
await TCountry.destroy({ id: IDS });
});

it('moves each country from its homonym to its own centre', async () => {
await runMigration();
const rows = await TCountry.find({ id: IDS });
should(rows).have.length(COUNTRIES.length);
COUNTRIES.forEach(({ id, box }) => {
const row = rows.find((r) => r.id === id);
shouldBeIn(parseFloat(row.latitude), parseFloat(row.longitude), box);
});
});

it('serves the country page map in Georgia, not in the US state', async () => {
const res = await supertest(sails.hooks.http.app)
.get('/api/v1/countries/GE')
.set('Accept', 'application/json')
.expect(200);
shouldBeIn(res.body.latitude, res.body.longitude, COUNTRIES[0].box);
});

it('keeps a coordinate corrected by hand and can run twice', async () => {
await TCountry.updateOne({ id: 'GE' }).set({
latitude: '42',
longitude: '43.5',
});
await runMigration();
const georgia = await TCountry.findOne({ id: 'GE' });
should(parseFloat(georgia.latitude)).equal(42);
should(parseFloat(georgia.longitude)).equal(43.5);
});

it('has the right coordinates in the seed of fresh databases', () => {
COUNTRIES.forEach(({ id, iso3, box }) => {
const match = SEED.match(
new RegExp(`\\('${id}', '${iso3}', \\d+, (-?[\\d.]+), (-?[\\d.]+),`)
);
should(match).not.be.null();
shouldBeIn(parseFloat(match[1]), parseFloat(match[2]), box);
});
});
});