From 3f20d48e17a03f762ca0baa9439128c7a3621958 Mon Sep 17 00:00:00 2001 From: Sawood Alam Date: Fri, 6 Jan 2017 12:34:41 -0500 Subject: [PATCH 1/2] Disabled Redis disc persistence and refactored integration test, fixes #95 --- test/backends/backend_redis_test.rb | 1 + test/bayes/bayesian_integration_test.rb | 19 +++++++++++++++---- test/bayes/bayesian_redis_test.rb | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/test/backends/backend_redis_test.rb b/test/backends/backend_redis_test.rb index 2448be2..3fe050f 100644 --- a/test/backends/backend_redis_test.rb +++ b/test/backends/backend_redis_test.rb @@ -9,6 +9,7 @@ class BackendRedisTest < Minitest::Test def setup begin @backend = ClassifierReborn::BayesRedisBackend.new + @backend.instance_variable_get(:@redis).config(:set, "save", "") rescue Redis::CannotConnectError => e skip(e) end diff --git a/test/bayes/bayesian_integration_test.rb b/test/bayes/bayesian_integration_test.rb index d426cc9..aca4b2a 100644 --- a/test/bayes/bayesian_integration_test.rb +++ b/test/bayes/bayesian_integration_test.rb @@ -3,17 +3,28 @@ require File.dirname(__FILE__) + '/../test_helper' class BayesianIntegrationTest < Minitest::Test + TRAINING_SIZE = 4000 + TESTING_SIZE = 1000 + def setup begin @memory_classifier = ClassifierReborn::Bayes.new 'Ham', 'Spam' @redis_backend = ClassifierReborn::BayesRedisBackend.new + @redis_backend.instance_variable_get(:@redis).config(:set, "save", "") @redis_classifier = ClassifierReborn::Bayes.new 'Ham', 'Spam', backend: @redis_backend rescue Redis::CannotConnectError => e skip(e) end sms_spam_collection = File.expand_path(File.dirname(__FILE__) + '/../data/corpus/SMSSpamCollection.tsv') - @training_set = File.read(sms_spam_collection).force_encoding("utf-8").split("\n") - @testing_set = @training_set.pop(1000) + File.open(sms_spam_collection) do |f| + begin + @training_set = TRAINING_SIZE.times.map { f.readline.force_encoding("utf-8") } + @testing_set = TESTING_SIZE.times.map { f.readline.force_encoding("utf-8") } + rescue EOFError => e + puts "Not enough records in the dataset" + skip(e) + end + end end def teardown @@ -24,8 +35,8 @@ def test_equality_of_backends train_model @memory_classifier train_model @redis_classifier assert_equal classification_scores(@memory_classifier).hash, classification_scores(@redis_classifier).hash - untrain_model @memory_classifier, 2000 - untrain_model @redis_classifier, 2000 + untrain_model @memory_classifier, TRAINING_SIZE/2 + untrain_model @redis_classifier, TRAINING_SIZE/2 assert_equal classification_scores(@memory_classifier).hash, classification_scores(@redis_classifier).hash end diff --git a/test/bayes/bayesian_redis_test.rb b/test/bayes/bayesian_redis_test.rb index 5bb7139..d9dd30d 100644 --- a/test/bayes/bayesian_redis_test.rb +++ b/test/bayes/bayesian_redis_test.rb @@ -9,6 +9,7 @@ class BayesianRedisTest < Minitest::Test def setup begin @redis_backend = ClassifierReborn::BayesRedisBackend.new + @redis_backend.instance_variable_get(:@redis).config(:set, "save", "") @alternate_redis_backend = ClassifierReborn::BayesRedisBackend.new(db: 1) @classifier = ClassifierReborn::Bayes.new 'Interesting', 'Uninteresting', backend: @redis_backend rescue Redis::CannotConnectError => e From 1dcad6dde7d930a6608afe80d3f295b9ed63bab6 Mon Sep 17 00:00:00 2001 From: Sawood Alam Date: Sat, 7 Jan 2017 17:25:23 -0500 Subject: [PATCH 2/2] Changed test class and file name as per #84 --- test/bayes/{bayesian_test.rb => bayesian_memory_test.rb} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test/bayes/{bayesian_test.rb => bayesian_memory_test.rb} (92%) diff --git a/test/bayes/bayesian_test.rb b/test/bayes/bayesian_memory_test.rb similarity index 92% rename from test/bayes/bayesian_test.rb rename to test/bayes/bayesian_memory_test.rb index 7b354a7..a50a3d3 100755 --- a/test/bayes/bayesian_test.rb +++ b/test/bayes/bayesian_memory_test.rb @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../test_helper' require_relative './bayesian_common_tests' -class BayesianTest < Minitest::Test +class BayesianMemoryTest < Minitest::Test include BayesianCommonTests def setup