suggester 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ DROP TABLE IF EXISTS `books`;
2
+
3
+ CREATE TABLE `books` (
4
+ `id` INT UNSIGNED AUTO_INCREMENT NOT NULL,
5
+ `title` VARCHAR(64),
6
+ `author` VARCHAR(125),
7
+ PRIMARY KEY (`id`)
8
+ );
9
+
10
+ INSERT INTO `books` SET `id` = 1, `title` = 'A Tale of Two Cities', `author` = 'Charles Dickens';
11
+ INSERT INTO `books` SET `id` = 2, `title` = 'Anna Karenina', `author` = 'Leo Tolstoy';
12
+ INSERT INTO `books` SET `id` = 3, `title` = 'Great Expectations', `author` = 'Charles Dickens';
13
+ INSERT INTO `books` SET `id` = 4, `title` = 'The Origin of Species', `author` = 'Charles Darwin';
14
+ INSERT INTO `books` SET `id` = 5, `title` = 'The Catcher in the Rye', `author` = 'J.D. Salinger';
15
+ INSERT INTO `books` SET `id` = 6, `title` = 'The Lord of the Rings', `author` = 'J.R.R. Tolkien';
@@ -0,0 +1,25 @@
1
+ ---
2
+ - :data:
3
+ :id: 500
4
+ :display_string: "A"
5
+ :search_term: "a"
6
+ - :data:
7
+ :id: 600
8
+ :display_string: "B"
9
+ :search_term: "b"
10
+ - :data:
11
+ :id: 700
12
+ :display_string: "D"
13
+ :search_term: "d"
14
+ - :data:
15
+ :id: 800
16
+ :display_string: "C"
17
+ :search_term: "c"
18
+ - :data:
19
+ :id: 900
20
+ :display_string: "Aardvark"
21
+ :search_term: "aardvark"
22
+ - :data:
23
+ :id: 1000
24
+ :display_string: "Aachen"
25
+ :search_term: "aachen"
@@ -0,0 +1,98 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "../test_helper.rb"))
2
+ require 'rack/test'
3
+
4
+ Suggester::Server.add_handler("yaml_handler", Suggester::Handlers::Yaml.new(:file => File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'yaml.yml'))))
5
+ Suggester::Server.add_handler("marshal_handler", Suggester::Handlers::Marshal.new(:file => File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'marshal.marshal'))))
6
+
7
+ class BasicTest < Test::Unit::TestCase
8
+ include Rack::Test::Methods
9
+
10
+ def app
11
+ Suggester::Server
12
+ end
13
+
14
+ should "list all handlers on index page" do
15
+ get '/'
16
+ assert last_response.ok?
17
+ assert last_response.body.include?('yaml_handler')
18
+ assert last_response.body.include?('marshal_handler')
19
+ end
20
+
21
+ should "dump data in yaml format" do
22
+ get '/yaml_handler/dump.yml'
23
+ assert last_response.ok?
24
+ assert_nothing_raised do
25
+ data = YAML::load(last_response.body)
26
+ assert_equal(6, data.length)
27
+ end
28
+ end
29
+
30
+ should "dump data in json format" do
31
+ get '/yaml_handler/dump.json'
32
+ assert last_response.ok?
33
+ assert_nothing_raised do
34
+ data = JSON::load(last_response.body)
35
+ assert_equal(6, data.length)
36
+ end
37
+ end
38
+
39
+ should "dump data in marshal format" do
40
+ get '/yaml_handler/dump.marshal'
41
+ assert last_response.ok?
42
+ assert_nothing_raised do
43
+ data = Marshal.load(last_response.body)
44
+ assert_equal(6, data.length)
45
+ end
46
+ end
47
+
48
+ should "find begins with matches in yaml format" do
49
+ get '/yaml_handler/find/a.yml'
50
+ assert last_response.ok?
51
+ assert_nothing_raised do
52
+ data = YAML::load(last_response.body)
53
+ assert_equal(3, data.length)
54
+ end
55
+ end
56
+
57
+ should "find begins with matches in json format" do
58
+ get '/yaml_handler/find/a.json'
59
+ assert last_response.ok?
60
+ assert_nothing_raised do
61
+ data = JSON::load(last_response.body)
62
+ assert_equal(3, data.length)
63
+ end
64
+ end
65
+
66
+ should "find with limit" do
67
+ get '/yaml_handler/find/a.yml', :limit => 2
68
+ assert last_response.ok?
69
+ assert_nothing_raised do
70
+ data = YAML::load(last_response.body)
71
+ assert_equal(2, data.length)
72
+ end
73
+ end
74
+
75
+ should "refresh" do
76
+ # handler shouldn't need a refresh
77
+ assert !Suggester::Server.handler("yaml_handler").needs_refresh?
78
+
79
+ # force the server to refresh when it gets a chance
80
+ get '/yaml_handler/refresh'
81
+ assert last_response.ok?
82
+ assert_equal("OK", last_response.body)
83
+
84
+ # should need a refresh
85
+ assert Suggester::Server.handler("yaml_handler").needs_refresh?
86
+
87
+ # restore state
88
+ Suggester::Server.handler("yaml_handler").refresh!
89
+ assert !Suggester::Server.handler("yaml_handler").needs_refresh?
90
+ end
91
+
92
+ should "fail to refresh non-existent handler" do
93
+ get '/non_existent/refresh'
94
+ assert last_response.ok?
95
+ assert_equal("FAIL", last_response.body)
96
+ end
97
+
98
+ end
@@ -0,0 +1,26 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ # add to the load path (done by loading gems for you, just not in test)
4
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
5
+
6
+ # require the suggester files
7
+ require 'suggester'
8
+ require 'suggester/server'
9
+ require 'bundler'
10
+ Bundler.setup
11
+ require 'active_support'
12
+ require 'active_support/test_case'
13
+ require 'shoulda'
14
+ require 'mocha'
15
+
16
+ # run the mysql tests if you have set up your mysql.yml
17
+ config_file = File.join(File.dirname(__FILE__), '..', 'config', 'database.yml')
18
+ RUN_AR_TESTS = File.exists?(config_file)
19
+ if RUN_AR_TESTS
20
+ require 'activerecord'
21
+ require 'mysql'
22
+ database_config = YAML::load_file(config_file)["test"]
23
+ ActiveRecord::Base.establish_connection(database_config)
24
+ else
25
+ raise "set up your config/database.yml"
26
+ end
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "../test_helper.rb"))
2
+
3
+ class ActiveRecordHandlerTest < ActiveSupport::TestCase
4
+ if RUN_AR_TESTS
5
+ class Book < ActiveRecord::Base
6
+ end
7
+
8
+ should "raise exception if no class given" do
9
+ assert_raises RuntimeError do
10
+ handler = Suggester::Handlers::ActiveRecord.new()
11
+ end
12
+ end
13
+
14
+ should "load basic active_record handler" do
15
+ assert_nothing_raised do
16
+ handler = Suggester::Handlers::ActiveRecord.new( :class => Book,
17
+ :id_field => :id,
18
+ :name_field => :title)
19
+ assert_equal(6, handler.cache.length)
20
+ end
21
+ end
22
+
23
+ should "load a sorted cache" do
24
+ handler = Suggester::Handlers::ActiveRecord.new( :class => Book,
25
+ :id_field => :id,
26
+ :name_field => :title)
27
+ search_strings = handler.cache.map{|entry| entry[:search_term]}
28
+ sorted_search_strings = search_strings.sort
29
+ assert_equal(sorted_search_strings, search_strings)
30
+ end
31
+
32
+ should "find begins with matches" do
33
+ handler = Suggester::Handlers::ActiveRecord.new( :class => Book,
34
+ :id_field => :id,
35
+ :name_field => :title)
36
+ matches = handler.find(:query => "the")
37
+ assert_equal(3, matches.length)
38
+
39
+ matches = handler.find(:query => "a ")
40
+ assert_equal(1, matches.length)
41
+ end
42
+
43
+ should "find exact matches" do
44
+ handler = Suggester::Handlers::ActiveRecord.new( :class => Book,
45
+ :id_field => :id,
46
+ :name_field => :title)
47
+ matches = handler.match(:query => "the")
48
+ assert_equal(0, matches.length)
49
+
50
+ matches = handler.match(:query => "the catcher in the rye")
51
+ assert_equal(1, matches.length)
52
+ end
53
+
54
+ should "normalize search string" do
55
+ handler = Suggester::Handlers::ActiveRecord.new( :class => Book,
56
+ :id_field => :id,
57
+ :name_field => :title)
58
+ matches = handler.find(:query => "THE")
59
+ assert_equal(3, matches.length)
60
+
61
+ matches = handler.match(:query => "THE cAtChEr In ThE rYe")
62
+ assert_equal(1, matches.length)
63
+ end
64
+
65
+ end
66
+
67
+
68
+ protected
69
+
70
+ def yaml_file
71
+ File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'yaml.yml'))
72
+ end
73
+
74
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "../test_helper.rb"))
2
+
3
+ class MarshalHandlerTest < ActiveSupport::TestCase
4
+
5
+ should "raise exception if no file given" do
6
+ assert_raises RuntimeError do
7
+ handler = Suggester::Handlers::Marshal.new
8
+ end
9
+ end
10
+
11
+ should "load basic marshal file" do
12
+ assert_nothing_raised do
13
+ handler = Suggester::Handlers::Marshal.new(:file => marshal_file)
14
+ assert_equal(6, handler.cache.length)
15
+ end
16
+ end
17
+
18
+ should "load a sorted cache" do
19
+ handler = Suggester::Handlers::Marshal.new(:file => marshal_file)
20
+ search_strings = handler.cache.map{|entry| entry[:search_term]}
21
+ sorted_search_strings = search_strings.sort
22
+ assert_equal(sorted_search_strings, search_strings)
23
+ end
24
+
25
+ should "load marshal from uri" do
26
+ # stub the open uri
27
+ OpenURI.stubs(:open_uri).returns(File.open(marshal_file))
28
+ url = "http://suggester.server.com/handler/dump.marshal"
29
+ assert_nothing_raised do
30
+ handler = Suggester::Handlers::Marshal.new(:file => url)
31
+ assert_equal(6, handler.cache.length)
32
+ end
33
+ end
34
+
35
+ should "find begins with matches" do
36
+ handler = Suggester::Handlers::Marshal.new(:file => marshal_file)
37
+ matches = handler.find(:query => "a")
38
+ assert_equal(3, matches.length)
39
+
40
+ matches = handler.find(:query => "aa")
41
+ assert_equal(2, matches.length)
42
+ end
43
+
44
+ should "find exact matches" do
45
+ handler = Suggester::Handlers::Marshal.new(:file => marshal_file)
46
+ matches = handler.match(:query => "aar")
47
+ assert_equal(0, matches.length)
48
+
49
+ matches = handler.match(:query => "aardvark")
50
+ assert_equal(1, matches.length)
51
+ end
52
+
53
+ should "normalize search string" do
54
+ handler = Suggester::Handlers::Marshal.new(:file => marshal_file)
55
+ matches = handler.match(:query => "AARDVARK")
56
+ assert_equal(1, matches.length)
57
+ end
58
+
59
+ protected
60
+
61
+ def marshal_file
62
+ File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'marshal.marshal'))
63
+ end
64
+
65
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "../test_helper.rb"))
2
+
3
+ class YamlHandlerTest < ActiveSupport::TestCase
4
+
5
+ should "raise exception if no file given" do
6
+ assert_raises RuntimeError do
7
+ handler = Suggester::Handlers::Yaml.new
8
+ end
9
+ end
10
+
11
+ should "load basic yaml file" do
12
+ assert_nothing_raised do
13
+ handler = Suggester::Handlers::Yaml.new(:file => yaml_file)
14
+ assert_equal(6, handler.cache.length)
15
+ end
16
+ end
17
+
18
+ should "load a sorted cache" do
19
+ handler = Suggester::Handlers::Yaml.new(:file => yaml_file)
20
+ search_strings = handler.cache.map{|entry| entry[:search_term]}
21
+ sorted_search_strings = search_strings.sort
22
+ assert_equal(sorted_search_strings, search_strings)
23
+ end
24
+
25
+ should "load yaml from uri" do
26
+ # stub the open uri
27
+ OpenURI.stubs(:open_uri).returns(File.open(yaml_file))
28
+ url = "http://suggester.server.com/handler/dump.yml"
29
+ assert_nothing_raised do
30
+ handler = Suggester::Handlers::Yaml.new(:file => url)
31
+ assert_equal(6, handler.cache.length)
32
+ end
33
+ end
34
+
35
+ should "find begins with matches" do
36
+ handler = Suggester::Handlers::Yaml.new(:file => yaml_file)
37
+ matches = handler.find(:query => "a")
38
+ assert_equal(3, matches.length)
39
+
40
+ matches = handler.find(:query => "aa")
41
+ assert_equal(2, matches.length)
42
+ end
43
+
44
+ should "find exact matches" do
45
+ handler = Suggester::Handlers::Yaml.new(:file => yaml_file)
46
+ matches = handler.match(:query => "aar")
47
+ assert_equal(0, matches.length)
48
+
49
+ matches = handler.match(:query => "aardvark")
50
+ assert_equal(1, matches.length)
51
+ end
52
+
53
+ should "normalize search string" do
54
+ handler = Suggester::Handlers::Yaml.new(:file => yaml_file)
55
+ matches = handler.match(:query => "AARDVARK")
56
+ assert_equal(1, matches.length)
57
+ end
58
+
59
+
60
+ protected
61
+
62
+ def yaml_file
63
+ File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'yaml.yml'))
64
+ end
65
+
66
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: suggester
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Jeff Ching
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-09 00:00:00 -08:00
18
+ default_executable: suggester_server
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sinatra
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ prerelease: false
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: vegas
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: bundler
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 1
68
+ - 0
69
+ - 0
70
+ version: 1.0.0
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: *id004
74
+ - !ruby/object:Gem::Dependency
75
+ name: jeweler
76
+ requirement: &id005 !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 1
83
+ - 5
84
+ - 1
85
+ version: 1.5.1
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: *id005
89
+ - !ruby/object:Gem::Dependency
90
+ name: rcov
91
+ requirement: &id006 !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *id006
102
+ description: Extensible, cache-based auto-suggest server for ruby. Includes refresh and replication support out of the box.
103
+ email: ching.jeff@gmail.com
104
+ executables:
105
+ - suggester_server
106
+ extensions: []
107
+
108
+ extra_rdoc_files:
109
+ - README.md
110
+ files:
111
+ - Gemfile
112
+ - Gemfile.lock
113
+ - README.md
114
+ - Rakefile
115
+ - VERSION
116
+ - bin/suggester_server
117
+ - config/database.yml.example
118
+ - lib/array_bsearch.rb
119
+ - lib/suggester.rb
120
+ - lib/suggester/client.rb
121
+ - lib/suggester/handlers.rb
122
+ - lib/suggester/handlers/active_record.rb
123
+ - lib/suggester/handlers/base.rb
124
+ - lib/suggester/handlers/helpers/normalization.rb
125
+ - lib/suggester/handlers/helpers/refresh.rb
126
+ - lib/suggester/handlers/marshal.rb
127
+ - lib/suggester/handlers/yaml.rb
128
+ - lib/suggester/server.rb
129
+ - pkg/suggester-0.0.1.gem
130
+ - pkg/suggester-0.0.2.gem
131
+ - suggester.gemspec
132
+ - test/fixtures/books.sql
133
+ - test/fixtures/marshal.marshal
134
+ - test/fixtures/yaml.yml
135
+ - test/functional/basic_test.rb
136
+ - test/test_helper.rb
137
+ - test/unit/active_record_handler_test.rb
138
+ - test/unit/marshal_handler_test.rb
139
+ - test/unit/yaml_handler_test.rb
140
+ has_rdoc: true
141
+ homepage: http://github.com/chingor13/suggester
142
+ licenses:
143
+ - MIT
144
+ post_install_message:
145
+ rdoc_options: []
146
+
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ hash: 595145928742037670
155
+ segments:
156
+ - 0
157
+ version: "0"
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ none: false
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ segments:
164
+ - 0
165
+ version: "0"
166
+ requirements: []
167
+
168
+ rubyforge_project:
169
+ rubygems_version: 1.3.7
170
+ signing_key:
171
+ specification_version: 3
172
+ summary: Extensible, cache-based auto-suggest server for ruby.
173
+ test_files:
174
+ - test/functional/basic_test.rb
175
+ - test/test_helper.rb
176
+ - test/unit/active_record_handler_test.rb
177
+ - test/unit/marshal_handler_test.rb
178
+ - test/unit/yaml_handler_test.rb