test/integration/rapidshare-ext_test.rb in rapidshare-ext-0.0.3 vs test/integration/rapidshare-ext_test.rb in rapidshare-ext-0.0.4

- old
+ new

@@ -18,11 +18,11 @@ @upload_file_1_md5 = Digest::MD5.hexdigest(File.read(@upload_file_1)) @upload_file_1_size = File.size @upload_file_1 end context "Api" do - should "Upload file" do + should "upload file" do upload_assertion = ->(resp, size_local, digest_local, remote_filename) do assert_instance_of Hash, resp assert_kind_of Integer, resp[:id] assert_kind_of Integer, resp[:size] assert_equal size_local, resp[:size] @@ -60,11 +60,11 @@ response = @rs.upload @upload_file_1, :to => remote_dir, :as => remote_filename upload_assertion.call response, @upload_file_1_size, @upload_file_1_md5, remote_filename assert_true response[:already_exists?] end - should "Download file" do + should "download file" do @rs.upload @upload_file_1, :to => "/a/b/c", :as => "upload_file_1.txt" assert_not_nil @rs.file_info("/a/b/c/upload_file_1.txt") @rs.download "/a/b/c/upload_file_1.txt", :downloads_dir => @download_dir, :save_as => "file1.txt" assert_path_exist "#@download_dir/file1.txt" @@ -83,11 +83,11 @@ assert_path_exist "#@download_dir/file2.txt" assert_equal @upload_file_1_size, File.size("#@download_dir/file2.txt") assert_equal @upload_file_1_md5, Digest::MD5.hexdigest(File.read("#@download_dir/file2.txt")) end - should "Rename file" do + should "rename file" do @rs.upload @upload_file_1, :to => "/a/b/c", :as => "upload_file_1.txt" assert_not_nil @rs.file_info("/a/b/c/upload_file_1.txt") @rs.rename_file "/a/b/c/upload_file_1.txt", "file_2.txt" info = @rs.file_info "/a/b/c/file_2.txt" @@ -97,11 +97,11 @@ assert_equal info[:realfolder].to_i, @rs.folder_id("/a/b/c") assert_equal info[:size].to_i, @upload_file_1_size assert_equal info[:md5hex].downcase, @upload_file_1_md5 end - should "Move file" do + should "move file" do @rs.upload @upload_file_1, :to => "/a/b/c", :as => "upload_file_1.txt" assert_not_nil @rs.file_info("/a/b/c/upload_file_1.txt") @rs.move_file "/a/b/c/upload_file_1.txt", :to => "/a/b" assert_nil @rs.file_info("/a/b/c/upload_file_1.txt") @@ -112,25 +112,25 @@ assert_equal info[:realfolder].to_i, @rs.folder_id("/a/b") assert_equal info[:size].to_i, @upload_file_1_size assert_equal info[:md5hex].downcase, @upload_file_1_md5 end - should "Delete file" do + should "delete file" do @rs.upload @upload_file_1, :to => "/a/b/c", :as => "upload_file_1.txt" assert_not_nil @rs.file_info("/a/b/c/upload_file_1.txt") @rs.remove_file "/a/b/c/upload_file_1.txt" assert_nil @rs.file_info("/a/b/c/upload_file_1.txt") end - should "Folder id <=> path conversions" do + should "folder id <=> path conversions" do @rs.add_folder "/a/b/c" id = @rs.folder_id("/a/b/c") assert_equal "/a/b/c", @rs.folder_path(id) end - should "Create folder" do + should "create folder" do folder_id = @rs.add_folder "a/b/c" assert_kind_of Integer, folder_id assert_not_equal 0, folder_id tree = @rs.folders_hierarchy @@ -138,11 +138,11 @@ assert_equal "/a/b/c", tree[folder_id][:path] assert_equal "/a/b", tree[tree[folder_id][:parent]][:path] assert_equal "/a", tree[tree[tree[folder_id][:parent]][:parent]][:path] end - should "Move folder" do + should "move folder" do folder_id = @rs.add_folder "/a/b/c" assert_kind_of Integer, folder_id assert_not_equal 0, folder_id tree = @rs.folders_hierarchy @@ -158,11 +158,11 @@ assert_equal 3, tree.count assert_equal "/a/c", tree[folder_id][:path] assert_equal @rs.folder_id("/a"), tree[folder_id][:parent] end - should "Remove folder" do + should "remove folder" do folder_id = @rs.add_folder "a/b/c" assert_kind_of Integer, folder_id assert_not_equal 0, folder_id tree = @rs.folders_hierarchy assert_equal 3, tree.count @@ -183,11 +183,11 @@ tree = @rs.folders_hierarchy! assert_equal 0, tree.count end - should "Build folders tree" do + should "build folders tree" do # Create folders folder_a_id = @rs.add_folder "/a" assert_kind_of Integer, folder_a_id assert_not_equal 0, folder_a_id @@ -222,11 +222,11 @@ assert_equal "/c", sub_tree[folder_c_id][:path] assert_equal "c", sub_tree[folder_c_id][:name] assert_equal folder_b_id, sub_tree[folder_c_id][:parent] end - should "Erase all account data" do + should "erase all account data" do folder_id = @rs.add_folder "/a/b/c" assert_kind_of Integer, folder_id folder_ids = @rs.folders_hierarchy.keys assert_true folder_ids.count > 0 @@ -234,9 +234,127 @@ # Delete all data from account @rs.erase_all_data! folder_ids = @rs.folders_hierarchy.keys assert_equal 0, folder_ids.count + end + + should "move orphans" do + # Create folders + folder_a_id = @rs.add_folder "/a" + assert_kind_of Integer, folder_a_id + assert_not_equal 0, folder_a_id + + folder_b_id = @rs.add_folder "/a/b" + assert_kind_of Integer, folder_b_id + assert_not_equal 0, folder_b_id + + folder_c_id = @rs.add_folder "/a/b/c" + assert_kind_of Integer, folder_c_id + assert_not_equal 0, folder_c_id + + @rs.reload! + + assert_true @rs.root_folder? folder_a_id + assert_false @rs.root_folder? folder_b_id + assert_false @rs.root_folder? folder_c_id + + assert_false @rs.orphan? folder_a_id + assert_false @rs.orphan? folder_b_id + assert_false @rs.orphan? folder_c_id + + # Delete just folder "/a" to accomplish tree inconsistency + @rs.delrealfolder :realfolder => folder_a_id + @rs.reload! :validate => false + + assert_equal [folder_b_id], @rs.detect_gaps + + assert_false @rs.root_folder? folder_a_id + assert_false @rs.root_folder? folder_b_id + assert_false @rs.root_folder? folder_c_id + + assert_false @rs.orphan? folder_a_id + assert_true @rs.orphan? folder_b_id + assert_true @rs.orphan? folder_c_id + + # Move orphan folders to root folder + @rs.move_orphans :to => "/" + + hierarchy_expected = { + folder_c_id => {:name=>"c", :parent => folder_b_id, :path => "/b/c"}, + folder_b_id => {:name=>"b", :parent => 0, :path => "/b"}, + } + assert_equal hierarchy_expected, @rs.folders_hierarchy! + end + + should "delete orphans" do + # Create folders + folder_a_id = @rs.add_folder "/a" + assert_kind_of Integer, folder_a_id + assert_not_equal 0, folder_a_id + + folder_b_id = @rs.add_folder "/a/b" + assert_kind_of Integer, folder_b_id + assert_not_equal 0, folder_b_id + + folder_c_id = @rs.add_folder "/a/b/c" + assert_kind_of Integer, folder_c_id + assert_not_equal 0, folder_c_id + + @rs.reload! + + assert_true @rs.root_folder? folder_a_id + assert_false @rs.root_folder? folder_b_id + assert_false @rs.root_folder? folder_c_id + + assert_false @rs.orphan? folder_a_id + assert_false @rs.orphan? folder_b_id + assert_false @rs.orphan? folder_c_id + + # Delete just folder "/a" to accomplish tree inconsistency + @rs.delrealfolder :realfolder => folder_a_id + @rs.reload! :validate => false + + @rs.remove_orphans! + + hierarchy_expected = { + folder_c_id => {:name=>"c", :parent => folder_b_id, :path => "/b/c"}, + folder_b_id => {:name=>"b", :parent => 0, :path => "/b"}, + } + assert_equal ({}), @rs.folders_hierarchy! + end + + should "work with invalid tree" do + # Create folders + folder_a_id = @rs.add_folder "/a" + assert_kind_of Integer, folder_a_id + assert_not_equal 0, folder_a_id + + folder_b_id = @rs.add_folder "/a/b" + assert_kind_of Integer, folder_b_id + assert_not_equal 0, folder_b_id + + folder_c_id = @rs.add_folder "/a/b/c" + assert_kind_of Integer, folder_c_id + assert_not_equal 0, folder_c_id + + @rs.reload! + + assert_true @rs.root_folder? folder_a_id + assert_false @rs.root_folder? folder_b_id + assert_false @rs.root_folder? folder_c_id + + assert_false @rs.orphan? folder_a_id + assert_false @rs.orphan? folder_b_id + assert_false @rs.orphan? folder_c_id + + # Delete just folder "/a" to accomplish tree inconsistency + @rs.delrealfolder :realfolder => folder_a_id + + msg = "Directory tree consistency error. Parent folder ##{folder_a_id} for the folder \"/<undefined>/b\" [#{folder_b_id}] could not be found" + assert_raise_message(msg) do + @rs.reload! :validate => true + end end end end