diff --git a/.gitignore b/.gitignore index 59c74047..86980021 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /tmp /log /public +.byebug_history diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..c99d2e73 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..7ec7b32e --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,15 @@ +require: + - rubocop-rails + - rubocop-rspec + - rubocop-performance + +AllCops: + NewCops: enable + Exclude: + - db/schema.rb + - db/migrate/**/* + - config/**/* + - bin/**/* + +Style/Documentation: + Enabled: false diff --git a/Gemfile b/Gemfile index e20b1260..2082042c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,26 +1,33 @@ +# frozen_string_literal: true + source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.3' -gem 'rails', '~> 5.2.3' +gem 'activerecord-import', require: false +gem 'bootsnap', '>= 1.1.0', require: false +gem 'mimemagic', github: 'mimemagicrb/mimemagic', ref: '01f92d86d15d85cfd0f20dabd025dcbd36a8a60f' gem 'pg', '>= 0.18', '< 2.0' gem 'puma', '~> 3.11' -gem 'bootsnap', '>= 1.1.0', require: false +gem 'rails', '~> 5.2.3' group :development, :test do - # Call 'byebug' anywhere in the code to stop execution and get a debugger console - gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] + gem 'byebug', platforms: %i[mri mingw x64_mingw] + gem 'rack-mini-profiler' end group :development do - # Access an interactive console on exception pages or by calling 'console' anywhere in the code. - gem 'web-console', '>= 3.3.0' + gem 'bullet' gem 'listen', '>= 3.0.5', '< 3.2' -end - -group :test do + gem 'meta_request' + gem 'rspec-rails' + gem 'rubocop', require: false + gem 'rubocop-performance', require: false + gem 'rubocop-rails', require: false + gem 'rubocop-rspec', require: false + gem 'web-console', '>= 3.3.0' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] +gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] diff --git a/Gemfile.lock b/Gemfile.lock index fccf6f5f..d6bcf5de 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,10 @@ +GIT + remote: https://github.com/mimemagicrb/mimemagic.git + revision: 01f92d86d15d85cfd0f20dabd025dcbd36a8a60f + ref: 01f92d86d15d85cfd0f20dabd025dcbd36a8a60f + specs: + mimemagic (0.3.5) + GEM remote: https://rubygems.org/ specs: @@ -33,6 +40,8 @@ GEM activemodel (= 5.2.3) activesupport (= 5.2.3) arel (>= 9.0) + activerecord-import (1.2.0) + activerecord (>= 3.2) activestorage (5.2.3) actionpack (= 5.2.3) activerecord (= 5.2.3) @@ -43,13 +52,18 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) arel (9.0.0) + ast (2.4.2) bindex (0.6.0) bootsnap (1.4.2) msgpack (~> 1.0) builder (3.2.3) + bullet (6.1.5) + activesupport (>= 3.0.0) + uniform_notifier (~> 1.11) byebug (11.0.1) concurrent-ruby (1.1.5) crass (1.0.4) + diff-lcs (1.4.4) erubi (1.8.0) ffi (1.10.0) globalid (0.4.2) @@ -67,8 +81,10 @@ GEM mini_mime (>= 0.1.1) marcel (0.3.3) mimemagic (~> 0.3.2) + meta_request (0.7.3) + rack-contrib (>= 1.1, < 3) + railties (>= 3.0.0, < 7) method_source (0.9.2) - mimemagic (0.3.3) mini_mime (1.0.1) mini_portile2 (2.4.0) minitest (5.11.3) @@ -76,9 +92,16 @@ GEM nio4r (2.3.1) nokogiri (1.10.2) mini_portile2 (~> 2.4.0) + parallel (1.21.0) + parser (3.0.2.0) + ast (~> 2.4.1) pg (1.1.4) puma (3.12.1) rack (2.0.6) + rack-contrib (2.3.0) + rack (~> 2.0) + rack-mini-profiler (2.3.3) + rack (>= 1.2.0) rack-test (1.1.0) rack (>= 1.0, < 3) rails (5.2.3) @@ -105,10 +128,51 @@ GEM method_source rake (>= 0.8.7) thor (>= 0.19.0, < 2.0) + rainbow (3.0.0) rake (12.3.2) rb-fsevent (0.10.3) rb-inotify (0.10.0) ffi (~> 1.0) + regexp_parser (2.1.1) + rexml (3.1.9) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-rails (5.0.2) + actionpack (>= 5.2) + activesupport (>= 5.2) + railties (>= 5.2) + rspec-core (~> 3.10) + rspec-expectations (~> 3.10) + rspec-mocks (~> 3.10) + rspec-support (~> 3.10) + rspec-support (3.10.2) + rubocop (1.22.1) + parallel (~> 1.10) + parser (>= 3.0.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml + rubocop-ast (>= 1.12.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.12.0) + parser (>= 3.0.1.1) + rubocop-performance (1.11.5) + rubocop (>= 1.7.0, < 2.0) + rubocop-ast (>= 0.4.0) + rubocop-rails (2.12.4) + activesupport (>= 4.2.0) + rack (>= 1.1) + rubocop (>= 1.7.0, < 2.0) + rubocop-rspec (2.5.0) + rubocop (~> 1.19) + ruby-progressbar (1.11.0) ruby_dep (1.5.0) sprockets (3.7.2) concurrent-ruby (~> 1.0) @@ -121,6 +185,8 @@ GEM thread_safe (0.3.6) tzinfo (1.2.5) thread_safe (~> 0.1) + unicode-display_width (2.1.0) + uniform_notifier (1.14.2) web-console (3.7.0) actionview (>= 5.0) activemodel (>= 5.0) @@ -134,12 +200,22 @@ PLATFORMS ruby DEPENDENCIES + activerecord-import bootsnap (>= 1.1.0) + bullet byebug listen (>= 3.0.5, < 3.2) + meta_request + mimemagic! pg (>= 0.18, < 2.0) puma (~> 3.11) + rack-mini-profiler rails (~> 5.2.3) + rspec-rails + rubocop + rubocop-performance + rubocop-rails + rubocop-rspec tzinfo-data web-console (>= 3.3.0) diff --git a/Readme.md b/Readme.md index 20b4eda3..ca7ff31b 100644 --- a/Readme.md +++ b/Readme.md @@ -3,26 +3,31 @@ В этом задании вам предлагается оптимизировать учебное `rails`-приложение. Для запуска потребуется: + - `ruby 2.6.3` - `postgres` Запуск и использование: + - `bundle install` - `bin/setup` - `rails s` - `open http://localhost:3000/автобусы/Самара/Москва` ## Описание учебного приложения + Зайдя на страницу `автобусы/Самара/Москва` вы увидите расписание автобусов по этому направлению. ## Что оптимизировать ### A. Импорт данных + При выполнении `bin/setup` в базу данных загружаются данные о рейсах из файла `fixtures/small.json` Сама загрузка данных из файла делается очень наивно (и не эффективно). В комплекте с заданием поставляются файлы + - `example.json` - `small.json` (1K трипов) - `medium.json` (10K трипов) @@ -33,25 +38,30 @@ `rake reload_json[fixtures/large.json]` Для импорта этого объёма данных + - вам может помочь гем https://github.com/zdennis/activerecord-import - избегайте создания лишних транзакций - профилируйте скрипт импорта изученными инструментами и оптимизируйте его! ### Б. Отображение расписаний + Сами страницы расписаний тоже формируются не эффективно и при росте объёмов начинают сильно тормозить. Нужно найти и устранить проблемы, замедляющие формирование этих страниц. Попробуйте воспользоваться -- [ ] `rack-mini-profiler` -- [ ] `rails panel` -- [ ] `bullet` -- [ ] `explain` запросов + +- [x] `rack-mini-profiler` +- [x] `rails panel` +- [x] `bullet` +- [x] `explain` запросов ### Сдача задания + `PR` в этот репозиторий с кодом и case-study наподобие первых двух недель. На этот раз шаблона нет, законспектируйте ваш процесс оптимизации в свободной форме. В case-study указать: + - за какое время выполняется импорт файла `fixtures/large.json` - за какое время рендерится страница `автобусы/Самара/Москва` @@ -60,7 +70,8 @@ Лучше защититься от такой регрессии тестом. ### bonus -*Советую приступать к бонусу только после завершения основной части ДЗ.* + +_Советую приступать к бонусу только после завершения основной части ДЗ._ В качестве бонуса нужно справиться с импортом файлов `1M.json` (`codename mega`) и `10M.json` (`codename hardcore`) @@ -72,6 +83,7 @@ ### Мета-информация о данных При реализации импорта нужно учесть наши инсайдерские знания о данных: + - первичным ключом для автобуса считаем `(model, number)` - уникальных автобусов в файле `10M.json` ~ `10_000` - ункикльных городов в файле `10M.json` ~ `100` diff --git a/app/controllers/trips_controller.rb b/app/controllers/trips_controller.rb index acb38be2..043a3076 100644 --- a/app/controllers/trips_controller.rb +++ b/app/controllers/trips_controller.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + class TripsController < ApplicationController def index - @from = City.find_by_name!(params[:from]) - @to = City.find_by_name!(params[:to]) - @trips = Trip.where(from: @from, to: @to).order(:start_time) + @from = City.find_by!(name: params[:from]) + @to = City.find_by!(name: params[:to]) + @trips = Trip.where(from: @from, to: @to).includes(bus: %i[buses_services services]).order(:start_time) end end diff --git a/app/models/bus.rb b/app/models/bus.rb index 1dcc54cb..7869b621 100644 --- a/app/models/bus.rb +++ b/app/models/bus.rb @@ -1,15 +1,17 @@ +# frozen_string_literal: true + class Bus < ApplicationRecord - MODELS = [ - 'Икарус', - 'Мерседес', - 'Сканиа', - 'Буханка', - 'УАЗ', - 'Спринтер', - 'ГАЗ', - 'ПАЗ', - 'Вольво', - 'Газель', + MODELS = %w[ + Икарус + Мерседес + Сканиа + Буханка + УАЗ + Спринтер + ГАЗ + ПАЗ + Вольво + Газель ].freeze has_many :trips diff --git a/app/models/service.rb b/app/models/service.rb index 9cbb2a32..5d8087b4 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Service < ApplicationRecord SERVICES = [ 'WiFi', @@ -9,7 +11,7 @@ class Service < ApplicationRecord 'Телевизор общий', 'Телевизор индивидуальный', 'Стюардесса', - 'Можно не печатать билет', + 'Можно не печатать билет' ].freeze has_and_belongs_to_many :buses, join_table: :buses_services diff --git a/app/views/trips/_delimiter.html.erb b/app/views/trips/_delimiter.html.erb deleted file mode 100644 index 3f845ad0..00000000 --- a/app/views/trips/_delimiter.html.erb +++ /dev/null @@ -1 +0,0 @@ -==================================================== diff --git a/app/views/trips/_service.html.erb b/app/views/trips/_service.html.erb deleted file mode 100644 index 178ea8c0..00000000 --- a/app/views/trips/_service.html.erb +++ /dev/null @@ -1 +0,0 @@ -
  • <%= "#{service.name}" %>
  • diff --git a/app/views/trips/_services.html.erb b/app/views/trips/_services.html.erb deleted file mode 100644 index 2de639fc..00000000 --- a/app/views/trips/_services.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -
  • Сервисы в автобусе:
  • - diff --git a/app/views/trips/_trip.html.erb b/app/views/trips/_trip.html.erb index fa1de9aa..38869954 100644 --- a/app/views/trips/_trip.html.erb +++ b/app/views/trips/_trip.html.erb @@ -1,5 +1,17 @@ -
  • <%= "Отправление: #{trip.start_time}" %>
  • -
  • <%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %>
  • -
  • <%= "В пути: #{trip.duration_minutes / 60}ч. #{trip.duration_minutes % 60}мин." %>
  • -
  • <%= "Цена: #{trip.price_cents / 100}р. #{trip.price_cents % 100}коп." %>
  • -
  • <%= "Автобус: #{trip.bus.model} №#{trip.bus.number}" %>
  • + +==================================================== diff --git a/app/views/trips/index.html.erb b/app/views/trips/index.html.erb index a60bce41..32597e04 100644 --- a/app/views/trips/index.html.erb +++ b/app/views/trips/index.html.erb @@ -2,15 +2,7 @@ <%= "Автобусы #{@from.name} – #{@to.name}" %>

    - <%= "В расписании #{@trips.count} рейсов" %> + <%= "В расписании #{@trips.size} рейсов" %>

    -<% @trips.each do |trip| %> - - <%= render "delimiter" %> -<% end %> +<%= render partial: 'trip', collection: @trips, as: :trip %> diff --git a/case-study.md b/case-study.md new file mode 100644 index 00000000..7bc3933c --- /dev/null +++ b/case-study.md @@ -0,0 +1,94 @@ +# Case-study оптимизации + +## Актуальная проблема + +В нашем проекте возникла серьёзная проблема. + +Необходимо было обработать файл с данными, чуть больше ста мегабайт. + +У нас уже была программа на `ruby`, которая умела делать нужную обработку. + +Она успешно работала на файлах размером пару мегабайт, но для большого файла она работала слишком долго, и не было понятно, закончит ли она вообще работу за какое-то разумное время. + +Я решил исправить эту проблему, оптимизировав эту программу. + +## Формирование метрики + +Для того, чтобы понимать, дают ли мои изменения положительный эффект на быстродействие программы я придумал использовать такую метрику: запуск rake-задачи с функцией `time` + +```bash +time be rake reload_json\[fixtures/small.json\] +bundle exec rake reload_json\[fixtures/small.json\] 5.44s user 0.39s system 79% cpu 7.377 total +``` + +## Гарантия корректности работы оптимизированной программы + +Для корректности работы был предоставлен пример результатов, которые должны получиться и с ними происходила сверка. + +## Feedback-Loop + +Для того, чтобы иметь возможность быстро проверять гипотезы я выстроил эффективный `feedback-loop`, который позволил мне получать обратную связь по эффективности сделанных изменений за, вначале, ~7s. + +Вот как я построил `feedback_loop`: запуск + +## Вникаем в детали системы, чтобы найти главные точки роста + +Для того, чтобы найти "точки роста" для оптимизации я воспользовался: `rack-mini-profiler`, `rails panel`, `bullet`, `activerecord-import`. + +Вот какие проблемы удалось найти и решить + +- В rake-задаче я решил, что все должно выполняться батчами и не лезть лишний раз в базу с `SELECT`-ами. + +```bash +time be rake reload_json\[fixtures/example.json\] +=== before [example.json] === +0.43s user 0.15s system 68% cpu 0.843 total + +=== after [example.json] === +0.38s user 0.14s system 69% cpu 0.755 total + +time be rake reload_json\[fixtures/small.json\] +=== before [small.json] === +5.57s user 0.42s system 74% cpu 8.032 total + +=== after [small.json] === +0.43s user 0.14s system 67% cpu 0.853 total + +time be rake reload_json\[fixtures/medium.json\] +=== before [medium.json] === +40.15s user 2.23s system 77% cpu 54.928 total + +=== after [medium.json] === +0.87s user 0.15s system 74% cpu 1.373 total + +time be rake reload_json\[fixtures/large.json\] +=== after [large.json] === +2.46s user 0.23s system 62% cpu 4.313 total + +time be rake reload_json\[fixtures/1M.json\] +=== after [1M.json] === +24.88s user 1.63s system 63% cpu 41.543 total +``` + +- Для оптимизации `SELECT`-ов внутри уже работающего приложения я увидел частые обращения через внешние ключи, поэтому накинул на них B-индексы. + +- Такие же индексы я добавил на поля, через которые идут частые `SELECT`-ы. + +- Для оптимизации SSR страницы Rails, с помощью `bullet` я заметил, что присутствует ряд N+1 запросов, поэтому добавил `includes` для прелоада информации из базы. + +- С помощью `rack-mini-profiler` обнаружил, что большое количество паршалов рендерятся очень медленно, поэтому слил их в один паршал `_trip.html.erb` и отрендерил их коллекцией. + +## Результаты + +В результате проделанной оптимизации наконец удалось обработать файл с данными. + +- Удалось улучшить метрику загрузки дампа с `~54s` до `~1.4s` для файла `medium.json` и уложиться в заданный бюджет. +- Страница `/автобусы/Самара/Москва` для дампа из файла `large.json` грузится со скоростью: + + - по информации `rack-mini-profiler` ~1328ms + - по информации Rails консоли ~772ms + +## Тесты + +- Были написаны тесты функциональности +- К сожалению, не получилось применить тесты производительности к Rake задаче. Они словно показывают время самого вызова функции, как результат это микросекунды (11 μs). diff --git a/config/environments/development.rb b/config/environments/development.rb index 1311e3e4..90792c10 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,4 +1,14 @@ Rails.application.configure do + config.after_initialize do + Bullet.enable = true + Bullet.alert = true + Bullet.bullet_logger = true + Bullet.console = true + # Bullet.growl = true + Bullet.rails_logger = true + Bullet.add_footer = true + end + # Settings specified here will take precedence over those in config/application.rb. # In the development environment your application's code is reloaded on diff --git a/db/migrate/20211015101647_add_indexes_to_tables.rb b/db/migrate/20211015101647_add_indexes_to_tables.rb new file mode 100644 index 00000000..d1ab2896 --- /dev/null +++ b/db/migrate/20211015101647_add_indexes_to_tables.rb @@ -0,0 +1,12 @@ +class AddIndexesToTables < ActiveRecord::Migration[5.2] + def change + add_index :buses, :number, unique: true + add_index :buses_services, %i[bus_id service_id], unique: true + add_index :buses_services, %i[service_id bus_id], unique: true + add_index :cities, :name, unique: true + add_index :services, :name + add_index :trips, :from_id + add_index :trips, :to_id + add_index :trips, :bus_id + end +end diff --git a/db/schema.rb b/db/schema.rb index f6921e45..3adf0c53 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_03_30_193044) do +ActiveRecord::Schema.define(version: 2021_10_15_101647) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -18,19 +18,24 @@ create_table "buses", force: :cascade do |t| t.string "number" t.string "model" + t.index ["number"], name: "index_buses_on_number", unique: true end create_table "buses_services", force: :cascade do |t| t.integer "bus_id" t.integer "service_id" + t.index ["bus_id", "service_id"], name: "index_buses_services_on_bus_id_and_service_id", unique: true + t.index ["service_id", "bus_id"], name: "index_buses_services_on_service_id_and_bus_id", unique: true end create_table "cities", force: :cascade do |t| t.string "name" + t.index ["name"], name: "index_cities_on_name", unique: true end create_table "services", force: :cascade do |t| t.string "name" + t.index ["name"], name: "index_services_on_name" end create_table "trips", force: :cascade do |t| @@ -40,6 +45,9 @@ t.integer "duration_minutes" t.integer "price_cents" t.integer "bus_id" + t.index ["bus_id"], name: "index_trips_on_bus_id" + t.index ["from_id"], name: "index_trips_on_from_id" + t.index ["to_id"], name: "index_trips_on_to_id" end end diff --git a/lib/tasks/utils.rake b/lib/tasks/utils.rake index 540fe871..969afd0e 100644 --- a/lib/tasks/utils.rake +++ b/lib/tasks/utils.rake @@ -1,6 +1,16 @@ +# frozen_string_literal: true + # Наивная загрузка данных из json-файла в БД # rake reload_json[fixtures/small.json] + +desc 'Import data from .json files' task :reload_json, [:file_name] => :environment do |_task, args| + require 'activerecord-import/base' + require 'activerecord-import/active_record/adapters/postgresql_adapter' + + class BusesService < ApplicationRecord; end + # ActiveRecord::Base.logger = Logger.new STDOUT + json = JSON.parse(File.read(args.file_name)) ActiveRecord::Base.transaction do @@ -10,25 +20,47 @@ task :reload_json, [:file_name] => :environment do |_task, args| Trip.delete_all ActiveRecord::Base.connection.execute('delete from buses_services;') + cities = Set[] + bus_numbers = Set[] + buses = [] + buses_services_dict = {} + trips = [] + + services_batch_ids = Service.import [:name], Service::SERVICES.zip, validate: false + services_ids = (Service::SERVICES.zip services_batch_ids.ids).to_h + json.each do |trip| - from = City.find_or_create_by(name: trip['from']) - to = City.find_or_create_by(name: trip['to']) - services = [] - trip['bus']['services'].each do |service| - s = Service.find_or_create_by(name: service) - services << s + cities << trip['from'] + cities << trip['to'] + + if bus_numbers.add?(trip['bus']['number']) + buses << [trip['bus']['model'], trip['bus']['number']] + buses_services_dict[trip['bus']['number']] = trip['bus']['services'] + end + + trips << [trip['from'], trip['to'], trip['bus']['number'], + trip['start_time'], trip['duration_minutes'], trip['price_cents']] + end + + cities_arr = cities.to_a + city_batch_ids = City.import [:name], cities_arr.zip, validate: false + city_ids = (cities_arr.zip city_batch_ids.ids).to_h + + buses_batch_ids = Bus.import %i[model number], buses, validate: false + buses_ids = (buses.map { |bus| bus[1] }.zip buses_batch_ids.ids).to_h + + bus_services = [] + buses_services_dict.each do |bus_number, services| + id = buses_ids[bus_number] + services.each do |service| + bus_services << [id, services_ids[service]] end - bus = Bus.find_or_create_by(number: trip['bus']['number']) - bus.update(model: trip['bus']['model'], services: services) - - Trip.create!( - from: from, - to: to, - bus: bus, - start_time: trip['start_time'], - duration_minutes: trip['duration_minutes'], - price_cents: trip['price_cents'], - ) end + + BusesService.import %i[bus_id service_id], bus_services, validate: false + + trips.map! { |trip| [city_ids[trip[0]], city_ids[trip[1]], buses_ids[trip[2]], trip[3], trip[4], trip[5]] } + + Trip.import %i[from_id to_id bus_id start_time duration_minutes price_cents], trips, validate: false end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 00000000..3155d425 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +# This file is copied to spec/ when you run 'rails generate rspec:install' +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../config/environment', __dir__) +# Prevent database truncation if the environment is production +abort('The Rails environment is running in production mode!') if Rails.env.production? +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } + +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + puts e.to_s.strip + exit 1 +end + +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # You can uncomment this line to turn off ActiveRecord support entirely. + # config.use_active_record = false + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..01f7c974 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + # config.disable_monkey_patching! + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = "doc" + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # order dependency and want to debug it, you can fix the order by providing + # # the seed, which is printed after each run. + # # --seed 1234 + # config.order = :random + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed +end diff --git a/spec/tasks/utils_spec.rb b/spec/tasks/utils_spec.rb new file mode 100644 index 00000000..ff4a2150 --- /dev/null +++ b/spec/tasks/utils_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'task', type: :task do + Rails.application.load_tasks + + before do + Rake::Task['reload_json'].reenable + end + + let(:task) { Rake::Task['reload_json'] } + + it 'works creates Buses' do + expect { task.invoke('fixtures/example.json') }.to change(Bus, :count).from(0).to(1) + end + + it 'works creates Cities' do + expect { task.invoke('fixtures/example.json') }.to change(City, :count).from(0).to(2) + end + + it 'works creates Services' do + expect { task.invoke('fixtures/example.json') }.to change(Service, :count).from(0).to(10) + end +end