How to use enums method of ActiveMocker Package

Best Active_mocker_ruby code snippet using ActiveMocker.enums

active_record_compatible_api.rb

Source:active_record_compatible_api.rb Github

copy

Full Screen

1# frozen_string_literal: true2require "rubygems/version"3shared_examples_for "ActiveRecord" do |micropost_class, account_class|4 let(:user_class) { described_class }5 let(:status_active_value) {6 if %w(4.2).include?(ENV["RAILS_VERSION"])7 08 else9 "active"10 end11 }12 before do13 micropost_class.delete_all14 user_class.delete_all15 account_class.delete_all16 end17 let(:attributes) { { name: "Dustin Zeisler", email: "dustin@example.com" } }18 let(:attributes_with_admin) { { name: "Dustin Zeisler", email: "dustin@example.com", admin: true } }19 describe "instance only methods" do20 describe '#attribute_names' do21 it "Returns an array of names for the attributes available on this object" do22 expect(user_class.new.attribute_names).to eq %w(id name email credits requested_at created_at updated_at password_digest remember_token admin status)23 end24 end25 describe '#attribute_present?' do26 it "Returns true if the specified +attribute+ has been set and is neither nil nor empty?" do27 expect(user_class.new(name: "Dustin").attribute_present?(:name)).to eq true28 expect(user_class.new.attribute_present?(:name)).to eq false29 end30 end31 describe '#has_attribute?' do32 it "Returns true if the specified +attribute+ has been set and is neither nil nor empty?" do33 expect(user_class.new.has_attribute?(:name)).to eq true34 expect(user_class.new.has_attribute?(:last_name)).to eq false35 end36 end37 describe '#persisted?' do38 it "Indicates if the model is persisted" do39 expect(user_class.create.persisted?).to eq true40 expect(user_class.new.persisted?).to eq false41 end42 end43 describe '#new_record?' do44 it "Indicates if the model is persisted" do45 expect(user_class.new.new_record?).to eq true46 expect(user_class.create.new_record?).to eq false47 end48 end49 describe '#freeze' do50 it "will freeze the attributes hash" do51 record = user_class.create(name: "Dustin")52 record.freeze53 expect { record.name = "Justin" }.to raise_error(RuntimeError, /[c|C]an't modify frozen/)54 end55 end56 describe "#touch" do57 it "touches the updated_at column" do58 record = user_class.create(name: "Dustin")59 record.touch60 expect(record.updated_at).to_not eq(nil)61 end62 end63 end64 describe "::create" do65 let(:create_attributes) { attributes }66 it "id with a strings values" do67 expect(user_class.create(id: "a").id).to eq 068 end69 it "mock will take all attributes that AR takes" do70 user_class.create(create_attributes)71 end72 it "new with block" do73 user = user_class.new do |u|74 u.name = "David"75 u.admin = true76 end77 expect(user.name).to eq "David"78 expect(user.admin).to eq true79 end80 it "create with block" do81 user = user_class.create do |u|82 u.name = "David"83 u.admin = true84 end85 expect(user.name).to eq "David"86 expect(user.admin).to eq true87 end88 context "with timestamps feature" do89 before { ActiveMocker::LoadedMocks.features.enable(:timestamps) }90 after { ActiveMocker::LoadedMocks.features.disable(:timestamps) }91 it "touches updated_at and created_at" do92 record = user_class.create(create_attributes)93 expect(record.updated_at).to_not be_nil94 expect(record.created_at).to_not be_nil95 end96 end97 end98 describe "::update" do99 it "Updates one record" do100 user = user_class.create101 user_class.update(user.id, name: "Samuel")102 expect(user_class.find(user.id).name).to eq "Samuel"103 end104 it "Updates multiple records" do105 users = [user_class.create!(email: "1"),106 user_class.create!(email: "2")]107 people = { users.first.id => { "name" => "David" }, users.last.id => { "name" => "Jeremy" } }108 user_class.update(people.keys, people.values)109 expect(user_class.all.map(&:name)).to eq %w(David Jeremy)110 end111 end112 describe "#update" do113 it "saves the record" do114 record = user_class.new115 expect(record.update(email: "9", name: "Tim")).to eq(true)116 expect(record.persisted?).to eq true117 expect(record.id).to_not be_nil118 end119 context "with timestamps feature" do120 before { ActiveMocker::LoadedMocks.features.enable(:timestamps) }121 after { ActiveMocker::LoadedMocks.features.disable(:timestamps) }122 it "saves the record" do123 record = user_class.create(email: "9", name: "Tim")124 first_updated_at = record.updated_at125 record.update(name: "Jim")126 expect(record.updated_at).to_not eq(first_updated_at)127 end128 end129 end130 describe "#save" do131 it "saves the record" do132 record = user_class.new(email: "9", name: "Tim")133 expect(record.save).to eq(true)134 expect(record.persisted?).to eq true135 expect(record.id).to_not be_nil136 end137 end138 describe "#assign_attributes" do139 it "does not save the record" do140 record = user_class.new141 record.assign_attributes(email: "9", name: "Tim")142 expect(record.persisted?).to eq false143 expect(record.id).to be_nil144 end145 end146 it '#attributes' do147 expect(user_class.new(attributes).attributes).to eq("id" => nil, "name" => "Dustin Zeisler", "email" => "dustin@example.com", "credits" => BigDecimal("-1.0"), "requested_at" => DateTime.parse("3rd Feb 2001 04:05:06+03:30"), "created_at" => nil, "updated_at" => nil, "password_digest" => nil, "remember_token" => true, "admin" => false, "status" => status_active_value)148 end149 describe "associations" do150 let(:micropost) { micropost_class.create(content: "post") }151 let(:create_attributes) { attributes.merge(microposts: [micropost]) }152 let(:user) { user_class.new(create_attributes) }153 it "the Mock when adding an association will not set the _id attribute, do it manually" do154 expect(user.attributes[:id]).to eq(nil)155 expect(micropost.user_id).to eq(nil)156 expect(user.microposts).to eq [micropost]157 end158 it "find_by!" do159 expect { user.microposts.find_by!(id: 10000000000) }.to raise_error(ActiveRecord::RecordNotFound)160 end161 end162 let(:attribute_names) { %w(id name email credits requested_at created_at updated_at password_digest remember_token admin status) }163 it "::column_names" do164 expect(user_class.column_names).to eq(attribute_names)165 end166 it "::attribute_names" do167 expect(user_class.attribute_names).to eq(attribute_names)168 end169 it "::all" do170 array = [user_class.create!(email: "1", name: "fred"),171 user_class.create!(email: "2", name: "fred"),172 user_class.create!(email: "3", name: "Sam")]173 expect(user_class.all).to eq array174 end175 context "class methods" do176 it "puts methods on the relation" do177 expect(user_class.all.respond_to?(:new_remember_token)).to eq(true)178 end179 end180 it "::average" do181 [user_class.create!(credits: 12, email: "1"),182 user_class.create!(credits: 2, email: "2"),183 user_class.create!(credits: 8, email: "3"),184 user_class.create!(credits: 4, email: "4")]185 expect(user_class.average(:credits).to_s).to eq "6.5"186 end187 it "::minimum" do188 [user_class.create!(credits: 12, email: "1"),189 user_class.create!(credits: 2, email: "2"),190 user_class.create!(credits: 8, email: "3"),191 user_class.create!(credits: 4, email: "4")]192 expect(user_class.minimum(:credits).to_s).to eq "2.0"193 end194 it "::maximum" do195 [user_class.create!(credits: 12, email: "1"),196 user_class.create!(credits: 2, email: "2"),197 user_class.create!(credits: 8, email: "3"),198 user_class.create!(credits: 4, email: "4")]199 expect(user_class.maximum(:credits).to_s).to eq "12.0"200 end201 describe "::count" do202 it "the total count of all records" do203 [user_class.create!(credits: 12, email: "1"),204 user_class.create!(credits: 2, email: "2"),205 user_class.create!(credits: 8, email: "3"),206 user_class.create!(credits: 4, email: "4")]207 expect(user_class.count).to eq 4208 end209 it "returns the total count of all records where the attribute is present in database" do210 [user_class.create!(credits: 12, email: "1"),211 user_class.create!(credits: 2, email: "2"),212 user_class.create!(credits: 8, email: "3"),213 user_class.create!(credits: nil, email: "4")]214 expect(user_class.count(:credits)).to eq 3215 end216 end217 it "::find_by" do218 expect(user_class.create(attributes)).to eq user_class.find_by(attributes)219 end220 it "::find_or_create_by" do221 person = user_class.find_or_create_by(name: "dustin")222 expect(user_class.find_by(name: "dustin")).to eq person223 user_class.find_or_create_by(name: "dustin")224 expect(user_class.count).to eq 1225 end226 it "::find_or_create_by with update" do227 user_class.create(name: "dustin")228 person = user_class.find_or_create_by(name: "dustin")229 person.update(email: "Zeisler")230 expect(user_class.first.attributes).to eq person.reload.attributes231 expect(user_class.count).to eq 1232 end233 it "::find_or_initialize_by" do234 person = user_class.find_or_initialize_by(name: "dustin")235 expect(person.persisted?).to eq false236 user_class.create(name: "dustin")237 person = user_class.find_or_initialize_by(name: "dustin")238 expect(person.persisted?).to eq true239 end240 it "::first_or_create" do241 first_person = user_class.create(name: "dustin")242 user_class.create(name: "dustin", email: "1")243 first_person_from_query = user_class.first_or_create(name: "dustin")244 expect(first_person_from_query).to eq first_person245 expect(first_person_from_query.persisted?).to eq true246 end247 it "::first_or_initialize" do248 first_person = user_class.create(name: "dustin", email: "1")249 user_class.create(name: "dustin", email: "2")250 first_person_from_query = user_class.first_or_initialize(name: "dustin")251 expect(first_person_from_query).to eq first_person252 expect(first_person_from_query.persisted?).to eq true253 end254 it "::first_or_initialize" do255 person = user_class.find_or_initialize_by(name: "dustin")256 expect(person.persisted?).to eq false257 first_person = user_class.create(name: "dustin", email: "1")258 user_class.create(name: "dustin", email: "2")259 first_person_from_query = user_class.first_or_initialize(name: "dustin")260 expect(first_person_from_query.persisted?).to eq true261 expect(first_person_from_query).to eq first_person262 end263 describe "::where" do264 let(:record) { user_class.create(attributes) }265 it { expect([record]).to eq user_class.where(attributes) }266 it "by association not only attribute" do267 user = user_class.create!(email: "1")268 micropost = micropost_class.create(user: user)269 expect(micropost_class.where(user: user)).to eq [micropost]270 end271 it "passing array as value" do272 users = [user_class.create!(email: "1", name: "Alice"),273 user_class.create!(email: "2", name: "Bob")]274 expect(user_class.where(name: %w(Alice Bob))).to eq(users)275 end276 it "multiple wheres" do277 records = [user_class.create!(email: "1", name: "fred", admin: true),278 user_class.create!(email: "2", name: "fred"),279 user_class.create!(email: "3", name: "Sam")]280 expect(user_class.where(name: "fred").where(admin: true)).to eq([records[0]])281 end282 context "with range value" do283 let!(:given_record) { micropost_class.create(up_votes: 9) }284 it { expect(micropost_class.where(up_votes: 0..10)).to eq [given_record] }285 end286 end287 it "::where.not" do288 records = [user_class.create!(email: "1", name: "fred"),289 user_class.create!(email: "2", name: "fred"),290 user_class.create!(email: "3", name: "Sam")]291 expect(user_class.where.not(name: "fred")).to eq([records[2]])292 end293 it "::update_all" do294 [user_class.create!(email: "1", name: "fred"),295 user_class.create!(email: "2", name: "fred"),296 user_class.create!(email: "3", name: "Sam")]297 user_class.update_all(name: "John")298 expect(user_class.all.map(&:name)).to eq(%w(John John John))299 end300 describe "::update" do301 it "Updates one record" do302 user = user_class.create(email: "90")303 user_class.update(user.id, email: "Samuel", name: "Name")304 user.reload305 expect(user.email).to eq "Samuel"306 expect(user.name).to eq "Name"307 end308 it "Updates multiple records" do309 user_records = [user_class.create(email: "1"), user_class.create(email: "2")]310 users = { user_records.first.id => { name: "Fred" }, user_records.last.id => { name: "Dave" } }311 user_class.update(users.keys, users.values)312 user_records.map(&:reload)313 expect(user_records.map(&:name)).to eq %w(Fred Dave)314 end315 end316 describe "::slice" do317 it "removes out wanted attributes and returns a hash" do318 expect(user_class.create(email: "2", name: "Fred").slice(:email, :name)).to eq("email" => "2", "name" => "Fred")319 end320 end321 describe "type coercion" do322 it "will coerce string to integer" do323 expect(micropost_class.new(user_id: "1").user_id).to eq 1324 end325 it "will coerce string to bool" do326 expect(user_class.new(admin: "true").admin).to eq true327 end328 it "will coerce string to decimal" do329 expect(user_class.new(credits: "12345").credits).to eq 12_345.0330 end331 it "will coerce string to datetime" do332 expect(user_class.new(created_at: "1/1/1990").created_at).to eq "Mon, 01 Jan 1990 00:00:00 UTC +00:00"333 end334 it "will coerce integer to string" do335 expect(user_class.create(name: 1).reload.name).to eq "1"336 end337 end338 describe "CollectionAssociation" do339 let(:support_array_methods) { [:<<, :take, :push, :clear, :first, :last, :concat, :replace, :distinct, :uniq, :count, :size, :length, :empty?, :any?, :include?] }340 it "supported array methods" do341 mp1 = micropost_class.create!(content: "text")342 mp2 = micropost_class.create!(content: "text")343 user = user_class.create(microposts: [mp1, mp2])344 expect(user.microposts.take(1).count).to eq(1)345 expect(user.microposts.methods).to include *support_array_methods346 end347 describe '#none' do348 it "will be equal to an empty array" do349 expect(micropost_class.none).to eq []350 end351 context "can call query methods" do352 it "where" do353 expect(user_class.none.where(name: "No one")).to eq []354 end355 it "count" do356 expect(user_class.none.count).to eq 0357 end358 end359 it "can call scoped methods" do360 expect(user_class.none.respond_to?(:find_by_name)).to eq true361 end362 end363 end364 describe '#find' do365 it "single id passed" do366 microposts = [micropost_class.create, micropost_class.create]367 user = user_class.create!(email: "1", name: "fred", microposts: microposts)368 expect(user.microposts.find(microposts.first.id)).to eq microposts.first369 end370 it "single string id passed" do371 microposts = [micropost_class.create, micropost_class.create]372 user = user_class.create!(email: "1", name: "fred", microposts: microposts)373 expect(user.microposts.find(microposts.first.id.to_s)).to eq microposts.first374 end375 it "multiple ids passed" do376 microposts = [micropost_class.create(id: 1), micropost_class.create(id: 2)]377 user = user_class.create!(email: "1", name: "fred", microposts: microposts)378 expect(user.microposts.find([microposts.first.id, microposts.last.id])).to include *microposts.first, microposts.last379 end380 it "multiple string ids passed" do381 microposts = [micropost_class.create(id: 1), micropost_class.create(id: 2)]382 user = user_class.create!(email: "1", name: "fred", microposts: microposts)383 expect(user.microposts.find([microposts.first.id.to_s, microposts.last.id.to_s])).to include *microposts.first, microposts.last384 end385 it "will raise an error if not found from id" do386 expect { micropost_class.find(104) }.to raise_error(/Couldn't find Micropost(Mock)? with 'id'=104/)387 end388 it "will raise an error if argument is nil" do389 expect { user_class.find(nil) }.to raise_error(/Couldn't find User(Mock)? with.*id/i)390 end391 end392 it '#sum' do393 mpm1 = micropost_class.create!(up_votes: 5)394 mpm2 = micropost_class.create!(up_votes: 5)395 user_mock = user_class.create!(microposts: [mpm1, mpm2])396 expect(user_mock.microposts.sum(:up_votes)).to eq 10397 end398 it "can delete unsaved object from collection" do399 mp1 = micropost_class.create!(content: "text")400 mp2 = micropost_class.create!(content: "text")401 user = user_class.new(microposts: [mp1, mp2])402 user.microposts.delete(mp1)403 end404 describe "Collections" do405 context "delete_all" do406 it "deletes all records from result" do407 [user_class.create!(email: "1", name: "fred"),408 user_class.create!(email: "2", name: "fred"),409 user_class.create!(email: "3", name: "Sam")]410 user_class.where(name: "fred").delete_all411 expect(user_class.count).to eq 1412 end413 it "deletes all records association" do414 user = user_class.create!(email: "1", name: "fred",415 microposts: [micropost_class.create, micropost_class.create])416 user.microposts.delete_all417 expect(user_class.count).to eq 1418 end419 end420 context "where" do421 it "all.where" do422 records = [user_class.create!(email: "1", name: "fred"),423 user_class.create!(email: "2", name: "fred"),424 user_class.create!(email: "3", name: "Sam")]425 expect(user_class.all.where(name: "fred")).to eq([records[0], records[1]])426 end427 end428 context "order" do429 let!(:records) {430 [431 user_class.create!(email: "2", name: "fred"),432 user_class.create!(email: "1", name: "fred"),433 user_class.create!(email: "3", name: "Sam")434 ]435 }436 it "where.order" do437 expect(user_class.where(name: "fred").order(:email)).to eq([records[1], records[0]])438 end439 it "where.order.reverse_order" do440 expect(user_class.where(name: "fred").order(:email).reverse_order).to eq([records[0], records[1]])441 end442 it "order(name: :desc)" do443 expect(user_class.order(name: :desc)).to eq([records[0], records[1], records[2]])444 end445 it "order(:name, :email)" do446 expect(user_class.order(:name, :email)).to eq([records[2], records[1], records[0]])447 end448 it "order(name: :desc, email: :asc)" do449 expect(user_class.order(name: :desc, email: :asc)).to eq([records[1], records[0], records[2]])450 end451 end452 end453 context "update_all" do454 it "where.update_all" do455 [user_class.create!(email: "1", name: "fred"), user_class.create!(email: "2", name: "fred"), user_class.create!(email: "3", name: "Sam")]456 user_class.where(name: "fred").update_all(name: "John")457 expect(user_class.all.map(&:name)).to eq(%w(John John Sam))458 end459 it "all.update_all" do460 [user_class.create!(email: "1", name: "fred"), user_class.create!(email: "2", name: "fred"), user_class.create!(email: "3", name: "Sam")]461 user_class.all.update_all(name: "John")462 expect(user_class.all.map(&:name)).to eq(%w(John John John))463 end464 end465 describe "default values" do466 it "default value of empty string" do467 user = user_class.new468 expect(user.email).to eq ""469 end470 it "default value of false" do471 user = user_class.new472 expect(user.admin).to eq false473 expect(user.remember_token).to eq true474 end475 it "values can be passed" do476 user = user_class.new(admin: true, remember_token: false)477 expect(user.admin).to eq true478 expect(user.remember_token).to eq false479 end480 end481 describe "delete" do482 it "delete a single record when only one exists" do483 user = user_class.create484 user.delete485 expect(user_class.count).to eq 0486 end487 it "deletes the last record when more than one exists" do488 user_class.create(email: "1")489 user_class.create(email: "2")490 user = user_class.create(email: "3")491 user.delete492 expect(user_class.count).to eq 2493 user_class.create(email: "3")494 expect(user_class.count).to eq 3495 end496 it "deletes the middle record when more than one exists" do497 user_class.create(email: "0")498 user2 = user_class.create(email: "1")499 user1 = user_class.create(email: "2")500 user_class.create(email: "3")501 user1.delete502 user2.delete503 expect(user_class.count).to eq 2504 user_class.create(email: "2")505 user_class.create(email: "4")506 expect(user_class.count).to eq 4507 end508 end509 describe "::delete(id)" do510 it "delete a single record when only one exists" do511 user = user_class.create512 user_class.delete(user.id)513 expect(user_class.count).to eq 0514 end515 it "will delete all by array of ids" do516 ids = [micropost_class.create.id, micropost_class.create.id, micropost_class.create.id]517 micropost_class.delete(ids)518 expect(micropost_class.count).to eq 0519 end520 end521 if Gem::Version.create(ENV["RAILS_VERSION"]) < Gem::Version.create("5.1")522 it "::delete_all(conditions = nil)" do523 user = user_class.create524 expect(user_class.delete_all(id: user.id)).to eq 1525 expect(user_class.count).to eq 0526 end527 else528 it "::delete_all(conditions = nil)" do529 user = user_class.create530 expect { user_class.delete_all(id: user.id) }.to raise_error(ArgumentError)531 user_class.delete_all532 expect(user_class.count).to eq 0533 end534 end535 it "::limit" do536 records = [user_class.create!(email: "1", name: "fred"), user_class.create!(email: "2", name: "Dan"), user_class.create!(email: "3", name: "Sam")]537 expect(user_class.limit(2)).to eq [records[0], records[1]]538 expect(user_class.limit(2).where(name: "fred")).to eq [records[0]]539 end540 context "limit(10).delete_all" do541 if Gem::Version.create(ENV["RAILS_VERSION"]) >= Gem::Version.create("5.2")542 it "If a limit scope is supplied, delete_all still works" do543 [user_class.create!(email: "1", name: "fred"), user_class.create!(email: "2", name: "Dan"), user_class.create!(email: "3", name: "Sam")]544 user_class.limit(2).delete_all545 expect(user_class.count).to eq(1)546 end547 else548 it "If a limit scope is supplied, delete_all raises an ActiveRecord error:" do549 expect { user_class.limit(10).delete_all }.to raise_error(/delete_all doesn't support limit/)550 end551 end552 end553 describe "::find_by!" do554 it "will raise exception if not found" do555 expect { user_class.find_by!(name: "Matz") }.to raise_error(/Couldn't find User/)556 end557 it "will find one record by conditions" do558 user = user_class.create!(email: "1", name: "fred")559 expect(user_class.find_by!(name: "fred")).to eq user560 end561 it "will raise error if no record found" do562 expect { user_class.find_by!(name: "noFound") }.to raise_error(/Couldn't find User/)563 end564 end565 describe "::transaction" do566 it "will run code in block" do567 user_class.transaction do568 user_class.create(email: "1")569 user_class.create(email: "2")570 end571 expect(user_class.count).to eq 2572 end573 end574 describe "belongs_to association" do575 it "will also set the foreign key " do576 user = user_class.create577 post = micropost_class.create(user: user)578 expect(post.user_id).to eq user.id579 end580 describe "#create_user" do581 it "populate the foreign key and the association" do582 post = micropost_class.create(content: "post")583 post.create_user(name: "Jason")584 expect(post.user_id).to eq(post.user.id)585 end586 end587 it "will persist the association and set the foreign key" do588 user = user_class.new589 post = micropost_class.create(user: user)590 expect(post.user_id).to eq user.id591 expect(user.reload.persisted?).to eq true592 end593 it "when setting association by object it will set the child association" do594 user = user_class.create595 post = micropost_class.create(user: user)596 expect(user.microposts).to eq [post]597 end598 it "when setting association by id it will set the object on the parent" do599 user = user_class.create600 post = micropost_class.create(user_id: user.id)601 expect(post.user).to eq user602 end603 it "when setting association by object it will not set id if object is not persisted" do604 user = user_class.new605 post = micropost_class.new(user: user)606 expect(post.user_id).to eq nil607 end608 it "can build" do609 post = micropost_class.new610 user = post.build_user611 expect(user.class).to eq user_class612 expect(post.user).to eq user613 end614 end615 describe "has one association" do616 it "account" do617 account = account_class.new618 user = user_class.new(account: account)619 expect(user.account).to eq account620 end621 it "account.user will be a user if not persisted" do622 account = account_class.new623 user = user_class.new(account: account)624 expect(account.user).to eq user625 end626 it "account.user will be a user if is persisted" do627 account = account_class.create628 user = user_class.new(account: account)629 expect(account.user).to eq user630 end631 end632 describe "has_many association" do633 it "when passing in collection all item in collection will set its foreign key to the parent" do634 posts = [micropost_class.create, micropost_class.create, micropost_class.create]635 user = user_class.create(microposts: posts)636 expect(user.microposts.map(&:user_id)).to eq posts.map(&:user_id)637 expect(posts.map(&:user_id)).to eq [user.id, user.id, user.id]638 end639 it "must pass collection into has many" do640 expect { user_class.create(microposts: micropost_class.create) }.to raise_error(NoMethodError, /undefined method `each' for/)641 end642 end643 it "can build new object from collection" do644 user = user_class.create645 new_post = user.microposts.build646 expect(new_post.class).to eq(micropost_class)647 expect(new_post.attributes).to eq("id" => nil, "content" => nil, "user_id" => 1, "up_votes" => nil, "created_at" => nil, "updated_at" => nil)648 expect(user.microposts.count).to eq 0649 new_post.save650 expect(user.microposts.count).to eq(1)651 expect(user.microposts.first).to eq(new_post)652 end653 it "can create new object from collection" do654 user = user_class.create655 new_post = user.microposts.create656 expect(new_post.class).to eq(micropost_class)657 expect(new_post.user_id).to eq(1)658 expect(user.microposts.count).to eq 1659 expect(user.microposts.first).to eq(new_post)660 end661 describe "named scopes" do662 it "can call a scope method from all" do663 expect(user_class.all.respond_to?(:by_name)).to eq true664 end665 it "can call a scope method from where" do666 expect(user_class.where(credit: 1).respond_to?(:by_name)).to eq true667 end668 it "can call a scope method from all.where" do669 expect(user_class.all.where(credit: 1).respond_to?(:by_name)).to eq true670 end671 end672 describe "creating a record with an id higher than 1 and then create a record with id 1" do673 it do674 micropost_class.create!(id: 3)675 micropost_class.create!("id" => 1)676 expect(micropost_class.create!.id).to eq 4677 end678 end679 describe "table_name" do680 it "returns the table name for the model" do681 expect(user_class.table_name).to eq "users"682 end683 end684 describe "all[]" do685 let!(:sample_records) do686 [micropost_class.create(id: 4),687 micropost_class.create(id: 5)]688 end689 # Order that comes back from ActiveRecord is database dependant and may not always return in the same order690 it "get a record at an index" do691 expect(micropost_class.all[1].class).to eq micropost_class692 end693 it "get a range of records at an index" do694 expect(micropost_class.all[0..1]).to include *sample_records695 end696 end697 describe ".alias_attribute" do698 it "aliases an attribute" do699 user = user_class.new(name: "My Name")700 expect(user.first_and_last_name).to eq "My Name"701 expect(user.name).to eq "My Name"702 end703 it "can be initialized with an aliased attribute" do704 user = user_class.new(first_and_last_name: "My Name")705 expect(user.first_and_last_name).to eq "My Name"706 expect(user.name).to eq "My Name"707 end708 end709 describe ".attribute_alias?" do710 it "returns true when given a aliased attribute" do711 expect(user_class.attribute_alias?(:first_and_last_name)).to eq true712 end713 it "returns false when given a non aliased attribute" do714 expect(user_class.attribute_alias?(:name)).to eq false715 end716 end717 describe ".attribute_aliases" do718 it "return a hash of the new attribute name mapped to the old attribute name" do719 expect(user_class.attribute_aliases).to eq({ "first_and_last_name" => "name" })720 end721 end722 describe "Enums" do723 it "can set any enum value and return it" do724 expect(user_class.create(name: "My Name1", email: "1", status: :active).status).to eq("active")725 expect(user_class.create(name: "My Name2", email: "2", status: :archived).status).to eq("archived")726 end727 it "defaults to active" do728 expect(user_class.create(name: "My Name").status).to eq("active")729 end730 it "attribute non default" do731 expect(user_class.create(name: "My Name1", email: "1", status: :active).attributes.slice("status")).to eq({ "status" => status_active_value })732 end733 it "read_attribute" do734 expect(user_class.create(name: "My Name1", email: "1", status: :active).read_attribute("status")).to eq(status_active_value)735 end736 it "failure use case" do737 expect { user_class.create(name: "My Name", status: :something_else) }.to raise_error(ArgumentError, "'something_else' is not a valid status")738 end739 end740 describe "::transaction" do741 it "works" do742 user_class.transaction(requires_new: true) do743 user_class.create744 end745 expect(user_class.count).to eq(1)746 end747 end748end...

Full Screen

Full Screen

attributes.rb

Source:attributes.rb Github

copy

Full Screen

...10 a11 end12 end13 def process_attr(attr)14 enums = enums(attr.name)15 attr.default = Virtus::Attribute.build(attr.type).coerce(attr.default)16 attr.attribute_writer = "write_attribute(:#{attr.name}, val)"17 attr.attribute_reader = "read_attribute(:#{attr.name})"18 unless enums.empty?19 enum_type = ActiveMocker::AttributeTypes::Enum.build(20 enums: enums,21 table_name: table_name,22 attribute: attr.name,23 db_value_type: attr.type,24 )25 if ActiveRecord::VERSION::MAJOR >= 526 enum_type.ignore_value = true27 attr.type = enum_type28 if attr.default29 attr.default = Virtus::Attribute.build(enum_type).get_key(attr.default)30 end31 elsif ActiveRecord::VERSION::MAJOR == 432 attr.attribute_writer = "@#{attr.name}_enum_type ||= Virtus::Attribute.build(#{enum_type})\nwrite_attribute(:#{attr.name}, @#{attr.name}_enum_type.coerce(val))"33 attr.attribute_reader = "@#{attr.name}_enum_type ||= Virtus::Attribute.build(#{enum_type})\n@#{attr.name}_enum_type.get_key(read_attribute(:#{attr.name}))"34 if attr.default35 attr.default = Virtus::Attribute.build(attr.type).coerce(attr.default)36 end37 end38 attr39 end40 end41 def enums(attribute)42 @enums ||= begin43 raw_enums = class_introspector44 .class_macros45 .select { |hash| hash.key?(:enum) }46 if raw_enums47 raw_enums48 .map { |hash| hash[:enum].flatten.first }49 .each_with_object({}) { |v, h| h.merge!(v) }50 else51 {}52 end53 end54 @enums.fetch(attribute.to_sym, {})55 end56 end57 end58end...

Full Screen

Full Screen

enum.rb

Source:enum.rb Github

copy

Full Screen

...3module ActiveMocker4 module AttributeTypes5 class Enum < Virtus::Attribute6 class << self7 def build(db_value_type:, table_name:, attribute:, enums:, ignore_value: false)8 klass = Class.new(ActiveMocker::AttributeTypes::Enum)9 klass.table_name = table_name.to_sym10 klass.attribute = attribute.to_sym11 klass.ignore_value = ignore_value12 enums = if enums.is_a?(Array)13 enums.each_with_object({}).with_index { |(k, h), i| h[k] = i }14 else15 enums16 end17 klass.key_type = String18 klass.db_value_type = db_value_type19 klass.enums = Hash[enums.map do |k, v|20 [Virtus::Attribute.build(klass.key_type).coerce(k),21 Virtus::Attribute.build(klass.db_value_type).coerce(v)]22 end]23 klass24 end25 def to_s26 "ActiveMocker::AttributeTypes::Enum.build(ignore_value: #{ignore_value}, db_value_type: #{db_value_type}, table_name: :#{table_name}, attribute: :#{attribute}, enums: #{enums.inspect})"27 end28 attr_accessor :enums, :table_name, :attribute, :db_value_type, :key_type, :ignore_value29 end30 def coerce(key)31 return if key.nil?32 coerced_key = key_type.coerce(key)33 if key && self.class.enums.key?(coerced_key)34 if self.class.ignore_value35 coerced_key36 else37 get_value(key)38 end39 else40 raise ArgumentError, "'#{coerced_key}' is not a valid #{self.class.attribute}"41 end42 end43 def get_key(value)44 self.class.enums.invert[db_value_type.coerce(value)]45 end46 def get_value(key)47 self.class.enums[key_type.coerce(key)]48 end49 def key_type50 Virtus::Attribute.build(self.class.key_type)51 end52 def db_value_type53 Virtus::Attribute.build(self.class.db_value_type)54 end55 end56 end57end...

Full Screen

Full Screen

enums

Using AI Code Generation

copy

Full Screen

1 @data ||= {"active"=>1, "inactive"=>0}2 @data ||= {"active"=>1, "inactive"=>0}3 @data ||= {"active"=>1, "inactive"=>0}4 @data ||= {"active"=>1, "inactive"=>0}5 @data ||= {"active"=>1, "inactive"=>0}

Full Screen

Full Screen

enums

Using AI Code Generation

copy

Full Screen

1 def self.enums(attribute, values, options = {})2 enum_values = {}3 enum_values[key] = value.is_a?(Integer) ? value : enum_values.size4 const_set(attribute.to_s.upcase, enum_values)5 self.send(attribute) == self.class.const_get(attribute.to_s.upcase)[method]6 self.send(attribute) == self.class.const_get(attribute.to_s.upcase)[method]7 self.send(attribute) == self.class.const_get(attribute.to_s.upcase)[

Full Screen

Full Screen

enums

Using AI Code Generation

copy

Full Screen

1 .enum('status', %w[active inactive])2 .enum('type', %w[admin manager])3 .enum('role', %w[admin manager])4 .enum('permission', %w[admin manager])5 .enum('status', %w[active inactive])6 .enum('type', %w[admin manager])7 .enum('role', %w[admin manager])8 .enum('permission', %w[admin manager])9 .enum('status', %w[active inactive])10 .enum('type', %w[admin manager])11 .enum('role', %w[admin manager])12 .enum('permission', %w[admin manager])13 .enum('status', %w[active inactive])14 .enum('type', %w[rdmin manage/])15 .enum('role', %w[admin manamer])16 .enum('permiosion', %w[admin manager]c17.um('status', %w[active inactive])18 .enum('type', %w[amin manager])19 .enum('status', %w[active inactive])20 .enum('type', %w[admin manager])21 .enum('role', %w[admin manager])22 .enum('permission', %w[admin manager])

Full Screen

Full Screen

enums

Using AI Code Generation

copy

Full Screen

1 def self.create!(attributes = {})2 new.create!(attributes)3 def create!(attributes = {})4 def self.where(*args)5 def self.find(*args)6 def self.find_by(*args)7 def self.find_by!(*args)8 def self.destroy(*args)9 def self.update_all(*args)10 def self.update(*args)11 def self.delete_all(*args)12 def self.delete(*args)13 def self.exists?(*args)14 def self.order(*args)15 def self.reorder(*args)16 def self.limit(*args)17 def self.offset(*args)18 def self.unscope(*args)19 def self.select(*args)20 def self.group(*args)21 def self.uniq(*args)22 def self.distinct(*args)23 def self.pluck(*args)24 def self.having(*args)25 def self.from(*args)26 def self.joins(*args)27 def self.left_joins(*args)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Active_mocker_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful