Skip to content

Commit f008e40

Browse files
committed
refactor: Enhance N1QL queries for airlines and routes
Updated N1QL queries in the Airline and Route models to include type checks for 'airline' and 'route', respectively, ensuring more accurate data retrieval. Additionally, modified the airlines_spec to include new airline entries and removed duplicates for improved test accuracy. Changes: - Added type conditions in N1QL queries for better data filtering - Updated expected airline data in airlines_spec for consistency
1 parent 653ebee commit f008e40

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

app/models/airline.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ class Airline < CouchbaseOrm::Base
2929
}
3030

3131
n1ql :to_airport, query_fn: proc { |bucket, values, options|
32-
cluster.query("SELECT raw META(air).id FROM (SELECT DISTINCT META(airline).id AS airlineId FROM `#{bucket.name}` AS route JOIN `#{bucket.name}` AS airline ON route.airlineid = META(airline).id WHERE route.destinationairport = #{quote(values[0])}) AS subquery JOIN `#{bucket.name}` AS air ON META(air).id = subquery.airlineId LIMIT #{values[1]} OFFSET #{values[2]}", options)
32+
cluster.query("SELECT raw META(air).id FROM (SELECT DISTINCT META(airline).id AS airlineId FROM `#{bucket.name}` AS route JOIN `#{bucket.name}` AS airline ON route.airlineid = META(airline).id AND airline.type = 'airline' WHERE route.type = 'route' AND route.destinationairport = #{quote(values[0])} ORDER BY META(airline).id) AS subquery JOIN `#{bucket.name}` AS air ON META(air).id = subquery.airlineId AND air.type = 'airline' LIMIT #{values[1]} OFFSET #{values[2]}", options)
3333
}
3434
end

app/models/route.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ class Route < CouchbaseOrm::Base
2828
validates :distance, presence: true, numericality: { greater_than_or_equal_to: 0 }
2929

3030
n1ql :direct_connections, query_fn: proc { |bucket, values, options|
31-
cluster.query("SELECT distinct raw meta(route).id FROM `#{bucket.name}` AS airport JOIN `#{bucket.name}` AS route ON route.sourceairport = airport.faa WHERE airport.faa = #{quote(values[0])} AND route.stops = 0 LIMIT #{values[1]} OFFSET #{values[2]}", options)
31+
cluster.query("SELECT distinct raw meta(route).id FROM `#{bucket.name}` AS airport JOIN `#{bucket.name}` AS route ON route.sourceairport = airport.faa AND route.type = 'route' WHERE airport.type = 'airport' AND airport.faa = #{quote(values[0])} AND route.stops = 0 LIMIT #{values[1]} OFFSET #{values[2]}", options)
3232
}
3333
end

spec/requests/api/v1/airlines_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,20 @@
191191
let(:offset) { '0' }
192192
let(:expected_airlines) do
193193
[
194-
{ 'callsign' => 'JETBLUE', 'country' => 'United States', 'iata' => 'B6', 'icao' => 'JBU',
195-
'name' => 'JetBlue Airways' },
196194
{ 'callsign' => 'SPEEDBIRD', 'country' => 'United Kingdom', 'iata' => 'BA', 'icao' => 'BAW',
197195
'name' => 'British Airways' },
196+
{ 'callsign' => 'AIRFRANS', 'country' => 'France', 'iata' => 'AF', 'icao' => 'AFR',
197+
'name' => 'Air France' },
198198
{ 'callsign' => 'DELTA', 'country' => 'United States', 'iata' => 'DL', 'icao' => 'DAL',
199199
'name' => 'Delta Air Lines' },
200+
{ 'callsign' => 'AMERICAN', 'country' => 'United States', 'iata' => 'AA', 'icao' => 'AAL',
201+
'name' => 'American Airlines' },
200202
{ 'callsign' => 'HAWAIIAN', 'country' => 'United States', 'iata' => 'HA', 'icao' => 'HAL',
201203
'name' => 'Hawaiian Airlines' },
204+
{ 'callsign' => 'JETBLUE', 'country' => 'United States', 'iata' => 'B6', 'icao' => 'JBU',
205+
'name' => 'JetBlue Airways' },
202206
{ 'callsign' => 'FLAGSHIP', 'country' => 'United States', 'iata' => '9E', 'icao' => 'FLG',
203207
'name' => 'Pinnacle Airlines' },
204-
{ 'callsign' => 'AMERICAN', 'country' => 'United States', 'iata' => 'AA', 'icao' => 'AAL',
205-
'name' => 'American Airlines' },
206-
{ 'callsign' => 'STARWAY', 'country' => 'France', 'iata' => 'SE', 'icao' => 'SEU',
207-
'name' => 'XL Airways France' },
208208
{ 'callsign' => 'SUN COUNTRY', 'country' => 'United States', 'iata' => 'SY', 'icao' => 'SCX',
209209
'name' => 'Sun Country Airlines' },
210210
{ 'callsign' => 'UNITED', 'country' => 'United States', 'iata' => 'UA', 'icao' => 'UAL',

0 commit comments

Comments
 (0)