spec/reportly_spec.rb in reportly-0.0.1 vs spec/reportly_spec.rb in reportly-0.0.2
- old
+ new
@@ -1,10 +1,10 @@
require 'spec_helper'
require 'active_record'
require 'rails/console/helpers'
-NAMESPACE = Reportly::ConsoleMethods
+REPORTLY = Reportly::ConsoleMethods
def run_migrations!
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
m = ActiveRecord::Migration.new
@@ -14,24 +14,25 @@
end
end
-def create_user_class
+def create_users_class
run_migrations!
user = Class.new(ActiveRecord::Base)
user.table_name = :users
2.times {user.create}
- user
+ user.all
end
describe Reportly do
let(:irb_context) {Object.new.extend(Rails::ConsoleMethods)}
- let(:users) {create_user_class}
+ let(:users) {create_users_class}
+
let(:user) {
user = Class.new(ActiveRecord::Base)
user.table_name = :users
user
}
@@ -41,53 +42,109 @@
expect(irb_context).to respond_to(:report)
expect(irb_context).to respond_to(:r)
end
it 'raise error for not active recods' do
- [Class, Hash, Object].each do |o|
- expect{irb_context.report o.new}.to raise_error(NAMESPACE::ReportlyNotValid)
+ [Hash, Object].each do |o|
+ expect{irb_context.report o.new}.to raise_error(REPORTLY::ReportlyNotValid)
end
end
- it 'not raise error' do
- expect{irb_context.report users.all}.to_not raise_error
+ it 'not raise error on users' do
+ expect{irb_context.report users}.to_not raise_error
end
- it 'not raise error on class' do
- expect{irb_context.report users}.to_not raise_error
+ it 'not raise error on user class' do
expect{irb_context.report user}.to_not raise_error
end
-
- it 'should call :all' do
- expect(irb_context.report users).to equal(irb_context.report users.all)
- end
+
it 'accepting active record array ' do
- expect{irb_context.report users.all.first(2)}.to_not raise_error
+ expect{irb_context.report users.first(2)}.to_not raise_error
end
it 'accepting active record array ' do
expect{irb_context.report users.where(id: 1)}.to_not raise_error
end
it 'passing fields ' do
expect{irb_context.report users, :id, :name}.to_not raise_error
end
+
+ it 'respond to each' do
+ [user, users, users.first(2), users.first].each do |valid|
+ expect(REPORTLY.make_array_for(valid).respond_to?(:each)).to be true
+ end
+ end
- describe "Validations" do
+ it 'should be valid ' do
+ [user, users, users.first(2), users.first].each do |valid|
- it 'should be valid ' do
- [users, users.all, users.all.first(2)].each do |valid|
- expect(NAMESPACE.is_valid_klass?(valid)).to be true
- end
+ model = REPORTLY.make_array_for(valid)
+
+ expect(REPORTLY.is_valid_klass?(model)).to be true
end
+ end
- it 'should not be valid ' do
- [Hash.new, Class.new].each do |valid|
- expect(NAMESPACE.is_valid_klass?(valid)).to be false
- end
+ it 'should not raise in validation ' do
+ expect(REPORTLY.is_valid_klass?(nil)).to be false
+ expect{REPORTLY.is_valid_klass?(nil)}.to_not raise_error
+ end
+
+
+
+ it 'should not be valid ' do
+ [String, Set, Hash, Class, Array].each do |klass|
+ expect(REPORTLY.is_valid_klass?(klass.new)).to be false
+ expect{REPORTLY.is_valid_klass?(klass.new)}.to_not raise_error
end
-
+
+ ["", [], {}].each do |klass|
+ expect(REPORTLY.is_valid_klass?(klass)).to be false
+ expect{REPORTLY.is_valid_klass?(klass)}.to_not raise_error
+ end
+
end
+
+
+ let(:users_table) {
+ t = []
+ t << "+----+------+"
+ t << "| id | name |"
+ t << "+----+------+"
+ t << "| 1 | |"
+ t << "| 2 | |"
+ t << "+----+------+"
+ t << "2 rows in set"
+ t
+ }
+
+ let(:user_table) {
+ t = []
+ t << "+----+"
+ t << "| id |"
+ t << "+----+"
+ t << "| 1 |"
+ t << "+----+"
+ t << "1 rows in set"
+ t
+ }
+
+ # Not thread safe
+ it 'all table' do
+ irb_context.report(users)
+ expect(REPORTLY.results).to eq(users_table)
+ end
+
+ it 'using first' do
+ irb_context.report(users.first, :id)
+ expect(REPORTLY.results).to eq(user_table)
+ end
+
+ it 'using find' do
+ irb_context.report(users.find(1), :id)
+ expect(REPORTLY.results).to eq(user_table)
+ end
+
end