Skip to content

Commit

Permalink
Check existance by name when file_id is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
hakobera committed Jul 20, 2016
1 parent 6602d9c commit bee923a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 17 deletions.
43 changes: 37 additions & 6 deletions lib/tumugi/plugin/target/google_drive_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class GoogleDriveFileTarget < Tumugi::Plugin::FileSystemTarget

attr_reader :file_id, :name, :parents

def initialize(file_id: nil, name: nil, parents: nil, fs: nil)
def initialize(file_id: nil, name:, parents: nil, fs: nil)
@fs = fs unless fs.nil?
@file_id = file_id || self.fs.generate_file_id
@file_id = file_id
@name = name
@parents = parents
super(@file_id)
super(file_id)
end

def fs
Expand All @@ -25,25 +25,56 @@ def fs

def open(mode="r", &block)
if mode.include? 'r'
if file_id.nil?
file = find_by_name(name)
@file_id = file.id unless file.nil?
end
fs.download(file_id, mode: mode, &block)
elsif mode.include? 'w'
file = Tumugi::Plugin::GoogleDrive::AtomicFile.new(name, fs, file_id: @file_id, parents: @parents)
file = Tumugi::Plugin::GoogleDrive::AtomicFile.new(name, fs, file_id: file_id, parents: @parents)
file.open(&block)
@file_id = file.id
else
raise Tumugi::TumugiError.new('Invalid mode: #{mode}')
raise Tumugi::TumugiError.new("Invalid mode: #{mode}")
end
end

def exist?
fs.exist?(file_id)
if file_id
fs.exist?(file_id)
else
!!find_by_name(name)
end
end

def to_s
s = "file_id: #{file_id}, name: #{name}"
s += ", parents: #{parents}" if parents
s
end

private

def find_by_name(n)
query = "name='#{n}'"
ps = parents
if parents.is_a?(String)
ps = [parents]
end
if parents
query += " and ("
query += "#{ps.map{|p| "'#{p}' in parents"}.join(" or ")}"
query += ")"
end
files = fs.list_files(query: query, page_size: 2).files
if files.size == 0
nil
elsif files.size == 1
files.first
else
raise Tumugi::TumugiError.new("Multiple files find for query: '#{query}'")
end
end
end
end
end
53 changes: 42 additions & 11 deletions test/plugin/target/google_drive_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

class Tumugi::Plugin::GoogleDriveFileTargetTest < Test::Unit::TestCase
setup do
@target = Tumugi::Plugin::GoogleDriveFileTarget.new(name: 'file1.txt')
@target = Tumugi::Plugin::GoogleDriveFileTarget.new(name: SecureRandom.uuid)
end

sub_test_case "initialize" do
test "with name" do
target = Tumugi::Plugin::GoogleDriveFileTarget.new(name: 'test')
assert_equal('test', target.name)
assert_nil(target.parents)
assert_match(/^[0-9a-zA-Z]{28}$/, target.file_id)
assert_nil(target.file_id)
assert_equal(target.path, target.file_id)
end

test "with name and parents" do
target = Tumugi::Plugin::GoogleDriveFileTarget.new(name: 'test', parents: 'parent')
assert_equal('test', target.name)
assert_equal('parent', target.parents)
assert_match(/^[0-9a-zA-Z]{28}$/, target.file_id)
assert_nil(target.file_id)
assert_equal(target.path, target.file_id)
end

Expand All @@ -32,11 +32,42 @@ class Tumugi::Plugin::GoogleDriveFileTargetTest < Test::Unit::TestCase
end
end

test "exist?" do
file1 = @target.fs.put_string('test', 'file1.txt')
readable_target = Tumugi::Plugin::GoogleDriveFileTarget.new(file_id: file1.id)
assert_true(readable_target.exist?)
assert_false(@target.exist?)
sub_test_case "exist?" do
test "match_by file_id" do
file1 = @target.fs.put_string('test', 'file1.txt')
readable_target = Tumugi::Plugin::GoogleDriveFileTarget.new(file_id: file1.id, name: 'test')
assert_true(readable_target.exist?)
assert_false(@target.exist?)
end

test "match_by name" do
name = SecureRandom.uuid
file1 = @target.fs.put_string('test', name)
target1 = Tumugi::Plugin::GoogleDriveFileTarget.new(name: file1.name)
target2 = Tumugi::Plugin::GoogleDriveFileTarget.new(name: SecureRandom.uuid)
assert_true(target1.exist?)
assert_false(target2.exist?)
end

test "match_by :name with parents" do
name = SecureRandom.uuid
parents = '0B62A9ARqgG8zWS1jcUQ3SkhQdzA'
file1 = @target.fs.put_string('test', name, parents: parents)
target1 = Tumugi::Plugin::GoogleDriveFileTarget.new(name: file1.name, parents: parents)
target2 = Tumugi::Plugin::GoogleDriveFileTarget.new(name: SecureRandom.uuid, parents: parents)
assert_true(target1.exist?)
assert_false(target2.exist?)
end

test 'raise error when multiple file found match by name' do
name = SecureRandom.uuid
@target.fs.put_string('test', name)
@target.fs.put_string('test', name)
target = Tumugi::Plugin::GoogleDriveFileTarget.new(name: name)
assert_raise(Tumugi::TumugiError) do
target.exist?
end
end
end

sub_test_case "open" do
Expand All @@ -50,12 +81,12 @@ class Tumugi::Plugin::GoogleDriveFileTargetTest < Test::Unit::TestCase
end

test "write and read with parents" do
@target = Tumugi::Plugin::GoogleDriveFileTarget.new(name: 'file1.txt', parents: '0B62A9ARqgG8zWS1jcUQ3SkhQdzA')
target = Tumugi::Plugin::GoogleDriveFileTarget.new(name: 'file1.txt', parents: '0B62A9ARqgG8zWS1jcUQ3SkhQdzA')

@target.open("w") do |f|
target.open("w") do |f|
f.puts("test")
end
@target.open("r") do |f|
target.open("r") do |f|
assert_equal("test\n", f.read)
end
end
Expand Down

0 comments on commit bee923a

Please sign in to comment.