beerdb-models 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ module BeerDb
4
+
5
+ class Stats
6
+ include Models
7
+
8
+ def tables
9
+ puts "Stats:"
10
+ puts " #{'%5d' % Beer.count} beers"
11
+ puts " #{'%5d' % Brand.count} brands"
12
+ puts " #{'%5d' % Brewery.count} breweries"
13
+ puts
14
+
15
+ ### fix: use if defined? BeerDbNote or similar or/and check if table exist ??
16
+ ### or move to beerdb-note ??
17
+
18
+ # puts " #{'%5d' % User.count} users" # db model extension - move to its own addon?
19
+ # puts " #{'%5d' % Bookmark.count} bookmarks" # db model extension - move to its own addon?
20
+ # puts " #{'%5d' % Drink.count} drinks" # db model extension - move to its own addon?
21
+ # puts " #{'%5d' % Note.count} notes" # db model extension - move to its own addon?
22
+ end
23
+
24
+ end # class Stats
25
+
26
+ end # module BeerDb
27
+
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ module BeerDb
4
+ MAJOR = 0 ## todo: namespace inside version or something - why? why not??
5
+ MINOR = 10
6
+ PATCH = 0
7
+ VERSION = [MAJOR,MINOR,PATCH].join('.')
8
+
9
+ def self.version
10
+ VERSION
11
+ end
12
+
13
+ def self.banner
14
+ "beerdb-models/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
15
+ end
16
+
17
+ def self.root
18
+ "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
19
+ end
20
+ end # module BeerDb
21
+
data/test/helper.rb ADDED
@@ -0,0 +1,63 @@
1
+
2
+ ## $:.unshift(File.dirname(__FILE__))
3
+
4
+ ## minitest setup
5
+
6
+ require 'minitest/autorun'
7
+
8
+
9
+ # our own code
10
+ require 'beerdb/models'
11
+
12
+
13
+ Country = WorldDb::Model::Country
14
+ Region = WorldDb::Model::Region
15
+
16
+ ## todo: get all models aliases (e.g. from console script)
17
+
18
+ Beer = BeerDb::Model::Beer
19
+ Brand = BeerDb::Model::Brand
20
+ Brewery = BeerDb::Model::Brewery
21
+
22
+
23
+ def setup_in_memory_db
24
+ # Database Setup & Config
25
+
26
+ db_config = {
27
+ adapter: 'sqlite3',
28
+ database: ':memory:'
29
+ }
30
+
31
+ pp db_config
32
+
33
+ ActiveRecord::Base.logger = Logger.new( STDOUT )
34
+ ## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?
35
+
36
+ ## NB: every connect will create a new empty in memory db
37
+ ActiveRecord::Base.establish_connection( db_config )
38
+
39
+
40
+ ## build schema
41
+ BeerDb.create_all
42
+ end
43
+
44
+
45
+ def fillup_in_memory_db
46
+ ## add some counties
47
+
48
+ at = Country.create!( key: 'at', title: 'Austria', code: 'AUT', pop: 0, area: 0 )
49
+ Region.create!( key: 'w', title: 'Wien', country_id: at.id )
50
+
51
+ de = Country.create!( key: 'de', title: 'Germany', code: 'DEU', pop: 0, area: 0 )
52
+ Region.create!( key: 'by', title: 'Bayern', country_id: de.id )
53
+
54
+ end
55
+
56
+ setup_in_memory_db()
57
+ fillup_in_memory_db()
58
+
59
+ AT = Country.find_by_key!( 'at' )
60
+ W = Region.find_by_key!( 'w' )
61
+
62
+ DE = Country.find_by_key!( 'de' )
63
+ BY = Region.find_by_key!( 'by' )
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ require 'helper'
5
+
6
+
7
+ class TestFixtureMatchers < MiniTest::Test
8
+
9
+ include WorldDb::Matcher
10
+ include BeerDb::Matcher
11
+
12
+
13
+ def test_country
14
+
15
+ beers_at = [
16
+ 'europe/at/beers',
17
+ 'europe/at-austria/beers',
18
+ 'at-austria/beers',
19
+ 'at-austria!/beers',
20
+ '1--at-austria--central/beers',
21
+ 'europe/1--at-austria--central/beers'
22
+ ]
23
+
24
+ beers_at.each do |name|
25
+ found = match_beers_for_country( name ) do |country_key|
26
+ assert( country_key == 'at')
27
+ end
28
+ assert( found == true )
29
+ end
30
+
31
+ breweries_at = [
32
+ 'europe/at/breweries',
33
+ 'europe/at-austria/breweries',
34
+ 'at-austria/breweries',
35
+ 'at-austria!/breweries',
36
+ '1--at-austria--central/breweries',
37
+ 'europe/1--at-austria--central/breweries'
38
+ ]
39
+
40
+ breweries_at.each do |name|
41
+ found = match_breweries_for_country( name ) do |country_key|
42
+ assert( country_key == 'at')
43
+ end
44
+ assert( found == true )
45
+ end
46
+ end # method test_country
47
+
48
+
49
+ def test_country_n_region
50
+
51
+ beers_at = [
52
+ 'europe/at-austria/w-wien/beers',
53
+ 'at-austria/w-wien/beers',
54
+ 'at-austria!/w-wien/beers',
55
+ '1--at-austria--central/1--w-wien--eastern/beers',
56
+ 'europe/1--at-austria--central/1--w-wien--eastern/beers'
57
+ ]
58
+
59
+ beers_at.each do |name|
60
+ found = match_beers_for_country_n_region( name ) do |country_key,region_key|
61
+ assert( country_key == 'at')
62
+ assert( region_key == 'w' )
63
+ end
64
+ assert( found == true )
65
+ end
66
+
67
+ breweries_at = [
68
+ 'europe/at-austria/w-wien/breweries',
69
+ 'at-austria/w-wien/breweries',
70
+ 'at-austria!/w-wien/breweries',
71
+ '1--at-austria--central/1--w-wien--eastern/breweries',
72
+ 'europe/1--at-austria--central/1--w-wien--eastern/breweries'
73
+ ]
74
+
75
+ breweries_at.each do |name|
76
+ found = match_breweries_for_country_n_region( name ) do |country_key,region_key|
77
+ assert( country_key == 'at')
78
+ assert( region_key == 'w' )
79
+ end
80
+ assert( found == true )
81
+ end
82
+ end # method test_country_n_region
83
+
84
+
85
+ end # class TestFixtureMatchers
@@ -0,0 +1,240 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_helper.rb
6
+ # or better
7
+ # rake test
8
+
9
+ require 'helper'
10
+
11
+
12
+ class TestValues < MiniTest::Test
13
+
14
+ def setup
15
+ # delete all beers, brands, breweries in in-memory only db
16
+ BeerDb.delete!
17
+ end
18
+
19
+ def test_load_beer_values
20
+
21
+ key = 'ottakringerpur'
22
+
23
+ values = [
24
+ 'Ottakringer (Gold Fassl) Pur {Bio}',
25
+ '5.2 %',
26
+ '11.8°',
27
+ 'bio'
28
+ ]
29
+
30
+ more_attribs = {
31
+ country_id: AT.id
32
+ }
33
+
34
+ beer = Beer.create_or_update_from_values( values, more_attribs )
35
+
36
+ beer2 = Beer.find_by_key!( key )
37
+ assert_equal beer.id, beer2.id
38
+
39
+ assert_equal beer.title, values[0]
40
+ assert_equal beer.country_id, AT.id
41
+ assert_equal beer.country.title, AT.title
42
+ assert_equal beer.abv, 5.2
43
+ assert_equal beer.og, 11.8
44
+ assert_equal beer.srm, nil
45
+ end
46
+
47
+ def test_load_brewery_values
48
+
49
+ # ottakringer, Ottakringer Brauerei, 1838, www.ottakringer.at, WI, city:wien, 1160 Wien // Ottakringer Platz 1
50
+ # brands: Ottakringer
51
+
52
+ key = 'ottakringer'
53
+
54
+ values = [
55
+ key,
56
+ 'Ottakringer Brauerei',
57
+ '1838',
58
+ 'www.ottakringer.at',
59
+ '1160 Wien // Ottakringer Platz 1',
60
+ 'brands: Ottakringer'
61
+ ]
62
+
63
+ more_attribs = {
64
+ country_id: AT.id
65
+ }
66
+
67
+
68
+ by = Brewery.create_or_update_from_values( values, more_attribs )
69
+
70
+ by2 = Brewery.find_by_key!( key )
71
+ assert_equal by.id, by2.id
72
+
73
+ assert_equal by.title, values[1]
74
+ assert_equal by.country_id, AT.id
75
+ assert_equal by.country.title, AT.title
76
+ assert_equal by.since, 1838
77
+ assert_equal by.web, 'www.ottakringer.at'
78
+ assert_equal by.address, '1160 Wien // Ottakringer Platz 1'
79
+ assert_equal by.grade, 4
80
+
81
+ # check auto-created brand
82
+
83
+ br = Brand.find_by_key!( 'ottakringer')
84
+
85
+ assert_equal br.title, 'Ottakringer'
86
+ assert_equal br.brewery_id, by.id
87
+ assert_equal br.brewery.title, by.title
88
+ assert_equal br.country_id, by.country_id
89
+ end
90
+
91
+
92
+ def test_load_brewery_values_w_grade
93
+
94
+ # ottakringer, Ottakringer Brauerei, 1838, www.ottakringer.at, WI, city:wien, 1160 Wien // Ottakringer Platz 1
95
+ # brands: Ottakringer
96
+
97
+ key = 'ottakringer'
98
+
99
+ values = [
100
+ key,
101
+ 'Ottakringer Brauerei **',
102
+ '1838',
103
+ 'www.ottakringer.at',
104
+ '1160 Wien // Ottakringer Platz 1',
105
+ 'brands: Ottakringer'
106
+ ]
107
+
108
+ more_attribs = {
109
+ country_id: AT.id
110
+ }
111
+
112
+ by = Brewery.create_or_update_from_values( values, more_attribs )
113
+
114
+ by2 = Brewery.find_by_key!( key )
115
+ assert_equal by.id, by2.id
116
+
117
+ assert_equal by.title, 'Ottakringer Brauerei'
118
+ assert_equal by.grade, 2
119
+ end
120
+
121
+ def test_load_brewery_values_w_grade_in_synonyms
122
+
123
+ ## fix!!!!!!!!: use different brewery! use one w/ synonyms
124
+ ###
125
+
126
+ # ottakringer, Ottakringer Brauerei, 1838, www.ottakringer.at, WI, city:wien, 1160 Wien // Ottakringer Platz 1
127
+ # brands: Ottakringer
128
+
129
+ key = 'ottakringer'
130
+
131
+ values = [
132
+ key,
133
+ 'Ottakringer Brauerei|Otta **', # NB: title will auto-gen grade n synonyms
134
+ '1838',
135
+ 'www.ottakringer.at',
136
+ '1160 Wien // Ottakringer Platz 1',
137
+ 'brands: Ottakringer'
138
+ ]
139
+
140
+ more_attribs = {
141
+ country_id: AT.id
142
+ }
143
+
144
+ by = Brewery.create_or_update_from_values( values, more_attribs )
145
+
146
+ by2 = Brewery.find_by_key!( key )
147
+ assert_equal by.id, by2.id
148
+
149
+ assert_equal by.title, 'Ottakringer Brauerei'
150
+ assert_equal by.synonyms, 'Otta'
151
+ assert_equal by.grade, 2
152
+ end
153
+
154
+
155
+ def test_load_brewery_values_w_city_at
156
+
157
+ # ottakringer, Ottakringer Brauerei, 1838, www.ottakringer.at, WI, city:wien, 1160 Wien // Ottakringer Platz 1
158
+ # brands: Ottakringer
159
+
160
+ key = 'ottakringer'
161
+
162
+ values = [
163
+ key,
164
+ 'Ottakringer Brauerei',
165
+ '1838',
166
+ 'www.ottakringer.at',
167
+ '1160 Wien // Ottakringer Platz 1',
168
+ 'brands: Ottakringer'
169
+ ]
170
+
171
+ more_attribs = {
172
+ country_id: AT.id,
173
+ region_id: W.id
174
+ }
175
+
176
+ by = Brewery.create_or_update_from_values( values, more_attribs )
177
+
178
+ by2 = Brewery.find_by_key!( key )
179
+ assert( by.id == by2.id )
180
+
181
+ assert( by.title == values[1] )
182
+ assert( by.country_id == AT.id )
183
+ assert( by.country.title == AT.title )
184
+ assert( by.since == 1838 )
185
+ assert( by.web == 'www.ottakringer.at' )
186
+ assert( by.address == '1160 Wien // Ottakringer Platz 1' )
187
+
188
+ # check auto-created brand
189
+
190
+ br = Brand.find_by_key!( 'ottakringer')
191
+
192
+ assert( br.title == 'Ottakringer' )
193
+ assert( br.brewery_id == by.id )
194
+ assert( br.brewery.title == by.title )
195
+ assert( br.country_id == by.country_id )
196
+
197
+ # todo: check for auto-created city
198
+ assert( by.city.title == 'Wien' )
199
+ end
200
+
201
+
202
+ def test_load_brewery_values_w_city_de
203
+
204
+ # hofbraeu, Hofbräu München, 1589, www.hofbraeu-muenchen.de, 81829 München // Hofbräuallee 1
205
+
206
+ key = 'hofbraeu'
207
+
208
+ values = [
209
+ key,
210
+ 'Hofbräu München',
211
+ '1589',
212
+ 'www.hofbraeu-muenchen.de',
213
+ '81829 München // Hofbräuallee 1',
214
+ 'brands: Hofbräu'
215
+ ]
216
+
217
+ more_attribs = {
218
+ country_id: DE.id,
219
+ region_id: BY.id
220
+ }
221
+
222
+ by = Brewery.create_or_update_from_values( values, more_attribs )
223
+
224
+ by2 = Brewery.find_by_key!( key )
225
+ assert( by.id == by2.id )
226
+
227
+ assert( by.title == values[1] )
228
+ assert( by.country_id == DE.id )
229
+ assert( by.country.title == DE.title )
230
+ assert( by.since == 1589 )
231
+ assert( by.web == 'www.hofbraeu-muenchen.de' )
232
+ assert( by.address == '81829 München // Hofbräuallee 1' )
233
+
234
+ # tood: check auto-created brand
235
+ # todo: check for auto-created city
236
+ assert( by.city.title == 'München' )
237
+
238
+ end
239
+
240
+ end # class TestValues
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beerdb-models
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.0
5
+ platform: ruby
6
+ authors:
7
+ - Gerald Bauer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: worlddb-models
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hoe
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.13'
55
+ description: beerdb-models - beer.db schema 'n' models for easy (re)use
56
+ email: beerdb@googlegroups.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files:
60
+ - HISTORY.md
61
+ - Manifest.txt
62
+ - README.md
63
+ files:
64
+ - ".gemtest"
65
+ - HISTORY.md
66
+ - Manifest.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/beerdb/deleter.rb
70
+ - lib/beerdb/models.rb
71
+ - lib/beerdb/models/beer.rb
72
+ - lib/beerdb/models/brand.rb
73
+ - lib/beerdb/models/brewery.rb
74
+ - lib/beerdb/models/forward.rb
75
+ - lib/beerdb/models/tag.rb
76
+ - lib/beerdb/models/world/city.rb
77
+ - lib/beerdb/models/world/country.rb
78
+ - lib/beerdb/models/world/region.rb
79
+ - lib/beerdb/reader.rb
80
+ - lib/beerdb/reader_file.rb
81
+ - lib/beerdb/reader_zip.rb
82
+ - lib/beerdb/schema.rb
83
+ - lib/beerdb/serializers/beer.rb
84
+ - lib/beerdb/serializers/brewery.rb
85
+ - lib/beerdb/stats.rb
86
+ - lib/beerdb/version.rb
87
+ - test/helper.rb
88
+ - test/test_fixture_matchers.rb
89
+ - test/test_values.rb
90
+ homepage: https://github.com/beerkit/beer.db.models
91
+ licenses:
92
+ - Public Domain
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options:
96
+ - "--main"
97
+ - README.md
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 1.9.2
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: beerdb-models - beer.db schema 'n' models for easy (re)use
116
+ test_files:
117
+ - test/test_values.rb
118
+ - test/test_fixture_matchers.rb