From e62d356dd23a2029a675b3e9462b2c005ee1060a Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Tue, 13 Oct 2015 17:46:06 -0400 Subject: [PATCH 01/14] add `filename` to Torrent, Torrent specs it'll contain the path a torrent was downloaded to. also add a spec for Yamazaki::Torrent --- lib/yamazaki/torrent.rb | 5 +++-- specs/torrent_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 specs/torrent_spec.rb diff --git a/lib/yamazaki/torrent.rb b/lib/yamazaki/torrent.rb index ab0c58f..c93034a 100644 --- a/lib/yamazaki/torrent.rb +++ b/lib/yamazaki/torrent.rb @@ -15,13 +15,14 @@ module Yamazaki class Torrent attr_reader :title, :description, :pub_date, :link, :index - - def initialize(title, description, pub_date, link, index = 0) + attr_accessor :filename + def initialize(title, description, pub_date, link, index = 0, filename="") @title = title @description = description @pub_date = pub_date @link = link @index = index + @filename = filename end def to_s diff --git a/specs/torrent_spec.rb b/specs/torrent_spec.rb new file mode 100644 index 0000000..d765b5d --- /dev/null +++ b/specs/torrent_spec.rb @@ -0,0 +1,27 @@ +require_relative 'spec_helper' + +describe Yamazaki::Torrent do + #include Yamazaki::Torrent + + describe '#creation' do + context 'torrent creation with standard arguments' do + it 'is created correctly' do + #(title, description, pub_date, link, index = 0, filename="") + expect { + Yamazaki::Torrent.new("Roxas the Animu", "10/10 would watch again", + "2015/10/13 21:42:33", "https://just-believe.in") + }.not_to raise_error() + end + end + + context 'setting filename' do + it 'does not explode' do + expect { + thor = Yamazaki::Torrent.new("Roxas the Animu", "10/10 would watch again", + "2015/10/13 21:42:33", "https://just-believe.in") + thor.filename = "/home/roxas/.watch/Roxas_s1_ep1_[ABAB65][CR].torrent" + }.not_to raise_error() + end + end + end +end From 4f2d6bd74e036c919e97783b4a897af6527a1cfe Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Fri, 16 Oct 2015 18:54:32 -0400 Subject: [PATCH 02/14] Add tests, Database#size Yamazaki now has more tests, covering the usual behaviour of the library. A travis.yml has been added too. `Database#size` returns the number of saved items inside the database. --- .travis.yml | 6 +++ lib/yamazaki/database.rb | 4 ++ lib/yamazaki/version.rb | 2 +- specs/database_spec.rb | 89 ++++++++++++++++++++++++++++++++++++++++ specs/spec_helper.rb | 22 ++++++++++ specs/torrent_spec.rb | 49 ++++++++++++++++------ 6 files changed, 159 insertions(+), 13 deletions(-) create mode 100644 .travis.yml create mode 100644 specs/database_spec.rb diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6d06b56 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +nguage: ruby +rvm: + - 1.9.3 + - 2.0.0 + - 2.1.2 + - 2.2.1 diff --git a/lib/yamazaki/database.rb b/lib/yamazaki/database.rb index 97efbad..7c96cac 100644 --- a/lib/yamazaki/database.rb +++ b/lib/yamazaki/database.rb @@ -31,6 +31,10 @@ def include?(filename) @db.count { |t| t[:filename] == filename } > 0 end + def size + @db.size + end + private def save diff --git a/lib/yamazaki/version.rb b/lib/yamazaki/version.rb index f4f6cb5..3c8d62c 100644 --- a/lib/yamazaki/version.rb +++ b/lib/yamazaki/version.rb @@ -13,5 +13,5 @@ ## module Yamazaki - VERSION = '0.3.6' + VERSION = '0.3.5' end diff --git a/specs/database_spec.rb b/specs/database_spec.rb new file mode 100644 index 0000000..e9ab8cb --- /dev/null +++ b/specs/database_spec.rb @@ -0,0 +1,89 @@ +require_relative 'spec_helper' + +describe Yamazaki::Database do + + describe '#new' do + context 'when track file does not exist' do + it 'is created correctly' do + File.rm "/tmp/yam.db" if File.exists? "/tmp/yam.db" + db = Yamazaki::Database.new("/tmp/yam.db", false) + expect(db.size).to eq(0) + end + end + + context 'when track file already exists' do + it 'is loaded correctly if the given `track_file` contains something' do + track_file = prepare_db([make_torrent("Nisekoi 01x11", {filename:"/home/roxas/nisenever_s1ep11.torrent"})]) + db = Yamazaki::Database.new(track_file) + expect(db.size).not_to eq(0) + expect(db.include? "/home/roxas/nisenever_s1ep11.torrent").to be_truthy() + end + + it 'is loaded correctly if `track_file` is empty' do + track_file = prepare_db([]) + db = Yamazaki::Database.new(track_file) + expect(db.size).to eq(0) + end + end + end + + describe '#<<' do + context 'insertion behaviour' do + it 'inserts a new filename into an empty database' do + track_file = prepare_db([]) + db = Yamazaki::Database.new(track_file) + db << "/home/pls/sword_art_online.torrent" + expect(db.size).to eq(1) + expect(db.include? "/home/pls/sword_art_online.torrent").to be_truthy() + end + + it 'does not insert the same filename if already present' do + track_file = prepare_db([]) + db = Yamazaki::Database.new(track_file) + db << "/home/pls/sword_art_online.torrent" + db << "/home/pls/sword_art_online.torrent" + expect(db.size).to be(1) + end + end + + context 'when `save_on_push` is enabled (default)' do + it 'database saves filenames on disk' do + #if `save_on_push` is disabled, we should not find + #any of the previously inserted filenames + #if we re-open the track file. + track_file = prepare_db([]) + db = Yamazaki::Database.new(track_file) + db << "/sora/no/woto.torrent" + other_db = Yamazaki::Database.new(track_file, true) + expect(db.size).to eq(1) + expect(other_db.size).to eq(1) + end + end + + + context 'when `save_on_push` is disabled' do + it 'database does not save filenames on disk' do + #if `save_on_push` is disabled, we should not find + #any of the previously inserted filenames + #if we re-open the track file. + track_file = prepare_db([]) + db = Yamazaki::Database.new(track_file, false) + db << "/sora/no/woto.torrent" + other_db = Yamazaki::Database.new(track_file, false) + expect(db.size).to eq(1) + expect(other_db.size).to eq(0) + end + end + end + + describe '#include?' do + context 'when looking into an empty database' do + it 'is not found inside the database' do + track_file = prepare_db([]) + db = Yamazaki::Database.new(track_file) + expect(db.include? "/home/winter/human_reignition_project_PV.torrent").to be_falsey() + end + end + end + +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 989aa25..8dba5ca 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1 +1,23 @@ require 'yamazaki' +require 'date' +require 'base64' +#require 'random' + +def make_torrent(episode, options={}) + description = options[:description] || "No description" + pub_date = options[:pub_date] || DateTime.now.to_s + link = options[:link] || "http:/u.rl/this.torrent" + index = options[:index] || 0 + filename = options[:filename] || "/no/filename" + Yamazaki::Torrent.new(episode, description, pub_date, link, index, filename) +end + +def prepare_db(torrents) + db_file_path = "/tmp/" + Base64.strict_encode64((Random.rand*255).to_s) + ".db" + File.rm db_file_path if File.exists? db_file_path + if torrents.size > 0 + db = Yamazaki::Database.new(db_file_path, save_on_push=true) + torrents.each { |torrent| db << torrent.filename } + end + db_file_path +end diff --git a/specs/torrent_spec.rb b/specs/torrent_spec.rb index d765b5d..fa91d19 100644 --- a/specs/torrent_spec.rb +++ b/specs/torrent_spec.rb @@ -3,24 +3,49 @@ describe Yamazaki::Torrent do #include Yamazaki::Torrent - describe '#creation' do - context 'torrent creation with standard arguments' do + describe '#new' do + context 'with default arguments' do it 'is created correctly' do #(title, description, pub_date, link, index = 0, filename="") - expect { - Yamazaki::Torrent.new("Roxas the Animu", "10/10 would watch again", + torrent = Yamazaki::Torrent.new("Kyoukaisen", "10/10 would watch again", "2015/10/13 21:42:33", "https://just-believe.in") - }.not_to raise_error() + + expect(torrent.title).to eq("Kyoukaisen") + expect(torrent.pub_date).to eq("2015/10/13 21:42:33") + #test defaults + expect(torrent.index).to eq(0) + expect(torrent.filename).to eq("") + end + + context 'default arguments are given a value' + it 'sets filename correctly at torrent creation' do + torrent = Yamazaki::Torrent.new("The Rolling Girls", ">Kyoto arc", "2015/10/13 21:42:33", + "http://u.rl", 15, "/home/roxas/trg.torrent") + expect(torrent.filename).to eq("/home/roxas/trg.torrent") end + + it 'creates a torrent with a positive or zero index' do + torrent = Yamazaki::Torrent.new("aaa", "bbb", "2015/10/13 21:42:33", "http://u.rl", 3) + expect(torrent.index).to eq(3) + end + +# it 'raises ArgumentError with a negative index' do +# expect { +# Yamazaki::Torrent.new("ccc", "ddd", "2016/01/18 18:43:29", "https://u.rl", -5) +# }.to raise_error(ArgumentError) +# end end - context 'setting filename' do - it 'does not explode' do - expect { - thor = Yamazaki::Torrent.new("Roxas the Animu", "10/10 would watch again", - "2015/10/13 21:42:33", "https://just-believe.in") - thor.filename = "/home/roxas/.watch/Roxas_s1_ep1_[ABAB65][CR].torrent" - }.not_to raise_error() + describe '#filename' + context 'filename setting in a torrent' do + it 'is set correctly after torrent creation' do + thor = Yamazaki::Torrent.new("Roxas the Animu", "10/10 would watch again", + "2015/10/13 21:42:33", "https://just-believe.in") + expect { + thor.filename = "/home/roxas/.watch/Roxas_s1_ep1_[ABAB65][CR].torrent" + }.not_to raise_error() + + expect(thor.filename).to eq("/home/roxas/.watch/Roxas_s1_ep1_[ABAB65][CR].torrent") end end end From 78054c64e6eb85beb84412d2ff6db98db28a1f53 Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Fri, 16 Oct 2015 19:02:14 -0400 Subject: [PATCH 03/14] ops --- lib/yamazaki/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/yamazaki/version.rb b/lib/yamazaki/version.rb index 3c8d62c..f4f6cb5 100644 --- a/lib/yamazaki/version.rb +++ b/lib/yamazaki/version.rb @@ -13,5 +13,5 @@ ## module Yamazaki - VERSION = '0.3.5' + VERSION = '0.3.6' end From f107ec229c6bc31cd725bbeae6e6ffa191697482 Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Fri, 16 Oct 2015 19:13:48 -0400 Subject: [PATCH 04/14] add test about negative index --- lib/yamazaki/torrent.rb | 2 ++ specs/torrent_spec.rb | 13 +++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/yamazaki/torrent.rb b/lib/yamazaki/torrent.rb index c93034a..42ad63a 100644 --- a/lib/yamazaki/torrent.rb +++ b/lib/yamazaki/torrent.rb @@ -17,6 +17,8 @@ class Torrent attr_reader :title, :description, :pub_date, :link, :index attr_accessor :filename def initialize(title, description, pub_date, link, index = 0, filename="") + raise ArgumentError.new("you cannot have a negative index!") if index < 0 + @title = title @description = description @pub_date = pub_date diff --git a/specs/torrent_spec.rb b/specs/torrent_spec.rb index fa91d19..9a49262 100644 --- a/specs/torrent_spec.rb +++ b/specs/torrent_spec.rb @@ -16,8 +16,9 @@ expect(torrent.index).to eq(0) expect(torrent.filename).to eq("") end + end - context 'default arguments are given a value' + context 'default arguments are given a value' do it 'sets filename correctly at torrent creation' do torrent = Yamazaki::Torrent.new("The Rolling Girls", ">Kyoto arc", "2015/10/13 21:42:33", "http://u.rl", 15, "/home/roxas/trg.torrent") @@ -29,11 +30,11 @@ expect(torrent.index).to eq(3) end -# it 'raises ArgumentError with a negative index' do -# expect { -# Yamazaki::Torrent.new("ccc", "ddd", "2016/01/18 18:43:29", "https://u.rl", -5) -# }.to raise_error(ArgumentError) -# end + it 'raises ArgumentError with a negative index' do + expect { + Yamazaki::Torrent.new("ccc", "ddd", "2016/01/18 18:43:29", "https://u.rl", -5) + }.to raise_error(ArgumentError) + end end describe '#filename' From f12b6b71400e3fa3f164ea613cab99abebf4028e Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Fri, 16 Oct 2015 19:32:22 -0400 Subject: [PATCH 05/14] skip test about unique-filename-in-database --- specs/database_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/specs/database_spec.rb b/specs/database_spec.rb index e9ab8cb..9af5779 100644 --- a/specs/database_spec.rb +++ b/specs/database_spec.rb @@ -37,7 +37,9 @@ expect(db.include? "/home/pls/sword_art_online.torrent").to be_truthy() end + it 'does not insert the same filename if already present' do + skip 'skip: if needed, a file can be re-downloaded, and we want it to be possible' track_file = prepare_db([]) db = Yamazaki::Database.new(track_file) db << "/home/pls/sword_art_online.torrent" From 7858f6554b4707651e67078b4995d8b0d22e84d2 Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Sat, 17 Oct 2015 04:35:56 -0400 Subject: [PATCH 06/14] refactor spec_helper a bit --- specs/spec_helper.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 8dba5ca..7295246 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -12,9 +12,14 @@ def make_torrent(episode, options={}) Yamazaki::Torrent.new(episode, description, pub_date, link, index, filename) end -def prepare_db(torrents) +def new_dbfile_path db_file_path = "/tmp/" + Base64.strict_encode64((Random.rand*255).to_s) + ".db" File.rm db_file_path if File.exists? db_file_path + db_file_path +end + +def prepare_db(torrents) + db_file_path = new_dbfile_path() if torrents.size > 0 db = Yamazaki::Database.new(db_file_path, save_on_push=true) torrents.each { |torrent| db << torrent.filename } From 2daef2504b5eac6fc1932cecbbde34adab1684e5 Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Sat, 17 Oct 2015 07:30:04 -0400 Subject: [PATCH 07/14] Fasten Database#include? Switching to Array#index given a 2x performance boost. --- lib/yamazaki/database.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/yamazaki/database.rb b/lib/yamazaki/database.rb index 7c96cac..61b3008 100644 --- a/lib/yamazaki/database.rb +++ b/lib/yamazaki/database.rb @@ -28,7 +28,7 @@ def <<(filename) end def include?(filename) - @db.count { |t| t[:filename] == filename } > 0 + @db.index { |t| t[:filename] == filename } != nil end def size From 5576b0178920639782f83a2a3eb6a4698290d3d8 Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Sat, 17 Oct 2015 09:38:24 -0400 Subject: [PATCH 08/14] move to Array#bsearch, getting too much faster O: --- lib/yamazaki/database.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/yamazaki/database.rb b/lib/yamazaki/database.rb index 61b3008..969919b 100644 --- a/lib/yamazaki/database.rb +++ b/lib/yamazaki/database.rb @@ -20,15 +20,21 @@ def initialize(track_file, save_on_push = true) @db = Oj.load(File.read(track_file)) if File.exists?(track_file) @db ||= [] + @db.sort_by! { |t| t[:filename] } end def <<(filename) @db << { filename: filename, added_at: Time.now } + @db.sort_by! { |t| t[:filename] } save if @save_on_push end def include?(filename) - @db.index { |t| t[:filename] == filename } != nil + begin + @db.bsearch { |t| t[:filename] >= filename }[:filename] == filename + rescue + false + end end def size From ccb67597e69dd98f828cb6f17e15590fcf9dedda Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Sat, 17 Oct 2015 10:16:29 -0400 Subject: [PATCH 09/14] ruby1.9.3 does not have Array#bsearch --- .travis.yml | 1 - specs/spec_helper.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6d06b56..fb66359 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ nguage: ruby rvm: - - 1.9.3 - 2.0.0 - 2.1.2 - 2.2.1 diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 7295246..a8033ba 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -21,7 +21,7 @@ def new_dbfile_path def prepare_db(torrents) db_file_path = new_dbfile_path() if torrents.size > 0 - db = Yamazaki::Database.new(db_file_path, save_on_push=true) + db = Yamazaki::Database.new(db_file_path, :save_on_push=>true) torrents.each { |torrent| db << torrent.filename } end db_file_path From d037521f7cfd454898d66ddd0da8595ab4059469 Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Sat, 17 Oct 2015 10:16:29 -0400 Subject: [PATCH 10/14] Remove ruby 1.9.3 from travis ruby1.9.3 is removed because `Array#bsearch` is not supported from <2.x ruby releases --- .travis.yml | 1 - specs/spec_helper.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6d06b56..fb66359 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ nguage: ruby rvm: - - 1.9.3 - 2.0.0 - 2.1.2 - 2.2.1 diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 7295246..a8033ba 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -21,7 +21,7 @@ def new_dbfile_path def prepare_db(torrents) db_file_path = new_dbfile_path() if torrents.size > 0 - db = Yamazaki::Database.new(db_file_path, save_on_push=true) + db = Yamazaki::Database.new(db_file_path, :save_on_push=>true) torrents.each { |torrent| db << torrent.filename } end db_file_path From 591125c45b15e8d9f786b7cc3b5f4e284e92b08f Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Sat, 17 Oct 2015 11:38:22 -0400 Subject: [PATCH 11/14] Fix Roxas suggestions --- .travis.yml | 2 +- lib/yamazaki/database.rb | 4 ++++ lib/yamazaki/torrent.rb | 5 +++-- specs/database_spec.rb | 12 ++++++++---- specs/spec_helper.rb | 3 +-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index fb66359..37fa126 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -nguage: ruby +language: ruby rvm: - 2.0.0 - 2.1.2 diff --git a/lib/yamazaki/database.rb b/lib/yamazaki/database.rb index 969919b..712ad69 100644 --- a/lib/yamazaki/database.rb +++ b/lib/yamazaki/database.rb @@ -41,6 +41,10 @@ def size @db.size end + def empty? + @db.size == 0 + end + private def save diff --git a/lib/yamazaki/torrent.rb b/lib/yamazaki/torrent.rb index 42ad63a..b925772 100644 --- a/lib/yamazaki/torrent.rb +++ b/lib/yamazaki/torrent.rb @@ -16,15 +16,16 @@ module Yamazaki class Torrent attr_reader :title, :description, :pub_date, :link, :index attr_accessor :filename + def initialize(title, description, pub_date, link, index = 0, filename="") - raise ArgumentError.new("you cannot have a negative index!") if index < 0 + raise ArgumentError.new("you cannot have a negative index!") if index < 0 @title = title @description = description @pub_date = pub_date @link = link @index = index - @filename = filename + @filename = filename end def to_s diff --git a/specs/database_spec.rb b/specs/database_spec.rb index 9af5779..8725cd0 100644 --- a/specs/database_spec.rb +++ b/specs/database_spec.rb @@ -5,9 +5,9 @@ describe '#new' do context 'when track file does not exist' do it 'is created correctly' do - File.rm "/tmp/yam.db" if File.exists? "/tmp/yam.db" + File.rm "./yam.db" if File.exists? "./yam.db" db = Yamazaki::Database.new("/tmp/yam.db", false) - expect(db.size).to eq(0) + expect(db).to be_empty end end @@ -15,7 +15,7 @@ it 'is loaded correctly if the given `track_file` contains something' do track_file = prepare_db([make_torrent("Nisekoi 01x11", {filename:"/home/roxas/nisenever_s1ep11.torrent"})]) db = Yamazaki::Database.new(track_file) - expect(db.size).not_to eq(0) + expect(db).not_to be_empty expect(db.include? "/home/roxas/nisenever_s1ep11.torrent").to be_truthy() end @@ -33,7 +33,7 @@ track_file = prepare_db([]) db = Yamazaki::Database.new(track_file) db << "/home/pls/sword_art_online.torrent" - expect(db.size).to eq(1) + expect(db).not_to be_empty expect(db.include? "/home/pls/sword_art_online.torrent").to be_truthy() end @@ -44,6 +44,8 @@ db = Yamazaki::Database.new(track_file) db << "/home/pls/sword_art_online.torrent" db << "/home/pls/sword_art_online.torrent" + #if we cannot have several duplicates, the size + #should always be 1 expect(db.size).to be(1) end end @@ -57,6 +59,7 @@ db = Yamazaki::Database.new(track_file) db << "/sora/no/woto.torrent" other_db = Yamazaki::Database.new(track_file, true) + #we want to explicitely check the size here expect(db.size).to eq(1) expect(other_db.size).to eq(1) end @@ -72,6 +75,7 @@ db = Yamazaki::Database.new(track_file, false) db << "/sora/no/woto.torrent" other_db = Yamazaki::Database.new(track_file, false) + #check the size, not its emptiness expect(db.size).to eq(1) expect(other_db.size).to eq(0) end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index a8033ba..93d07c2 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,7 +1,6 @@ require 'yamazaki' require 'date' require 'base64' -#require 'random' def make_torrent(episode, options={}) description = options[:description] || "No description" @@ -20,7 +19,7 @@ def new_dbfile_path def prepare_db(torrents) db_file_path = new_dbfile_path() - if torrents.size > 0 + if torrents.any? db = Yamazaki::Database.new(db_file_path, :save_on_push=>true) torrents.each { |torrent| db << torrent.filename } end From 0c6ae12d200d669b1dd1b11e0fa45a7c12637b01 Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Sat, 17 Oct 2015 12:03:42 -0400 Subject: [PATCH 12/14] Fix database#<<, now filenames are unique Filenames should be considered unique in the database, so duplicates are not allowed. If duplicates are present in the source file, they won't be deleted after the JSON load (and it would be very, very time consuming to do this, unless we switch to hashtables). --- lib/yamazaki/database.rb | 8 +++++--- specs/database_spec.rb | 1 - 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/yamazaki/database.rb b/lib/yamazaki/database.rb index 712ad69..3114b66 100644 --- a/lib/yamazaki/database.rb +++ b/lib/yamazaki/database.rb @@ -24,9 +24,11 @@ def initialize(track_file, save_on_push = true) end def <<(filename) - @db << { filename: filename, added_at: Time.now } - @db.sort_by! { |t| t[:filename] } - save if @save_on_push + unless self.include? filename + @db << { filename: filename, added_at: Time.now } + @db.sort_by! { |t| t[:filename] } + save if @save_on_push + end end def include?(filename) diff --git a/specs/database_spec.rb b/specs/database_spec.rb index 8725cd0..aa4d27b 100644 --- a/specs/database_spec.rb +++ b/specs/database_spec.rb @@ -39,7 +39,6 @@ it 'does not insert the same filename if already present' do - skip 'skip: if needed, a file can be re-downloaded, and we want it to be possible' track_file = prepare_db([]) db = Yamazaki::Database.new(track_file) db << "/home/pls/sword_art_online.torrent" From 580356517f50ba8ae900d80f9e5a81c80c585ce1 Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Sat, 17 Oct 2015 12:27:05 -0400 Subject: [PATCH 13/14] Improve fake database creation and removal Fake database files were created under `/tmp`, and it may break compatibility with non-posix platform. Moreover they were not deleted, leaving a lot of random files around: now they are. --- .gitignore | 1 + specs/database_spec.rb | 6 ++++++ specs/spec_helper.rb | 6 +++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c111b33..f60ba3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.gem +*.db diff --git a/specs/database_spec.rb b/specs/database_spec.rb index aa4d27b..2b80724 100644 --- a/specs/database_spec.rb +++ b/specs/database_spec.rb @@ -3,6 +3,8 @@ describe Yamazaki::Database do describe '#new' do + after { remove_databases } + context 'when track file does not exist' do it 'is created correctly' do File.rm "./yam.db" if File.exists? "./yam.db" @@ -28,6 +30,8 @@ end describe '#<<' do + after { remove_databases } + context 'insertion behaviour' do it 'inserts a new filename into an empty database' do track_file = prepare_db([]) @@ -82,6 +86,8 @@ end describe '#include?' do + after { remove_databases } + context 'when looking into an empty database' do it 'is not found inside the database' do track_file = prepare_db([]) diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 93d07c2..e742ef1 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -12,7 +12,7 @@ def make_torrent(episode, options={}) end def new_dbfile_path - db_file_path = "/tmp/" + Base64.strict_encode64((Random.rand*255).to_s) + ".db" + db_file_path = "./dbfile_" + Base64.strict_encode64((Random.rand*255).to_s) + ".db" File.rm db_file_path if File.exists? db_file_path db_file_path end @@ -25,3 +25,7 @@ def prepare_db(torrents) end db_file_path end + +def remove_databases + Dir.glob("./*.db").each { |f| FileUtils.rm f } +end From 72ec3e558c38672d50b18bc45b0ec7a5f6b3d834 Mon Sep 17 00:00:00 2001 From: alfateam123 Date: Sat, 17 Oct 2015 12:27:05 -0400 Subject: [PATCH 14/14] Improve fake database creation and removal Fake database files were created under `/tmp`, and it may break compatibility with non-posix platform. Moreover they were not deleted, leaving a lot of random files around: now they are. --- .gitignore | 1 + specs/database_spec.rb | 6 ++++++ specs/spec_helper.rb | 7 ++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c111b33..f60ba3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.gem +*.db diff --git a/specs/database_spec.rb b/specs/database_spec.rb index aa4d27b..2b80724 100644 --- a/specs/database_spec.rb +++ b/specs/database_spec.rb @@ -3,6 +3,8 @@ describe Yamazaki::Database do describe '#new' do + after { remove_databases } + context 'when track file does not exist' do it 'is created correctly' do File.rm "./yam.db" if File.exists? "./yam.db" @@ -28,6 +30,8 @@ end describe '#<<' do + after { remove_databases } + context 'insertion behaviour' do it 'inserts a new filename into an empty database' do track_file = prepare_db([]) @@ -82,6 +86,8 @@ end describe '#include?' do + after { remove_databases } + context 'when looking into an empty database' do it 'is not found inside the database' do track_file = prepare_db([]) diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 93d07c2..1a93a72 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,6 +1,7 @@ require 'yamazaki' require 'date' require 'base64' +require 'fileutils' def make_torrent(episode, options={}) description = options[:description] || "No description" @@ -12,7 +13,7 @@ def make_torrent(episode, options={}) end def new_dbfile_path - db_file_path = "/tmp/" + Base64.strict_encode64((Random.rand*255).to_s) + ".db" + db_file_path = "./dbfile_" + Base64.strict_encode64((Random.rand*255).to_s) + ".db" File.rm db_file_path if File.exists? db_file_path db_file_path end @@ -25,3 +26,7 @@ def prepare_db(torrents) end db_file_path end + +def remove_databases + Dir.glob("./*.db").each { |f| FileUtils.rm f } +end