wukong 3.0.0.pre2 → 3.0.0.pre3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/Gemfile +13 -0
  2. data/README.md +182 -6
  3. data/bin/wu-local +13 -5
  4. data/bin/wu-server +1 -1
  5. data/examples/Gemfile +2 -1
  6. data/examples/basic/string_reverser.rb +23 -0
  7. data/examples/{tiny_count.rb → basic/tiny_count.rb} +0 -0
  8. data/examples/{word_count → basic/word_count}/accumulator.rb +0 -0
  9. data/examples/{word_count → basic/word_count}/tokenizer.rb +0 -0
  10. data/examples/{word_count → basic/word_count}/word_count.rb +0 -0
  11. data/examples/deploy_pack/Gemfile +7 -0
  12. data/examples/deploy_pack/README.md +6 -0
  13. data/examples/{text/latinize_text.rb → deploy_pack/a/b/c/.gitkeep} +0 -0
  14. data/examples/deploy_pack/app/processors/string_reverser.rb +5 -0
  15. data/examples/deploy_pack/config/environment.rb +1 -0
  16. data/examples/{dataflow → dsl/dataflow}/fibonacci_series.rb +0 -0
  17. data/examples/dsl/dataflow/scraper_macro_flow.rb +28 -0
  18. data/examples/{dataflow → dsl/dataflow}/simple.rb +0 -0
  19. data/examples/{dataflow → dsl/dataflow}/telegram.rb +0 -0
  20. data/examples/{workflow → dsl/workflow}/cherry_pie.dot +0 -0
  21. data/examples/{workflow → dsl/workflow}/cherry_pie.md +0 -0
  22. data/examples/{workflow → dsl/workflow}/cherry_pie.png +0 -0
  23. data/examples/{workflow → dsl/workflow}/cherry_pie.rb +0 -0
  24. data/examples/empty/.gitkeep +0 -0
  25. data/examples/graph/implied_geolocation/README.md +63 -0
  26. data/examples/graph/{minimum_spanning_tree.rb → minimum_spanning_tree/airfares_graphviz.rb} +0 -0
  27. data/examples/munging/airline_flights/indexable.rb +75 -0
  28. data/examples/munging/airline_flights/indexable_spec.rb +90 -0
  29. data/examples/munging/geo/geonames_models.rb +29 -0
  30. data/examples/munging/wikipedia/dbpedia/dbpedia_common.rb +1 -0
  31. data/examples/munging/wikipedia/dbpedia/extract_links-cruft.rb +66 -0
  32. data/examples/munging/wikipedia/dbpedia/extract_links.rb +213 -146
  33. data/examples/rake_helper.rb +12 -0
  34. data/examples/ruby_project/Gemfile +7 -0
  35. data/examples/ruby_project/README.md +6 -0
  36. data/examples/ruby_project/a/b/c/.gitkeep +0 -0
  37. data/examples/serverlogs/geo_ip_mapping/munge_geolite.rb +82 -0
  38. data/examples/serverlogs/models/logline.rb +102 -0
  39. data/examples/{dataflow/parse_apache_logs.rb → serverlogs/parser/apache_parser_widget.rb} +0 -0
  40. data/examples/serverlogs/visit_paths/common.rb +4 -0
  41. data/examples/serverlogs/visit_paths/page_counts.pig +48 -0
  42. data/examples/serverlogs/visit_paths/serverlogs-01-parse-script.rb +11 -0
  43. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-full.rb +31 -0
  44. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-mapper.rb +12 -0
  45. data/examples/serverlogs/visit_paths/serverlogs-03-breadcrumbs-full.rb +67 -0
  46. data/examples/serverlogs/visit_paths/serverlogs-04-page_page_edges-full.rb +38 -0
  47. data/examples/text/{pig_latin.rb → pig_latin/pig_latinizer.rb} +0 -0
  48. data/examples/{dataflow/pig_latinizer.rb → text/pig_latin/pig_latinizer_widget.rb} +0 -0
  49. data/lib/hanuman/graph.rb +6 -1
  50. data/lib/wu/geo.rb +4 -0
  51. data/lib/wu/geo/geo_grids.numbers +0 -0
  52. data/lib/wu/geo/geolocated.rb +331 -0
  53. data/lib/wu/geo/quadtile.rb +69 -0
  54. data/{examples → lib/wu}/graph/union_find.rb +0 -0
  55. data/lib/wu/model/reconcilable.rb +63 -0
  56. data/{examples/munging/wikipedia/utils/munging_utils.rb → lib/wu/munging.rb} +7 -4
  57. data/lib/wu/social/models/twitter.rb +31 -0
  58. data/{examples/models/wikipedia.rb → lib/wu/wikipedia/models.rb} +0 -0
  59. data/lib/wukong.rb +9 -4
  60. data/lib/wukong/boot.rb +10 -1
  61. data/lib/wukong/driver.rb +65 -71
  62. data/lib/wukong/logger.rb +93 -0
  63. data/lib/wukong/processor.rb +38 -29
  64. data/lib/wukong/runner.rb +144 -0
  65. data/lib/wukong/server.rb +119 -0
  66. data/lib/wukong/spec_helpers.rb +1 -0
  67. data/lib/wukong/spec_helpers/integration_driver.rb +22 -9
  68. data/lib/wukong/spec_helpers/integration_driver_matchers.rb +26 -4
  69. data/lib/wukong/spec_helpers/processor_helpers.rb +4 -10
  70. data/lib/wukong/spec_helpers/shared_examples.rb +12 -13
  71. data/lib/wukong/version.rb +1 -1
  72. data/lib/wukong/widget/processors.rb +13 -0
  73. data/lib/wukong/widget/serializers.rb +55 -65
  74. data/lib/wukong/widgets.rb +0 -2
  75. data/spec/hanuman/graph_spec.rb +14 -0
  76. data/spec/spec_helper.rb +4 -30
  77. data/spec/support/{wukong_test_helpers.rb → example_test_helpers.rb} +29 -2
  78. data/spec/support/integration_helper.rb +38 -0
  79. data/spec/support/model_test_helpers.rb +115 -0
  80. data/spec/wu/geo/geolocated_spec.rb +247 -0
  81. data/spec/wu/model/reconcilable_spec.rb +152 -0
  82. data/spec/wukong/widget/processors_spec.rb +0 -1
  83. data/spec/wukong/widget/serializers_spec.rb +88 -62
  84. data/spec/wukong/wu_local_spec.rb +125 -0
  85. data/wukong.gemspec +3 -16
  86. metadata +72 -266
  87. data/examples/dataflow/apache_log_line.rb +0 -100
  88. data/examples/jabberwocky.txt +0 -36
  89. data/examples/munging/Gemfile +0 -8
  90. data/examples/munging/airline_flights/airline.rb +0 -57
  91. data/examples/munging/airline_flights/airport.rb +0 -211
  92. data/examples/munging/airline_flights/flight.rb +0 -156
  93. data/examples/munging/airline_flights/models.rb +0 -4
  94. data/examples/munging/airline_flights/parse.rb +0 -26
  95. data/examples/munging/airline_flights/route.rb +0 -35
  96. data/examples/munging/airline_flights/timezone_fixup.rb +0 -62
  97. data/examples/munging/airports/40_wbans.txt +0 -40
  98. data/examples/munging/airports/filter_weather_reports.rb +0 -37
  99. data/examples/munging/airports/join.pig +0 -31
  100. data/examples/munging/airports/to_tsv.rb +0 -33
  101. data/examples/munging/airports/usa_wbans.pig +0 -19
  102. data/examples/munging/airports/usa_wbans.txt +0 -2157
  103. data/examples/munging/airports/wbans.pig +0 -19
  104. data/examples/munging/airports/wbans.txt +0 -2310
  105. data/examples/munging/rake_helper.rb +0 -62
  106. data/examples/munging/weather/.gitignore +0 -1
  107. data/examples/munging/weather/Gemfile +0 -4
  108. data/examples/munging/weather/Rakefile +0 -28
  109. data/examples/munging/weather/extract_ish.rb +0 -13
  110. data/examples/munging/weather/models/weather.rb +0 -119
  111. data/examples/munging/weather/utils/noaa_downloader.rb +0 -46
  112. data/examples/munging/wikipedia/README.md +0 -34
  113. data/examples/munging/wikipedia/Rakefile +0 -193
  114. data/examples/munging/wikipedia/n1_subuniverse/n1_nodes.pig +0 -18
  115. data/examples/munging/wikipedia/page_metadata/extract_page_metadata.rb +0 -21
  116. data/examples/munging/wikipedia/page_metadata/extract_page_metadata.rb.old +0 -27
  117. data/examples/munging/wikipedia/pagelinks/augment_pagelinks.pig +0 -29
  118. data/examples/munging/wikipedia/pagelinks/extract_pagelinks.rb +0 -14
  119. data/examples/munging/wikipedia/pagelinks/extract_pagelinks.rb.old +0 -25
  120. data/examples/munging/wikipedia/pagelinks/undirect_pagelinks.pig +0 -29
  121. data/examples/munging/wikipedia/pageviews/augment_pageviews.pig +0 -32
  122. data/examples/munging/wikipedia/pageviews/extract_pageviews.rb +0 -85
  123. data/examples/munging/wikipedia/pig_style_guide.md +0 -25
  124. data/examples/munging/wikipedia/redirects/redirects_page_metadata.pig +0 -19
  125. data/examples/munging/wikipedia/subuniverse/sub_articles.pig +0 -23
  126. data/examples/munging/wikipedia/subuniverse/sub_page_metadata.pig +0 -24
  127. data/examples/munging/wikipedia/subuniverse/sub_pagelinks_from.pig +0 -22
  128. data/examples/munging/wikipedia/subuniverse/sub_pagelinks_into.pig +0 -22
  129. data/examples/munging/wikipedia/subuniverse/sub_pagelinks_within.pig +0 -26
  130. data/examples/munging/wikipedia/subuniverse/sub_pageviews.pig +0 -29
  131. data/examples/munging/wikipedia/subuniverse/sub_undirected_pagelinks_within.pig +0 -24
  132. data/examples/munging/wikipedia/utils/get_namespaces.rb +0 -86
  133. data/examples/munging/wikipedia/utils/namespaces.json +0 -1
  134. data/examples/string_reverser.rb +0 -26
  135. data/examples/twitter/locations.rb +0 -29
  136. data/examples/twitter/models.rb +0 -24
  137. data/examples/twitter/pt1-fiddle.pig +0 -8
  138. data/examples/twitter/pt2-simple_parse.pig +0 -31
  139. data/examples/twitter/pt2-simple_parse.rb +0 -18
  140. data/examples/twitter/pt3-join_on_zips.pig +0 -39
  141. data/examples/twitter/pt4-strong_links.rb +0 -20
  142. data/examples/twitter/pt5-lnglat_and_strong_links.pig +0 -16
  143. data/examples/twitter/states.tsv +0 -50
  144. data/examples/workflow/package_gem.rb +0 -55
  145. data/lib/wukong/widget/sink.rb +0 -16
  146. data/lib/wukong/widget/source.rb +0 -14
@@ -0,0 +1,247 @@
1
+ require 'gorillib/data_munging'
2
+ require 'wu/geo/geolocated'
3
+
4
+ describe Wukong::Geolocated do
5
+ let(:aus_lng){ -97.759003 } # Austin, TX -- infochimps HQ
6
+ let(:aus_lat){ 30.273884 }
7
+ let(:sat_lng){ -98.486123 } # San Antonio, TX
8
+ let(:sat_lat){ 29.42575 }
9
+ let(:dpi){ 72 }
10
+ #
11
+ let(:aus_tile_x_3){ 1.82758 } # zoom level 3
12
+ let(:aus_tile_y_3){ 3.29356 }
13
+ let(:aus_pixel_x_3){ 468 }
14
+ let(:aus_pixel_y_3){ 843 }
15
+ #
16
+ let(:aus_tile_x_8){ 58.48248675555555 } # zoom level 8
17
+ let(:aus_tile_y_8){ 105.39405073699557 }
18
+ let(:aus_tile_x_11){ 467 } # zoom level 11
19
+ let(:aus_tile_y_11){ 843 }
20
+ #
21
+ let(:aus_quadkey ){ "0231301203311211" }
22
+ let(:aus_quadkey_3){ "023" }
23
+ let(:radius){ 1_000_000 } # 1,000 km
24
+
25
+ context Wukong::Geolocated::ByCoordinates do
26
+ let(:point_klass) do
27
+ module Wukong
28
+ class TestPoint
29
+ include Gorillib::Model
30
+ include Wukong::Geolocated::ByCoordinates
31
+ field :name, String, position: 0, doc: "Name of this location"
32
+ field :longitude, Float, position: 1, doc: "Longitude (X) of a point, in decimal degrees"
33
+ field :latitude, Float, position: 2, doc: "Latitude (Y) of a point, in decimal degrees"
34
+ end
35
+ end
36
+ Wukong::TestPoint
37
+ end
38
+ subject{ point_klass.new("Infochimps HQ", aus_lng, aus_lat) }
39
+
40
+ context '#tile_xf' do
41
+ it "tile X coordinate, as a float" do
42
+ subject.tile_xf(3).should be_within(0.0001).of( 1.82758)
43
+ subject.tile_xf(8).should be_within(0.0001).of(58.48248)
44
+ subject.tile_xf(11).should be_within(0.0001).of(467.8598)
45
+ end
46
+ end
47
+ context '#tile_x' do
48
+ it "tile X coordinate, as an integer" do
49
+ subject.tile_x(3).should == 1
50
+ subject.tile_x(8).should == 58
51
+ subject.tile_x(11).should == 467
52
+ end
53
+ end
54
+ context '#tile_yf' do
55
+ it "tile Y coordinate, as a float" do
56
+ subject.tile_yf(3).should be_within(0.0001).of( 3.29356)
57
+ subject.tile_yf(8).should be_within(0.0001).of(105.394051)
58
+ subject.tile_yf(11).should be_within(0.0001).of(843.152406)
59
+ end
60
+ end
61
+ context '#tile_x' do
62
+ it "tile Y coordinate, as an integer" do
63
+ subject.tile_y(3).should == 3
64
+ subject.tile_y(8).should == 105
65
+ subject.tile_y(11).should == 843
66
+ end
67
+ end
68
+ context '#quadkey' do
69
+ it "a string of 2-bit tile selectors" do
70
+ subject.quadkey(3).should == "023"
71
+ subject.quadkey(16).should == "0231301203311211"
72
+ end
73
+ end
74
+ end
75
+
76
+ context Wukong::Geolocated do
77
+
78
+ it "gives private methods on including class as well as the methods on itself" do
79
+ klass = Class.new{ include Wukong::Geolocated }
80
+ klass.should be_private_method_defined(:lng_lat_zl_to_tile_xy)
81
+ klass.should be_private_method_defined(:haversine_distance)
82
+ end
83
+
84
+ #
85
+ # Tile coordinates
86
+ #
87
+
88
+ it "returns a map tile size given a zoom level" do
89
+ Wukong::Geolocated.map_tile_size(3).should == 8
90
+ end
91
+
92
+ it "returns a tile_x, tile_y pair given a longitude, latitude and zoom level" do
93
+ Wukong::Geolocated.lng_lat_zl_to_tile_xy(aus_lng, aus_lat, 8).should == [ 58, 105]
94
+ Wukong::Geolocated.lng_lat_zl_to_tile_xy(aus_lng, aus_lat, 11).should == [467, 843]
95
+ end
96
+
97
+ it "returns a longitude, latitude pair given tile_x, tile_y and zoom level" do
98
+ lng, lat = Wukong::Geolocated.tile_xy_zl_to_lng_lat(aus_tile_x_8, aus_tile_y_8, 8)
99
+ lng.should be_within(0.0001).of(aus_lng)
100
+ lat.should be_within(0.0001).of(aus_lat)
101
+ end
102
+
103
+ #
104
+ # Pixel coordinates
105
+ #
106
+
107
+ it "returns a map pizel size given a zoom level" do
108
+ Wukong::Geolocated.map_pixel_size(3).should == 2048
109
+ end
110
+
111
+ it "returns a pixel_x, pixel_y pair given a longitude, latitude and zoom level" do
112
+ Wukong::Geolocated.lng_lat_zl_to_pixel_xy(aus_lng, aus_lat, 3).should == [468, 843]
113
+ end
114
+
115
+ it "returns a longitude, latitude pair given pixel_x, pixel_y and zoom level" do
116
+ lng, lat = Wukong::Geolocated.pixel_xy_zl_to_lng_lat(aus_pixel_x_3, aus_pixel_y_3, 3)
117
+ lat.round(4).should == 30.2970
118
+ lng.round(4).should == -97.7344
119
+ end
120
+
121
+ it "returns a tile x-y pair given a pixel x-y pair" do
122
+ Wukong::Geolocated.pixel_xy_to_tile_xy(aus_pixel_x_3, aus_pixel_y_3).should == [1,3]
123
+ end
124
+
125
+ it "returns a pixel x-y pair given a float tile x-y pair" do
126
+ Wukong::Geolocated.tile_xy_to_pixel_xy(aus_tile_x_3, aus_tile_y_3 ).should == [467.86048, 843.15136]
127
+ end
128
+
129
+ it "returns a pixel x-y pair given an integer tile x-y pair" do
130
+ Wukong::Geolocated.tile_xy_to_pixel_xy(aus_tile_x_3.to_i, aus_tile_y_3.to_i).should == [256, 768]
131
+ end
132
+
133
+ #
134
+ # Quadkey coordinates
135
+ #
136
+
137
+ it "returns a quadkey given a tile x-y pair and a zoom level" do
138
+ Wukong::Geolocated.tile_xy_zl_to_quadkey(aus_tile_x_3, aus_tile_y_3, 3).should == "023"
139
+ Wukong::Geolocated.tile_xy_zl_to_quadkey(aus_tile_x_8, aus_tile_y_8, 8).should == "02313012"
140
+ Wukong::Geolocated.tile_xy_zl_to_quadkey(aus_tile_x_11, aus_tile_y_11,11).should == "02313012033"
141
+ end
142
+
143
+ it "returns a quadkey given a longitude, latitude and a zoom level" do
144
+ Wukong::Geolocated.lng_lat_zl_to_quadkey(aus_lng, aus_lat, 3).should == "023"
145
+ Wukong::Geolocated.lng_lat_zl_to_quadkey(aus_lng, aus_lat, 8).should == "02313012"
146
+ Wukong::Geolocated.lng_lat_zl_to_quadkey(aus_lng, aus_lat, 11).should == "02313012033"
147
+ Wukong::Geolocated.lng_lat_zl_to_quadkey(aus_lng, aus_lat, 16).should == "0231301203311211"
148
+ end
149
+
150
+ it "returns a packed quadkey (an integer) given a tile xy and zoom level" do
151
+ Wukong::Geolocated.tile_xy_zl_to_packed_qk(aus_tile_x_3.floor, aus_tile_y_3.floor, 3).should == "023".to_i(4)
152
+ Wukong::Geolocated.tile_xy_zl_to_packed_qk(aus_tile_x_8.floor, aus_tile_y_8.floor, 8).should == "02313012".to_i(4)
153
+ Wukong::Geolocated.tile_xy_zl_to_packed_qk(aus_tile_x_11.floor, aus_tile_y_11.floor,11).should == "02313012033".to_i(4)
154
+ end
155
+
156
+ context '.packed_qk_zl_to_tile_xy' do
157
+ let(:packed_qk){ "0231301203311211".to_i(4) }
158
+ it "returns a tile xy given a packed quadkey (integer)" do
159
+ Wukong::Geolocated.packed_qk_zl_to_tile_xy(packed_qk >> 26, 3).should == [ 1, 3, 3]
160
+ Wukong::Geolocated.packed_qk_zl_to_tile_xy(packed_qk >> 16, 8).should == [ 58, 105, 8]
161
+ Wukong::Geolocated.packed_qk_zl_to_tile_xy(packed_qk >> 10, 11).should == [467, 843, 11]
162
+ end
163
+
164
+ it "defaults to zl=16 for packed quadkeys" do
165
+ Wukong::Geolocated.packed_qk_zl_to_tile_xy(packed_qk ).should == [14971, 26980, 16]
166
+ Wukong::Geolocated.packed_qk_zl_to_tile_xy(packed_qk, 16).should == [14971, 26980, 16]
167
+ end
168
+ end
169
+
170
+ it "returns tile x-y pair and a zoom level given a quadkey" do
171
+ Wukong::Geolocated.quadkey_to_tile_xy_zl(aus_quadkey[0..2] ).should == [1, 3, 3]
172
+ Wukong::Geolocated.quadkey_to_tile_xy_zl(aus_quadkey[0..7] ).should == [aus_tile_x_8.floor, aus_tile_y_8.floor, 8]
173
+ Wukong::Geolocated.quadkey_to_tile_xy_zl(aus_quadkey[0..10]).should == [aus_tile_x_11.floor, aus_tile_y_11.floor, 11]
174
+ end
175
+
176
+ it "allows '' to be a quadkey (whole map)" do
177
+ Wukong::Geolocated.quadkey_to_tile_xy_zl("").should == [0, 0, 0]
178
+ end
179
+
180
+ it "maps tile xyz [0,0,0] to quadkey ''" do
181
+ Wukong::Geolocated.tile_xy_zl_to_quadkey(0,0,0).should == ""
182
+ end
183
+
184
+ it "throws an error if a bad quadkey is given" do
185
+ expect{ Wukong::Geolocated.quadkey_to_tile_xy_zl("bad_key") }.to raise_error(ArgumentError, /Quadkey.*characters/)
186
+ end
187
+
188
+ it "returns a bounding box given a quadkey" do
189
+ left, btm, right, top = Wukong::Geolocated.quadkey_to_bbox(aus_quadkey_3)
190
+ left.should be_within(0.0001).of(-135.0)
191
+ right.should be_within(0.0001).of(- 90.0)
192
+ btm.should be_within(0.0001).of( 0.0)
193
+ top.should be_within(0.0001).of( 40.9799)
194
+ end
195
+
196
+ it "returns the smallest quadkey containing two points" do
197
+ Wukong::Geolocated.quadkey_containing_bbox(aus_lng, aus_lat, sat_lng, sat_lat).should == "023130"
198
+ end
199
+
200
+ it "returns a bounding box given a point and radius" do
201
+ left, btm, right, top = Wukong::Geolocated.lng_lat_rad_to_bbox(aus_lng, aus_lat, radius)
202
+
203
+ left.should be_within(0.0001).of(-108.1723)
204
+ right.should be_within(0.0001).of(- 87.3457)
205
+ btm.should be_within(0.0001).of( 21.2807)
206
+ top.should be_within(0.0001).of( 39.2671)
207
+ end
208
+
209
+ it "returns a centroid given a bounding box" do
210
+ mid_lng, mid_lat = Wukong::Geolocated.bbox_centroid([aus_lng, sat_lat], [sat_lng, aus_lat])
211
+ mid_lng.should be_within(0.0001).of(-98.1241)
212
+ mid_lat.should be_within(0.0001).of( 29.8503)
213
+ end
214
+
215
+ it "returns a pixel resolution given a latitude and zoom level" do
216
+ Wukong::Geolocated.pixel_resolution(aus_lat, 3).should be_within(0.0001).of(16880.4081)
217
+ end
218
+
219
+ it "returns a map scale given a latitude, zoom level and dpi" do
220
+ Wukong::Geolocated.map_scale_for_dpi(aus_lat, 3, dpi).should be_within(0.0001).of(47849975.8302)
221
+ end
222
+
223
+ it "calculates the haversine distance between two points" do
224
+ Wukong::Geolocated.haversine_distance(aus_lng, aus_lat, sat_lng, sat_lat).should be_within(0.0001).of(117522.1219)
225
+ end
226
+
227
+ it "calculates the haversine midpoint between two points" do
228
+ lng, lat = Wukong::Geolocated.haversine_midpoint(aus_lng, sat_lat, sat_lng, aus_lat)
229
+ lng.should be_within(0.0001).of(-98.1241)
230
+ lat.should be_within(0.0001).of( 29.8503)
231
+ end
232
+
233
+ it "calculates the point a given distance directly north from a lat/lng" do
234
+ lng, lat = Wukong::Geolocated.point_north(aus_lng, aus_lat, 1000000)
235
+ lng.should be_within(0.0001).of(-97.7590)
236
+ lat.should be_within(0.0001).of( 39.2671)
237
+ end
238
+
239
+ it "calculates the point a given distance directly east from a lat/lng" do
240
+ lng, lat = Wukong::Geolocated.point_east(aus_lng, aus_lat, 1000000)
241
+ lng.should be_within(0.0001).of(-87.3457)
242
+ lat.should be_within(0.0001).of( 30.2739)
243
+ end
244
+
245
+
246
+ end # module methods
247
+ end
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+ require 'gorillib/model'
3
+ require 'wu/model/reconcilable'
4
+ require 'support/model_test_helpers'
5
+
6
+ describe Gorillib::Model::Reconcilable, :model_spec do
7
+
8
+ before do
9
+ smurfhouse_class.class_eval do
10
+ include Gorillib::Model::Reconcilable
11
+ end
12
+ smurf_class.class_eval do
13
+ include Gorillib::Model::Reconcilable
14
+ field :cromulence, Integer
15
+ field :smurfhouse, Gorillib::Test::Smurfhouse
16
+ end
17
+ end
18
+ let(:empty_smurf){ smurf_class.new(name: nil) }
19
+ let(:angry_smurf){ smurf_class.new(name: 'Angry Smurf', smurfiness: 20, weapon: :smurfchucks) }
20
+ let(:handy_smurf){ smurf_class.new(name: 'Handy Smurf', smurfiness: 20, weapon: :monkeysmurf) }
21
+ subject{ smurf_class.new(name: 'Handy Smurf', smurfiness: 20, weapon: :monkeysmurf) }
22
+
23
+ context '#reconcilable_attributes' do
24
+ it 'returns only unset, non-nil values' do
25
+ subject.weapon = nil
26
+ subject.reconcilable_attributes.should == { name: 'Handy Smurf', smurfiness: 20 }
27
+ subject.compact_attributes.should == { name: 'Handy Smurf', smurfiness: 20, weapon: nil }
28
+ empty_smurf.reconcilable_attributes.should == {}
29
+ end
30
+ end
31
+
32
+ context '#conflicting_attribute!' do
33
+ it 'warns by default' do
34
+ stdout, stderr = capture_output{ subject.conflicting_attribute!(:weapon, :monkeysmurf, :smurfwrench) }
35
+ stderr.string.should =~ /conflicting.* weapon: had :monkeysmurf got :smurfwrench\b/
36
+ end
37
+ it 'returns false always by default' do
38
+ subject.stub(:warn)
39
+ subject.conflicting_attribute!(:weapon, :monkeysmurf, :smurfwrench).should == false
40
+ end
41
+ end
42
+
43
+ context '#adopt' do
44
+ it 'returns true on compatible, false on incompatible' do
45
+ subject.stub(:warn)
46
+ subject.adopt(empty_smurf).should be_true
47
+ subject.adopt(angry_smurf).should be_false
48
+ end
49
+
50
+ it 'calls adopt_foo instead of adopt_attribute(:foo, ...) if present' do
51
+ subject.should_receive(:adopt_weapon).with(:smurfchucks, angry_smurf).and_return true
52
+ subject.should_receive(:adopt_name ).with('Angry Smurf', angry_smurf).and_return true
53
+ subject.adopt(angry_smurf).should be_true
54
+ end
55
+
56
+ context 'on compatible objects' do
57
+ before{ subject.should_not_receive(:conflicting_attribute!) }
58
+ it 'makes no changes from unset or nil values' do
59
+ subject.adopt(empty_smurf).should be_true
60
+ subject.should == handy_smurf
61
+ end
62
+ it 'makes no changes from equal values' do
63
+ subject.adopt(handy_smurf).should be_true
64
+ subject.should == handy_smurf
65
+ end
66
+ it 'sets unset attributes to the other value' do
67
+ handy_smurf.cromulence = 99
68
+ subject.attribute_set?(:cromulence).should be_false
69
+ subject.adopt(handy_smurf).should be_true
70
+ subject.should == handy_smurf
71
+ subject.cromulence.should == 99
72
+ end
73
+ it 'sets nil attributes to the other value' do
74
+ handy_smurf.cromulence = 99
75
+ subject.cromulence = nil
76
+ subject.attribute_set?(:cromulence).should be_true
77
+ subject.adopt(handy_smurf).should be_true
78
+ subject.should == handy_smurf
79
+ subject.cromulence.should == 99
80
+ end
81
+ it 'asks adoptable attribute to adopt other value' do
82
+ handy_smurf.smurfhouse = smurfhouse_class.new(color: :brown)
83
+ sh = subject.smurfhouse = smurfhouse_class.new(shape: :mushroom)
84
+ subject.adopt(handy_smurf).should be_true
85
+ subject.smurfhouse.attributes.should == { shape: :mushroom, color: :brown }
86
+ subject.smurfhouse.should equal(sh)
87
+ end
88
+ it 'does not call block' do
89
+ subject.adopt(empty_smurf){ raise 'should not call block' }
90
+ end
91
+ end
92
+
93
+ context 'on incompatible objects' do
94
+ it 'returns the value of conflicting_attribute!' do
95
+ subject.weapon = :smurfwrench
96
+ subject.should_receive(:conflicting_attribute!).with(:weapon, :smurfwrench, :monkeysmurf).and_return(false)
97
+ subject.adopt(handy_smurf).should be_false
98
+ end
99
+ it 'returns the value of conflicting_attribute!' do
100
+ subject.weapon = :smurfwrench
101
+ subject.should_receive(:conflicting_attribute!).with(:weapon, :smurfwrench, :monkeysmurf).and_return(true)
102
+ subject.adopt(handy_smurf).should be_true
103
+ end
104
+ it 'keeps its own value' do
105
+ subject.stub(:conflicting_attribute!)
106
+ subject.weapon = :smurfwrench
107
+ subject.adopt(handy_smurf)
108
+ subject.weapon.should == :smurfwrench
109
+ end
110
+ it 'adoptible attributes reconcile' do
111
+ handy_smurf.smurfhouse = smurfhouse_class.new(color: :brown)
112
+ sh = subject.smurfhouse = smurfhouse_class.new(shape: :mushroom, color: :red)
113
+ sh.should_receive(:conflicting_attribute!).with(:color, :red, :brown)
114
+ #
115
+ subject.adopt(handy_smurf).should be_false
116
+ subject.smurfhouse.attributes.should == { shape: :mushroom, color: :red }
117
+ subject.smurfhouse.should equal(sh)
118
+ end
119
+ it 'adoptible attributes reconcile and warn' do
120
+ subject.should_not_receive(:conflicting_attribute!)
121
+ handy_smurf.smurfhouse = smurfhouse_class.new(color: :brown)
122
+ sh = subject.smurfhouse = smurfhouse_class.new(shape: :mushroom, color: :red)
123
+ sh.should_receive(:conflicting_attribute!).with(:color, :red, :brown)
124
+ #
125
+ subject.adopt(handy_smurf)
126
+ end
127
+ it 'does not take a block' do
128
+ subject.stub(:warn)
129
+ subject.adopt(angry_smurf){ raise 'should not call block' }
130
+ end
131
+ end
132
+ end
133
+
134
+ context '#adopt_attribute' do
135
+ context 'takse a block (useful in overrides):' do
136
+ it 'on compatible values, block is not called' do
137
+ subject.should_not_receive(:conflicting_attribute!)
138
+ subject.send(:adopt_attribute, :weapon, :monkeysmurf) do
139
+ raise 'should not call block'
140
+ end
141
+ end
142
+ it 'on incompatible values, block is called instead of conflicting_attribute!' do
143
+ subject.should_not_receive(:conflicting_attribute!)
144
+ subject.send(:adopt_attribute, :weapon, :smurfwrench) do
145
+ mock_val
146
+ end.should == mock_val
147
+ end
148
+ end
149
+ end
150
+
151
+
152
+ end
@@ -27,7 +27,6 @@ describe Wukong::Processor do
27
27
  end
28
28
  end
29
29
 
30
-
31
30
  context :extract do
32
31
  subject { processor(:extract) }
33
32
 
@@ -1,92 +1,118 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "Serializing" do
4
-
5
- context :to_json do
6
-
7
- let(:emittable) { {"hi" => "there"} }
8
- let(:not_emittable) { {"n" => Float::INFINITY} }
3
+ shared_context 'serializers', serializer: true do
4
+ let(:bad_record){ nil }
5
+ let(:serializer){ create_processor(self.class.top_level_description, on_error: :skip) }
6
+ end
9
7
 
10
- it_behaves_like 'a processor', :named => :to_json
11
-
12
- it "should handle valid records" do
13
- processor.given(emittable).should emit_json(emittable)
14
- end
8
+ shared_examples_for 'a serializer' do
9
+ it_behaves_like 'a processor'
10
+ end
15
11
 
16
- it "should skip bad records" do
17
- processor.given(not_emittable).should emit(0).records
18
- end
19
-
12
+ shared_examples_for 'a serializer that complains on bad recors', :handles_errors => true do
13
+ it 'handles errors on bad records' do
14
+ serializer.should_receive(:handle_error).with(bad_record, a_kind_of(Exception)).and_return(nil)
15
+ serializer.given(bad_record).should emit(0).records
20
16
  end
17
+ end
21
18
 
22
- context :to_tsv do
23
- let(:emittable) { ["foo", 2, :a] }
24
- let(:not_emittable) { nil }
19
+ describe :to_json, serializer: true do
20
+ let(:valid_record) { { hi: 'there' } }
21
+ it 'serializes valid records' do
22
+ serializer.given(valid_record).should emit('{"hi":"there"}')
23
+ end
25
24
 
26
- it_behaves_like 'a processor', :named => :to_tsv
25
+ it 'handles errors on bad records' do
26
+ MultiJson.should_receive(:dump).with(:unserializable_record, kind_of(Hash)).and_raise(StandardError)
27
+ serializer.should_receive(:handle_error).with(:unserializable_record, a_kind_of(Exception)).and_return(nil)
28
+ serializer.given(:unserializable_record).should emit(0).records
29
+ end
30
+
31
+ context 'pretty' do
32
+ let(:serializer){ create_processor(:to_json, pretty: true) }
27
33
 
28
- it "should handle valid records" do
29
- processor.given(emittable).should emit_tsv(emittable.map(&:to_s))
30
- end
34
+ it 'prettifies valid records' do
35
+ serializer.given(valid_record).output.first.should include("\n")
36
+ end
37
+ end
38
+
39
+ context 'given a model' do
40
+ let(:json_record) { '{"foo":"bar"}' }
41
+ let(:valid_model) { double('model', to_json: json_record) }
31
42
 
32
- it "should skip bad records" do
33
- processor.given(not_emittable).should emit(0).records
43
+ it 'defers to the model to serialize' do
44
+ valid_model.should_receive(:to_json).and_return(json_record)
45
+ serializer.given(valid_model).should emit(json_record)
34
46
  end
35
47
  end
36
48
  end
37
49
 
38
- describe "Deserializing" do
39
-
40
- context :from_json do
41
- let(:parseable) { '{"hi": "there"}' }
42
- let(:not_parseable) { '{"832323:' }
43
-
44
- it_behaves_like 'a processor', :named => :from_json
50
+ describe :to_tsv, serializer: true, handles_errors: true do
51
+ let(:valid_record) { ["foo", 2, :a] }
52
+
53
+ it 'serializes valid records' do
54
+ serializer.given(valid_record).should emit("foo\t2\ta")
55
+ end
45
56
 
46
- it "should handle valid records" do
47
- processor.given(parseable).should emit({'hi' => 'there'})
48
- end
57
+ context 'given a model' do
58
+ let(:tsv_record) { "foo\tbar\tbaz" }
59
+ let(:valid_model) { double('model', to_tsv: tsv_record) }
49
60
 
50
- it "should skip bad records" do
51
- processor.given(not_parseable).should emit(0).records
61
+ it 'defers to the model to serialize' do
62
+ valid_model.should_receive(:to_tsv).and_return(tsv_record)
63
+ serializer.given(valid_model).should emit(tsv_record)
52
64
  end
53
65
  end
54
-
55
- context :from_tsv do
66
+ end
56
67
 
57
- let(:parseable) { "foo\t2\ta" }
58
- let(:not_parseable) { nil }
68
+ describe :from_json, serializer: true, handles_errors: true do
69
+ let(:valid_record) { '{"hi": "there"}' }
70
+ let(:bad_record) { '{"832323:' }
71
+
72
+ it 'deserializes valid records' do
73
+ serializer.given(valid_record).should emit({'hi' => 'there'})
74
+ end
59
75
 
60
- it_behaves_like 'a processor', :named => :from_tsv
76
+ context 'given a model' do
77
+ let(:wire_format) { { foo: 'bar' } }
78
+ let(:valid_model) { double('model', from_json: wire_format) }
61
79
 
62
- it "should handle valid records" do
63
- processor.given(parseable).should emit(parseable.split("\t"))
64
- end
65
-
66
- it "should skip bad records" do
67
- processor.given(not_parseable).should emit(0).records
80
+ it 'defers to the model to serialize' do
81
+ valid_model.should_receive(:from_json).and_return(wire_format)
82
+ serializer.given(valid_model).should emit(wire_format)
68
83
  end
69
84
  end
70
85
  end
71
86
 
72
- describe "Pretty printing" do
73
-
74
- context "JSON" do
75
- let(:parseable) { '{"hi": "there"}' }
76
- let(:not_parseable) { '{"832323:' }
77
-
78
- it_behaves_like 'a processor', :named => :pretty
79
-
80
- it "should prettify parseable records" do
81
- processor(:pretty).given(parseable).should emit_json({'hi' => 'there'})
82
- end
87
+ describe :from_tsv, serializer: true, handles_errors: true do
88
+ let(:valid_record) { "foo\t2\ta" }
89
+
90
+ it 'deserializes valid records' do
91
+ serializer.given(valid_record).should emit(['foo', '2', 'a' ])
92
+ end
83
93
 
84
- it "should pass on non parseable records" do
85
- processor(:pretty).given(not_parseable).should emit(not_parseable)
94
+ context 'given a model' do
95
+ let(:wire_format) { { foo: 'bar' } }
96
+ let(:valid_model) { double('model', from_tsv: wire_format) }
97
+
98
+ it 'defers to the model to serialize' do
99
+ valid_model.should_receive(:from_tsv).and_return(wire_format)
100
+ serializer.given(valid_model).should emit(wire_format)
86
101
  end
87
102
  end
103
+ end
104
+
105
+ describe :to_inspect do
106
+ it_behaves_like 'a processor'
107
+ end
108
+
109
+ describe :recordize, serializer: true, handles_errors: true do
110
+ let(:model_instance) { double('model') }
111
+ let(:model_klass) { double('model_def', receive: model_instance) }
112
+ let(:serializer) { create_processor(:recordize, model: model_klass, on_error: :skip) }
113
+ let(:valid_record) { { foo: 'bar' } }
88
114
 
89
- it "should pass on everything else" do
90
- processor(:pretty).given('foobar').should emit('foobar')
115
+ it 'recordizes valid records' do
116
+ serializer.given(valid_record).should emit(model_instance)
91
117
  end
92
118
  end