How to use find method of FactoryBot Package

Best Factory_bot_ruby code snippet using FactoryBot.find

document_spec.rb

Source:document_spec.rb Github

copy

Full Screen

...23 enabled: true)24 end25 end26 end27 field = convention.document_fields.find_by(codification_kind: :originating_company)28 value = field.document_field_values.create!(value: 'FFF', position: 5)29 document = Document.build_from_convention(convention, user)30 expect(document['document_fields'].detect{ |i| i['codification_kind'] == 'originating_company' }['document_field_values'].length).to eql(1)31 team = project.dms_teams.create!32 team.users << user33 team.document_rights.create!(document_field: field,34 document_field_value: value,35 limit_for: :value,36 enabled: true,37 view_only: false)38 document = Document.build_from_convention(convention, user)39 expect(document['document_fields'].detect{ |i| i['codification_kind'] == 'originating_company' }['document_field_values'].length).to eql(2)40 end41 it 'project creator should have access to all fields and values even without rights' do42 user = FactoryBot.create(:user)43 document = document_attributes(user)44 project = get_project_from_document_attrs(document)45 document = Document.build_from_convention(project.conventions.active, project.user)46 field = document['document_fields'].detect{ |i| i['codification_kind'] == 'originating_company' }47 expect(field).to be_present48 expect(field['document_field_values'].length).to eql(1)49 end50 it 'upload field' do51 doc = FactoryBot.create(:document)52 field = doc.document_fields.create(kind: :upload_field)53 field.file.attach(fixture_file_upload('test.txt'))54 expect(field.file.download.strip).to eql('111')55 end56 it 'return filename' do57 user = FactoryBot.create(:user)58 document = Document.create(document_attributes(user))59 field1 =60 document.document_fields.find_by(codification_kind: :document_native_file)61 field2 =62 document.attributes_for_edit['document_fields']63 .detect{ |i| i['codification_kind'] == 'document_native_file' }64 expect(field2['filename']).to include(document.codification_string)65 end66 context '#additional_information' do67 it do68 doc1 = FactoryBot.create(:document)69 doc1.revision.update!(revision_number: '1')70 doc1.document_fields.find_by(codification_kind: :additional_information).update!(value: '111')71 doc2 = FactoryBot.create(:document)72 doc2.revision.update!(revision_number: '2')73 doc2.document_fields.find_by(codification_kind: :additional_information).update!(value: '111')74 doc3 = FactoryBot.create(:document)75 doc3.revision.update!(revision_number: '3')76 doc3.document_fields.find_by(codification_kind: :additional_information).update!(value: '222')77 doc2.revision.update_columns(document_main_id: doc1.document_main.id)78 doc3.revision.update_columns(document_main_id: doc1.document_main.id)79 attrs = doc3.reload.attributes_for_show80 info = attrs['additional_information']81 expect(info.length).to eql(2)82 first_info = info.detect{ |i| i[:min] == '1' }83 second_info = info.detect{ |i| i[:min] == '3' }84 expect(first_info[:max]).to eql('2')85 expect(first_info[:value]).to eql('111')86 expect(second_info[:max]).to eql('3')87 expect(second_info[:value]).to eql('222')88 end89 it do90 doc1 = FactoryBot.create(:document)91 doc1.revision.update!(revision_number: '1')92 doc1.document_fields.find_by(codification_kind: :additional_information).update!(value: '111')93 doc2 = FactoryBot.create(:document)94 doc2.revision.update!(revision_number: '2')95 doc2.document_fields.find_by(codification_kind: :additional_information).update!(value: '222')96 doc3 = FactoryBot.create(:document)97 doc3.revision.update!(revision_number: '3')98 doc3.document_fields.find_by(codification_kind: :additional_information).update!(value: '111')99 doc2.revision.update_columns(document_main_id: doc1.document_main.id)100 doc3.revision.update_columns(document_main_id: doc1.document_main.id)101 attrs = doc3.reload.attributes_for_show102 info = attrs['additional_information']103 expect(info.length).to eql(3)104 first_info = info.detect{ |i| i[:min] == '1' }105 second_info = info.detect{ |i| i[:min] == '2' }106 third_info = info.detect{ |i| i[:min] == '3' }107 expect(first_info[:max]).to eql('1')108 expect(first_info[:value]).to eql('111')109 expect(second_info[:max]).to eql('2')110 expect(second_info[:value]).to eql('222')111 expect(third_info[:max]).to eql('3')112 expect(third_info[:value]).to eql('111')113 end114 it do115 doc1 = FactoryBot.create(:document)116 doc1.revision.update!(revision_number: '1')117 doc1.document_fields.find_by(codification_kind: :additional_information).update!(value: '111')118 doc2 = FactoryBot.create(:document)119 doc2.revision.update!(revision_number: '2')120 doc2.document_fields.find_by(codification_kind: :additional_information).update!(value: '111')121 doc3 = FactoryBot.create(:document)122 doc3.revision.update!(revision_number: '3')123 doc3.document_fields.find_by(codification_kind: :additional_information).update!(value: '111')124 doc2.revision.update_columns(document_main_id: doc1.document_main.id)125 doc3.revision.update_columns(document_main_id: doc1.document_main.id)126 attrs = doc3.reload.attributes_for_show127 info = attrs['additional_information']128 expect(info.length).to eql(1)129 info = info.detect{ |i| i[:min] == '1' }130 expect(info[:max]).to eql('3')131 expect(info[:value]).to eql('111')132 end133 end134 it '#filter_by_codification_kind_and_value' do135 doc1 = FactoryBot.create(:document)136 doc2 = FactoryBot.create(:document)137 FactoryBot.create(:document)138 field1 = doc1.document_fields.find_by(codification_kind: :originating_company)139 field_value1 = field1.document_field_values.find_by(selected: true)140 field_value1.update(value: Faker::Name.initials)141 field2 = doc1.document_fields.find_by(codification_kind: :discipline)142 field_value2 = field2.document_field_values.find_by(selected: true)143 field_value2.update(value: Faker::Name.initials)144 ids = Document.all.filter_by_codification_kind_and_value(:originating_company, field_value1.value)145 ids = ids.filter_by_codification_kind_and_value(:discipline, field_value2.value).pluck(:id)146 expect(ids).to eql([doc1.id])147 field2 = doc2.document_fields.find_by(codification_kind: :originating_company)148 field_value2 = field2.document_field_values.find_by(selected: true)149 field_value2.update(value: Faker::Name.initials)150 ids = Document.all.filter_by_codification_kind_and_value(:originating_company, [field_value1.value, field_value2.value]).pluck(:id)151 expect(ids).to match_array([doc1.id, doc2.id])152 end153 context 'can_view?' do154 it 'solo' do155 user = FactoryBot.create(:user)156 document = document_attributes(user)157 convention = Convention.find(document['convention_id'])158 con_field =159 convention.document_fields.find_by(codification_kind: :originating_company)160 con_value =161 con_field.document_field_values162 .create(value: Faker::Name.initials,163 position: 1,164 title: '')165 field = document['document_fields_attributes'].detect{ |i| i['codification_kind'] == 'originating_company' }166 field['document_field_values_attributes'] << con_value.attributes.except('id')167 doc = Document.new(document)168 doc.save!169 expect(doc.can_view?(user)).to eql(true)170 field = doc.document_fields.find_by(codification_kind: :originating_company)171 field_true = field.document_field_values.find_by(selected: true)172 field_false = field.document_field_values.find_by(selected: false)173 field_true.update_columns(selected: false)174 field_false.update_columns(selected: true)175 expect(doc.reload.can_view?(user)).to eql(false)176 doc.project.members.create!(user: user,177 dms_module_master: true,178 employment_type: :employee)179 expect(doc.can_view?(user)).to eql(true)180 end181 it 'in team' do182 user = FactoryBot.create(:user)183 document = document_attributes(user)184 doc = Document.new(document)185 doc.save!186 project = doc.project187 expect(doc.can_view?(user)).to eql(true)188 DocumentRight.destroy_all189 expect(doc.can_view?(user)).to eql(false)190 project.dms_teams.create191 attrs = DocumentRight.attributes_for_teams(project, true)192 team_attrs = attrs[:teams].first193 team_attrs[:document_rights].each{ |i| i['enabled'] = true }194 team = DmsTeam.find(team_attrs[:id])195 team.users << user196 team.update!(document_rights_attributes: team_attrs[:document_rights])197 expect(doc.can_view?(user)).to eql(true)198 end199 end200 context 'can_create?' do201 it 'solo' do202 user = FactoryBot.create(:user)203 document = document_attributes(user)204 doc = Document.new(document)205 expect(doc.can_create?(user)).to eql(true)206 DocumentRight.destroy_all207 expect(doc.can_create?(user)).to eql(false)208 doc.project.members.create!(user: user,209 dms_module_master: true,210 employment_type: :employee)211 expect(doc.can_create?(user)).to eql(true)212 end213 it 'in team' do214 user = FactoryBot.create(:user)215 document = document_attributes(user)216 doc = Document.new(document)217 project = doc.project218 expect(doc.can_create?(user)).to eql(true)219 DocumentRight.destroy_all220 expect(doc.can_create?(user)).to eql(false)221 project.dms_teams.create222 attrs = DocumentRight.attributes_for_teams(project, true)223 team_attrs = attrs[:teams].first224 team_attrs[:document_rights].each{ |i| i['enabled'] = true }225 team = DmsTeam.find(team_attrs[:id])226 team.users << user227 team.update!(document_rights_attributes: team_attrs[:document_rights])228 expect(doc.can_create?(user)).to eql(true)229 end230 end231 context 'prevent update of fields and values from convention' do232 let(:user) { FactoryBot.create(:user) }233 let(:doc_attrs) do234 document_attributes(user)235 end236 it 'expect document to be valid' do237 doc = Document.new(doc_attrs)238 expect(doc).to be_valid239 end240 # it 'removes one field' do241 # attrs = doc_attrs242 # attrs['document_fields_attributes'].delete_at(1)243 # doc = Document.new(attrs)244 # expect(doc).to_not be_valid245 # expect(doc.errors.count).to eql(2)246 # end247 it 'adds one field' do248 attrs = doc_attrs249 attrs['document_fields_attributes'] << FactoryBot.attributes_for(:document_field)250 doc = Document.new(attrs)251 expect(doc).to_not be_valid252 expect(doc.errors.count).to eql(1)253 end254 it 'removes valid field and adds invalid field' do255 attrs = doc_attrs256 attrs['document_fields_attributes'].delete_at(1)257 attrs['document_fields_attributes'] << FactoryBot.attributes_for(:document_field)258 doc = Document.new(attrs)259 expect(doc).to_not be_valid260 expect(doc.errors.count).to eql(1)261 end262 context 'changes attribute of field' do263 attrs =264 [ 'kind', 'codification_kind', 'column', 'row',265 'required', 'multiselect', 'title', 'command' ]266 let!(:field) do267 doc = Document.create(doc_attrs)268 value = Faker::Name.initials269 field = FactoryBot.build(:document_field, kind: :text_field, title: value)270 @attrs = doc_attrs271 project = get_project_from_document_attrs(@attrs)272 project.conventions.active.document_fields << field273 field.document_rights.create!(parent: user, limit_for: :field, enabled: true)274 field_attrs = field.build_for_new_document(user)275 @attrs['document_fields_attributes'] << field_attrs276 fields = @attrs['document_fields_attributes']277 fields.compact.detect{ |i| i['kind'] == 'text_field' && i['title'] == value }278 end279 attrs.each do |attribute|280 it attribute do281 doc = Document.new(@attrs)282 expect(doc).to be_valid283 old_value = field[attribute]284 field[attribute] =285 if attribute == 'kind'286 'textarea_field'287 elsif attribute == 'codification_kind'288 field['value'] = '1000'289 'document_number'290 elsif attribute == 'column' || attribute == 'row'291 if field[attribute] == 1292 '2'293 elsif field[attribute] == 2294 '1'295 else296 rand(100..999)297 end298 elsif attribute == 'required'299 !field[attribute]300 else301 Faker::Name.initials(number: 9)302 end303 expect(field[attribute]).to_not eql(old_value)304 doc = Document.new(@attrs)305 expect(doc).to_not be_valid306 expect(doc.errors.count).to eql(1)307 end308 end309 end310 it 'adds value to field' do311 attrs = doc_attrs312 fields = attrs['document_fields_attributes']313 field = fields.detect{ |i| i['kind'] == 'select_field' }314 field['document_field_values_attributes'] << FactoryBot.attributes_for(:document_field_value)315 doc = Document.new(attrs)316 expect(doc).to_not be_valid317 expect(doc.errors.count).to eql(1)318 end319 it 'replaces value with wrong value' do320 attrs = doc_attrs321 fields = attrs['document_fields_attributes']322 field = fields.detect{ |i| i['kind'] == 'select_field' }323 field['document_field_values_attributes'] = [FactoryBot.attributes_for(:document_field_value, selected: true)]324 doc = Document.new(attrs)325 expect(doc).to_not be_valid326 expect(doc.errors.count).to eql(1)327 end328 context 'changes attribute of field value' do329 attrs = [ 'value', 'title', 'position' ]330 let!(:field_value) do331 @attrs = doc_attrs332 fields = @attrs['document_fields_attributes']333 field = fields.detect{ |i| i['codification_kind'] == 'originating_company' }334 field['document_field_values_attributes'].first335 end336 attrs.each do |attribute|337 it attribute do338 doc = Document.new(@attrs)339 expect(doc).to be_valid340 old_value = field_value[attribute]341 field_value[attribute] =342 if attribute == 'position'343 rand(100..999)344 else345 Faker::Name.initials(number: 9)346 end347 expect(field_value[attribute]).to_not eql(old_value)348 doc = Document.new(@attrs)349 expect(doc).to_not be_valid350 expect(doc.errors.count).to eql(1)351 end352 end353 it 'title' do354 doc = Document.new(@attrs)355 expect(doc).to be_valid356 Convention.find(@attrs['convention_id']).document_fields.find_by(codification_kind: :originating_company).document_field_values.first.update!(title: nil)357 field_value['title'] = nil358 doc = Document.new(@attrs)359 expect(doc).to be_valid360 end361 end362 end363 it 'assign convention' do364 user = FactoryBot.create(:user)365 document = document_attributes(user)366 document['convention_id'] = nil367 doc = Document.new(document)368 expect(doc.convention).to be_blank369 doc.valid?370 expect(doc.convention).to be_present371 con1 = doc.project.conventions.active372 expect(doc.convention).to eql(con1)373 doc.save!374 con_attrs = assign_attributes_suffix_to_document(con1.attributes_for_edit)375 con2 = doc.project.conventions.create!(con_attrs)376 doc2_attrs = assign_attributes_suffix_to_document(doc.attributes_for_edit)377 doc2 = doc.revision.versions.create!(doc2_attrs)378 expect(doc2.convention).to eql(con1)379 end380 it '#prevent_update_of_previous_revisions' do381 user = FactoryBot.create(:user)382 attrs = document_attributes(user)383 doc1 = Document.create(attrs)384 edit_attrs = assign_attributes_suffix_to_document(doc1.attributes_for_edit)385 doc2 = doc1.revision.versions.new(edit_attrs)386 expect(doc2).to be_valid387 doc1.revision.document_main.revisions.create388 doc3 = doc1.revision.versions.new(edit_attrs)389 expect(doc3).to_not be_valid390 end391 it '#prevent_update_of_revision_number' do392 user = FactoryBot.create(:user)393 attrs = document_attributes(user)394 doc1 = Document.create(attrs)395 edit_attrs = assign_attributes_suffix_to_document(doc1.attributes_for_edit)396 doc2 = doc1.revision.versions.new(edit_attrs)397 expect(doc2).to be_valid398 field = edit_attrs['document_fields_attributes'].detect{ |i| i['codification_kind'] == 'revision_number' }399 field['value'] = field['value'].to_i + 1400 doc3 = doc1.revision.versions.new(edit_attrs)401 expect(doc3).to_not be_valid402 end403 it '#set_review_status_in_document_main' do404 user = FactoryBot.create(:user)405 attrs = document_attributes(user)406 attrs['review_status'] = 'issued_for_approval'407 attrs['reviewers'] = [FactoryBot.create(:user).id]408 attrs['review_issuers'] = [FactoryBot.create(:user).id]409 doc = Document.create!(attrs)410 expect(doc.revision.document_main).to be_issued_for_approval411 end412 it '#review_status_value' do413 user = FactoryBot.create(:user)414 attrs = document_attributes(user)415 attrs['reviewers'] = [FactoryBot.create(:user).id]416 attrs['review_issuers'] = [FactoryBot.create(:user).id]417 doc = Document.new(attrs)418 expect(doc).to be_valid419 doc.review_status = 'in_progress'420 expect(doc).to_not be_valid421 doc.review_status = 'issued_for_review'422 expect(doc).to be_valid423 doc.review_status = 'accepted'424 expect(doc).to_not be_valid425 doc.review_status = 'issued_for_review'426 expect(doc).to be_valid427 doc.review_status = 'rejected'428 expect(doc).to_not be_valid429 doc.review_status = 'issued_for_information'430 expect(doc).to be_valid431 end432 context 'set_reviewers_and_review_issuers_in_document_main' do433 let(:user1) { FactoryBot.create(:user) }434 let(:user2) { FactoryBot.create(:user) }435 let(:user3) { FactoryBot.create(:user) }436 let(:attrs) do437 attrs = document_attributes(user1)438 attrs['review_status'] = 'issued_for_approval'439 attrs['reviewers'] = [user2.id]440 attrs['review_issuers'] = [user3.id]441 attrs442 end443 it do444 doc = Document.create!(attrs)445 main = doc.document_main446 expect(main.reviewers.pluck(:id)).to eql([])447 expect(main.review_issuers.pluck(:id)).to eql([])448 end449 it do450 project = get_project_from_document_attrs(attrs)451 project.members.create!(user: user2,452 dms_module_access: true,453 employment_type: :employee)454 project.members.create!(user: user3,455 dms_module_access: true,456 employment_type: :employee)457 doc = Document.create!(attrs)458 main = doc.document_main459 expect(main.reviewers.pluck(:id)).to eql([user2.id])460 expect(main.review_issuers.pluck(:id)).to eql([user3.id])461 end462 end463 it 'validates reviewers and review issuers length' do464 user1 = FactoryBot.create(:user)465 user2 = FactoryBot.create(:user)466 user3 = FactoryBot.create(:user)467 attrs = document_attributes(user1)468 attrs['review_status'] = 'issued_for_information'469 expect(Document.new(attrs)).to be_valid470 ['issued_for_approval', 'issued_for_review'].each do |status|471 attrs = document_attributes(user1)472 attrs['review_status'] = status473 expect(Document.new(attrs)).to_not be_valid474 attrs['reviewers'] = [user2.id]475 expect(Document.new(attrs)).to_not be_valid476 attrs['review_issuers'] = [user3.id]477 expect(Document.new(attrs)).to be_valid478 end479 end480 it 'codification_string' do481 user = FactoryBot.create(:user)482 attrs = document_attributes(user)483 doc = Document.new(attrs)484 doc.document_main.update(project_code: 'AAA')485 codes = doc.codification_string486 fields = doc.document_fields487 value1 =488 fields.detect{ |i| i['codification_kind'] == 'originating_company' }489 .document_field_values.detect{ |i| i['selected'] == true }.value490 value2 =491 fields.detect{ |i| i['codification_kind'] == 'discipline' }492 .document_field_values.detect{ |i| i['selected'] == true }.value493 value3 =494 fields.detect{ |i| i['codification_kind'] == 'document_type' }495 .document_field_values.detect{ |i| i['selected'] == true }.value496 value4 =497 fields.detect{ |i| i['codification_kind'] == 'document_number' }.value498 string = "AAA-#{value1}-#{value2}-#{value3}-#{value4}"499 expect(codes).to eql(string)500 end501 it 'attributes_for_edit' do502 user = FactoryBot.create(:user)503 attrs = document_attributes(user)504 doc = Document.create(attrs)505 attrs = doc.attributes_for_edit506 expect(attrs['revision_version']).to be_present507 expect(attrs).to have_key('additional_information')508 end509 it 'attributes_for_show' do510 user = FactoryBot.create(:user)511 attrs = document_attributes(user)512 doc = Document.create(attrs)513 expect(doc).to receive(:attributes_for_edit).and_call_original514 team = doc.project.dms_teams.create515 team.document_rights.update_all(enabled: true)516 team.users << user517 doc.save_emails(user, ["#{user.id}"])518 attrs = doc.attributes_for_show519 expect(attrs).to have_key('users_with_rights')520 expect(attrs['users_with_rights'].length).to eql(2)521 user_attrs = attrs['users_with_rights'].first522 expect(user_attrs).to have_key('id')523 expect(user_attrs).to have_key('first_name')524 expect(user_attrs).to have_key('last_name')525 expect(attrs).to have_key('teams_with_rights')526 expect(attrs['teams_with_rights'].length).to eql(1)527 team_attrs = attrs['teams_with_rights'].first528 expect(team_attrs).to have_key('id')529 expect(team_attrs).to have_key('name')530 expect(team_attrs).to have_key('users')531 expect(team_attrs['users'].length).to eql(1)532 expect(team_attrs['users'].first).to have_key('id')533 expect(team_attrs['users'].first).to have_key('first_name')534 expect(team_attrs['users'].first).to have_key('last_name')535 expect(attrs).to have_key('document_email_groups')536 expect(attrs['document_email_groups'].length).to eql(1)537 expect(attrs['document_email_groups'].first).to have_key('created_at')538 expect(attrs['document_email_groups'].first).to have_key('user')539 emails = attrs['document_email_groups'].first['document_emails']540 expect(emails.length).to eql(1)541 expect(emails.first).to have_key('user')542 expect(emails.first).to have_key('email')543 end544 it 'validates document number' do545 user = FactoryBot.create(:user)546 attrs = document_attributes(user)547 field =548 attrs['document_fields_attributes'].detect{ |i| i['codification_kind'] == 'document_number' }549 field['value'] = '10000'550 expect(Document.new(attrs)).to_not be_valid551 field['value'] = 'AAA'552 expect(Document.new(attrs)).to_not be_valid553 field['value'] = '9999'554 expect(Document.new(attrs)).to be_valid555 field['value'] = '0000'556 expect(Document.new(attrs)).to be_valid557 end558 it '#values_for_filters' do559 values = Document.all.values_for_filters(codification_kind: :originating_company)560 expect(values.length).to eql(0)561 doc = FactoryBot.create(:document)562 values = Document.all.values_for_filters(codification_kind: :originating_company)563 expect(values.length).to eql(1)564 expect(values.first.first).to eql('---')565 expect(values.first.last).to eql('Originating company')566 value = doc.document_fields.find_by(codification_kind: :originating_company).document_field_values.first567 value.update_columns(selected: false)568 values = Document.all.values_for_filters(codification_kind: :originating_company)569 expect(values.length).to eql(0)570 end571 it '#filter_by_document_select_field_title_and_value' do572 doc1 = FactoryBot.create(:document)573 doc2 = FactoryBot.create(:document)574 FactoryBot.create(:document)575 field1 = doc1.document_fields.find_by(codification_kind: :originating_company)576 field_value1 = field1.document_field_values.find_by(selected: true)577 field_value1.update(value: Faker::Name.initials)578 field2 = doc1.document_fields.find_by(codification_kind: :discipline)579 field_value2 = field2.document_field_values.find_by(selected: true)580 field_value2.update(value: Faker::Name.initials)581 ids = Document.all.filter_by_document_select_field_title_and_value('Originating company', field_value1.value)582 ids = ids.filter_by_document_select_field_title_and_value('Discipline', field_value2.value).pluck(:id)583 expect(ids).to eql([doc1.id])584 field2 = doc2.document_fields.find_by(codification_kind: :originating_company)585 field_value2 = field2.document_field_values.find_by(selected: true)586 field_value2.update(value: Faker::Name.initials)587 ids = Document.all.filter_by_document_select_field_title_and_value('Originating company', [field_value1.value, field_value2.value]).pluck(:id)588 expect(ids).to match_array([doc1.id, doc2.id])589 end590 it '#filter_by_document_text_field_title_and_value' do591 doc1 = FactoryBot.create(:document)592 doc2 = FactoryBot.create(:document)593 FactoryBot.create(:document)594 field = doc1.document_fields.find_by(codification_kind: :document_number)595 field.update!(value: '1111')596 field = doc1.document_fields.find_by(codification_kind: :revision_number)597 field.update!(value: '111')598 ids = Document.all.filter_by_document_text_field_title_and_value('Document number', '1111').pluck(:id)599 expect(ids).to eql([doc1.id])600 ids = Document.all.filter_by_document_text_field_title_and_value('Document number', '1111')601 ids = ids.filter_by_document_text_field_title_and_value('Revision number', '111').pluck(:id)602 expect(ids).to eql([doc1.id])603 end604 it '#search' do605 doc1 = FactoryBot.create(:document)606 doc2 = FactoryBot.create(:document)607 FactoryBot.create(:document)608 field = doc1.document_fields.find_by(codification_kind: :document_number)609 field.update!(value: '1111')610 ids = Document.all.search('1111').pluck(:id)611 expect(ids).to eql([doc1.id])612 doc2.update!(title: '2222')613 ids = Document.all.search('2222').pluck(:id)614 expect(ids).to eql([doc2.id])615 field = doc1.document_fields.find_by(codification_kind: :originating_company)616 field_value = field.document_field_values.find_by(selected: true)617 field_value.update!(value: '333')618 ids = Document.all.search('333').pluck(:id)619 expect(ids).to eql([doc1.id])620 end621 it 'planned' do622 user = FactoryBot.create(:user)623 attrs = document_attributes(user)624 attrs['review_status'] = 'issued_for_review'625 expect(Document.new(attrs)).to_not be_valid626 rev = DocumentRevision.find(attrs['document_revision_id'])627 rev.document_main.update(planned: true)628 expect(Document.new(attrs)).to be_valid629 end630 it 'unplan document_main' do631 doc = FactoryBot.create(:document)632 user = FactoryBot.create(:user)633 doc.document_main.update!(planned: true)634 edit_attrs = assign_attributes_suffix_to_document(doc.attributes_for_edit)635 edit_attrs['unplan_document'] = true636 edit_attrs['reviewers'] = [user.id]637 edit_attrs['review_issuers'] = [user.id]638 doc.revision.versions.create!(edit_attrs)639 expect(doc.document_main.reload).to_not be_planned640 end641 it 'validate_reviewers and review issuers when unplan document' do642 doc = FactoryBot.create(:document)643 user = FactoryBot.create(:user)644 doc.document_main.update!(planned: true)645 edit_attrs = assign_attributes_suffix_to_document(doc.attributes_for_edit)646 edit_attrs['unplan_document'] = true647 doc2 = doc.revision.versions.new(edit_attrs)648 expect(doc2).to_not be_valid649 edit_attrs['reviewers'] = [user.id]650 edit_attrs['review_issuers'] = [user.id]651 doc2 = doc.revision.versions.new(edit_attrs)652 expect(doc2).to be_valid653 end654 it 'save_emails' do655 doc = FactoryBot.create(:document)656 user = FactoryBot.create(:user)657 user2 = FactoryBot.create(:user)658 email = Faker::Internet.email659 doc.save_emails(user, ['rtwwvwv'])660 expect(doc.document_email_groups.count).to eql(0)661 doc.save_emails(user, [email])662 expect(doc.document_email_groups.count).to eql(1)663 group = doc.document_email_groups.last664 expect(group.document_emails.count).to eql(1)665 expect(group.document_emails.last.email).to eql(email)666 doc.save_emails(user, ["#{user2.id}"])667 expect(doc.document_email_groups.reload.count).to eql(2)668 group = doc.document_email_groups.last669 expect(group.document_emails.count).to eql(1)670 expect(group.document_emails.last.user).to eql(user2)671 end672 it 'send_emails' do673 doc = FactoryBot.create(:document)674 user = FactoryBot.create(:user)675 email = Faker::Internet.email676 doc.save_emails(user, [email])677 dbl = double678 expect(doc).to receive(:document_email_groups).and_return(dbl)679 expect(dbl).to receive(:last).and_return(dbl)680 expect(dbl).to receive(:send_emails)681 doc.send_emails682 end683 it 'users_with_rights' do684 doc = FactoryBot.create(:document)685 user = FactoryBot.create(:user)686 value =687 doc.document_fields688 .find_by(codification_kind: :originating_company)689 .document_field_values690 .find_by(selected: true)691 .value692 con_field =693 doc.convention.document_fields.find_by(codification_kind: :originating_company)694 con_value =695 con_field.document_field_values.find_by(value: value)696 con_field.document_rights.create!(document_field_value: con_value,697 limit_for: :value,698 enabled: true,699 parent: user)700 ids = doc.users_with_rights701 expect(ids).to include(doc.user.id)702 expect(ids).to include(doc.project.user.id)703 expect(ids).to_not include(user.id)704 end705 it 'teams_with_rights' do706 doc = FactoryBot.create(:document)707 team1 = doc.project.dms_teams.create708 team2 = doc.project.dms_teams.create709 team1.document_rights.update_all(enabled: true)...

Full Screen

Full Screen

dev.rake

Source:dev.rake Github

copy

Full Screen

...4 task load_sample_data: :environment do5 require_relative '../../spec/factories'6 require_relative '../../spec/support/spree/init'7 task_name = "openfoodnetwork:dev:load_sample_data"8 country = Spree::Country.find_by_iso(ENV.fetch('DEFAULT_COUNTRY_CODE'))9 state = country.states.first10 # -- Shipping / payment information11 unless Spree::Zone.find_by_name 'Australia'12 puts "[#{task_name}] Seeding shipping / payment information"13 zone = FactoryBot.create(:zone, name: 'Australia', zone_members: [])14 Spree::ZoneMember.create(zone: zone, zoneable: country)15 address = FactoryBot.create(16 :address,17 address1: "15/1 Ballantyne Street",18 zipcode: "3153",19 city: "Thornbury",20 country: country,21 state: state22 )23 enterprise = FactoryBot.create(:enterprise, address: address)24 FactoryBot.create(:shipping_method, zone: zone, distributors: [enterprise])25 end26 # -- Taxonomies27 unless Spree::Taxonomy.find_by_name 'Products'28 puts "[#{task_name}] Seeding taxonomies"29 taxonomy = Spree::Taxonomy.find_by_name('Products') || FactoryBot.create(:taxonomy, name: 'Products')30 taxonomy_root = taxonomy.root31 ['Vegetables', 'Fruit', 'Oils', 'Preserves and Sauces', 'Dairy', 'Meat and Fish'].each do |taxon_name|32 FactoryBot.create(:taxon, name: taxon_name, parent_id: taxonomy_root.id)33 end34 end35 # -- Addresses36 unless Spree::Address.find_by_zipcode "3160"37 puts "[#{task_name}] Seeding addresses"38 FactoryBot.create(:address, address1: "25 Myrtle Street", zipcode: "3153", city: "Bayswater", country: country, state: state)39 FactoryBot.create(:address, address1: "6 Rollings Road", zipcode: "3156", city: "Upper Ferntree Gully", country: country, state: state)40 FactoryBot.create(:address, address1: "72 Lake Road", zipcode: "3130", city: "Blackburn", country: country, state: state)41 FactoryBot.create(:address, address1: "7 Verbena Street", zipcode: "3195", city: "Mordialloc", country: country, state: state)42 FactoryBot.create(:address, address1: "20 Galvin Street", zipcode: "3018", city: "Altona", country: country, state: state)43 FactoryBot.create(:address, address1: "59 Websters Road", zipcode: "3106", city: "Templestowe", country: country, state: state)44 FactoryBot.create(:address, address1: "17 Torresdale Drive", zipcode: "3155", city: "Boronia", country: country, state: state)45 FactoryBot.create(:address, address1: "21 Robina CRT", zipcode: "3764", city: "Kilmore", country: country, state: state)46 FactoryBot.create(:address, address1: "25 Kendall Street", zipcode: "3134", city: "Ringwood", country: country, state: state)47 FactoryBot.create(:address, address1: "2 Mines Road", zipcode: "3135", city: "Ringwood East", country: country, state: state)48 FactoryBot.create(:address, address1: "183 Millers Road", zipcode: "3025", city: "Altona North", country: country, state: state)49 FactoryBot.create(:address, address1: "310 Pascoe Vale Road", zipcode: "3040", city: "Essendon", country: country, state: state)50 FactoryBot.create(:address, address1: "6 Martin Street", zipcode: "3160", city: "Belgrave", country: country, state: state)51 end52 # -- Enterprises53 unless Enterprise.count > 154 puts "[#{task_name}] Seeding enterprises"55 3.times { FactoryBot.create(:supplier_enterprise, address: Spree::Address.find_by_zipcode("3160")) }56 FactoryBot.create(:distributor_enterprise, name: "Green Grass", address: Spree::Address.find_by_zipcode("3153"))57 FactoryBot.create(:distributor_enterprise, name: "AusFarmers United", address: Spree::Address.find_by_zipcode("3156"))58 FactoryBot.create(:distributor_enterprise, name: "Blackburn FreeGrossers", address: Spree::Address.find_by_zipcode("3130"))59 FactoryBot.create(:distributor_enterprise, name: "MegaFoods", address: Spree::Address.find_by_zipcode("3195"))60 FactoryBot.create(:distributor_enterprise, name: "Eco Butchers", address: Spree::Address.find_by_zipcode("3018"))61 FactoryBot.create(:distributor_enterprise, name: "Western Wines", address: Spree::Address.find_by_zipcode("3106"))62 FactoryBot.create(:distributor_enterprise, name: "QuickFresh", address: Spree::Address.find_by_zipcode("3155"))63 FactoryBot.create(:distributor_enterprise, name: "Fooderers", address: Spree::Address.find_by_zipcode("3764"))64 FactoryBot.create(:distributor_enterprise, name: "Food Local", address: Spree::Address.find_by_zipcode("3134"))65 FactoryBot.create(:distributor_enterprise, name: "Green Food Trading Corporation", address: Spree::Address.find_by_zipcode("3135"))66 FactoryBot.create(:distributor_enterprise, name: "Better Food", address: Spree::Address.find_by_zipcode("3025"))67 FactoryBot.create(:distributor_enterprise, name: "Gippsland Poultry", address: Spree::Address.find_by_zipcode("3040"))68 end69 # -- Enterprise users70 unless Spree::User.count > 171 puts "[#{task_name}] Seeding enterprise users"72 pw = "spree123"73 u = FactoryBot.create(:user, email: "sup@example.com", password: pw, password_confirmation: pw)74 u.enterprises << Enterprise.is_primary_producer.first75 u.enterprises << Enterprise.is_primary_producer.second76 puts " Supplier User created: #{u.email}/#{pw} (" + u.enterprise_roles.map{ |er| er.enterprise.name}.join(", ") + ")"77 u = FactoryBot.create(:user, email: "dist@example.com", password: pw, password_confirmation: pw)78 u.enterprises << Enterprise.is_distributor.first79 u.enterprises << Enterprise.is_distributor.second80 puts " Distributor User created: #{u.email}/#{pw} (" + u.enterprise_roles.map{ |er| er.enterprise.name}.join(", ") + ")"81 end82 # -- Enterprise fees83 unless EnterpriseFee.count > 184 Enterprise.is_distributor.each do |distributor|85 FactoryBot.create(:enterprise_fee, enterprise: distributor)86 end87 end88 # -- Enterprise Payment Methods89 unless Spree::PaymentMethod.count > 190 Enterprise.is_distributor.each do |distributor|91 FactoryBot.create(:payment_method, distributors: [distributor], name: "Cheque (#{distributor.name})", environment: 'development')92 end93 end94 # -- Products95 unless Spree::Product.count > 096 puts "[#{task_name}] Seeding products"97 prod1 = FactoryBot.create(:product,98 name: 'Garlic', price: 20.00,99 supplier: Enterprise.is_primary_producer[0],100 taxons: [Spree::Taxon.find_by_name('Vegetables')])101 ProductDistribution.create(product: prod1,102 distributor: Enterprise.is_distributor[0],103 enterprise_fee: Enterprise.is_distributor[0].enterprise_fees.first)104 prod2 = FactoryBot.create(:product,105 name: 'Fuji Apple', price: 5.00,106 supplier: Enterprise.is_primary_producer[1],107 taxons: [Spree::Taxon.find_by_name('Fruit')])108 ProductDistribution.create(product: prod2,109 distributor: Enterprise.is_distributor[1],110 enterprise_fee: Enterprise.is_distributor[1].enterprise_fees.first)111 prod3 = FactoryBot.create(:product,112 name: 'Beef - 5kg Trays', price: 50.00,113 supplier: Enterprise.is_primary_producer[2],114 taxons: [Spree::Taxon.find_by_name('Meat and Fish')])115 ProductDistribution.create(product: prod3,116 distributor: Enterprise.is_distributor[2],117 enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first)118 prod4 = FactoryBot.create(:product,119 name: 'Carrots', price: 3.00,120 supplier: Enterprise.is_primary_producer[2],121 taxons: [Spree::Taxon.find_by_name('Meat and Fish')])122 ProductDistribution.create(product: prod4,123 distributor: Enterprise.is_distributor[2],124 enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first)125 prod5 = FactoryBot.create(:product,126 name: 'Potatoes', price: 2.00,127 supplier: Enterprise.is_primary_producer[2],128 taxons: [Spree::Taxon.find_by_name('Meat and Fish')])129 ProductDistribution.create(product: prod5,130 distributor: Enterprise.is_distributor[2],131 enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first)132 prod6 = FactoryBot.create(:product,133 name: 'Tomatoes', price: 2.00,134 supplier: Enterprise.is_primary_producer[2],135 taxons: [Spree::Taxon.find_by_name('Meat and Fish')])136 ProductDistribution.create(product: prod6,137 distributor: Enterprise.is_distributor[2],138 enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first)139 prod7 = FactoryBot.create(:product,140 name: 'Potatoes', price: 2.00,141 supplier: Enterprise.is_primary_producer[2],142 taxons: [Spree::Taxon.find_by_name('Meat and Fish')])143 ProductDistribution.create(product: prod7,144 distributor: Enterprise.is_distributor[2],145 enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first)146 end147 enterprise2 = Enterprise.find_by_name('Enterprise 2')148 enterprise2.sells = 'any'149 enterprise2.shipping_methods.build(150 name: 'Pickup',151 zone_id: 3,152 require_ship_address: true,153 calculator_type: 'OpenFoodNetwork::Calculator::Weight',154 distributor_ids: [enterprise2.id]155 )156 enterprise2.payment_methods << Spree::PaymentMethod.last157 enterprise2.save!158 variants = Spree::Variant159 .joins(:product)160 .where('spree_products.supplier_id = ?', enterprise2.id)161 CreateOrderCycle.new(enterprise2, variants).call...

Full Screen

Full Screen

task_spec.rb

Source:task_spec.rb Github

copy

Full Screen

...3 describe 'バリデーションチェック(User)' do4 describe 'カラムの有無' do5 # 成功パターン6 it "name/detail/task_time/tag_idsがある場合、有効な状態であること" do7 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)8 tag = FactoryBot.create(:tag, user_id: user.id)9 custom = FactoryBot.create(:custom, user_id: user.id) 10 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "test01", task_time: "0.5") 11 expect(task).to be_valid 12 end13 # 失敗パターン14 it "nameがない場合、無効な状態であること" do15 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)16 tag = FactoryBot.create(:tag, user_id: user.id)17 custom = FactoryBot.create(:custom, user_id: user.id) 18 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "", detail: "test01", task_time: "0.5") 19 expect(task).not_to be_valid20 end21 it "detailがない場合、無効な状態であること" do22 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)23 tag = FactoryBot.create(:tag, user_id: user.id)24 custom = FactoryBot.create(:custom, user_id: user.id) 25 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "", task_time: "0.5") 26 expect(task).not_to be_valid27 end28 it "task_timeがない場合、無効な状態であること" do29 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)30 tag = FactoryBot.create(:tag, user_id: user.id)31 custom = FactoryBot.create(:custom, user_id: user.id) 32 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "test01", task_time: "") 33 expect(task).not_to be_valid34 end35 it "tag_idsがない場合、無効な状態であること" do36 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)37 tag = FactoryBot.create(:tag, user_id: user.id)38 custom = FactoryBot.create(:custom, user_id: user.id) 39 task = Task.new(user_id: user.id, custom_id: custom.id, name: "test01", detail: "test01", task_time: "3.0") 40 expect(task).not_to be_valid41 end42 end43 describe '全角を半角に変更するメソッドのチェック' do44 # 成功パターンのみ45 it "task_timeが全角な場合でも、有効な状態であること" do46 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)47 tag = FactoryBot.create(:tag, user_id: user.id)48 custom = FactoryBot.create(:custom, user_id: user.id) 49 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "test01", task_time: "3") 50 expect(task).to be_valid51 end52 end53 describe '文字数制限' do54 # 成功パターン55 it "nameが16文字以内の場合、有効な状態であること" do56 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)57 tag = FactoryBot.create(:tag, user_id: user.id)58 custom = FactoryBot.create(:custom, user_id: user.id) 59 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "#{"a"*16}", detail: "test01", task_time: "3.0") 60 expect(task).to be_valid61 end62 it "detailが512文字以内の場合、有効な状態であること" do63 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)64 tag = FactoryBot.create(:tag, user_id: user.id)65 custom = FactoryBot.create(:custom, user_id: user.id) 66 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "#{"a"*512}", task_time: "3.0") 67 expect(task).to be_valid68 end69 it "task_timeが0より大きく24より小さい場合、有効な状態であること(1をテスト)" do70 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)71 tag = FactoryBot.create(:tag, user_id: user.id)72 custom = FactoryBot.create(:custom, user_id: user.id) 73 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "test01", task_time: "1") 74 expect(task).to be_valid75 end76 it "task_timeが0より大きく24より小さい場合、有効な状態であること(23をテスト)" do77 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)78 tag = FactoryBot.create(:tag, user_id: user.id)79 custom = FactoryBot.create(:custom, user_id: user.id) 80 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "test01", task_time: "23") 81 expect(task).to be_valid82 end83 # 失敗パターン84 it "nameが17文字以上の場合、無効な状態であること" do85 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)86 tag = FactoryBot.create(:tag, user_id: user.id)87 custom = FactoryBot.create(:custom, user_id: user.id) 88 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "#{"a"*17}", detail: "test01", task_time: "3.0") 89 expect(task).not_to be_valid90 end91 it "detailが513文字以上の場合、無効な状態であること" do92 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)93 tag = FactoryBot.create(:tag, user_id: user.id)94 custom = FactoryBot.create(:custom, user_id: user.id) 95 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "#{"a"*513}", task_time: "3.0") 96 expect(task).not_to be_valid97 end98 it "task_timeが0以下の場合、無効な状態であること" do99 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)100 tag = FactoryBot.create(:tag, user_id: user.id)101 custom = FactoryBot.create(:custom, user_id: user.id) 102 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "test01", task_time: "0") 103 expect(task).not_to be_valid104 end105 it "task_timeが24より大きい場合、無効な状態であること" do106 user = User.find_by(email: FactoryBot.build(:user).email) || FactoryBot.create(:user)107 tag = FactoryBot.create(:tag, user_id: user.id)108 custom = FactoryBot.create(:custom, user_id: user.id) 109 task = Task.new(user_id: user.id, tag_ids: [tag.id], custom_id: custom.id, name: "test01", detail: "test01", task_time: "24.1") 110 expect(task).not_to be_valid111 end112 end113 end114end...

Full Screen

Full Screen

find

Using AI Code Generation

copy

Full Screen

1 first_name { Faker::Name.first_name }2 last_name { Faker::Name.last_name }3 email { Faker::Internet.email }4 first_name { Faker::Name.first_name }5 last_name { Faker::Name.last_name }6 email { Faker::Internet.email }7 first_name { Faker::Name.first_name }8 last_name { Faker::Name.last_name }9 email { Faker::Internet.email }10 first_name { Faker::Name.first_name }11 last_name { Faker::Name.last_name }12 email { Faker::Internet.email }13 first_name { Faker::Name.first_name }14 last_name { Faker::Name.last_name }15 email { Faker::Internet.email }16 first_name { Faker::Name.first_name }17 last_name { Faker::Name.last_name }18 email { Faker::Internet.email }19 first_name { Faker::Name.first_name }20 last_name { Faker::Name.last_name }21 email {

Full Screen

Full Screen

find

Using AI Code Generation

copy

Full Screen

1@user = FactoryBot.find(1)2@user = FactoryBot.find(1)3@user = FactoryBot.find(1)

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 Factory_bot_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