errol 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,62 @@
1
+ require_relative '../test_config'
2
+ require_relative './query_test'
3
+
4
+ class Item < Sequel::Model(:items)
5
+ def name=(name)
6
+ super name.reverse
7
+ end
8
+ end
9
+
10
+ class RepositoryQueyTest < RecordTest
11
+ def test_can_build_new_record
12
+ assert_equal Item.new, TestRepository.build.__getobj__
13
+ end
14
+
15
+ def test_can_build_new_record_with_name
16
+ assert_equal 'ylurnu', TestRepository.build(:name => 'unruly').name
17
+ end
18
+
19
+ def test_build_yields_to_block
20
+ mock = MiniTest::Mock.new
21
+ mock.expect :record, true, [Item.new]
22
+ TestRepository.build do |item|
23
+ mock.record item.__getobj__
24
+ end
25
+ mock.verify
26
+ end
27
+
28
+ def test_can_save_an_item
29
+ # TODO incorperate, update.amend/renew/revise, remove/obliterate/withdraw
30
+ item = SimpleDelegator.new Item.new
31
+ TestRepository.save(item)
32
+ refute_empty TestRepository
33
+ end
34
+
35
+ def test_save_returns_repository
36
+ item = SimpleDelegator.new Item.new
37
+ assert_equal TestRepository, TestRepository.save(item)
38
+ end
39
+
40
+ def test_can_remove_an_item
41
+ item = SimpleDelegator.new Item.create
42
+ TestRepository.remove(item)
43
+ assert_empty TestRepository
44
+ end
45
+
46
+ def test_cant_remove_missing_item
47
+ item = SimpleDelegator.new Item.new
48
+ assert_raises Errol::Repository::RecordAbsent do
49
+ TestRepository.remove(item)
50
+ end
51
+ end
52
+
53
+ def test_remove_returns_repository
54
+ item = SimpleDelegator.new Item.create
55
+ assert_equal TestRepository, TestRepository.remove(item)
56
+ end
57
+
58
+ def test_can_create_item
59
+ TestRepository.create
60
+ refute_empty TestRepository
61
+ end
62
+ end
@@ -0,0 +1,163 @@
1
+ require_relative '../test_config'
2
+
3
+ class PaginatedInquiry < Errol::Inquiry
4
+ default :page, 1
5
+ default :page_size, 2
6
+ end
7
+
8
+ class PaginatedRepository < Errol::Repository
9
+ def self.record_class
10
+ Item
11
+ end
12
+
13
+ def dataset
14
+ dataset = raw_dataset
15
+ end
16
+
17
+ def self.inquiry(requirements)
18
+ PaginatedInquiry.new(requirements)
19
+ end
20
+
21
+ def self.dispatch(record)
22
+ SimpleDelegator.new record
23
+ end
24
+
25
+ def self.receive(entity)
26
+ entity.__getobj__
27
+ end
28
+ end
29
+
30
+ class DemoRepositoryQueyTest < RecordTest
31
+ def items
32
+ DB[:items]
33
+ end
34
+
35
+ def first
36
+ @first ||= items.insert({})
37
+ end
38
+
39
+ def second
40
+ @second ||= items.insert({})
41
+ end
42
+
43
+ def third
44
+ @third ||= items.insert({})
45
+ end
46
+
47
+ def setup
48
+ first
49
+ second
50
+ third
51
+ end
52
+
53
+ def teardown
54
+ @first = nil
55
+ @second = nil
56
+ @third = nil
57
+ end
58
+
59
+ def test_can_get_first_page_number
60
+ page = PaginatedRepository.new
61
+ assert_equal 1, page.current_page
62
+ end
63
+
64
+ def test_can_get_first_page_size
65
+ page = PaginatedRepository.new
66
+ assert_equal 2, page.page_size
67
+ end
68
+
69
+ def test_identifies_as_first_page
70
+ page = PaginatedRepository.new
71
+ assert page.first_page?
72
+ end
73
+
74
+ def test_identifies_as_not_first_page
75
+ page = PaginatedRepository.new :page => 2
76
+ refute page.first_page?
77
+ end
78
+
79
+ def test_identifies_as_last_page
80
+ page = PaginatedRepository.new :page => 2
81
+ assert page.last_page?
82
+ end
83
+
84
+ def test_identifies_as_not_first_page
85
+ page = PaginatedRepository.new
86
+ refute page.last_page?
87
+ end
88
+
89
+ def test_can_get_page_count_from_each_page
90
+ page = PaginatedRepository.new
91
+ assert_equal 2, page.page_count
92
+ end
93
+
94
+ def test_can_get_page_range
95
+ page = PaginatedRepository.new
96
+ assert_equal 1..2, page.page_range
97
+ end
98
+
99
+ def test_can_get_next_page
100
+ page = PaginatedRepository.new
101
+ assert_equal 2, page.next_page
102
+ end
103
+
104
+ def test_can_get_previous_page
105
+ page = PaginatedRepository.new
106
+ assert_equal nil, page.previous_page
107
+ end
108
+
109
+ def test_page_reports_not_empty
110
+ page = PaginatedRepository.new
111
+ refute_empty page
112
+ end
113
+
114
+ def test_page_reports_empty
115
+ page = PaginatedRepository.new :page => 3
116
+ assert_empty page
117
+ end
118
+
119
+ def test_counts_page_items
120
+ page = PaginatedRepository.new :page => 2
121
+ assert_equal 1, page.count
122
+ end
123
+
124
+ def test_can_get_first_item_on_second_page
125
+ page = PaginatedRepository.new :page => 2
126
+ assert_equal third, page.first.id
127
+ end
128
+
129
+ def test_can_get_last_item_on_first_page
130
+ page = PaginatedRepository.new :page => 1
131
+ assert_equal second, page.last.id
132
+ end
133
+
134
+ # def test_nil_if_not_found_on_page
135
+ # # TODO note pagination applied after filtering so finds record
136
+ # page = PaginatedRepository.new :page => 1
137
+ # assert_nil page[third]
138
+ # end
139
+
140
+ def test_can_get_all_items_on_second_page
141
+ page = PaginatedRepository.new :page => 2
142
+ assert_equal [third], page.all.map{ |entity| entity.id }
143
+ end
144
+
145
+ def test_can_get_each_item_on_page
146
+ mock = MiniTest::Mock.new
147
+ mock.expect :record, true, [third]
148
+ page = PaginatedRepository.new :page => 2
149
+ page.each { |entity| mock.record entity.id }
150
+ mock.verify
151
+ end
152
+
153
+ def test_each_method_returns_self
154
+ page = PaginatedRepository.new :page => 2
155
+ assert_equal page, page.each{}
156
+ end
157
+
158
+ def test_can_map_items_on_page
159
+ page = PaginatedRepository.new :page => 2
160
+ assert_equal [3], page.map(&:id)
161
+ end
162
+
163
+ end
@@ -0,0 +1,190 @@
1
+ require_relative '../test_config'
2
+
3
+ class Inquiry < Errol::Inquiry
4
+ default :show_offers, false
5
+ end
6
+
7
+ class TestRepository < Errol::Repository
8
+ def self.record_class
9
+ Item
10
+ end
11
+
12
+ def dataset
13
+ dataset = raw_dataset
14
+
15
+ inquiry.show_offers? ? dataset.where(:discounted) : dataset
16
+
17
+ end
18
+
19
+ def self.inquiry(requirements)
20
+ Inquiry.new(requirements)
21
+ end
22
+
23
+ def self.dispatch(record)
24
+ SimpleDelegator.new record
25
+ end
26
+
27
+ def self.receive(entity)
28
+ entity.__getobj__
29
+ end
30
+ end
31
+
32
+
33
+ class RepositoryQueyTest < RecordTest
34
+ def items
35
+ DB[:items]
36
+ end
37
+
38
+ def test_repository_starts_empty
39
+ assert_equal true, TestRepository.empty?
40
+ end
41
+
42
+ def test_repository_not_empty_with_item
43
+ items.insert(:name => 'abc')
44
+ assert_equal false, TestRepository.empty?
45
+ end
46
+
47
+ def test_filtered_repository_is_empty
48
+ items.insert(:name => 'abc', :discounted => false)
49
+ assert_equal true, TestRepository.empty?(:show_offers => true)
50
+ end
51
+
52
+ def test_repository_starts_with_count_zero
53
+ assert_equal 0, TestRepository.count
54
+ end
55
+
56
+ def test_respository_counts_one_item
57
+ items.insert(:name => 'abc')
58
+ assert_equal 1, TestRepository.count
59
+ end
60
+
61
+ def test_filtered_repository_counts_zero_items
62
+ items.insert(:name => 'abc', :discounted => false)
63
+ assert_equal 0, TestRepository.count(:show_offers => true)
64
+ end
65
+
66
+ def test_can_get_first_item
67
+ item = Item.create(:name => 'abc')
68
+ assert_equal item, TestRepository.first.__getobj__
69
+ end
70
+
71
+ def test_wraps_first_item
72
+ items.insert(:name => 'abc')
73
+ assert_equal SimpleDelegator, TestRepository.first.class
74
+ end
75
+
76
+ def test_returns_nil_for_no_first_item
77
+ assert_nil TestRepository.first
78
+ end
79
+
80
+ def test_returns_first_filtered_item
81
+ items.insert(:name => 'abc', :discounted => false)
82
+ i = items.insert(:name => 'abc', :discounted => true)
83
+ items.insert(:name => 'abc', :discounted => false)
84
+ assert_equal i, TestRepository.first(:show_offers => true).id
85
+ end
86
+
87
+ def test_can_get_last_item
88
+ item = Item.create(:name => 'abc')
89
+ assert_equal item, TestRepository.last.__getobj__
90
+ end
91
+
92
+ def test_wraps_last_item
93
+ items.insert(:name => 'abc')
94
+ assert_equal SimpleDelegator, TestRepository.last.class
95
+ end
96
+
97
+ def test_returns_nil_for_no_last_item
98
+ assert_nil TestRepository.last
99
+ end
100
+
101
+ def test_returns_last_filtered_item
102
+ items.insert(:name => 'abc', :discounted => false)
103
+ i = items.insert(:name => 'abc', :discounted => true)
104
+ items.insert(:name => 'abc', :discounted => false)
105
+ assert_equal i, TestRepository.last(:show_offers => true).id
106
+ end
107
+
108
+ def test_can_find_item_by_id
109
+ item = Item.create(:name => 'abc')
110
+ assert_equal item, TestRepository[item.id].__getobj__
111
+ end
112
+
113
+ def test_wraps_found_item
114
+ item = Item.create(:name => 'abc')
115
+ assert_equal SimpleDelegator, TestRepository[item.id].class
116
+ end
117
+
118
+ def test_returns_nil_for_no_found_item
119
+ assert_nil TestRepository[2]
120
+ end
121
+
122
+ def test_returns_nil_on_filtered_find
123
+ item = Item.create(:name => 'abc')
124
+ assert_nil TestRepository[item.id, :show_offers => true]
125
+ end
126
+
127
+ def test_can_fetch_item_by_id
128
+ item = Item.create(:name => 'abc')
129
+ assert_equal item, TestRepository.fetch(item.id).__getobj__
130
+ end
131
+
132
+ def test_wraps_fetched_item
133
+ item = Item.create(:name => 'abc')
134
+ assert_equal SimpleDelegator, TestRepository.fetch(item.id).class
135
+ end
136
+
137
+ def test_raises_error_for_no_fetched_item
138
+ err = assert_raises Errol::Repository::RecordAbsent do
139
+ TestRepository.fetch(2)
140
+ end
141
+ assert_equal 'TestRepository contains no record with id: 2', err.message
142
+ end
143
+
144
+ def test_raises_error_on_filtered_fetch
145
+ item = Item.create(:name => 'abc')
146
+ assert_raises Errol::Repository::RecordAbsent do
147
+ TestRepository.fetch(item.id, :show_offers => true)
148
+ end
149
+ end
150
+
151
+ def test_fetch_calls_block_for_missing_item
152
+ mock = MiniTest::Mock.new
153
+ mock.expect :record, true, [2]
154
+ TestRepository.fetch(2) { |id| mock.record id }
155
+ end
156
+
157
+ def test_returns_all_items
158
+ item = Item.create(:name => 'abc')
159
+ assert_equal [item], TestRepository.all.map(&:__getobj__)
160
+ end
161
+
162
+ def test_wraps_all_items
163
+ item = Item.create(:name => 'abc')
164
+ assert_equal [SimpleDelegator], TestRepository.all.map(&:class)
165
+ end
166
+
167
+ def test_filters_all_records
168
+ discounted = Item.create(:name => 'abc', :discounted => true)
169
+ Item.create(:name => 'abc')
170
+ assert_equal [discounted], TestRepository.all(:show_offers => true).map(&:__getobj__)
171
+ end
172
+
173
+ def test_can_get_each_item_in_repository
174
+ item = Item.create(:name => 'abc')
175
+ mock = MiniTest::Mock.new
176
+ mock.expect :record, true, [item]
177
+ TestRepository.each { |entity| mock.record entity.__getobj__ }
178
+ mock.verify
179
+ end
180
+
181
+ def test_can_get_each_item_in_filtered_repository
182
+ item = Item.create(:name => 'abc')
183
+ discounted = Item.create(:name => 'abc', :discounted => true)
184
+ mock = MiniTest::Mock.new
185
+ mock.expect :record, true, [discounted]
186
+ TestRepository.each(:show_offers => true) { |entity| mock.record entity.__getobj__ }
187
+ mock.verify
188
+ end
189
+
190
+ end
@@ -0,0 +1,27 @@
1
+ require 'errol'
2
+ require 'minitest/autorun'
3
+ require 'minitest/reporters'
4
+ require 'sequel'
5
+
6
+ reporter_options = {color: true, slow_count: 5}
7
+ Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(reporter_options)]
8
+
9
+ DB = Sequel.sqlite
10
+
11
+ DB.create_table :items do
12
+ primary_key :id
13
+ String :name
14
+ TrueClass :discounted
15
+ Float :price
16
+ end
17
+ DB.extension(:pagination)
18
+
19
+ class RecordTest < MiniTest::Test
20
+
21
+ def run(*args, &block)
22
+ result = nil
23
+ Sequel::Model.db.transaction(:rollback=>:always, :auto_savepoint=>true){result = super}
24
+ result
25
+ end
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: errol
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Saxton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.19'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.19'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ description: Based of the Sequel Library to deliver a repository interface to persisted
84
+ data. Handles pagination by default
85
+ email:
86
+ - peterhsaxton@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - errol.gemspec
97
+ - lib/errol.rb
98
+ - lib/errol/entity.rb
99
+ - lib/errol/inquiry.rb
100
+ - lib/errol/repository.rb
101
+ - lib/errol/version.rb
102
+ - test/entity/access_generation_test.rb
103
+ - test/entity/access_methods_test.rb
104
+ - test/entity/entity_test.rb
105
+ - test/entity/repository_method_test.rb
106
+ - test/inquiry/inquiry_test.rb
107
+ - test/repository/contruction_test.rb
108
+ - test/repository/page_test.rb
109
+ - test/repository/query_test.rb
110
+ - test/test_config.rb
111
+ homepage: ''
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.5
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Repository to store and deliver encapsulated records.
135
+ test_files:
136
+ - test/entity/access_generation_test.rb
137
+ - test/entity/access_methods_test.rb
138
+ - test/entity/entity_test.rb
139
+ - test/entity/repository_method_test.rb
140
+ - test/inquiry/inquiry_test.rb
141
+ - test/repository/contruction_test.rb
142
+ - test/repository/page_test.rb
143
+ - test/repository/query_test.rb
144
+ - test/test_config.rb