Best Rr_ruby code snippet using TestFile.content
attachment_test.rb
Source:attachment_test.rb
...21 fixtures :users, :projects, :roles, :members, :member_roles,22 :enabled_modules, :issues, :trackers, :attachments23 24 class MockFile25 attr_reader :original_filename, :content_type, :content, :size26 27 def initialize(attributes)28 @original_filename = attributes[:original_filename]29 @content_type = attributes[:content_type]30 @content = attributes[:content] || "Content"31 @size = content.size32 end33 end34 def setup35 set_tmp_attachments_directory36 end37 def test_container_for_new_attachment_should_be_nil38 assert_nil Attachment.new.container39 end40 def test_create41 a = Attachment.new(:container => Issue.find(1),42 :file => uploaded_test_file("testfile.txt", "text/plain"),43 :author => User.find(1))44 assert a.save45 assert_equal 'testfile.txt', a.filename46 assert_equal 59, a.filesize47 assert_equal 'text/plain', a.content_type48 assert_equal 0, a.downloads49 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest50 assert File.exist?(a.diskfile)51 assert_equal 59, File.size(a.diskfile)52 end53 def test_size_should_be_validated_for_new_file54 with_settings :attachment_max_size => 0 do55 a = Attachment.new(:container => Issue.find(1),56 :file => uploaded_test_file("testfile.txt", "text/plain"),57 :author => User.find(1))58 assert !a.save59 end60 end61 def test_size_should_not_be_validated_when_copying62 a = Attachment.create!(:container => Issue.find(1),63 :file => uploaded_test_file("testfile.txt", "text/plain"),64 :author => User.find(1))65 with_settings :attachment_max_size => 0 do66 copy = a.copy67 assert copy.save68 end69 end70 def test_destroy71 a = Attachment.new(:container => Issue.find(1),72 :file => uploaded_test_file("testfile.txt", "text/plain"),73 :author => User.find(1))74 assert a.save75 assert_equal 'testfile.txt', a.filename76 assert_equal 59, a.filesize77 assert_equal 'text/plain', a.content_type78 assert_equal 0, a.downloads79 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest80 diskfile = a.diskfile81 assert File.exist?(diskfile)82 assert_equal 59, File.size(a.diskfile)83 assert a.destroy84 assert !File.exist?(diskfile)85 end86 def test_destroy_should_not_delete_file_referenced_by_other_attachment87 a = Attachment.create!(:container => Issue.find(1),88 :file => uploaded_test_file("testfile.txt", "text/plain"),89 :author => User.find(1))90 diskfile = a.diskfile91 copy = a.copy92 copy.save!93 assert File.exists?(diskfile)94 a.destroy95 assert File.exists?(diskfile)96 copy.destroy97 assert !File.exists?(diskfile)98 end99 def test_create_should_auto_assign_content_type100 a = Attachment.new(:container => Issue.find(1),101 :file => uploaded_test_file("testfile.txt", ""),102 :author => User.find(1))103 assert a.save104 assert_equal 'text/plain', a.content_type105 end106 def test_identical_attachments_at_the_same_time_should_not_overwrite107 a1 = Attachment.create!(:container => Issue.find(1),108 :file => uploaded_test_file("testfile.txt", ""),109 :author => User.find(1))110 a2 = Attachment.create!(:container => Issue.find(1),111 :file => uploaded_test_file("testfile.txt", ""),112 :author => User.find(1))113 assert a1.disk_filename != a2.disk_filename114 end115 116 def test_filename_should_be_basenamed117 a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))118 assert_equal 'file', a.filename119 end120 121 def test_filename_should_be_sanitized122 a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))123 assert_equal 'valid_[] invalid_chars', a.filename124 end125 def test_diskfilename126 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/127 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]128 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1]129 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]130 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]131 end132 def test_prune_should_destroy_old_unattached_attachments133 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)134 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)135 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)136 assert_difference 'Attachment.count', -2 do137 Attachment.prune138 end139 end140 context "Attachmnet.attach_files" do141 should "attach the file" do142 issue = Issue.first143 assert_difference 'Attachment.count' do144 Attachment.attach_files(issue,145 '1' => {146 'file' => uploaded_test_file('testfile.txt', 'text/plain'),147 'description' => 'test'148 })149 end150 attachment = Attachment.first(:order => 'id DESC')151 assert_equal issue, attachment.container152 assert_equal 'testfile.txt', attachment.filename153 assert_equal 59, attachment.filesize154 assert_equal 'test', attachment.description155 assert_equal 'text/plain', attachment.content_type156 assert File.exists?(attachment.diskfile)157 assert_equal 59, File.size(attachment.diskfile)158 end159 should "add unsaved files to the object as unsaved attachments" do160 # Max size of 0 to force Attachment creation failures161 with_settings(:attachment_max_size => 0) do162 @project = Project.find(1)163 response = Attachment.attach_files(@project, {164 '1' => {'file' => mock_file, 'description' => 'test'},165 '2' => {'file' => mock_file, 'description' => 'test'}166 })167 assert response[:unsaved].present?168 assert_equal 2, response[:unsaved].length169 assert response[:unsaved].first.new_record?...
write.rb
Source:write.rb
...9 files.each do |file|10 if File.exists?(file)11 File.open(file, "a") {}12 else13 File.write(file, opts[:content])14 end15 end16 end17 def write(path, data)18 File.open(path, "w+") do |file|19 file.write(data) if data20 end21 path22 end23end24File.extend File::Write25module File::Write::Etest26 TESTFILE = "#{__FILE__}.test"27 28 def setup29 File.unlink TESTFILE if File.exist?(TESTFILE)30 end31 def teardown32 File.unlink TESTFILE if File.exist?(TESTFILE)33 end34 35 def test_touches36 assert !File.exist?(TESTFILE)37 File.touch TESTFILE38 assert File.exist?(TESTFILE)39 File.touch TESTFILE40 assert File.exist?(TESTFILE)41 File.unlink TESTFILE42 assert !File.exist?(TESTFILE)43 end44 45 def test_touch_w_content46 assert !File.exist?(TESTFILE)47 File.touch TESTFILE, :content => "TEST CONTENT"48 assert_equal "TEST CONTENT", File.read(TESTFILE)49 File.touch TESTFILE, :content => "TEST CONTENT2"50 assert_equal "TEST CONTENT", File.read(TESTFILE)51 File.unlink TESTFILE52 assert !File.exist?(TESTFILE)53 end54 def test_writes55 assert !File.exist?(TESTFILE)56 File.write TESTFILE, "blabber"57 assert_equal("blabber", File.read(TESTFILE))58 File.write TESTFILE, "bla"59 assert_equal("bla", File.read(TESTFILE))60 File.write TESTFILE, ""61 assert_equal("", File.read(TESTFILE))62 File.unlink TESTFILE63 assert !File.exist?(TESTFILE)...
content
Using AI Code Generation
1puts TestFile.content("test_file.rb")2puts TestFile.content("test_file.rb")3puts TestFile.content("test_file.rb")4puts TestFile.content("test_file.rb")5puts TestFile.content("test_file.rb")6puts TestFile.content("test_file.rb")7puts TestFile.content("test_file.rb")8puts TestFile.content("test_file.rb")9puts TestFile.content("test_file.rb")10puts TestFile.content("test_file.rb")
content
Using AI Code Generation
1puts TestFile.content("test_file.rb")2puts TestFile.content("test_file.rb")3puts TestFile.content("test_file.rb")4puts TestFile.content("test_file.rb")5puts TestFile.content("test_file.rb")6puts TestFile.content("test_file.rb")7puts TestFile.content("test_file.rb")8puts TestFile.content("test_file.rb")9puts TestFile.content("test_file.rb")10puts TestFile.content("test_file.rb")
content
Using AI Code Generation
1test_file = TestFile.new('test.txt')2 def initialize(file_path)3 File.read(@file_path)4 test_file = TestFile.new('test.txt')
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!!