Skip to content

Commit

Permalink
added test cases for save and create
Browse files Browse the repository at this point in the history
  • Loading branch information
VishalTaj committed Nov 17, 2018
1 parent cc7d80d commit 65d51dd
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ test/dummy/db/*.sqlite3-journal
test/dummy/log/*.log
test/dummy/tmp/
test/dummy/.sass-cache
.byebug_history
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ gemspec
# your gem to rubygems.org.

# To use a debugger
# gem 'byebug', group: [:development, :test]
gem 'byebug', group: [:development, :test]
gem 'sqlite3', group: [:development, :test]

6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ GEM
tzinfo (~> 1.1)
arel (9.0.0)
builder (3.2.3)
byebug (10.0.2)
concurrent-ruby (1.0.5)
crass (1.0.4)
erubi (1.7.1)
Expand Down Expand Up @@ -107,6 +108,7 @@ 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)
Expand All @@ -119,7 +121,9 @@ PLATFORMS
ruby

DEPENDENCIES
byebug
mti!
sqlite3

BUNDLED WITH
1.15.3
1.15.3
9 changes: 9 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ Add +has_subclass+ association to *ParentModel*
$ has_subclass :child_model


== Test

If you are using this gem for the first time please do a migration for our dummy app.

$ cd test/dummy/app/ && rake db:migrate

Once done with the migration go back to the gem root directory then:

$ rake test

That's all! you are ready to go... :)
12 changes: 6 additions & 6 deletions lib/mti.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module Mti
extend ActiveSupport::Concern

included do
attr_accessor :mti_models

def self.has_subclass table
@@mti_models ||= []
mattr_accessor :mti_models unless defined? self.mti_models
self.mti_models ||= []

table_columns = table.to_s.camelize.constantize.column_names
.reject {|col| ["id", "#{self.to_s.underscore}_id"].include? col }
.compact

@@mti_models << table
self.mti_models << table

has_one table, autosave: true, dependent: :destroy

Expand All @@ -20,12 +20,12 @@ def self.has_subclass table
end
end

def initialize(attributes = nil, options = {})
def initialize(attributes = nil)
super
@@mti_models.map {|model|
self.mti_models.map {|model|
mti_model_column = model.to_s.camelize.constantize.column_names
self.send("build_#{model.to_s}", attributes.to_h.reject {|key, val| val unless mti_model_column.include? key.to_s})
}
} if defined? self.mti_models
end
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/models/employee_detail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class EmployeeDetail < ActiveRecord::Base
end
4 changes: 4 additions & 0 deletions test/dummy/app/models/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Profile < ActiveRecord::Base
# belongs_to :user
has_subclass :employee_detail
end
4 changes: 4 additions & 0 deletions test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class User < ActiveRecord::Base
has_subclass :profile
has_subclass :user_preference
end
2 changes: 2 additions & 0 deletions test/dummy/app/models/user_preference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class UserPreference < ActiveRecord::Base
end
3 changes: 2 additions & 1 deletion test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class Application < Rails::Application
# config.i18n.default_locale = :de

# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
# config.active_record.raise_in_transactional_callbacks = true
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
end
end

5 changes: 5 additions & 0 deletions test/dummy/config/initializers/mti.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'mti'

ActiveRecord::Base.class_eval do
include Mti
end
8 changes: 8 additions & 0 deletions test/dummy/db/migrate/20181117100451_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :email
t.string :password
end
end
end
9 changes: 9 additions & 0 deletions test/dummy/db/migrate/20181117100457_create_profiles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateProfiles < ActiveRecord::Migration[5.2]
def change
create_table :profiles do |t|
t.references :user, index: true
t.string :name
t.text :address
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateUserPreferences < ActiveRecord::Migration[5.2]
def change
create_table :user_preferences do |t|
t.references :user, index: true
t.string :hobbies
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateEmployeeDetail < ActiveRecord::Migration[5.2]
def change
create_table :employee_details do |t|
t.references :profile, index: true
t.string :emp_id
end
end
end
39 changes: 39 additions & 0 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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: 2018_11_17_110530) do

create_table "employee_details", force: :cascade do |t|
t.integer "profile_id"
t.string "emp_id"
t.index ["profile_id"], name: "index_employee_details_on_profile_id"
end

create_table "profiles", force: :cascade do |t|
t.integer "user_id"
t.string "name"
t.text "address"
t.index ["user_id"], name: "index_profiles_on_user_id"
end

create_table "user_preferences", force: :cascade do |t|
t.integer "user_id"
t.string "hobbies"
t.index ["user_id"], name: "index_user_preferences_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "email"
t.string "password"
end

end
27 changes: 25 additions & 2 deletions test/mti_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
require 'test_helper'

class MtiTest < ActiveSupport::TestCase
test "truth" do
assert_kind_of Module, Mti

def setup
@user_params = {
email: 'dummy@test.com',
name: 'dummy',
address: 'dummy address',
hobbies: 'nothing'
}
@profile_params = {
emp_id: 'E01'
}
end

test "for create method" do
user = User.create(@user_params)
user.profile.emp_id = @profile_params[:emp_id]
user.save
assert_equal(@user_params.values + @profile_params.values, [user.email, user.name, user.address, user.hobbies, user.profile.emp_id], 'Something went wrong while creating')
end

test "for save method" do
user = User.new(@user_params)
user.profile.emp_id = @profile_params[:emp_id]
user.save
assert_equal(@user_params.values + @profile_params.values, [user.email, user.name, user.address, user.hobbies, user.profile.emp_id], 'Something went wrong while saving')
end
end

0 comments on commit 65d51dd

Please sign in to comment.