Best Factory_bot_ruby code snippet using FactoryBot.value
document_spec.rb
Source:document_spec.rb
...6 user = FactoryBot.create(:user)7 document = document_attributes(user)8 expect(Document.new(document)).to be_valid9 expect(document['document_fields_attributes'].length).to eql(8)10 expect(document['document_fields_attributes'].detect{ |i| i['codification_kind'] == 'originating_company' }['document_field_values_attributes'].length).to eql(1)11 end12 it 'check rights in teams' do13 user = FactoryBot.create(:user)14 project = FactoryBot.create(:project)15 convention = project.conventions.active16 convention.document_fields.each do |field|17 if field.select_field?18 value = field.document_field_values.first19 if field.can_limit_by_value?20 field.document_rights.create!(parent: user,21 limit_for: :value,22 document_field_value: value,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)710 ids = doc.teams_with_rights711 expect(ids).to include(team1.id)...
document_field_spec.rb
Source:document_field_spec.rb
1require 'rails_helper'2RSpec.describe DocumentField, type: :model do3 context 'validate :value_by_kind' do4 let(:document) { FactoryBot.build(:document) }5 subject { FactoryBot.build(:document_field, parent: document) }6 it { should be_valid }7 [:select_field, :project_phase_field].each do |kind|8 it kind do9 subject.kind = kind10 should be_valid11 subject.document_field_values.delete_all12 should_not be_valid13 end14 end15 context 'codification field' do16 [:originating_company, :receiving_company, :discipline, :document_type].each do |kind|17 it kind do18 subject.kind = :select_field19 subject.codification_kind = kind20 subject.document_field_values.first.selected = true21 should be_valid22 subject.document_field_values.delete_all23 should_not be_valid24 end25 end26 [:document_number, :revision_number, :revision_version].each do |kind|27 it kind do28 subject.kind = :text_field29 subject.codification_kind = kind30 subject.value = '1000' if kind == :document_number31 should be_valid32 subject.document_field_values.delete_all33 should be_valid34 end35 end36 end37 end38 context '#can_build?' do39 subject { FactoryBot.create(:document_field) }40 let(:user) { FactoryBot.create(:user) }41 context 'non codification' do42 before do43 subject.document_rights.create(document_field: subject,44 limit_for: :field,45 parent: user,46 enabled: true)47 end48 # limitation by field is temporarily disabled49 # it do50 # expect(subject).to_not be_select_field51 # expect(subject.parent.class.name).to eql('Convention')52 # expect(subject.can_build?(user)).to eql(true)53 # end54 it do55 subject.parent = FactoryBot.create(:document_main)56 expect(subject.can_build?(user)).to eql(false)57 end58 end59 context 'codification' do60 before do61 subject.kind = :select_field62 subject.codification_kind = :originating_company63 end64 it do65 expect(subject).to be_select_field66 expect(subject.document_rights).to_not be_any67 expect(subject.can_build?(user)).to eql(false)68 end69 it do70 expect(subject.document_field_values).to be_any71 subject.document_rights.create(limit_for: :field, parent: user)72 expect(subject.can_build?(user)).to eql(false)73 end74 it do75 subject.document_rights.create(limit_for: :value,76 parent: user,77 document_field_value: subject.document_field_values.first)78 expect(subject.can_build?(user)).to eql(true)79 end80 end81 end82 context '#can_limit_by_value?' do83 subject { FactoryBot.build(:document_field, kind: :select_field) }84 kinds = ['originating_company', 'discipline', 'document_type']85 kinds.each do |kind|86 it kind do87 subject.codification_kind = kind88 expect(subject).to be_can_limit_by_value89 end90 end91 (DocumentField.codification_kinds.keys - kinds).each do |kind|92 it kind do93 subject.codification_kind = kind94 expect(subject).to_not be_can_limit_by_value95 end96 end97 end98 context '#should_have_document_field_values?' do99 subject { FactoryBot.build(:document_field) }100 kinds = ['select_field', 'project_phase_field']101 kinds.each do |kind|102 it kind do103 subject.kind = kind104 expect(subject).to be_should_have_document_field_values105 end106 end107 (DocumentField.kinds.keys - kinds).each do |kind|108 it kind do109 subject.kind = kind110 expect(subject).to_not be_should_have_document_field_values111 end112 end113 end114 it 'revision_number_valid' do115 doc1 = FactoryBot.create(:document)116 rev1 = doc1.revision117 main = rev1.document_main118 field = doc1.document_fields.find_by(codification_kind: :revision_number)119 field.update!(value: 0)120 attrs = assign_attributes_suffix_to_document(doc1.reload.attributes_for_edit)121 rev2 = main.revisions.create122 doc2 = rev2.versions.new(attrs)123 expect(doc2).to be_valid124 expect(doc2.document_fields.detect{ |i| i['codification_kind'] == 'revision_number' }.value).to eql('01')125 doc2.document_fields.detect{ |i| i['codification_kind'] == 'revision_number' }.value = '2'126 expect(doc2).to be_valid127 doc2.document_fields.detect{ |i| i['codification_kind'] == 'revision_number' }.value = '100'128 expect(doc2).to_not be_valid129 doc2.document_fields.detect{ |i| i['codification_kind'] == 'revision_number' }.value = '99'130 expect(doc2).to be_valid131 end132 it 'revision_version_valid' do133 ver1 = FactoryBot.create(:document)134 rev = ver1.revision135 field = ver1.document_fields.find_by(codification_kind: :revision_version)136 expect(field.value).to eql('0')137 expect(ver1.revision_version).to eql('0')138 attrs = assign_attributes_suffix_to_document(ver1.attributes_for_edit)139 ver2 = rev.versions.new(attrs)140 ver2.save!141 expect(ver2.document_fields.detect{ |i| i['codification_kind'] == 'revision_version' }.value).to eql('1')142 expect(ver2.revision_version).to eql('1')143 ver1.save!144 expect(ver1.document_fields.detect{ |i| i['codification_kind'] == 'revision_version' }.value).to eql('0')145 expect(ver1.revision_version).to eql('0')146 end147 context 'prevents codification fields updating by' do148 it 'updating document' do149 doc = FactoryBot.create(:document)150 field = doc.document_fields.find_by(codification_kind: DocumentField.codification_kinds[:originating_company])151 field.document_field_values.update_all(selected: false)152 value1 = FactoryBot.create(:document_field_value, value: '111', selected: true, document_field: field)153 value2 = FactoryBot.create(:document_field_value, value: '222', document_field: field)154 doc_attrs = assign_attributes_suffix_to_document(doc.reload.attributes_for_edit)155 doc_attrs['document_fields_attributes']156 .detect{ |i| i['codification_kind'] == 'originating_company' }['document_field_values_attributes']157 .detect{ |i| i['value'] == value1.value }['value'] = '333'158 doc.assign_attributes(doc_attrs)159 expect(doc).to_not be_valid160 end161 it 'creating new version' do162 rev = FactoryBot.create(:document_revision)163 doc = FactoryBot.create(:document, revision: rev)164 field = doc.document_fields.find_by(codification_kind: DocumentField.codification_kinds[:originating_company])165 field.document_field_values.update_all(selected: false)166 value1 = FactoryBot.create(:document_field_value, value: '111', selected: true, document_field: field)167 value2 = FactoryBot.create(:document_field_value, value: '222', document_field: field)168 doc_attrs = assign_attributes_suffix_to_document(doc.reload.attributes_for_edit)169 doc_attrs['document_fields_attributes']170 .detect{ |i| i['codification_kind'] == 'originating_company' }['document_field_values_attributes']171 .detect{ |i| i['value'] == value1.value }['value'] = '333'172 doc2 = rev.versions.new(doc_attrs)173 expect(doc2).to_not be_valid174 end175 it 'creating new revision' do176 doc = FactoryBot.create(:document)177 rev = doc.revision178 main = rev.document_main179 field = doc.document_fields.find_by(codification_kind: :originating_company)180 field.document_field_values.update_all(selected: false)181 value1 = FactoryBot.create(:document_field_value, value: '111', selected: true, document_field: field)182 value2 = FactoryBot.create(:document_field_value, value: '222', document_field: field)183 doc_attrs = assign_attributes_suffix_to_document(doc.reload.attributes_for_edit)184 doc_attrs['document_fields_attributes']185 .detect{ |i| i['codification_kind'] == 'originating_company' }['document_field_values_attributes']186 .detect{ |i| i['value'] == value1.value }['value'] = '333'187 rev2 = FactoryBot.create(:document_revision, document_main: main)188 doc2 = rev2.versions.new(doc_attrs)189 expect(doc2).to_not be_valid190 end191 end192 context '#field_is_required' do193 let(:document) { FactoryBot.create(:document) }194 it 'text_field' do195 field = FactoryBot.build(:document_field, required: true)196 document.document_fields << field197 expect(document).to be_valid198 field.value = nil199 expect(document).to_not be_valid200 end201 [:textarea_field, :date_field].each do |kind|202 it kind do203 field = FactoryBot.build(:document_field, required: true, kind: kind)204 document.document_fields << field205 expect(document).to be_valid206 field.value = nil207 expect(document).to_not be_valid208 end209 end210 it 'select_field' do211 field = FactoryBot.build(:document_field, required: true, kind: :select_field)212 value = FactoryBot.build(:document_field_value, selected: true)213 field.document_field_values << value214 document.document_fields << field215 expect(document).to be_valid216 value.selected = false217 expect(document).to_not be_valid218 end219 it 'project_phase_field' do220 field = FactoryBot.build(:document_field, required: true, kind: :project_phase_field)221 value = FactoryBot.build(:document_field_value, selected: true)222 field.document_field_values << value223 document.document_fields << field224 expect(document).to be_valid225 value.selected = false226 expect(document).to_not be_valid227 end228 ['originating_company', 'discipline', 'document_type'].each do |kind|229 it kind do230 document.document_fields231 .detect{ |i| i['codification_kind'] == kind }232 .document_field_values233 .first.selected = false234 expect(document).to_not be_valid235 end236 end237 it 'receiving_company' do238 field = FactoryBot.build(:document_field, required: true, kind: :select_field, codification_kind: :receiving_company)239 value = FactoryBot.build(:document_field_value, selected: true)240 field.document_field_values << value241 document.document_fields << field242 expect(document).to be_valid243 value.selected = false244 expect(document).to_not be_valid245 end246 [:document_number, :revision_number].each do |kind|247 it kind do248 attrs = assign_attributes_suffix_to_document(document.attributes_for_edit)249 field = attrs['document_fields_attributes'].detect{ |i| i['codification_kind'] == kind.to_s }250 doc2 = Document.new(attrs)251 expect(doc2).to be_valid252 field['value'] = nil253 doc2 = Document.new(attrs)254 expect(doc2).to_not be_valid255 end256 end257 it 'revision_date' do258 field = document.document_fields.find_by(codification_kind: :revision_date)259 expect(field).to be_valid260 field.value = nil261 expect(field).to_not be_valid262 end263 it 'upload_field' do264 field = FactoryBot.create(:document_field, required: true, kind: :upload_field)265 document.project.conventions.active.document_fields << field266 field.file.attach(fixture_file_upload('test.txt'))267 document.document_fields << field268 expect(document).to be_valid269 allow(document).to receive(:first_document_in_chain?).and_return(true)270 field.file.purge271 expect(document).to_not be_valid272 end273 end274 it '#multiselect_is_not_allowed' do275 document = FactoryBot.create(:document)276 value = FactoryBot.build(:document_field_value, selected: true)277 field = document.document_fields.find_by(codification_kind: :originating_company)278 field.document_field_values << value279 expect(field).to_not be_valid280 expect(field.errors.count).to eql(1)281 value.selected = false282 expect(field).to be_valid283 end284 context '#attach_previous_native_file' do285 let(:doc) { FactoryBot.create(:document) }286 let(:rev) { doc.revision }287 let(:doc_attrs) { assign_attributes_suffix_to_document(doc.attributes_for_edit) }288 let(:file_field1) { doc.document_fields.find_by(codification_kind: :document_native_file) }289 it 'new version' do290 file1 = file_field1.file291 doc2 = rev.versions.create!(doc_attrs)292 file_field2 = doc2.document_fields.find_by(codification_kind: :document_native_file)293 file2 = file_field2.file294 expect(file1.blob_id).to_not eql(file2.blob_id)295 expect(file1.download).to eql(file2.download)296 end297 it 'new revision' do298 file1 = file_field1.file299 rev2 = FactoryBot.create(:document_revision, document_main: rev.document_main)300 doc_attrs['document_fields_attributes'].detect{ |i| i['codification_kind'] == 'revision_number' }['value'] = '2'301 doc2 = rev2.versions.create!(doc_attrs)302 file_field2 = doc2.document_fields.find_by(codification_kind: :document_native_file)303 file2 = file_field2.file304 expect(file1.blob_id).to_not eql(file2.blob_id)305 expect(file1.download).to eql(file2.download)306 end307 end308 it 'check if field has values' do309 doc = FactoryBot.create(:document)310 field = FactoryBot.create(:document_field, kind: :select_field, title: '111')311 field.update!(parent: doc)312 attrs = doc.reload.attributes_for_edit313 field_attrs = attrs['document_fields'].detect{ |i| i['title'] == '111' }314 expect(field_attrs['document_field_values'].length).to eql(1)315 end316end...
user_factories.rb
Source:user_factories.rb
...45 end46 factory :preference do47 user48 name { raise "Please specify :name for the preference" }49 value { raise "Please specify :value for the preference" }50 updated_at { FactoryBot.generate(:time) }51 created_at { FactoryBot.generate(:time) }52 end53 factory :group do54 name { FFaker::Company.name }55 updated_at { FactoryBot.generate(:time) }56 created_at { FactoryBot.generate(:time) }57 end58end...
value
Using AI Code Generation
1FactoryBot.create(:user, :admin)2FactoryBot.create(:user, :admin)3FactoryBot.create(:user, :admin)4FactoryBot.create(:user, :admin)5FactoryBot.create(:user, :admin)6FactoryBot.create(:user, :admin)7FactoryBot.create(:user, :admin)8FactoryBot.create(:user, :admin)9FactoryBot.create(:user, :admin)10FactoryBot.create(:user, :admin)11FactoryBot.create(:user, :admin)12FactoryBot.create(:user, :admin)13FactoryBot.create(:user, :admin)14FactoryBot.create(:user, :admin)15FactoryBot.create(:user, :admin)16FactoryBot.create(:user, :admin)17FactoryBot.create(:user, :admin)18FactoryBot.create(:user, :admin)19FactoryBot.create(:user, :admin)20FactoryBot.create(:user, :admin)21FactoryBot.create(:user, :admin)22FactoryBot.create(:user, :admin)23FactoryBot.create(:user, :admin)24FactoryBot.create(:user, :admin)
value
Using AI Code Generation
1FactoryBot.value(:string, 10)2FactoryBot.value(:integer, 10)3FactoryBot.value(:float, 10)4FactoryBot.value(:boolean)5FactoryBot.value(:date)6FactoryBot.value(:time)7FactoryBot.value(:datetime)8FactoryBot.value(:array, 10)9FactoryBot.value(:hash, 10)10FactoryBot.value(:symbol, 10)11FactoryBot.value(:range, 10)12FactoryBot.value(:range, 10)
value
Using AI Code Generation
1 @user = FactoryBot.create(:user)2 @user = FactoryBot.create(:user)3 @user = FactoryBot.create(:user)4 @user = FactoryBot.create(:user)5 @user = FactoryBot.create(:user)6 @user = FactoryBot.create(:user)7 @user = FactoryBot.create(:user)8 @user = FactoryBot.create(:user)
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!