mongoscript 0.0.8
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.
- data/.gitignore +19 -0
- data/Gemfile +32 -0
- data/Guardfile +19 -0
- data/Rakefile +11 -0
- data/lib/mongoscript/execution.rb +90 -0
- data/lib/mongoscript/javascripts/multiquery.js +40 -0
- data/lib/mongoscript/multiquery.rb +171 -0
- data/lib/mongoscript/orm/mongoid_adapter.rb +82 -0
- data/lib/mongoscript/orm/mongoid_document_methods.rb +11 -0
- data/lib/mongoscript/version.rb +3 -0
- data/lib/mongoscript.rb +28 -0
- data/mongoscript.gemspec +20 -0
- data/readme.md +22 -0
- data/spec/cases/execution_spec.rb +157 -0
- data/spec/cases/mongoid_adapter_spec.rb +126 -0
- data/spec/cases/mongoscript_spec.rb +25 -0
- data/spec/cases/multiquery_spec.rb +295 -0
- data/spec/fixtures/mongoid.yml +9 -0
- data/spec/fixtures/sample_script.js +3 -0
- data/spec/javascripts/helpers/SpecHelper.js +82 -0
- data/spec/javascripts/multiquery_spec.js +109 -0
- data/spec/javascripts/support/jasmine.yml +73 -0
- data/spec/javascripts/support/jasmine_config.rb +23 -0
- data/spec/javascripts/support/jasmine_runner.rb +32 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/support/mongoid.yml +9 -0
- data/spec/support/mongoid_classes.rb +2 -0
- metadata +98 -0
@@ -0,0 +1,109 @@
|
|
1
|
+
describe("Built-in MongoScript functions", function() {
|
2
|
+
var queries;
|
3
|
+
|
4
|
+
beforeEach(function() {
|
5
|
+
queries = {
|
6
|
+
cars: {
|
7
|
+
selector: {wheels: 4},
|
8
|
+
fields: {name: 1, _id: 1},
|
9
|
+
modifiers: {
|
10
|
+
limit: 3,
|
11
|
+
sort: {name: 1}
|
12
|
+
},
|
13
|
+
collection: "vehicles",
|
14
|
+
},
|
15
|
+
routes: {
|
16
|
+
selector: {hyperspace: true},
|
17
|
+
fields: {distance: 1, _id: 1, _type: 1},
|
18
|
+
modifiers: {
|
19
|
+
limit: 2
|
20
|
+
},
|
21
|
+
collection: "paths"
|
22
|
+
}
|
23
|
+
};
|
24
|
+
})
|
25
|
+
|
26
|
+
describe("multiquery", function() {
|
27
|
+
it("calls find for each provided query (selector and fields)", function() {
|
28
|
+
var query, details;
|
29
|
+
for (query in queries) {
|
30
|
+
spyOn(db[queries[query].collection], "find").andCallThrough();
|
31
|
+
}
|
32
|
+
|
33
|
+
multiquery(queries);
|
34
|
+
|
35
|
+
for (var query in queries) {
|
36
|
+
details = queries[query];
|
37
|
+
expect(db[details.collection].find).toHaveBeenCalledWith(details.selector, details.fields)
|
38
|
+
}
|
39
|
+
})
|
40
|
+
|
41
|
+
it("calls any subsequent modifiers on the find", function() {
|
42
|
+
var query, details;
|
43
|
+
for (query in queries) {
|
44
|
+
details = queries[query];
|
45
|
+
for (var modifier in (details.modifiers || {})) {
|
46
|
+
spyOn(db[details.collection].find(), modifier).andCallThrough();
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
multiquery(queries);
|
51
|
+
|
52
|
+
for (query in queries) {
|
53
|
+
details = queries[query];
|
54
|
+
for (var modifier in (details.modifiers || {})) {
|
55
|
+
expect(db[details.collection].find()[modifier]).toHaveBeenCalledWith(details.modifiers[modifier])
|
56
|
+
}
|
57
|
+
}
|
58
|
+
})
|
59
|
+
|
60
|
+
it("returns an array of the results, keyed to the query name", function() {
|
61
|
+
var query, details, queryResults = {};
|
62
|
+
|
63
|
+
// stub out the results of the find, so we can see what we get back
|
64
|
+
for (query in queries) {
|
65
|
+
queryResults[query] = {result: query};
|
66
|
+
// we use toArray to turn cursors into results, so mock that here
|
67
|
+
spyOn(db[queries[query].collection].find(), "toArray").andReturn(queryResults[query]);
|
68
|
+
}
|
69
|
+
|
70
|
+
results = multiquery(queries);
|
71
|
+
|
72
|
+
// now see if for each named query, we got the right result
|
73
|
+
for (query in queries) {
|
74
|
+
expect(results[query]).toBe(queryResults[query])
|
75
|
+
}
|
76
|
+
})
|
77
|
+
|
78
|
+
it("catches any errors and returns them as the result of the appropriate query", function() {
|
79
|
+
error = {error: true};
|
80
|
+
spyOn(db.vehicles, "find").andThrow(error);
|
81
|
+
results = multiquery(queries);
|
82
|
+
expect(results.cars.error).toBe(error);
|
83
|
+
expect(results.routes.error).not.toBeDefined();
|
84
|
+
})
|
85
|
+
|
86
|
+
describe("querying a non-existent collection", function() {
|
87
|
+
beforeEach(function() {
|
88
|
+
queries.bogus = {
|
89
|
+
selector: {privacy: 2},
|
90
|
+
collection: "bogus"
|
91
|
+
};
|
92
|
+
})
|
93
|
+
|
94
|
+
it("returns an error for that result", function() {
|
95
|
+
results = multiquery(queries);
|
96
|
+
expect(results.bogus.error).toBeDefined()
|
97
|
+
})
|
98
|
+
|
99
|
+
it("returns other queries as expected", function() {
|
100
|
+
results = multiquery(queries);
|
101
|
+
for (var result in results) {
|
102
|
+
if (result !== "bogus") {
|
103
|
+
expect(results[result].error).not.toBeDefined()
|
104
|
+
}
|
105
|
+
}
|
106
|
+
})
|
107
|
+
})
|
108
|
+
})
|
109
|
+
})
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# src_files
|
2
|
+
#
|
3
|
+
# Return an array of filepaths relative to src_dir to include before jasmine specs.
|
4
|
+
# Default: []
|
5
|
+
#
|
6
|
+
# EXAMPLE:
|
7
|
+
#
|
8
|
+
# src_files:
|
9
|
+
# - lib/source1.js
|
10
|
+
# - lib/source2.js
|
11
|
+
# - dist/**/*.js
|
12
|
+
#
|
13
|
+
src_files:
|
14
|
+
- lib/mongoscript/javascripts/*.js
|
15
|
+
|
16
|
+
# stylesheets
|
17
|
+
#
|
18
|
+
# Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.
|
19
|
+
# Default: []
|
20
|
+
#
|
21
|
+
# EXAMPLE:
|
22
|
+
#
|
23
|
+
# stylesheets:
|
24
|
+
# - css/style.css
|
25
|
+
# - stylesheets/*.css
|
26
|
+
#
|
27
|
+
stylesheets:
|
28
|
+
|
29
|
+
# helpers
|
30
|
+
#
|
31
|
+
# Return an array of filepaths relative to spec_dir to include before jasmine specs.
|
32
|
+
# Default: ["helpers/**/*.js"]
|
33
|
+
#
|
34
|
+
# EXAMPLE:
|
35
|
+
#
|
36
|
+
# helpers:
|
37
|
+
# - helpers/**/*.js
|
38
|
+
#
|
39
|
+
helpers:
|
40
|
+
|
41
|
+
# spec_files
|
42
|
+
#
|
43
|
+
# Return an array of filepaths relative to spec_dir to include.
|
44
|
+
# Default: ["**/*[sS]pec.js"]
|
45
|
+
#
|
46
|
+
# EXAMPLE:
|
47
|
+
#
|
48
|
+
# spec_files:
|
49
|
+
# - **/*[sS]pec.js
|
50
|
+
#
|
51
|
+
spec_files:
|
52
|
+
|
53
|
+
# src_dir
|
54
|
+
#
|
55
|
+
# Source directory path. Your src_files must be returned relative to this path. Will use root if left blank.
|
56
|
+
# Default: project root
|
57
|
+
#
|
58
|
+
# EXAMPLE:
|
59
|
+
#
|
60
|
+
# src_dir: public
|
61
|
+
#
|
62
|
+
src_dir:
|
63
|
+
|
64
|
+
# spec_dir
|
65
|
+
#
|
66
|
+
# Spec directory path. Your spec_files must be returned relative to this path.
|
67
|
+
# Default: spec/javascripts
|
68
|
+
#
|
69
|
+
# EXAMPLE:
|
70
|
+
#
|
71
|
+
# spec_dir: spec/javascripts
|
72
|
+
#
|
73
|
+
spec_dir:
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Jasmine
|
2
|
+
class Config
|
3
|
+
|
4
|
+
# Add your overrides or custom config code here
|
5
|
+
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
# Note - this is necessary for rspec2, which has removed the backtrace
|
11
|
+
module Jasmine
|
12
|
+
class SpecBuilder
|
13
|
+
def declare_spec(parent, spec)
|
14
|
+
me = self
|
15
|
+
example_name = spec["name"]
|
16
|
+
@spec_ids << spec["id"]
|
17
|
+
backtrace = @example_locations[parent.description + " " + example_name]
|
18
|
+
parent.it example_name, {} do
|
19
|
+
me.report_spec(spec["id"])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
$:.unshift(ENV['JASMINE_GEM_PATH']) if ENV['JASMINE_GEM_PATH'] # for gem testing purposes
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'jasmine'
|
5
|
+
jasmine_config_overrides = File.expand_path(File.join(File.dirname(__FILE__), 'jasmine_config.rb'))
|
6
|
+
require jasmine_config_overrides if File.exist?(jasmine_config_overrides)
|
7
|
+
if Jasmine::Dependencies.rspec2?
|
8
|
+
require 'rspec'
|
9
|
+
else
|
10
|
+
require 'spec'
|
11
|
+
end
|
12
|
+
|
13
|
+
jasmine_config = Jasmine::Config.new
|
14
|
+
spec_builder = Jasmine::SpecBuilder.new(jasmine_config)
|
15
|
+
|
16
|
+
should_stop = false
|
17
|
+
|
18
|
+
if Jasmine::Dependencies.rspec2?
|
19
|
+
RSpec.configuration.after(:suite) do
|
20
|
+
spec_builder.stop if should_stop
|
21
|
+
end
|
22
|
+
else
|
23
|
+
Spec::Runner.configure do |config|
|
24
|
+
config.after(:suite) do
|
25
|
+
spec_builder.stop if should_stop
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
spec_builder.start
|
31
|
+
should_stop = true
|
32
|
+
spec_builder.declare_suites
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
require 'mongoid'
|
4
|
+
require 'mongoscript'
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.mock_with :mocha
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
|
11
|
+
config.before :each do
|
12
|
+
database_adapter = stub("MongoDB database")
|
13
|
+
database_adapter.stubs(:command).returns({"ok" => 1.0})
|
14
|
+
MongoScript.stubs(:database).returns(database_adapter)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Dir[File.join(File.dirname(__FILE__), "support", "**", "*.rb")].each {|f| load f}
|
19
|
+
|
20
|
+
SCRIPTS_PATH = File.join(File.dirname(__FILE__), "fixtures")
|
21
|
+
|
22
|
+
# Integration testing
|
23
|
+
# Mongoid requires a RAILS_ENV to be set
|
24
|
+
ENV["RACK_ENV"] ||= "test"
|
25
|
+
Mongoid.load!(File.join(File.dirname(__FILE__), "support", "mongoid.yml"))
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoscript
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex Koppel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70212414216240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70212414216240
|
25
|
+
description: An experimental Ruby library for running serverside Javascript in MongoDB.
|
26
|
+
email:
|
27
|
+
- alex+git@alexkoppel.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Guardfile
|
35
|
+
- Rakefile
|
36
|
+
- lib/mongoscript.rb
|
37
|
+
- lib/mongoscript/execution.rb
|
38
|
+
- lib/mongoscript/javascripts/multiquery.js
|
39
|
+
- lib/mongoscript/multiquery.rb
|
40
|
+
- lib/mongoscript/orm/mongoid_adapter.rb
|
41
|
+
- lib/mongoscript/orm/mongoid_document_methods.rb
|
42
|
+
- lib/mongoscript/version.rb
|
43
|
+
- mongoscript.gemspec
|
44
|
+
- readme.md
|
45
|
+
- spec/cases/execution_spec.rb
|
46
|
+
- spec/cases/mongoid_adapter_spec.rb
|
47
|
+
- spec/cases/mongoscript_spec.rb
|
48
|
+
- spec/cases/multiquery_spec.rb
|
49
|
+
- spec/fixtures/mongoid.yml
|
50
|
+
- spec/fixtures/sample_script.js
|
51
|
+
- spec/javascripts/helpers/SpecHelper.js
|
52
|
+
- spec/javascripts/multiquery_spec.js
|
53
|
+
- spec/javascripts/support/jasmine.yml
|
54
|
+
- spec/javascripts/support/jasmine_config.rb
|
55
|
+
- spec/javascripts/support/jasmine_runner.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
- spec/support/mongoid.yml
|
58
|
+
- spec/support/mongoid_classes.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses: []
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.8.15
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: An experimental Ruby library for running serverside Javascript in MongoDB.
|
83
|
+
test_files:
|
84
|
+
- spec/cases/execution_spec.rb
|
85
|
+
- spec/cases/mongoid_adapter_spec.rb
|
86
|
+
- spec/cases/mongoscript_spec.rb
|
87
|
+
- spec/cases/multiquery_spec.rb
|
88
|
+
- spec/fixtures/mongoid.yml
|
89
|
+
- spec/fixtures/sample_script.js
|
90
|
+
- spec/javascripts/helpers/SpecHelper.js
|
91
|
+
- spec/javascripts/multiquery_spec.js
|
92
|
+
- spec/javascripts/support/jasmine.yml
|
93
|
+
- spec/javascripts/support/jasmine_config.rb
|
94
|
+
- spec/javascripts/support/jasmine_runner.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
- spec/support/mongoid.yml
|
97
|
+
- spec/support/mongoid_classes.rb
|
98
|
+
has_rdoc:
|