Skip to content

Commit

Permalink
Merge pull request #552 from rfcx/bug/lat-long-0-country-code
Browse files Browse the repository at this point in the history
Change country_code to null when setting lat/long to 0/null
  • Loading branch information
Tooseriuz authored Mar 5, 2024
2 parents a088c47 + 480cecd commit cc3e2e1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
39 changes: 37 additions & 2 deletions core/streams/create.int.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,58 @@ describe('POST /streams', () => {
expect(stream.countryCode).toBe('GB')
})

test('country code is null for undefined lat', async () => {
test('country code is null and timezone is UTC for undefined lat', async () => {
const response = await request(app).post('/').send({ id: 'qwertyuiop40', name: 'my stream 4', latitude: undefined, longitude: -4.5 })

expect(response.statusCode).toBe(201)
const id = response.header.location.replace('/streams/', '')
const stream = await models.Stream.findByPk(id)
expect(stream.id).toBe('qwertyuiop40')
expect(stream.timezone).toBe('UTC')
expect(stream.countryCode).toBe(null)
})

test('country code is null for null lat', async () => {
test('country code is null and timezone is UTC for null lat', async () => {
const response = await request(app).post('/').send({ id: 'qwertyuiop40', name: 'my stream 4', latitude: null, longitude: -4.5 })

expect(response.statusCode).toBe(201)
const id = response.header.location.replace('/streams/', '')
const stream = await models.Stream.findByPk(id)
expect(stream.id).toBe('qwertyuiop40')
expect(stream.timezone).toBe('UTC')
expect(stream.countryCode).toBe(null)
})

test('country code is null and timezone is UTC for null lat long', async () => {
const response = await request(app).post('/').send({ id: 'qwertyuiop40', name: 'my stream 4', latitude: null, longitude: null })

expect(response.statusCode).toBe(201)
const id = response.header.location.replace('/streams/', '')
const stream = await models.Stream.findByPk(id)
expect(stream.id).toBe('qwertyuiop40')
expect(stream.timezone).toBe('UTC')
expect(stream.countryCode).toBe(null)
})

test('country code is null and timezone is UTC for 0 lat long', async () => {
const response = await request(app).post('/').send({ id: 'qwertyuiop40', name: 'my stream 4', latitude: 0, longitude: 0 })

expect(response.statusCode).toBe(201)
const id = response.header.location.replace('/streams/', '')
const stream = await models.Stream.findByPk(id)
expect(stream.id).toBe('qwertyuiop40')
expect(stream.timezone).toBe('UTC')
expect(stream.countryCode).toBe(null)
})

test('country code is null and timezone is UTC for 0 lat', async () => {
const response = await request(app).post('/').send({ id: 'qwertyuiop40', name: 'my stream 4', latitude: 0, longitude: -4.5 })

expect(response.statusCode).toBe(201)
const id = response.header.location.replace('/streams/', '')
const stream = await models.Stream.findByPk(id)
expect(stream.id).toBe('qwertyuiop40')
expect(stream.timezone).toBe('UTC')
expect(stream.countryCode).toBe(null)
})

Expand Down
1 change: 1 addition & 0 deletions core/streams/dao/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function computedAdditions (data, stream = {}) {
additions.countryCode = getCountryCodeByLatLng(latitude, longitude)
} else {
additions.timezone = 'UTC'
additions.countryCode = null
}
return additions
}
Expand Down
3 changes: 2 additions & 1 deletion core/streams/dao/index.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ describe('test computedAdditions function', () => {
test('return an empty object if lat or lng is undefined', async () => {
const additions = computedAdditions({ latitude: 54.2, longitude: undefined })
expect(additions).toStrictEqual({
timezone: 'UTC'
timezone: 'UTC',
countryCode: null
})
})
test('return expected result for the correct coordinates', async () => {
Expand Down
69 changes: 65 additions & 4 deletions core/streams/update.int.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe('PATCH /streams/:id', () => {
expect(streamUpdated.countryCode).toBe('PL')
})

test('country code is not changed for undefined lat', async () => {
test('country code is changed to null and timezone is UTC for undefined lat', async () => {
const stream = { id: 'qwertyuiop40', name: 'my stream 4', latitude: 54.2, longitude: -4.5, timezone: 'Europe/Britain', countryCode: 'GB', createdById: seedValues.primaryUserId }
await models.Stream.create(stream)
await models.UserStreamRole.create({ stream_id: stream.id, user_id: stream.createdById, role_id: seedValues.roleOwner })
Expand All @@ -245,10 +245,11 @@ describe('PATCH /streams/:id', () => {
const response = await request(app).patch(`/${stream.id}`).send(requestBody)
expect(response.statusCode).toBe(204)
const streamUpdated = await models.Stream.findByPk(stream.id)
expect(streamUpdated.countryCode).toBe('GB')
expect(streamUpdated.timezone).toBe('UTC')
expect(streamUpdated.countryCode).toBeNull()
})

test('country code is not changed for null lat', async () => {
test('country code is changed to null and timezone is UTC for null lat', async () => {
const stream = { id: 'qwertyuiop40', name: 'my stream 4', latitude: 54.2, longitude: -4.5, timezone: 'Europe/Britain', countryCode: 'GB', createdById: seedValues.primaryUserId }
await models.Stream.create(stream)
await models.UserStreamRole.create({ stream_id: stream.id, user_id: stream.createdById, role_id: seedValues.roleOwner })
Expand All @@ -257,7 +258,47 @@ describe('PATCH /streams/:id', () => {
const response2 = await request(app).patch(`/${stream.id}`).send(requestBody)
expect(response2.statusCode).toBe(204)
const streamUpdated = await models.Stream.findByPk(stream.id)
expect(streamUpdated.countryCode).toBe('GB')
expect(streamUpdated.timezone).toBe('UTC')
expect(streamUpdated.countryCode).toBeNull()
})

test('country code is changed to null and timezone is UTC for null lat long', async () => {
const stream = { id: 'qwertyuiop40', name: 'my stream 4', latitude: 54.2, longitude: -4.5, timezone: 'Europe/Britain', countryCode: 'GB', createdById: seedValues.primaryUserId }
await models.Stream.create(stream)
await models.UserStreamRole.create({ stream_id: stream.id, user_id: stream.createdById, role_id: seedValues.roleOwner })

const requestBody = { latitude: null, longitude: null }
const response2 = await request(app).patch(`/${stream.id}`).send(requestBody)
expect(response2.statusCode).toBe(204)
const streamUpdated = await models.Stream.findByPk(stream.id)
expect(streamUpdated.timezone).toBe('UTC')
expect(streamUpdated.countryCode).toBeNull()
})

test('country code is changed to null and timezone is UTC for 0 lat long', async () => {
const stream = { id: 'qwertyuiop40', name: 'my stream 4', latitude: 54.2, longitude: -4.5, timezone: 'Europe/Britain', countryCode: 'GB', createdById: seedValues.primaryUserId }
await models.Stream.create(stream)
await models.UserStreamRole.create({ stream_id: stream.id, user_id: stream.createdById, role_id: seedValues.roleOwner })

const requestBody = { latitude: 0, longitude: 0 }
const response2 = await request(app).patch(`/${stream.id}`).send(requestBody)
expect(response2.statusCode).toBe(204)
const streamUpdated = await models.Stream.findByPk(stream.id)
expect(streamUpdated.timezone).toBe('UTC')
expect(streamUpdated.countryCode).toBeNull()
})

test('country code is changed to null and timezone is UTC for 0 lat', async () => {
const stream = { id: 'qwertyuiop40', name: 'my stream 4', latitude: 54.2, longitude: -4.5, timezone: 'Europe/Britain', countryCode: 'GB', createdById: seedValues.primaryUserId }
await models.Stream.create(stream)
await models.UserStreamRole.create({ stream_id: stream.id, user_id: stream.createdById, role_id: seedValues.roleOwner })

const requestBody = { latitude: 0, longitude: -4.5 }
const response2 = await request(app).patch(`/${stream.id}`).send(requestBody)
expect(response2.statusCode).toBe(204)
const streamUpdated = await models.Stream.findByPk(stream.id)
expect(streamUpdated.timezone).toBe('UTC')
expect(streamUpdated.countryCode).toBeNull()
})

test('country code is null for coordinates somewhere in the ocean', async () => {
Expand Down Expand Up @@ -349,4 +390,24 @@ describe('PATCH /streams/:id', () => {
expect(projectAfterUpdated.minLongitude).toBe(stream2.longitude)
expect(projectAfterUpdated.maxLongitude).toBe(stream2.longitude)
})

test('min/max latitude/longitude of project change when update stream with 0 latitude 0 longitude', async () => {
const project = { id: 'p123p', createdById: seedValues.primaryUserId, name: 'Primary User Project' }
await models.Project.create(project)
await models.UserProjectRole.create({ user_id: seedValues.primaryUserId, project_id: project.id, role_id: seedValues.roleAdmin })
const stream = { id: 'jagu1', createdById: seedValues.primaryUserId, name: 'Jaguar Station', latitude: 54.2, longitude: -4.5, projectId: project.id }
const stream2 = { id: 'jagu2', createdById: seedValues.primaryUserId, name: 'Jaguar Station 2', latitude: 66.2, longitude: -10.5, projectId: project.id }
await models.Stream.create(stream)
await models.Stream.create(stream2)

const requestBody = { latitude: 0, longitude: 0 }
const response = await request(app).patch(`/${stream.id}`).send(requestBody)

expect(response.statusCode).toBe(204)
const projectAfterUpdated = await models.Project.findByPk(project.id)
expect(projectAfterUpdated.minLatitude).toBe(stream2.latitude)
expect(projectAfterUpdated.maxLatitude).toBe(stream2.latitude)
expect(projectAfterUpdated.minLongitude).toBe(stream2.longitude)
expect(projectAfterUpdated.maxLongitude).toBe(stream2.longitude)
})
})

0 comments on commit cc3e2e1

Please sign in to comment.