ki 0.3.3 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +1 -1
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +2 -21
  7. data/Gemfile.lock +60 -100
  8. data/Guardfile +1 -1
  9. data/LICENSE.md +21 -0
  10. data/README.md +63 -0
  11. data/Rakefile +3 -45
  12. data/bin/ki +1 -1
  13. data/ki.gemspec +31 -137
  14. data/lib/ki.rb +21 -21
  15. data/lib/ki/api_error.rb +28 -0
  16. data/lib/ki/base_request.rb +34 -0
  17. data/lib/ki/helpers.rb +55 -0
  18. data/lib/ki/ki.rb +40 -0
  19. data/lib/ki/ki_cli.rb +46 -0
  20. data/lib/ki/ki_config.rb +20 -0
  21. data/lib/ki/middleware.rb +155 -0
  22. data/lib/ki/model.rb +70 -0
  23. data/lib/{modules → ki/modules}/callbacks.rb +0 -0
  24. data/lib/ki/modules/format_of.rb +10 -0
  25. data/lib/{modules → ki/modules}/model_helpers.rb +0 -12
  26. data/lib/ki/modules/public_file_helper.rb +13 -0
  27. data/lib/ki/modules/query_interface.rb +36 -0
  28. data/lib/{modules → ki/modules}/restrictions.rb +0 -30
  29. data/lib/ki/modules/view_helper.rb +13 -0
  30. data/lib/ki/orm.rb +65 -0
  31. data/lib/ki/version.rb +3 -0
  32. data/spec/examples/base/.ruby-gemset +1 -0
  33. data/spec/examples/base/.ruby-version +1 -0
  34. data/spec/examples/base/Gemfile +4 -0
  35. data/spec/examples/base/app.rb +1 -0
  36. data/{examples → spec/examples}/base/config.ru +0 -3
  37. data/spec/examples/base/config.yml +17 -0
  38. data/spec/examples/base/public/favicon.ico +0 -0
  39. data/spec/examples/base/public/javascripts/.gitkeep +0 -0
  40. data/spec/examples/base/public/stylesheets/.gitkeep +0 -0
  41. data/spec/examples/base/views/index.haml +6 -0
  42. data/spec/examples/couch-lock/.ruby-gemset +1 -0
  43. data/spec/examples/couch-lock/.ruby-version +1 -0
  44. data/spec/examples/couch-lock/Gemfile +4 -0
  45. data/spec/examples/couch-lock/Gemfile.lock +48 -0
  46. data/spec/examples/couch-lock/app.rb +42 -0
  47. data/spec/examples/couch-lock/config.ru +3 -0
  48. data/spec/examples/couch-lock/config.yml +17 -0
  49. data/spec/examples/couch-lock/install.sh +3 -0
  50. data/spec/examples/couch-lock/public/favicon.ico +0 -0
  51. data/spec/examples/couch-lock/public/javascripts/.gitkeep +0 -0
  52. data/spec/examples/couch-lock/public/stylesheets/.gitkeep +0 -0
  53. data/spec/examples/couch-lock/views/index.haml +18 -0
  54. data/spec/examples/json.northpole.ro/.ruby-gemset +1 -0
  55. data/spec/examples/json.northpole.ro/.ruby-version +1 -0
  56. data/spec/examples/json.northpole.ro/Capfile +25 -0
  57. data/spec/examples/json.northpole.ro/Gemfile +6 -0
  58. data/spec/examples/json.northpole.ro/Gemfile.lock +68 -0
  59. data/spec/examples/json.northpole.ro/app.rb +79 -0
  60. data/spec/examples/json.northpole.ro/client.rb +25 -0
  61. data/spec/examples/json.northpole.ro/config.ru +3 -0
  62. data/spec/examples/json.northpole.ro/config.yml +17 -0
  63. data/spec/examples/json.northpole.ro/config/deploy.rb +59 -0
  64. data/spec/examples/json.northpole.ro/config/deploy/production.rb +39 -0
  65. data/spec/examples/json.northpole.ro/foo.rb +10 -0
  66. data/spec/examples/json.northpole.ro/public/JNorthPole.jar +0 -0
  67. data/spec/examples/json.northpole.ro/public/favicon.ico +0 -0
  68. data/spec/examples/json.northpole.ro/public/font/FontAwesome.otf +0 -0
  69. data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.eot +0 -0
  70. data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.svg +284 -0
  71. data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.ttf +0 -0
  72. data/spec/examples/json.northpole.ro/public/font/fontawesome-webfont.woff +0 -0
  73. data/spec/examples/json.northpole.ro/public/images/bear.png +0 -0
  74. data/spec/examples/json.northpole.ro/public/images/bg.png +0 -0
  75. data/spec/examples/json.northpole.ro/public/images/cloud.png +0 -0
  76. data/spec/examples/json.northpole.ro/public/images/dotnet.png +0 -0
  77. data/spec/examples/json.northpole.ro/public/images/dotnet_small.png +0 -0
  78. data/spec/examples/json.northpole.ro/public/images/footer.png +0 -0
  79. data/spec/examples/json.northpole.ro/public/images/glyphicons-halflings-white.png +0 -0
  80. data/spec/examples/json.northpole.ro/public/images/glyphicons-halflings.png +0 -0
  81. data/spec/examples/json.northpole.ro/public/images/ice.png +0 -0
  82. data/spec/examples/json.northpole.ro/public/images/java.png +0 -0
  83. data/spec/examples/json.northpole.ro/public/images/java_small.png +0 -0
  84. data/spec/examples/json.northpole.ro/public/images/json.png +0 -0
  85. data/spec/examples/json.northpole.ro/public/images/json_small.png +0 -0
  86. data/spec/examples/json.northpole.ro/public/images/logo.png +0 -0
  87. data/spec/examples/json.northpole.ro/public/images/mobile.png +0 -0
  88. data/spec/examples/json.northpole.ro/public/images/php.png +0 -0
  89. data/spec/examples/json.northpole.ro/public/images/php_small.png +0 -0
  90. data/spec/examples/json.northpole.ro/public/images/python.png +0 -0
  91. data/spec/examples/json.northpole.ro/public/images/python_small.png +0 -0
  92. data/spec/examples/json.northpole.ro/public/images/ruby.png +0 -0
  93. data/spec/examples/json.northpole.ro/public/images/ruby_small.png +0 -0
  94. data/spec/examples/json.northpole.ro/public/images/shell.png +0 -0
  95. data/spec/examples/json.northpole.ro/public/images/shell_small.png +0 -0
  96. data/spec/examples/json.northpole.ro/public/javascripts/analytics.js +9 -0
  97. data/spec/examples/json.northpole.ro/public/javascripts/app.coffee +133 -0
  98. data/spec/examples/json.northpole.ro/public/javascripts/docs.min.js +16 -0
  99. data/spec/examples/json.northpole.ro/public/javascripts/jnorthpole.coffee +34 -0
  100. data/spec/examples/json.northpole.ro/public/javascripts/util.coffee +31 -0
  101. data/spec/examples/json.northpole.ro/public/stylesheets/app.sass +23 -0
  102. data/spec/examples/json.northpole.ro/public/stylesheets/application.sass +222 -0
  103. data/spec/examples/json.northpole.ro/public/stylesheets/cover.css +154 -0
  104. data/spec/examples/json.northpole.ro/views/curl.haml +19 -0
  105. data/spec/examples/json.northpole.ro/views/doc.haml +16 -0
  106. data/spec/examples/json.northpole.ro/views/doc_table.haml +45 -0
  107. data/spec/examples/json.northpole.ro/views/dotnet.haml +24 -0
  108. data/spec/examples/json.northpole.ro/views/examples.haml +28 -0
  109. data/spec/examples/json.northpole.ro/views/faq.haml +32 -0
  110. data/spec/examples/json.northpole.ro/views/foo.haml +7 -0
  111. data/spec/examples/json.northpole.ro/views/index.haml +4 -0
  112. data/spec/examples/json.northpole.ro/views/java.haml +0 -0
  113. data/spec/examples/json.northpole.ro/views/layout.haml +42 -0
  114. data/spec/examples/json.northpole.ro/views/php.haml +37 -0
  115. data/spec/examples/json.northpole.ro/views/playground.haml +38 -0
  116. data/spec/examples/json.northpole.ro/views/python.haml +17 -0
  117. data/spec/examples/json.northpole.ro/views/ruby.haml +12 -0
  118. data/spec/examples/json.northpole.ro/views/signup.haml +2 -0
  119. data/spec/examples/json.northpole.ro/views/thanks.haml +12 -0
  120. data/spec/functional_spec.rb +4 -0
  121. data/spec/lib/ki/base_request_spec.rb +88 -0
  122. data/spec/lib/ki/helpers_spec.rb +12 -0
  123. data/spec/lib/ki/ki_config_spec.rb +7 -0
  124. data/spec/lib/ki/middleware_spec.rb +11 -0
  125. data/spec/lib/ki/model_spec.rb +100 -0
  126. data/spec/lib/ki/modules/format_of_spec.rb +15 -0
  127. data/spec/lib/ki/orm_spec.rb +83 -0
  128. data/spec/spec_helper.rb +18 -5
  129. data/spec/util_spec.rb +7 -36
  130. metadata +269 -193
  131. data/.document +0 -5
  132. data/.rvmrc +0 -1
  133. data/LICENSE.txt +0 -20
  134. data/README.rdoc +0 -3
  135. data/VERSION +0 -1
  136. data/examples/base/Gemfile +0 -3
  137. data/examples/base/Gemfile.lock +0 -24
  138. data/examples/base/app.rb +0 -4
  139. data/examples/base/config.yml +0 -16
  140. data/gfx/colors.xcf +0 -0
  141. data/gfx/ki3.eps +0 -0
  142. data/gfx/logo.png +0 -0
  143. data/ki_wrap.rb +0 -35
  144. data/lib/conf.rb +0 -28
  145. data/lib/db.rb +0 -73
  146. data/lib/helpers.rb +0 -34
  147. data/lib/ki_cli.rb +0 -76
  148. data/lib/mockreq.rb +0 -45
  149. data/lib/model.rb +0 -110
  150. data/lib/modules/email.rb +0 -51
  151. data/lib/modules/query_interface.rb +0 -42
  152. data/lib/req.rb +0 -57
  153. data/lib/resp.rb +0 -70
  154. data/lib/static_file.rb +0 -68
  155. data/lib/util.rb +0 -114
  156. data/lib/views/404.haml +0 -1
  157. data/lib/views/406.haml +0 -1
  158. data/lib/views/index.haml +0 -7
  159. data/lib/views/layout.haml +0 -1
  160. data/spec/conf_spec.rb +0 -6
  161. data/spec/db_spec.rb +0 -93
  162. data/spec/integration/integration_spec.rb +0 -114
  163. data/spec/ki_spec.rb +0 -7
  164. data/spec/model_spec.rb +0 -91
  165. data/spec/modules/callbacks_spec.rb +0 -25
  166. data/spec/modules/email_spec.rb +0 -19
  167. data/spec/modules/query_interface_spec.rb +0 -93
  168. data/spec/modules/restrictions_spec.rb +0 -110
  169. data/spec/performance/performance.sh +0 -1
  170. 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,2 @@
1
+ %input#user{ :type => 'text', :placeholder => 'api_key' }
2
+ %input#pass{ :type => 'text', :placeholder => 'secret' }
@@ -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,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'the app' do
4
+ end
@@ -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,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ki::Helpers do
4
+ include Ki::Helpers
5
+
6
+ it 'should render_haml' do
7
+ render_haml('%div.foo').should == "<div class='foo'></div>\n"
8
+ end
9
+
10
+ it 'should render a partial' do
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ki::KiConfig do
4
+ it 'should know db name in test env' do
5
+ Ki::KiConfig.instance.environment.should == 'test'
6
+ end
7
+ 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
@@ -1,11 +1,24 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
- require 'rspec'
2
+ ENV['RACK_ENV'] = 'test'
3
+
4
+ require 'rack/test'
5
+ include Rack::Test::Methods
6
+
4
7
  require 'ki'
5
8
 
6
- DB_NAME = 'ki_test_db'
7
- Ki::Conf.instance.yaml['database_name'] = DB_NAME
8
- Ki::Db.instance.config
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
@@ -1,41 +1,12 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
- def format s, output
4
- Ki::Util.format(s).should == output
5
- end
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 "should correctly convert to a class" do
35
- "dog".to_class.should == nil
36
- class Dog
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