ananke 1.1.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,31 +0,0 @@
1
- require './spec/spec_helper'
2
- require './lib/ananke'
3
-
4
- describe 'Link-To Resource' do
5
- include Rack::Test::Methods
6
- include Ananke
7
-
8
- def app
9
- Sinatra::Base
10
- end
11
-
12
- it """
13
- Should be able to Output link to link_to
14
- """ do
15
- module Repository
16
- module Link_to
17
- def self.one(id)
18
- {:link_id => id}
19
- end
20
- end
21
- end
22
- route :link_to do
23
- id :link_id
24
- link_to :to
25
- end
26
-
27
- get "/link_to/1"
28
- check_status(200)
29
- last_response.body.should == '{"link_id":"1","links":[{"rel":"self","uri":"/link_to/1"},{"rel":"to","uri":"/to/link_to/1"}]}'
30
- end
31
- end
@@ -1,107 +0,0 @@
1
- require './spec/spec_helper'
2
- require './lib/ananke'
3
-
4
- describe 'Resource' do
5
- include Rack::Test::Methods
6
- include Ananke
7
-
8
- def app
9
- Sinatra::Base
10
- end
11
-
12
- it """
13
- Should be able to Output link to Self and Call that link
14
- """ do
15
- module Repository
16
- module Self
17
- def self.one(id) end
18
- def self.add(data)
19
- {:user_id => 1}
20
- end
21
- end
22
- end
23
- route :self do
24
- id :user_id
25
- end
26
-
27
- post "/self", body={:user_id => 1, :username => '1234'}
28
- check_status(201)
29
- last_response.body.should == '{"user_id":1,"links":[{"rel":"self","uri":"/self/1"}]}'
30
-
31
- hash = JSON.parse(last_response.body)
32
- uri = hash['links'].map{|l| l['uri'] if l['rel'] == 'self'}[0]
33
- get uri
34
- check_status(200)
35
- end
36
-
37
- module Repository
38
- module Linked
39
- def self.one(id)
40
- {:user_id => 1}
41
- end
42
- def self.all
43
- [{:user_id => 1}, {:user_id => 2}]
44
- end
45
- def self.add(data)
46
- {:user_id => 1}
47
- end
48
- def self.edit(id, data)
49
- {:user_id => 1}
50
- end
51
- def self.line_id_list(user_id)
52
- [1,2]
53
- end
54
- end
55
- module Line
56
- def self.one(id) end
57
- end
58
- end
59
- route :linked do
60
- id :user_id
61
- linked :line
62
- end
63
- route :line do
64
- id :line_id
65
- end
66
-
67
- it """
68
- Should be able to Output links to linkeds and Call those links
69
- """ do
70
- post "/linked", body={:user_id => 1, :username => '1234'}
71
- check_status(201)
72
- last_response.body.should == '{"user_id":1,"links":[{"rel":"self","uri":"/linked/1"},{"rel":"line","uri":"/line/1"},{"rel":"line","uri":"/line/2"}]}'
73
-
74
- hash = JSON.parse(last_response.body)
75
- hash['links'].each do |l|
76
- get l['uri']
77
- check_status(200)
78
- end
79
- end
80
-
81
- it "Should return links on Get One" do
82
- get "/linked/1"
83
- check_status(200)
84
- last_response.body.should == '{"user_id":1,"links":[{"rel":"self","uri":"/linked/1"},{"rel":"line","uri":"/line/1"},{"rel":"line","uri":"/line/2"}]}'
85
- end
86
-
87
- it """
88
- Should not inject links where it cannot find Repository Id lookup method
89
- """ do
90
- module Repository
91
- module Linked_fail
92
- def self.one(id) end
93
- def self.add(data)
94
- {:user_id => 1}
95
- end
96
- end
97
- end
98
- route :linked_fail do
99
- id :user_id
100
- linked :line
101
- end
102
-
103
- post "/linked_fail", body={:user_id => 1, :username => '1234'}
104
- check_status(201)
105
- last_response.body.should == '{"user_id":1,"links":[{"rel":"self","uri":"/linked_fail/1"}]}'
106
- end
107
- end
data/spec/lib/out_spec.rb DELETED
@@ -1,20 +0,0 @@
1
- require './spec/spec_helper'
2
- require './lib/ananke'
3
-
4
- describe 'Ananke Console Output' do
5
- include Ananke
6
-
7
- it """
8
- Should be able to output in different Colors
9
- """ do
10
- output = Ananke.settings[:output]
11
- Ananke.set :output, true
12
-
13
- Ananke.send(:out, :info, 'test').should == 'test'.blue
14
- Ananke.send(:out, :error, 'test').should == 'test'.red
15
- Ananke.send(:out, :warning, 'test').should == 'test'.yellow
16
- Ananke.send(:out, :some_other, 'test').should == nil
17
-
18
- Ananke.set :output, output
19
- end
20
- end
@@ -1,97 +0,0 @@
1
- require './spec/spec_helper'
2
- require './lib/ananke'
3
-
4
- describe 'Resource Route-For' do
5
- include Rack::Test::Methods
6
- include Ananke
7
-
8
- def app
9
- Sinatra::Base
10
- end
11
-
12
- it """
13
- should be able to accept route_for Registrations
14
- """ do
15
- module Repository
16
- module Route_for
17
- def self.custom(id)
18
- {:route_for_id => id, :content => 'Test'}
19
- end
20
- end
21
- end
22
- route :route_for do
23
- id :route_for_id
24
- route_for :custom
25
- end
26
-
27
- get "/route_for/custom/1"
28
- check_status(200)
29
- last_response.body.should == '{"items":[{"route_for_id":"1","content":"Test","links":[{"rel":"self","uri":"/route_for/1"}]}]}'
30
- end
31
-
32
- it """
33
- should be able to accept route_for Registrations with POST and multiple input parameters
34
- """ do
35
- module Repository
36
- module Route_for
37
- def self.multi(id, name)
38
- {:route_for_id => id, :content => 'Test'}
39
- end
40
- end
41
- end
42
- route :route_for do
43
- id :route_for_id
44
- route_for :multi, :post
45
- end
46
-
47
- post "/route_for/multi", body={:id => 1, :name => 'some name'}
48
- check_status(200)
49
- last_response.body.should == '{"items":[{"route_for_id":"1","content":"Test","links":[{"rel":"self","uri":"/route_for/1"}]}]}'
50
- end
51
-
52
- it """
53
- should be able to register multiple route_for's in one declaration
54
- """ do
55
- module Repository
56
- module Route_for
57
- def self.get_1(id)
58
- end
59
- def self.get_2(id)
60
- end
61
- end
62
- end
63
- route :route_for do
64
- id :route_for_id
65
- route_for :get_1
66
- route_for :get_2
67
- end
68
-
69
- get "/route_for/get_1/1"
70
- check_status(200)
71
- get "/route_for/get_2/2"
72
- check_status(200)
73
- end
74
-
75
- it """
76
- should be able to register multiple route_for's with POST in one declaration
77
- """ do
78
- module Repository
79
- module Route_for
80
- def self.post_1(id)
81
- end
82
- def self.post_2(id)
83
- end
84
- end
85
- end
86
- route :route_for do
87
- id :route_for_id
88
- route_for :post_1, :post
89
- route_for :post_2, :post
90
- end
91
-
92
- post "/route_for/post_1/1"
93
- check_status(200)
94
- post "/route_for/post_2/2"
95
- check_status(200)
96
- end
97
- end
@@ -1,65 +0,0 @@
1
- require './spec/spec_helper'
2
- require './lib/ananke/serialize'
3
-
4
- class Simple
5
- attr_accessor :name, :surname
6
- def initialize(name, surname)
7
- @name = name
8
- @surname = surname
9
- end
10
- end
11
-
12
- class SimplePlus
13
- attr_accessor :name, :surname, :cars
14
- def initialize(name, surname, cars)
15
- @name = name
16
- @surname = surname
17
- @cars = cars
18
- end
19
- end
20
-
21
- class Nested
22
- attr_accessor :id, :simple_plus
23
- def initialize(id, simple_plus)
24
- @id = id
25
- @simple_plus = simple_plus
26
- end
27
- end
28
-
29
- describe Serialize, '#self.to_hash' do
30
-
31
- it "should serialize a simple class to hash form" do
32
- obj = Simple.new('test', 'test')
33
- Serialize.to_h(obj).should == {"name"=>"test", "surname" => "test"}
34
- end
35
-
36
- it "should serialize a simple class with a hash to hash form" do
37
- obj = SimplePlus.new('test', 'test', {:one => 1, :two => 2})
38
- Serialize.to_h(obj).should == {"name" => "test", "surname" => "test", "cars" => {:one => 1, :two => 2}}
39
- end
40
-
41
- it "should serialize a nested class to hash form" do
42
- obj = Nested.new(1, SimplePlus.new('test', 'test', {:one => 1, :two => 2}))
43
- Serialize.to_h(obj).should == {"id" => 1, "simple_plus" => {"name" => "test", "surname" => "test", "cars" => {:one => 1, :two => 2}}}
44
- end
45
-
46
- end
47
-
48
- describe Serialize, '#self.to_json' do
49
-
50
- it "should serialize a simple class to json" do
51
- obj = Simple.new('test', 'test')
52
- Serialize.to_j(obj).should == '{"name":"test","surname":"test"}'
53
- end
54
-
55
- it "should serialize a simple class with a hash to json" do
56
- obj = SimplePlus.new('test', 'test', {:one => 1, :two => 2})
57
- Serialize.to_j(obj).should == '{"name":"test","surname":"test","cars":{"one":1,"two":2}}'
58
- end
59
-
60
- it "should serialize a nested class to json" do
61
- obj = Nested.new(1, SimplePlus.new('test', 'test', {:one => 1, :two => 2}))
62
- Serialize.to_j(obj).should == '{"id":1,"simple_plus":{"name":"test","surname":"test","cars":{"one":1,"two":2}}}'
63
- end
64
-
65
- end
@@ -1,91 +0,0 @@
1
- require './spec/spec_helper'
2
- require './lib/ananke'
3
-
4
- describe 'Resource' do
5
- include Rack::Test::Methods
6
- include Ananke
7
-
8
- def app
9
- Sinatra::Base
10
- end
11
-
12
- #--------------------------VALIDATION-----------------------------------
13
- it """
14
- Should be able to use Predefined Validation:
15
- length
16
- """ do
17
- module Repository
18
- module Basic
19
- def self.add(data)end
20
- end
21
- end
22
- route :basic do
23
- id :user_id
24
- required :username, :length => 4
25
- end
26
-
27
- post "/basic", body={:user_id => 1, :username => ''}
28
- check_status(400)
29
- last_response.body.should == 'username: Value must be at least 4 characters long'
30
-
31
- post "/basic", body={:user_id => 1, :username => '1234'}
32
- check_status(201)
33
- end
34
-
35
- it """
36
- Should be able to use Explicitly Defined Rule
37
- """ do
38
- module Ananke
39
- module Rules
40
- def self.validate_email
41
- value =~ /[\w._%-]+@[\w.-]+.[a-zA-Z]{2,4}/ ? nil : "Invalid Email: #{value}"
42
- end
43
- end
44
- end
45
- Ananke::Rules.respond_to?('validate_email').should == true
46
-
47
- module Repository
48
- module Explicit
49
- def self.add(data)end
50
- end
51
- end
52
- route :explicit do
53
- id :user_id
54
- required :email, :email
55
- end
56
-
57
- post "/explicit", body={:user_id => 1, :email => 'some'}
58
- check_status(400)
59
- last_response.body.should == 'email: Invalid Email: some'
60
-
61
- post "/explicit", body={:user_id => 1, :email => 'some1@some.com'}
62
- check_status(201)
63
- end
64
-
65
- it """
66
- Should be able to Add new Validations and Use them
67
- """ do
68
- rule :country, do
69
- value == 'South Africa' ? nil : 'Not from South Africa'
70
- end
71
- module Repository
72
- module Added
73
- def self.add(data)
74
- nil
75
- end
76
- end
77
- end
78
- route :added do
79
- id :user_id
80
- required :country, :country
81
- end
82
- Ananke::Rules.respond_to?('validate_country').should == true
83
-
84
- post "/added", body={:user_id => 1, :country => 'England'}
85
- check_status(400)
86
- last_response.body.should == 'country: Not from South Africa'
87
-
88
- post "/added", body={:user_id => 1, :country => 'South Africa'}
89
- check_status(201)
90
- end
91
- end