ki 0.3.3 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +1 -1
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +2 -21
- data/Gemfile.lock +60 -100
- data/Guardfile +1 -1
- data/LICENSE.md +21 -0
- data/README.md +63 -0
- data/Rakefile +3 -45
- data/bin/ki +1 -1
- data/ki.gemspec +31 -137
- data/lib/ki.rb +21 -21
- data/lib/ki/api_error.rb +28 -0
- data/lib/ki/base_request.rb +34 -0
- data/lib/ki/helpers.rb +55 -0
- data/lib/ki/ki.rb +40 -0
- data/lib/ki/ki_cli.rb +46 -0
- data/lib/ki/ki_config.rb +20 -0
- data/lib/ki/middleware.rb +155 -0
- data/lib/ki/model.rb +70 -0
- data/lib/{modules → ki/modules}/callbacks.rb +0 -0
- data/lib/ki/modules/format_of.rb +10 -0
- data/lib/{modules → ki/modules}/model_helpers.rb +0 -12
- data/lib/ki/modules/public_file_helper.rb +13 -0
- data/lib/ki/modules/query_interface.rb +36 -0
- data/lib/{modules → ki/modules}/restrictions.rb +0 -30
- data/lib/ki/modules/view_helper.rb +13 -0
- data/lib/ki/orm.rb +65 -0
- data/lib/ki/version.rb +3 -0
- data/spec/examples/base/.ruby-gemset +1 -0
- data/spec/examples/base/.ruby-version +1 -0
- data/spec/examples/base/Gemfile +4 -0
- data/spec/examples/base/app.rb +1 -0
- data/{examples → spec/examples}/base/config.ru +0 -3
- data/spec/examples/base/config.yml +17 -0
- data/spec/examples/base/public/favicon.ico +0 -0
- data/spec/examples/base/public/javascripts/.gitkeep +0 -0
- data/spec/examples/base/public/stylesheets/.gitkeep +0 -0
- data/spec/examples/base/views/index.haml +6 -0
- data/spec/examples/couch-lock/.ruby-gemset +1 -0
- data/spec/examples/couch-lock/.ruby-version +1 -0
- data/spec/examples/couch-lock/Gemfile +4 -0
- data/spec/examples/couch-lock/Gemfile.lock +48 -0
- data/spec/examples/couch-lock/app.rb +42 -0
- data/spec/examples/couch-lock/config.ru +3 -0
- data/spec/examples/couch-lock/config.yml +17 -0
- data/spec/examples/couch-lock/install.sh +3 -0
- data/spec/examples/couch-lock/public/favicon.ico +0 -0
- data/spec/examples/couch-lock/public/javascripts/.gitkeep +0 -0
- data/spec/examples/couch-lock/public/stylesheets/.gitkeep +0 -0
- data/spec/examples/couch-lock/views/index.haml +18 -0
- data/spec/examples/json.northpole.ro/.ruby-gemset +1 -0
- data/spec/examples/json.northpole.ro/.ruby-version +1 -0
- data/spec/examples/json.northpole.ro/Capfile +25 -0
- data/spec/examples/json.northpole.ro/Gemfile +6 -0
- data/spec/examples/json.northpole.ro/Gemfile.lock +68 -0
- data/spec/examples/json.northpole.ro/app.rb +79 -0
- data/spec/examples/json.northpole.ro/client.rb +25 -0
- data/spec/examples/json.northpole.ro/config.ru +3 -0
- data/spec/examples/json.northpole.ro/config.yml +17 -0
- data/spec/examples/json.northpole.ro/config/deploy.rb +59 -0
- data/spec/examples/json.northpole.ro/config/deploy/production.rb +39 -0
- data/spec/examples/json.northpole.ro/foo.rb +10 -0
- data/spec/examples/json.northpole.ro/public/JNorthPole.jar +0 -0
- data/spec/examples/json.northpole.ro/public/favicon.ico +0 -0
- data/spec/examples/json.northpole.ro/public/font/FontAwesome.otf +0 -0
- data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.eot +0 -0
- data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.svg +284 -0
- data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.ttf +0 -0
- data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.woff +0 -0
- data/spec/examples/json.northpole.ro/public/images/bear.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/bg.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/cloud.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/dotnet.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/dotnet_small.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/footer.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/glyphicons-halflings-white.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/glyphicons-halflings.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/ice.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/java.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/java_small.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/json.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/json_small.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/logo.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/mobile.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/php.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/php_small.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/python.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/python_small.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/ruby.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/ruby_small.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/shell.png +0 -0
- data/spec/examples/json.northpole.ro/public/images/shell_small.png +0 -0
- data/spec/examples/json.northpole.ro/public/javascripts/analytics.js +9 -0
- data/spec/examples/json.northpole.ro/public/javascripts/app.coffee +133 -0
- data/spec/examples/json.northpole.ro/public/javascripts/docs.min.js +16 -0
- data/spec/examples/json.northpole.ro/public/javascripts/jnorthpole.coffee +34 -0
- data/spec/examples/json.northpole.ro/public/javascripts/util.coffee +31 -0
- data/spec/examples/json.northpole.ro/public/stylesheets/app.sass +23 -0
- data/spec/examples/json.northpole.ro/public/stylesheets/application.sass +222 -0
- data/spec/examples/json.northpole.ro/public/stylesheets/cover.css +154 -0
- data/spec/examples/json.northpole.ro/views/curl.haml +19 -0
- data/spec/examples/json.northpole.ro/views/doc.haml +16 -0
- data/spec/examples/json.northpole.ro/views/doc_table.haml +45 -0
- data/spec/examples/json.northpole.ro/views/dotnet.haml +24 -0
- data/spec/examples/json.northpole.ro/views/examples.haml +28 -0
- data/spec/examples/json.northpole.ro/views/faq.haml +32 -0
- data/spec/examples/json.northpole.ro/views/foo.haml +7 -0
- data/spec/examples/json.northpole.ro/views/index.haml +4 -0
- data/spec/examples/json.northpole.ro/views/java.haml +0 -0
- data/spec/examples/json.northpole.ro/views/layout.haml +42 -0
- data/spec/examples/json.northpole.ro/views/php.haml +37 -0
- data/spec/examples/json.northpole.ro/views/playground.haml +38 -0
- data/spec/examples/json.northpole.ro/views/python.haml +17 -0
- data/spec/examples/json.northpole.ro/views/ruby.haml +12 -0
- data/spec/examples/json.northpole.ro/views/signup.haml +2 -0
- data/spec/examples/json.northpole.ro/views/thanks.haml +12 -0
- data/spec/functional_spec.rb +4 -0
- data/spec/lib/ki/base_request_spec.rb +88 -0
- data/spec/lib/ki/helpers_spec.rb +12 -0
- data/spec/lib/ki/ki_config_spec.rb +7 -0
- data/spec/lib/ki/middleware_spec.rb +11 -0
- data/spec/lib/ki/model_spec.rb +100 -0
- data/spec/lib/ki/modules/format_of_spec.rb +15 -0
- data/spec/lib/ki/orm_spec.rb +83 -0
- data/spec/spec_helper.rb +18 -5
- data/spec/util_spec.rb +7 -36
- metadata +269 -193
- data/.document +0 -5
- data/.rvmrc +0 -1
- data/LICENSE.txt +0 -20
- data/README.rdoc +0 -3
- data/VERSION +0 -1
- data/examples/base/Gemfile +0 -3
- data/examples/base/Gemfile.lock +0 -24
- data/examples/base/app.rb +0 -4
- data/examples/base/config.yml +0 -16
- data/gfx/colors.xcf +0 -0
- data/gfx/ki3.eps +0 -0
- data/gfx/logo.png +0 -0
- data/ki_wrap.rb +0 -35
- data/lib/conf.rb +0 -28
- data/lib/db.rb +0 -73
- data/lib/helpers.rb +0 -34
- data/lib/ki_cli.rb +0 -76
- data/lib/mockreq.rb +0 -45
- data/lib/model.rb +0 -110
- data/lib/modules/email.rb +0 -51
- data/lib/modules/query_interface.rb +0 -42
- data/lib/req.rb +0 -57
- data/lib/resp.rb +0 -70
- data/lib/static_file.rb +0 -68
- data/lib/util.rb +0 -114
- data/lib/views/404.haml +0 -1
- data/lib/views/406.haml +0 -1
- data/lib/views/index.haml +0 -7
- data/lib/views/layout.haml +0 -1
- data/spec/conf_spec.rb +0 -6
- data/spec/db_spec.rb +0 -93
- data/spec/integration/integration_spec.rb +0 -114
- data/spec/ki_spec.rb +0 -7
- data/spec/model_spec.rb +0 -91
- data/spec/modules/callbacks_spec.rb +0 -25
- data/spec/modules/email_spec.rb +0 -19
- data/spec/modules/query_interface_spec.rb +0 -93
- data/spec/modules/restrictions_spec.rb +0 -110
- data/spec/performance/performance.sh +0 -1
- data/spec/req_spec.rb +0 -29
@@ -0,0 +1,17 @@
|
|
1
|
+
%pre.code.python
|
2
|
+
= preserve do
|
3
|
+
:escaped
|
4
|
+
import requests
|
5
|
+
import json
|
6
|
+
|
7
|
+
API_KEY = '#{NP_GUEST_API_KEY}'
|
8
|
+
SECRET = '#{NP_GUEST_SECRET}'
|
9
|
+
URL = '#{NP_URL}/storage.json'
|
10
|
+
|
11
|
+
result = requests.post(URL, params = {'#{NP_API_KEY}': API_KEY, '#{NP_SECRET}': SECRET, 'foo': 'bar'})
|
12
|
+
resource = json.loads(result.text)
|
13
|
+
resource_id = resource['id']
|
14
|
+
|
15
|
+
requests.get(URL, params = {'#{NP_API_KEY}': API_KEY, '#{NP_SECRET}': SECRET, 'id': resource_id})
|
16
|
+
requests.put(URL, params = {'#{NP_API_KEY}': API_KEY, '#{NP_SECRET}': SECRET, 'id': resource_id, 'foo': 'not bar'})
|
17
|
+
requests.delete(URL, params = {'#{NP_API_KEY}': API_KEY, '#{NP_SECRET}': SECRET, 'id': resource_id})
|
@@ -0,0 +1,12 @@
|
|
1
|
+
%pre.code
|
2
|
+
= preserve do
|
3
|
+
:escaped
|
4
|
+
require 'uri'
|
5
|
+
require 'net/http'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
uri = URI.parse("#{NP_URL}")
|
9
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
10
|
+
request = Net::HTTP::Post.new("/storage.json")
|
11
|
+
request.body = {'#{NP_API_KEY}' => '#{NP_SECRET}', '#{NP_GUEST_API_KEY}' => '#{NP_GUEST_SECRET}'}.to_json
|
12
|
+
response = http.request(request)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
%p
|
2
|
+
= other_developer_count
|
3
|
+
%br
|
4
|
+
%p
|
5
|
+
Thanks Gabi Nagy for
|
6
|
+
%a{:href => 'https://github.com/northpole-api/northpole.ro/blob/master/public/favicon.ico'} the favicon
|
7
|
+
%p
|
8
|
+
Thanks Rares Musina for
|
9
|
+
%a{:href => 'https://github.com/northpole-api/NorthPoleNetWrapper'} NorthPoleNetWrapper
|
10
|
+
%p
|
11
|
+
Thanks for
|
12
|
+
%a{:href => 'http://openclipart.org/detail/97081/polar-bear-by-aguasas'} the bear
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ki::BaseRequest do
|
4
|
+
let(:req) { Ki::BaseRequest }
|
5
|
+
|
6
|
+
it 'knows if current path is root' do
|
7
|
+
req.new({'PATH_INFO' => '/'}).root?.should be_true
|
8
|
+
req.new({'PATH_INFO' => '/foo'}).root?.should be_false
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'coverts path to class' do
|
12
|
+
class Foo < Ki::Model; end
|
13
|
+
class Bar < Ki::Model; end
|
14
|
+
req.new({'PATH_INFO' => ''}).to_ki_model_class.should == nil
|
15
|
+
req.new({'PATH_INFO' => '/'}).to_ki_model_class.should == nil
|
16
|
+
req.new({'PATH_INFO' => 'foo'}).to_ki_model_class.should == Foo
|
17
|
+
req.new({'PATH_INFO' => '/foo'}).to_ki_model_class.should == Foo
|
18
|
+
req.new({'PATH_INFO' => '/Foo.json'}).to_ki_model_class.should == Foo
|
19
|
+
req.new({'PATH_INFO' => '/bar.json'}).to_ki_model_class.should == Bar
|
20
|
+
req.new({'PATH_INFO' => '/Foo/bar.json'}).to_ki_model_class.should == nil
|
21
|
+
req.new({'PATH_INFO' => '/Foo/bar'}).to_ki_model_class.should == nil
|
22
|
+
req.new({'PATH_INFO' => '/Foo_bar'}).to_ki_model_class.should == nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'converts verb to action' do
|
26
|
+
req.new({'REQUEST_METHOD' => 'GET'}).to_action.should == :find
|
27
|
+
req.new({'REQUEST_METHOD' => 'POST'}).to_action.should == :create
|
28
|
+
req.new({'REQUEST_METHOD' => 'PUT'}).to_action.should == :update
|
29
|
+
req.new({'REQUEST_METHOD' => 'DELETE'}).to_action.should == :delete
|
30
|
+
req.new({'REQUEST_METHOD' => 'SEARCH'}).to_action.should == :find
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'json' do
|
34
|
+
it 'considers application/json content type as a json request' do
|
35
|
+
req.new({'CONTENT_TYPE' => 'application/xml'}).json?.should be_false
|
36
|
+
req.new({}).json?.should be_false
|
37
|
+
req.new({'CONTENT_TYPE' => 'application/json'}).json?.should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'considers .json url format as a json request' do
|
41
|
+
req.new({'PATH_INFO' => '/foo'}).json?.should be_false
|
42
|
+
req.new({'PATH_INFO' => '/foo.json'}).json?.should be_true
|
43
|
+
end
|
44
|
+
|
45
|
+
# context 'params' do
|
46
|
+
# it 'handles get with no params' do
|
47
|
+
# asd = req.new({
|
48
|
+
# 'PATH_INFO' => "/foo.json",
|
49
|
+
# 'QUERY_STRING' => '',
|
50
|
+
# 'REQUEST_METHOD' => 'GET',
|
51
|
+
# 'rack.input' => ''
|
52
|
+
# })
|
53
|
+
# asd.params.should == {}
|
54
|
+
# end
|
55
|
+
|
56
|
+
# it 'handles query params' do
|
57
|
+
# asd = req.new({
|
58
|
+
# 'PATH_INFO' => "/foo.json",
|
59
|
+
# 'QUERY_STRING' => 'foo=bar&cez[]=dar&cez[]=asd',
|
60
|
+
# 'REQUEST_METHOD' => 'GET',
|
61
|
+
# 'rack.input' => ''
|
62
|
+
# })
|
63
|
+
# asd.params.should == {'foo' => 'bar', 'cez' => ['dar', 'asd']}
|
64
|
+
# end
|
65
|
+
|
66
|
+
# it 'handles both query and body params' do
|
67
|
+
# # asd = req.new({
|
68
|
+
# # 'PATH_INFO' => "/foo.json",
|
69
|
+
# # 'QUERY_STRING' => 'foo=bar&cez[]=dar&cez[]=asd',
|
70
|
+
# # 'REQUEST_METHOD' => 'POST',
|
71
|
+
# # 'rack.input' => '{"bar":"foo"}'
|
72
|
+
# # })
|
73
|
+
|
74
|
+
# asd = StringIO.new
|
75
|
+
# asd << '{"bar":"foo"}'
|
76
|
+
|
77
|
+
# env = Rack::MockRequest.env_for('foo.json', {
|
78
|
+
# 'REQUEST_METHOD' => 'POST',
|
79
|
+
# 'QUERY_STRING' => 'foo=bar&cez[]=dar&cez[]=asd',
|
80
|
+
# :input => asd
|
81
|
+
# })
|
82
|
+
# asd = req.new(env)
|
83
|
+
# # p asd.body.read
|
84
|
+
# # Foo.new(asd)
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ki::Middleware do
|
4
|
+
context Ki::Middleware::ApiHandler do
|
5
|
+
it 'should only handle urls maped from objects that extend Ki::Model' do
|
6
|
+
get '/asd.json'
|
7
|
+
JSON.parse(last_response.body)
|
8
|
+
last_response.status.should == 404
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ki::Model do
|
4
|
+
before :all do
|
5
|
+
class Foo < Ki::Model; end
|
6
|
+
class Bar < Ki::Model; end
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should know about all descendants" do
|
10
|
+
desc = Ki::Model.descendants
|
11
|
+
desc.include?(Foo).should be_true
|
12
|
+
desc.include?(Bar).should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
context Ki::QueryInterface do
|
16
|
+
it 'should have the query interface' do
|
17
|
+
Foo.class_name.should == 'Foo'
|
18
|
+
expect {
|
19
|
+
id = Foo.create({hello: 'world'})['id']
|
20
|
+
Foo.find(id).first['hello'].should == 'world'
|
21
|
+
Foo.update('id' => id, 'hello' => 'universe')
|
22
|
+
Foo.find(id).first['hello'].should == 'universe'
|
23
|
+
Foo.delete(id)
|
24
|
+
}.to change{Foo.count}.by 0
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should find or create' do
|
28
|
+
expect {
|
29
|
+
h = { 'time' => Time.now.to_i.to_s }
|
30
|
+
r = Foo.find_or_create(h)
|
31
|
+
r = Foo.find_or_create(h)
|
32
|
+
}.to change { Foo.count }.by 1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should have restrictions configured' do
|
37
|
+
Bar.unique_attributes.should == []
|
38
|
+
Bar.required_attributes.should == []
|
39
|
+
Bar.forbidden_actions.should == []
|
40
|
+
|
41
|
+
class Bar < Ki::Model
|
42
|
+
unique :foo
|
43
|
+
requires :foo
|
44
|
+
forbid :delete
|
45
|
+
end
|
46
|
+
|
47
|
+
Bar.unique_attributes.should == [:foo]
|
48
|
+
Bar.required_attributes.should == [:foo]
|
49
|
+
Bar.forbidden_actions.should == [:delete]
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'default properties' do
|
53
|
+
class DefaultProperties < Ki::Model
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should create object' do
|
57
|
+
expect {
|
58
|
+
DefaultProperties.new(:create, { 'name' => 'zim' })
|
59
|
+
}.to change { DefaultProperties.count }.by 1
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'required, unique, forbid' do
|
64
|
+
class SpecialProperties < Ki::Model
|
65
|
+
unique :foo
|
66
|
+
requires :foo
|
67
|
+
forbid :delete
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should create object' do
|
71
|
+
t = Time.now.to_i
|
72
|
+
# keep in mind that delete does not call unique/requires/forbid filters
|
73
|
+
# make sure there are no duplicates
|
74
|
+
SpecialProperties.delete({'foo' => t})
|
75
|
+
expect {
|
76
|
+
SpecialProperties.new(:create, { 'name' => 'zim', 'foo' => t })
|
77
|
+
}.to change { SpecialProperties.count }.by 1
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should check for required attributes' do
|
81
|
+
expect {
|
82
|
+
SpecialProperties.new(:create, { 'name' => 'zim' })
|
83
|
+
}.to raise_error Ki::RequiredAttributeMissing
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'should check for unique attributes' do
|
87
|
+
expect {
|
88
|
+
t = Time.now.to_i
|
89
|
+
SpecialProperties.new(:create, { 'name' => 'zim', 'foo' => t })
|
90
|
+
SpecialProperties.new(:create, { 'name' => 'zim', 'foo' => t })
|
91
|
+
}.to raise_error Ki::AttributeNotUnique
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should not allow forbidden actions' do
|
95
|
+
expect {
|
96
|
+
SpecialProperties.new(:delete, {})
|
97
|
+
}.to raise_error Ki::ForbiddenAction
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ki::FormatOf do
|
4
|
+
let(:obj) { @obj = Object.new; @obj.extend(Ki::FormatOf) }
|
5
|
+
|
6
|
+
it 'should parse url format' do
|
7
|
+
[nil, {}, '', '.json'].each do |s|
|
8
|
+
obj.format_of(s).should == ''
|
9
|
+
end
|
10
|
+
|
11
|
+
['asd.json', 'asd.json?asd=1'].each do |s|
|
12
|
+
obj.format_of(s).should == 'json'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ki::Orm do
|
4
|
+
before :all do
|
5
|
+
@db = Ki::Orm::Db.instance.establish_connection
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should know db name in test env' do
|
9
|
+
@db.config['name'].should == 'np_test'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should create a db object' do
|
13
|
+
expect {
|
14
|
+
oid = @db.insert 'foo', {hello: 'world'}
|
15
|
+
oid.class.should == Hash
|
16
|
+
}.to change{@db.count('foo')}.by(1)
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'count' do
|
20
|
+
it 'should count' do
|
21
|
+
expect {
|
22
|
+
@db.insert 'foo', {hello: 'world'}
|
23
|
+
}.to change{@db.count('foo')}.by(1)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should count by hash' do
|
27
|
+
t = Time.now.to_i.to_s
|
28
|
+
expect {
|
29
|
+
@db.insert 'foo', {hello: t}
|
30
|
+
}.to change{@db.count('foo', {hello: t})}.by(1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'find' do
|
35
|
+
it 'should find all' do
|
36
|
+
@db.insert 'foo', {hello: 'world'}
|
37
|
+
@db.insert 'foo', {hello: 'world'}
|
38
|
+
@db.find('foo').to_a.count.should >= 2
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should find by string id' do
|
42
|
+
obj_id = @db.insert('foo', {hello: 'world'})['id']
|
43
|
+
@db.find('foo', obj_id).first['id'].should == obj_id
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should find by hash["id"]' do
|
47
|
+
obj_id = @db.insert 'foo', {'hello' => 'world'}
|
48
|
+
@db.find('foo', {'id' => obj_id['id']}).first.should == obj_id
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should find by hash["_id"]' do
|
52
|
+
obj_id = @db.insert('foo', {hello: 'world'})['id']
|
53
|
+
@db.find('foo', {'_id' => obj_id}).first['id'].should == obj_id
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should find by BSON::ObjectId(id)' do
|
57
|
+
obj_id = @db.insert('foo', {'hello' => 'world'})['id']
|
58
|
+
@db.find('foo', {'id' => BSON::ObjectId(obj_id)}).first['id'].should == obj_id
|
59
|
+
@db.find('foo', {'_id' => BSON::ObjectId(obj_id)}).first['id'].should == obj_id
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should return empty array when nothing found' do
|
63
|
+
r = @db.find('foo', {'it does not exist' => 'sure hope so' + Time.now.to_i.to_s })
|
64
|
+
r.class.should == Array
|
65
|
+
r.should be_empty
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should update' do
|
70
|
+
obj_id = @db.insert('foo', { 'hello' => 'world' })['id']
|
71
|
+
@db.find('foo', obj_id).first['hello'].should == 'world'
|
72
|
+
up = @db.update('foo', { 'id' => obj_id, 'hello' => 'universe' })['id']
|
73
|
+
up.should == obj_id
|
74
|
+
@db.find('foo', obj_id).first['hello'].should == 'universe'
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should delete by id' do
|
78
|
+
obj_id = @db.insert 'foo', { 'hello' => 'world' }
|
79
|
+
expect {
|
80
|
+
@db.delete('foo', obj_id).should == {}
|
81
|
+
}.to change { @db.count 'foo' }.by -1
|
82
|
+
end
|
83
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,24 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
-
|
3
|
-
|
2
|
+
ENV['RACK_ENV'] = 'test'
|
3
|
+
|
4
|
+
require 'rack/test'
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
4
7
|
require 'ki'
|
5
8
|
|
6
|
-
|
7
|
-
Ki::
|
8
|
-
Ki::Db.instance.
|
9
|
+
Ki::KiConfig::CONFIG_FILE_PATH = 'spec/examples/base/config.yml'
|
10
|
+
Ki::KiConfig.instance.read 'test'
|
11
|
+
Ki::Orm::Db.instance.establish_connection
|
9
12
|
|
13
|
+
def app
|
14
|
+
Ki::Ki.new
|
15
|
+
end
|
16
|
+
|
17
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
10
18
|
RSpec.configure do |config|
|
19
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
20
|
+
config.run_all_when_everything_filtered = true
|
21
|
+
config.filter_run :focus
|
22
|
+
|
23
|
+
config.order = 'random'
|
11
24
|
end
|
data/spec/util_spec.rb
CHANGED
@@ -1,41 +1,12 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def base s, output
|
8
|
-
Ki::Util.base(s).should == output
|
9
|
-
end
|
10
|
-
|
11
|
-
describe Ki::Util do
|
12
|
-
it "should parse format correctly" do
|
13
|
-
format '/', :html
|
14
|
-
format '/index', :html
|
15
|
-
format '/index.html', :html
|
16
|
-
format '/index.html?asd=1', :html
|
17
|
-
format '/index?asd=1', :html
|
18
|
-
format '/john?asd=', :html
|
19
|
-
format '/asd?asd=1&bar=2', :html
|
20
|
-
format '/welcome.json', :json
|
21
|
-
format '/welcome.json?foo', :json
|
22
|
-
format '/welcome.json?foo=1&ar=2', :json
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should get the base url correctly" do
|
26
|
-
base '/', ''
|
27
|
-
base '/index', 'index'
|
28
|
-
base '/foo?bar=1', 'foo'
|
29
|
-
base '/asd/bar', 'asd'
|
30
|
-
base '/asd.json', 'asd'
|
31
|
-
base '/foo.html', 'foo'
|
3
|
+
describe Ki do
|
4
|
+
it 'should run in test env' do
|
5
|
+
Ki::Ki.new.environment.should == 'test'
|
32
6
|
end
|
33
7
|
|
34
|
-
it
|
35
|
-
|
36
|
-
|
37
|
-
end
|
38
|
-
"dog".to_class.should == Dog
|
39
|
-
"cat".to_class.should == nil
|
8
|
+
it 'should convert string to class corectly' do
|
9
|
+
class Cez; end
|
10
|
+
'cez'.to_class.should == Cez
|
40
11
|
end
|
41
12
|
end
|