+
@@ -212,7 +320,7 @@
-
+
@@ -235,4 +343,142 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.rspec b/.rspec
new file mode 100644
index 0000000..c99d2e7
--- /dev/null
+++ b/.rspec
@@ -0,0 +1 @@
+--require spec_helper
diff --git a/Gemfile b/Gemfile
index ac1a552..c82a200 100644
--- a/Gemfile
+++ b/Gemfile
@@ -9,7 +9,7 @@ end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.5'
# Use sqlite3 as the database for Active Record
-gem 'sqlite3'
+gem 'mysql2', '>= 0.4.10'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
@@ -27,9 +27,17 @@ gem 'puma', '~> 3.7'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
+ gem 'rspec-rails', '~> 3.5'
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
+group :test do
+ gem 'factory_girl_rails', '~> 4.0'
+ gem 'shoulda-matchers', '~> 3.1'
+ gem 'faker'
+ gem 'database_cleaner'
+end
+
group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
diff --git a/Gemfile.lock b/Gemfile.lock
index f2fbbdd..538f048 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -43,7 +43,16 @@ GEM
byebug (10.0.0)
concurrent-ruby (1.0.5)
crass (1.0.3)
+ database_cleaner (1.6.2)
+ diff-lcs (1.3)
erubi (1.7.0)
+ factory_girl (4.9.0)
+ activesupport (>= 3.0.0)
+ factory_girl_rails (4.9.0)
+ factory_girl (~> 4.9.0)
+ railties (>= 3.0.0)
+ faker (1.8.7)
+ i18n (>= 0.7)
ffi (1.9.21)
globalid (0.4.1)
activesupport (>= 4.2.0)
@@ -62,6 +71,7 @@ GEM
mini_mime (1.0.0)
mini_portile2 (2.3.0)
minitest (5.11.3)
+ mysql2 (0.4.10)
nio4r (2.2.0)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
@@ -96,7 +106,26 @@ GEM
rb-fsevent (0.10.2)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
+ rspec-core (3.7.1)
+ rspec-support (~> 3.7.0)
+ rspec-expectations (3.7.0)
+ diff-lcs (>= 1.2.0, < 2.0)
+ rspec-support (~> 3.7.0)
+ rspec-mocks (3.7.0)
+ diff-lcs (>= 1.2.0, < 2.0)
+ rspec-support (~> 3.7.0)
+ rspec-rails (3.7.2)
+ actionpack (>= 3.0)
+ activesupport (>= 3.0)
+ railties (>= 3.0)
+ rspec-core (~> 3.7.0)
+ rspec-expectations (~> 3.7.0)
+ rspec-mocks (~> 3.7.0)
+ rspec-support (~> 3.7.0)
+ rspec-support (3.7.1)
ruby_dep (1.5.0)
+ shoulda-matchers (3.1.2)
+ activesupport (>= 4.0.0)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
@@ -109,7 +138,6 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
- sqlite3 (1.3.13)
thor (0.20.0)
thread_safe (0.3.6)
tzinfo (1.2.5)
@@ -123,12 +151,17 @@ PLATFORMS
DEPENDENCIES
byebug
+ database_cleaner
+ factory_girl_rails (~> 4.0)
+ faker
listen (>= 3.0.5, < 3.2)
+ mysql2 (>= 0.4.10)
puma (~> 3.7)
rails (~> 5.1.5)
+ rspec-rails (~> 3.5)
+ shoulda-matchers (~> 3.1)
spring
spring-watcher-listen (~> 2.0.0)
- sqlite3
tzinfo-data
BUNDLED WITH
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
new file mode 100644
index 0000000..5c818d7
--- /dev/null
+++ b/app/controllers/items_controller.rb
@@ -0,0 +1,2 @@
+class ItemsController < ApplicationController
+end
diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb
new file mode 100644
index 0000000..06daccf
--- /dev/null
+++ b/app/controllers/todos_controller.rb
@@ -0,0 +1,2 @@
+class TodosController < ApplicationController
+end
diff --git a/app/models/item.rb b/app/models/item.rb
new file mode 100644
index 0000000..08e6ed7
--- /dev/null
+++ b/app/models/item.rb
@@ -0,0 +1,5 @@
+class Item < ApplicationRecord
+ belongs_to :todo
+
+ validates_presence_of :name
+end
diff --git a/app/models/todo.rb b/app/models/todo.rb
new file mode 100644
index 0000000..d9d7868
--- /dev/null
+++ b/app/models/todo.rb
@@ -0,0 +1,5 @@
+class Todo < ApplicationRecord
+ has_many :items, dependent: :destroy
+
+ validates_presence_of :title, :created_by
+end
diff --git a/config/database.yml b/config/database.yml
index 0d02f24..4de6498 100644
--- a/config/database.yml
+++ b/config/database.yml
@@ -5,21 +5,26 @@
# gem 'sqlite3'
#
default: &default
- adapter: sqlite3
+ adapter: mysql2
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
+ host: localhost
+ username: root
+ password: SuperSecret
+ socket: /var/run/mysqld/mysqld.sock
+
development:
<<: *default
- database: db/development.sqlite3
+ database: vereto-api-dev
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
- database: db/test.sqlite3
+ database: vereto-api-dev
production:
<<: *default
- database: db/production.sqlite3
+ database: vereto-api-prod
diff --git a/db/migrate/20180216101219_create_todos.rb b/db/migrate/20180216101219_create_todos.rb
new file mode 100644
index 0000000..ad38eef
--- /dev/null
+++ b/db/migrate/20180216101219_create_todos.rb
@@ -0,0 +1,10 @@
+class CreateTodos < ActiveRecord::Migration[5.1]
+ def change
+ create_table :todos do |t|
+ t.string :title
+ t.string :created_by
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20180216101247_create_items.rb b/db/migrate/20180216101247_create_items.rb
new file mode 100644
index 0000000..e13e30f
--- /dev/null
+++ b/db/migrate/20180216101247_create_items.rb
@@ -0,0 +1,11 @@
+class CreateItems < ActiveRecord::Migration[5.1]
+ def change
+ create_table :items do |t|
+ t.string :name
+ t.boolean :done
+ t.references :todo, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
new file mode 100644
index 0000000..41b62f1
--- /dev/null
+++ b/db/schema.rb
@@ -0,0 +1,32 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended that you check this file into your version control system.
+
+ActiveRecord::Schema.define(version: 20180216101247) do
+
+ create_table "items", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
+ t.string "name"
+ t.boolean "done"
+ t.bigint "todo_id"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["todo_id"], name: "index_items_on_todo_id"
+ end
+
+ create_table "todos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
+ t.string "title"
+ t.string "created_by"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
+ add_foreign_key "items", "todos"
+end
diff --git a/spec/controllers/items_controller_spec.rb b/spec/controllers/items_controller_spec.rb
new file mode 100644
index 0000000..aedcf3a
--- /dev/null
+++ b/spec/controllers/items_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe ItemsController, type: :controller do
+
+end
diff --git a/spec/controllers/todos_controller_spec.rb b/spec/controllers/todos_controller_spec.rb
new file mode 100644
index 0000000..164019c
--- /dev/null
+++ b/spec/controllers/todos_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe TodosController, type: :controller do
+
+end
diff --git a/spec/factories/items.rb b/spec/factories/items.rb
new file mode 100644
index 0000000..6beddc6
--- /dev/null
+++ b/spec/factories/items.rb
@@ -0,0 +1,8 @@
+# spec/factories/items.rb
+FactoryGirl.define do
+ factory :item do
+ name { Faker::StarWars.character }
+ done false
+ todo_id nil
+ end
+end
\ No newline at end of file
diff --git a/spec/factories/todos.rb b/spec/factories/todos.rb
new file mode 100644
index 0000000..6e3e282
--- /dev/null
+++ b/spec/factories/todos.rb
@@ -0,0 +1,7 @@
+# spec/factories/todos.rb
+FactoryGirl.define do
+ factory :todo do
+ title { Faker::Lorem.word }
+ created_by { Faker::Number.number(10) }
+ end
+end
\ No newline at end of file
diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb
new file mode 100644
index 0000000..7ecafdf
--- /dev/null
+++ b/spec/models/item_spec.rb
@@ -0,0 +1,10 @@
+require 'rails_helper'
+
+RSpec.describe Item, type: :model do
+ # Association test
+ # ensure an item record belongs to a single todo record
+ it { should belong_to(:todo) }
+ # Validation test
+ # ensure column name is present before saving
+ it { should validate_presence_of(:name) }
+end
diff --git a/spec/models/todo_spec.rb b/spec/models/todo_spec.rb
new file mode 100644
index 0000000..be399c4
--- /dev/null
+++ b/spec/models/todo_spec.rb
@@ -0,0 +1,11 @@
+require 'rails_helper'
+
+RSpec.describe Todo, type: :model do
+ # Association test
+ # ensure Todo model has a 1:m relationship with the Item model
+ it { should have_many(:items).dependent(:destroy) }
+ # Validation tests
+ # ensure columns title and created_by are present before saving
+ it { should validate_presence_of(:title) }
+ it { should validate_presence_of(:created_by) }
+end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
new file mode 100644
index 0000000..d0754dd
--- /dev/null
+++ b/spec/rails_helper.rb
@@ -0,0 +1,76 @@
+# 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', __FILE__)
+# 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!
+require 'database_cleaner'
+# configure shoulda matchers to use rspec as the test framework and full matcher libraries for rails
+Shoulda::Matchers.configure do |config|
+ config.integrate do |with|
+ with.test_framework :rspec
+ with.library :rails
+ end
+end
+# 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')].each { |f| require f }
+
+# Checks for pending migrations and applies them before tests are run.
+# If you are not using ActiveRecord, you can remove this line.
+ActiveRecord::Migration.maintain_test_schema!
+
+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
+ # add `FactoryGirl` methods
+ config.include FactoryGirl::Syntax::Methods
+
+ # start by truncating all the tables but then use the faster transaction strategy the rest of the time.
+ config.before(:suite) do
+ DatabaseCleaner.clean_with(:truncation)
+ DatabaseCleaner.strategy = :transaction
+ end
+
+ # 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!
+ config.around(:each) do |example|
+ DatabaseCleaner.cleaning do
+ example.run
+ end
+ end
+ # 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/requests/items_spec.rb b/spec/requests/items_spec.rb
new file mode 100644
index 0000000..e69de29
diff --git a/spec/requests/todos_spec.rb b/spec/requests/todos_spec.rb
new file mode 100644
index 0000000..e69de29
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 0000000..ce33d66
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,96 @@
+# 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.
+=begin
+ # 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
+end