tequila 0.2.1

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,3 @@
1
+ +pets
2
+ :only
3
+ .id
@@ -0,0 +1,392 @@
1
+ require 'test/unit'
2
+ require 'test_helper'
3
+ require 'json'
4
+
5
+ require File.dirname(__FILE__) + '/../lib/preprocessor'
6
+
7
+ class Tequila::Config::Default
8
+ show_initial_label!
9
+ end
10
+
11
+ class TestTequila < Test::Unit::TestCase
12
+ DIR = "data/"
13
+
14
+ def run_test(jazz, json, init = 'humans = Human.all :order => "name"', explicit = false)
15
+ parser = TequilaParser.new
16
+ eval(init)
17
+ jazz = TequilaPreprocessor.run(jazz)
18
+ parsed_template = parser.parse(jazz)
19
+ evaluated_template = parsed_template.eval(binding)
20
+ json2 = evaluated_template.build_hash
21
+ return json2 if explicit
22
+ assert_json_equal(json, json2.to_json, name)
23
+ end
24
+
25
+ def assert_json_equal(expected, actual, message=nil)
26
+ expected = JSON.parse(expected)
27
+ actual = JSON.parse(actual)
28
+ full_message = build_message(message, "<?> expected but was\n<?>.\n", expected.inspect, actual.inspect)
29
+ assert_block(full_message) { expected == actual }
30
+ end
31
+
32
+ def test_big_request
33
+ jazz = <<END
34
+ -eugene
35
+ :except
36
+ .id
37
+ :code => where
38
+ 'I am from ' + address
39
+ +pets
40
+ :except
41
+ .id
42
+ .human_id
43
+ .pet_type_id
44
+ :methods
45
+ .which('wet', 'cold')
46
+ +toys
47
+ :only
48
+ .label
49
+ <pet_type
50
+ :only
51
+ .class_name => pet_type
52
+ END
53
+ json2 = run_test jazz, nil, 'eugene = Human.first', true
54
+ eugene = json2['eugene']
55
+ pets = eugene['pets']
56
+ pet = pets[0]['pet']
57
+ assert_equal 'Eugene', eugene['name']
58
+ assert_equal 'district3', eugene['address']
59
+ assert_equal 'I am from district3', eugene['where']
60
+ assert_equal 1, pets.size
61
+ assert_equal 'Poiyo', pet['name']
62
+ assert_equal 'Poiyo is wet and cold', pet['which']
63
+ assert_equal [ { 'toy' => { 'label' => 'Humanity' } }, { 'toy' => { 'label' => 'LHC' } } ], pet['toys'].sort { |x, y|
64
+ x['toy']['label'] <=> y['toy']['label']
65
+ }
66
+ end
67
+
68
+ def test_only
69
+ jazz = <<END
70
+ -humans
71
+ :only
72
+ .name
73
+ END
74
+ json = '{ "humans" : [ { "human" : {"name":"Alex"} },{ "human" : {"name":"Eugene"} }, { "human" : {"name":"Ivan"} }, { "human" : {"name":"Oleg"} }] }'
75
+ run_test jazz, json
76
+ end
77
+
78
+ def test_except
79
+ jazz = <<END
80
+ -humans
81
+ :except
82
+ .address
83
+ .id
84
+ END
85
+ json = '{ "humans" : [ { "human" : {"name":"Alex"} },{ "human" : {"name":"Eugene"} }, { "human" : {"name":"Ivan"} }, { "human" : {"name":"Oleg"} }] }'
86
+ run_test jazz, json
87
+ end
88
+ def test_methods
89
+ jazz = <<END
90
+ -scooby
91
+ :except
92
+ .id
93
+ .human_id
94
+ .pet_type_id
95
+ :methods
96
+ .which
97
+ END
98
+ json = '{ "scooby" : { "which" : "Skooby Doo is big and cheerful", "name" : "Skooby Doo"}}'
99
+ run_test(jazz, json, 'scooby = Pet.find_by_name "Skooby Doo"')
100
+ end
101
+ def test_methods_with_params
102
+ jazz = <<END
103
+ -mat
104
+ :except
105
+ .id
106
+ .human_id
107
+ .pet_type_id
108
+ :methods
109
+ .which('striped','reasonable')
110
+ END
111
+ json = '{ "mat" : { "which" : "Matroskin is striped and reasonable", "name" : "Matroskin"}}'
112
+ run_test(jazz, json, 'mat = Pet.find_by_name "Matroskin"')
113
+ end
114
+ def test_association
115
+ jazz = <<END
116
+ -eugene
117
+ :only
118
+ .name
119
+ +pets
120
+ :only
121
+ .name
122
+ END
123
+ json = '{ "eugene" : { "name" : "Eugene", "pets" : [ { "pet" : { "name" : "Poiyo"}} ] }}'
124
+ run_test(jazz, json, 'eugene = Human.find_by_name "Eugene"')
125
+ end
126
+ def test_rename
127
+ jazz = <<END
128
+ -humans => people
129
+ :only
130
+ .name => login
131
+ END
132
+ json = '{ "people" : [ { "person" : {"login":"Alex"} },{ "person" : {"login":"Eugene"} }, { "person" : {"login":"Ivan"} }, { "person" : {"login":"Oleg"} }] }'
133
+ run_test jazz, json
134
+ end
135
+ def test_code
136
+ jazz = <<END
137
+ -humans => people
138
+ :except
139
+ .name
140
+ .id
141
+ .address
142
+ :code => hello
143
+ "Hello, " + name + "!"
144
+ END
145
+ json = '{ "people" : [ { "person" : {"hello":"Hello, Alex!"} }, { "person" : {"hello":"Hello, Eugene!"} }] }'
146
+ run_test jazz, json, 'humans = Human.all :conditions => "name in (\"Eugene\", \"Alex\")", :order => "name"'
147
+ end
148
+ def test_inner_association
149
+ jazz = <<END
150
+ -ivan
151
+ :except
152
+ .id
153
+ .address
154
+ +pets
155
+ :only
156
+ .name
157
+ +pet_type
158
+ :only
159
+ .class_name => whois
160
+ END
161
+ json = '{
162
+ "ivan" :
163
+ { "name" : "Ivan",
164
+ "pets" : [
165
+ { "pet" :
166
+ { "name" : "Skooby Doo" ,
167
+ "pet_type":
168
+ { "whois" : "dog" } } } ] } }'
169
+ run_test jazz, json, 'ivan = Human.find_by_name "Ivan"'
170
+ end
171
+ def test_gluing
172
+ jazz = <<END
173
+ -ivan
174
+ :except
175
+ .id
176
+ .address
177
+ +pets
178
+ :only
179
+ .name
180
+ <pet_type
181
+ :only
182
+ .class_name => whois
183
+ END
184
+ json = '{
185
+ "ivan" :
186
+ { "name" : "Ivan",
187
+ "pets" : [
188
+ { "pet" :
189
+ { "name" : "Skooby Doo" ,
190
+ "whois" : "dog" } } ] } }'
191
+ run_test jazz, json, 'ivan = Human.find_by_name "Ivan"'
192
+ end
193
+ #### include files ####
194
+ def test_include_symbol
195
+ jazz = <<END
196
+ -human
197
+ :only
198
+ .name
199
+ &test/pets
200
+ END
201
+ jazz_for_treetop = <<END
202
+ -human
203
+ :only
204
+ .name
205
+ +pets
206
+ :only
207
+ .id
208
+ end
209
+ end
210
+ END
211
+ assert_equal TequilaPreprocessor.run(jazz), jazz_for_treetop
212
+ end
213
+
214
+ def test_include_keyword
215
+ jazz = <<END
216
+ -human
217
+ :only
218
+ .name
219
+ include test/pets
220
+ END
221
+ jazz_for_treetop = <<END
222
+ -human
223
+ :only
224
+ .name
225
+ +pets
226
+ :only
227
+ .id
228
+ end
229
+ end
230
+ END
231
+ assert_equal TequilaPreprocessor.run(jazz), jazz_for_treetop
232
+ end
233
+
234
+
235
+ def test_label_building
236
+ jazz =<<END
237
+ -@eugene
238
+ :only
239
+ .name
240
+ END
241
+ json = %q{{"eugene" : { "name" : "Eugene" }}}
242
+ run_test jazz, json, '@eugene = Human.find_by_name "Eugene"'
243
+ end
244
+
245
+ def test_has_many_empty_associan
246
+ jazz =<<END
247
+ -josh
248
+ :only
249
+ .name
250
+ +pets
251
+ :only
252
+ .name
253
+ END
254
+ @josh = Human.create({:name => 'Josh'})
255
+ json = %q{{"josh" : { "name" : "Josh", "pets" : [] }}}
256
+ run_test jazz, json, 'josh = Human.find_by_name "Josh"'
257
+ @josh.destroy
258
+ end
259
+
260
+ def test_nil_association
261
+ jazz =<<END
262
+ -pet
263
+ :only
264
+ .name
265
+ +human
266
+ :only
267
+ .name
268
+ END
269
+ @pet = Pet.create({:name => 'Godzilla'})
270
+ json = %q{{"pet" : { "name" : "Godzilla"}}}
271
+ run_test jazz, json, 'pet = Pet.find_by_name "Godzilla"'
272
+ @pet.destroy
273
+ end
274
+
275
+ def test_suppress_label
276
+ jazz = <<END
277
+ -humans~
278
+ :only
279
+ .name
280
+ END
281
+ json = '{ "humans" : [ {"name":"Alex"} ,{"name":"Eugene"}, {"name":"Ivan"}, {"name":"Oleg"} ] }'
282
+ run_test jazz, json
283
+ end
284
+
285
+
286
+ def test_static_values
287
+ jazz = <<END
288
+ -obj
289
+ :static
290
+ name => static_field
291
+ END
292
+ json = '{ "obj" : {"name" : "static_field"}}'
293
+ run_test jazz, json, "obj = Object.new"
294
+ end
295
+
296
+ def test_headers
297
+ jazz = <<END
298
+ #!hide_initial_label!
299
+ -humans~
300
+ :only
301
+ .name
302
+ END
303
+ json = '[ {"name":"Alex"} ,{"name":"Eugene"}, {"name":"Ivan"}, {"name":"Oleg"} ]'
304
+ run_test jazz, json
305
+
306
+ end
307
+
308
+ def test_space_after_control_chars
309
+ jazz = <<END
310
+ - ivan
311
+ :only
312
+ .name
313
+ END
314
+ json = '{"ivan" :{ "name" : "Ivan" }}'
315
+ run_test jazz, json, 'ivan = Human.find_by_name "Ivan"'
316
+ end
317
+
318
+ def test_aliases
319
+ jazz = <<END
320
+ source ivan
321
+ :except
322
+ .id
323
+ .address
324
+ join pets
325
+ :only
326
+ .name
327
+ merge pet_type
328
+ :only
329
+ .class_name => whois
330
+ END
331
+ json = ' {
332
+ "ivan" :
333
+ { "name" : "Ivan",
334
+ "pets" : [
335
+ { "pet" :
336
+ { "name" : "Skooby Doo" ,
337
+ "whois" : "dog" } } ] } }'
338
+ run_test jazz, json, 'ivan = Human.find_by_name "Ivan"'
339
+
340
+ end
341
+
342
+ def test_clean_syntax
343
+ jazz = <<END
344
+ source 'ivan
345
+ except .id .address
346
+ join 'pets
347
+ only .name
348
+ merge 'pet_type
349
+ only .class_name label 'whois
350
+ END
351
+ json = '{
352
+ "ivan" :
353
+ { "name" : "Ivan",
354
+ "pets" : [
355
+ { "pet" :
356
+ { "name" : "Skooby Doo" ,
357
+ "whois" : "dog" } } ] } }'
358
+ run_test jazz, json, 'ivan = Human.find_by_name "Ivan"'
359
+
360
+ end
361
+
362
+ def test_drop_all_feature
363
+ jazz = <<END
364
+ source ivan
365
+ drop all
366
+ join pets
367
+ pick .name
368
+ END
369
+ json = '{
370
+ "ivan" :
371
+ { "pets" : [
372
+ { "pet" :
373
+ { "name" : "Skooby Doo" } } ] } }'
374
+ run_test jazz, json, 'ivan = Human.find_by_name "Ivan"'
375
+
376
+ end
377
+
378
+ def test_pick_all_feature
379
+ jazz = "-ivan pick all"
380
+ hash = run_test jazz, nil, 'ivan = Human.find_by_name "Ivan"', true
381
+ ivan = Human.find_by_name "Ivan"
382
+ assert_equal ivan.attribute_names.sort, hash["ivan"].keys.sort
383
+ end
384
+
385
+ def test_pick_all_by_default
386
+ jazz = "-ivan"
387
+ hash = run_test jazz, nil, 'ivan = Human.find_by_name "Ivan"', true
388
+ ivan = Human.find_by_name "Ivan"
389
+ assert_equal ivan.attribute_names.sort, hash["ivan"].keys.sort
390
+ end
391
+
392
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+ require 'treetop'
4
+ require 'ostruct'
5
+ require File.dirname(__FILE__) + '/../lib/tree'
6
+ require File.dirname(__FILE__) + '/../lib/tequila'
7
+ require File.dirname(__FILE__) + '/../lib/preprocessor'
8
+ require File.dirname(__FILE__) + '/db/preparing'
9
+
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tequila
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 1
10
+ version: 0.2.1
11
+ platform: ruby
12
+ authors:
13
+ - Eugene Hlyzov
14
+ - Ivan Nemytchenko
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-05-16 00:00:00 +07:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Language for advanced JSON generation
24
+ email: eugene.hlyzov@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README.markdown
31
+ - TODO
32
+ files:
33
+ - .gitignore
34
+ - Changelog
35
+ - MIT-LICENSE
36
+ - README.markdown
37
+ - Rakefile
38
+ - TODO
39
+ - VERSION
40
+ - demo.sqlite3
41
+ - init.rb
42
+ - lib/preprocessor.rb
43
+ - lib/tequila.treetop
44
+ - lib/tequila_jazz_handler.rb
45
+ - lib/tree.rb
46
+ - test/bench.rb
47
+ - test/db/database.yml
48
+ - test/db/fixtures/humans.yml
49
+ - test/db/fixtures/pet_types.yml
50
+ - test/db/fixtures/pets.yml
51
+ - test/db/fixtures/toys.yml
52
+ - test/db/preparing.rb
53
+ - test/demo.sqlite3
54
+ - test/pets.jazz
55
+ - test/tequila_test.rb
56
+ - test/test_helper.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/inem/tequila
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Language for advanced JSON generation
91
+ test_files:
92
+ - test/bench.rb
93
+ - test/db/preparing.rb
94
+ - test/tequila_test.rb
95
+ - test/test_helper.rb