orientdb4r 0.2.5 → 0.2.6

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/README.rdoc CHANGED
@@ -13,6 +13,9 @@ see Wiki page for more sample at https://github.com/veny/orientdb4r/wiki
13
13
 
14
14
  client = Orientdb4r.client # equivalent for :host => 'localhost', :port => 2480, :ssl => false
15
15
 
16
+ client.database_exists? :database => 'temp', :user => 'admin', :password => 'admin'
17
+ => true
18
+
16
19
  client.connect :database => 'temp', :user => 'admin', :password => 'admin'
17
20
 
18
21
  client.create_class(CLASS) do |c|
@@ -28,22 +31,22 @@ see Wiki page for more sample at https://github.com/veny/orientdb4r/wiki
28
31
  end
29
32
 
30
33
  puts client.query "SELECT FROM #{CLASS}"
31
- > {"@type"=>"d", "@rid"=>"#6:0", "@version"=>0, "@class"=>"myclass", "prop1"=>1, "prop2"=>"text1", "users"=>["#4:0"]}
32
- > {"@type"=>"d", "@rid"=>"#6:1", "@version"=>0, "@class"=>"myclass", "prop1"=>2, "prop2"=>"text2", "users"=>["#4:0"]}
33
- > {"@type"=>"d", "@rid"=>"#6:2", "@version"=>0, "@class"=>"myclass", "prop1"=>3, "prop2"=>"text3", "users"=>[]}
34
- > {"@type"=>"d", "@rid"=>"#6:3", "@version"=>0, "@class"=>"myclass", "prop1"=>4, "prop2"=>"text4", "users"=>[]}
35
- > {"@type"=>"d", "@rid"=>"#6:4", "@version"=>0, "@class"=>"myclass", "prop1"=>5, "prop2"=>"text5", "users"=>[]}
34
+ => {"@type"=>"d", "@rid"=>"#6:0", "@version"=>0, "@class"=>"myclass", "prop1"=>1, "prop2"=>"text1", "users"=>["#4:0"]}
35
+ => {"@type"=>"d", "@rid"=>"#6:1", "@version"=>0, "@class"=>"myclass", "prop1"=>2, "prop2"=>"text2", "users"=>["#4:0"]}
36
+ => {"@type"=>"d", "@rid"=>"#6:2", "@version"=>0, "@class"=>"myclass", "prop1"=>3, "prop2"=>"text3", "users"=>[]}
37
+ => {"@type"=>"d", "@rid"=>"#6:3", "@version"=>0, "@class"=>"myclass", "prop1"=>4, "prop2"=>"text4", "users"=>[]}
38
+ => {"@type"=>"d", "@rid"=>"#6:4", "@version"=>0, "@class"=>"myclass", "prop1"=>5, "prop2"=>"text5", "users"=>[]}
36
39
 
37
40
  puts client.query "SELECT count(*) FROM #{CLASS}"
38
- > {"@type"=>"d", "@version"=>0, "count"=>5, "@fieldTypes"=>"count=l"}
41
+ => {"@type"=>"d", "@version"=>0, "count"=>5, "@fieldTypes"=>"count=l"}
39
42
 
40
43
  puts client.query "SELECT max(prop1) FROM #{CLASS}"
41
- > {"@type"=>"d", "@version"=>0, "max"=>5}
44
+ => {"@type"=>"d", "@version"=>0, "max"=>5}
42
45
 
43
46
  puts client.query "TRAVERSE any() FROM (SELECT FROM #{CLASS} WHERE prop1 = 1)"
44
- > {"@type"=>"d", "@rid"=>"#6:0", "@version"=>0, "@class"=>"myclass", "prop1"=>1, "prop2"=>"text1", "users"=>["#4:0"]}
45
- > {"@type"=>"d", "@rid"=>"#4:0", "@version"=>0, "@class"=>"OUser", "name"=>"admin", "password"=>"{SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873FC4BB8A81F6F2AB448A918", "status"=>"ACTIVE", "roles"=>["#3:0"]}
46
- > {"@type"=>"d", "@rid"=>"#3:0", "@version"=>0, "@class"=>"ORole", "name"=>"admin", "mode"=>1, "rules"=>{}, "@fieldTypes"=>"mode=b"}
47
+ => {"@type"=>"d", "@rid"=>"#6:0", "@version"=>0, "@class"=>"myclass", "prop1"=>1, "prop2"=>"text1", "users"=>["#4:0"]}
48
+ => {"@type"=>"d", "@rid"=>"#4:0", "@version"=>0, "@class"=>"OUser", "name"=>"admin", "password"=>"{SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873FC4BB8A81F6F2AB448A918", "status"=>"ACTIVE", "roles"=>["#3:0"]}
49
+ => {"@type"=>"d", "@rid"=>"#3:0", "@version"=>0, "@class"=>"ORole", "name"=>"admin", "mode"=>1, "rules"=>{}, "@fieldTypes"=>"mode=b"}
47
50
 
48
51
 
49
52
  client.drop_class CLASS
@@ -72,6 +75,7 @@ see Wiki page for more sample at https://github.com/veny/orientdb4r/wiki
72
75
  Tested on
73
76
  * Ruby 1.9.3
74
77
  * OrientDB 1.0.0
78
+ * OrientDB 1.0.1
75
79
  * OrientDB 1.1.0-SNAPSHOT r5913+
76
80
 
77
81
  == TESTS
@@ -62,17 +62,19 @@ module Orientdb4r
62
62
 
63
63
  ###
64
64
  # Retrieves all the information about a database.
65
- def get_database(name)
65
+ # Client has not to be connected to see databases suitable to connect.
66
+ def get_database(options)
66
67
  raise NotImplementedError, 'this should be overridden by concrete client'
67
68
  end
68
69
 
69
70
 
70
71
  ###
71
72
  # Checks existence of a given database.
72
- def database_exists?(name)
73
+ # Client has not to be connected to see databases suitable to connect.
74
+ def database_exists?(options)
73
75
  rslt = true
74
76
  begin
75
- get_database name
77
+ get_database options
76
78
  rescue NotFoundError
77
79
  rslt = false
78
80
  end
@@ -84,7 +86,7 @@ module Orientdb4r
84
86
 
85
87
  ###
86
88
  # Executes a query against the database.
87
- def query(sql)
89
+ def query(sql, options)
88
90
  raise NotImplementedError, 'this should be overridden by concrete client'
89
91
  end
90
92
 
@@ -6,7 +6,9 @@ module Orientdb4r
6
6
  # Name of cookie that represents a session.
7
7
  SESSION_COOKIE_NAME = 'OSESSIONID'
8
8
 
9
- before [:create_database, :get_database, :get_class, :query, :command], :assert_connected
9
+ before [:query, :command], :assert_connected
10
+ before [:create_class, :get_class, :drop_class, :create_property], :assert_connected
11
+ before [:create_document, :get_document, :update_document, :delete_document], :assert_connected
10
12
  around [:query, :command], :time_around
11
13
 
12
14
  attr_reader :host, :port, :ssl, :user, :password, :database, :session_id
@@ -130,9 +132,23 @@ module Orientdb4r
130
132
  end
131
133
 
132
134
 
133
- def get_database(name) #:nodoc:
135
+ def get_database(options=nil) #:nodoc:
136
+ raise ArgumentError, 'options have to be a Hash' if !options.nil? and !options.kind_of? Hash
137
+
138
+ if options.nil?
139
+ # use values from connect
140
+ raise ConnectionError, 'client has to be connected if no params' unless connected?
141
+ options = { :database => database, :user => user, :password => password }
142
+ end
143
+
144
+ options_pattern = { :database => :mandatory, :user => :optional, :password => :optional }
145
+ verify_options(options, options_pattern)
146
+
147
+ u = options.include?(:user) ? options[:user] : user
148
+ p = options.include?(:password) ? options[:password] : password
149
+ resource = ::RestClient::Resource.new(url, :user => u, :password => p)
134
150
  begin
135
- response = @resource["database/#{name}"].get
151
+ response = resource["database/#{options[:database]}"].get
136
152
  rescue
137
153
  raise NotFoundError
138
154
  end
@@ -142,10 +158,15 @@ module Orientdb4r
142
158
 
143
159
  # ---------------------------------------------------------------------- SQL
144
160
 
145
- def query(sql) #:nodoc:
161
+ def query(sql, options=nil) #:nodoc:
146
162
  raise ArgumentError, 'query is blank' if blank? sql
147
163
 
148
- response = @resource["query/#{@database}/sql/#{CGI::escape(sql)}"].get
164
+ options_pattern = { :limit => :optional }
165
+ verify_options(options, options_pattern) unless options.nil?
166
+
167
+ limit = ''
168
+ limit = "/#{options[:limit]}" if !options.nil? and options.include?(:limit)
169
+ response = @resource["query/#{@database}/sql/#{CGI::escape(sql)}#{limit}"].get
149
170
  entries = process_response(response)
150
171
  rslt = entries['result']
151
172
  # mixin all document entries (they have '@class' attribute)
@@ -78,30 +78,6 @@ module Orientdb4r
78
78
 
79
79
  end # Proxy
80
80
 
81
-
82
- class DataGenerator
83
-
84
- def initialize
85
- @words = IO.readlines('/usr/share/dict/words')
86
- 0.upto(@words.size - 1) do |i|
87
- word = @words[i]
88
- word.strip!
89
- idx = word.index("'")
90
- word = word[0..(idx - 1)] unless idx.nil?
91
- @words[i] = word
92
- end
93
- @words.uniq
94
- Orientdb4r::logger.info "DataGenerator: #{@words.size} words"
95
- end
96
-
97
- ###
98
- # Gets a random word.
99
- def word
100
- @words[rand(@words.size)]
101
- end
102
-
103
- end # DataGenerator
104
-
105
81
  end # Utils
106
82
 
107
83
 
@@ -2,6 +2,7 @@ module Orientdb4r
2
2
 
3
3
  # Version history.
4
4
  VERSION_HISTORY = [
5
+ ['0.2.6', '2012-07-03', "BF #8, BF #6"],
5
6
  ['0.2.5', '2012-07-01', "Added 'get_database' into database CRUD"],
6
7
  # v-- https://groups.google.com/forum/?fromgroups#!topic/orient-database/5MAMCvFavTc
7
8
  ['0.2.4', '2012-06-26', "Added session management"],
@@ -95,20 +95,25 @@ class TestDatabase < Test::Unit::TestCase
95
95
  ###
96
96
  # GET DATABASE
97
97
  def test_get_database
98
+ # not connected
99
+ assert_nothing_thrown do @client.get_database :database => 'temp', :user => 'admin', :password => 'admin' ; end
100
+ assert_raise Orientdb4r::ConnectionError do @client.get_database; end
101
+ # connected
98
102
  @client.connect :database => 'temp', :user => 'admin', :password => 'admin'
99
- assert_nothing_thrown do @client.get_database 'temp'; end
103
+ assert_nothing_thrown do @client.get_database; end # gets info about connected DB
100
104
 
101
- rslt = @client.get_database 'temp'
105
+ rslt = @client.get_database
102
106
  assert_not_nil rslt
103
107
  assert_instance_of Hash, rslt
104
108
  assert rslt.include? 'classes'
105
- assert @client.database_exists?('temp')
109
+ assert @client.database_exists?(:database => 'temp', :user => 'admin', :password => 'admin')
110
+ assert @client.database_exists?(:database => 'temp') # use credentials of logged in user
106
111
 
107
112
  # bad databases
108
- assert_raise Orientdb4r::NotFoundError do @client.get_database 'UnknownDB'; end
109
- assert_raise Orientdb4r::NotFoundError do @client.get_database 'temp/admin'; end
110
- assert !@client.database_exists?('UnknownDB')
111
- assert !@client.database_exists?('temp/admin')
113
+ assert_raise Orientdb4r::NotFoundError do @client.get_database :database => 'UnknownDB'; end
114
+ assert_raise Orientdb4r::NotFoundError do @client.get_database :database => 'temp/admin'; end
115
+ assert !@client.database_exists?(:database => 'UnknownDB')
116
+ assert !@client.database_exists?(:database => 'temp/admin')
112
117
  end
113
118
 
114
119
 
@@ -126,4 +131,20 @@ class TestDatabase < Test::Unit::TestCase
126
131
  assert_not_nil rslt['connections']
127
132
  end
128
133
 
134
+
135
+ ###
136
+ # Test of :assert_connected before advice.
137
+ def test_assert_connected
138
+ assert_raise Orientdb4r::ConnectionError do @client.query 'SELECT FROM OUser'; end
139
+ assert_raise Orientdb4r::ConnectionError do @client.query "INSERT INTO OUser(name) VALUES('x')"; end
140
+ assert_raise Orientdb4r::ConnectionError do @client.create_class 'x'; end
141
+ assert_raise Orientdb4r::ConnectionError do @client.create_property 'x', 'prop', :boolean; end
142
+ assert_raise Orientdb4r::ConnectionError do @client.get_class 'x'; end
143
+ assert_raise Orientdb4r::ConnectionError do @client.drop_class 'x'; end
144
+ assert_raise Orientdb4r::ConnectionError do @client.create_document({ '@class' => 'x', :prop => 1 }); end
145
+ assert_raise Orientdb4r::ConnectionError do @client.get_document('#1:0'); end
146
+ assert_raise Orientdb4r::ConnectionError do @client.update_document({}); end
147
+ assert_raise Orientdb4r::ConnectionError do @client.delete_document('#1:0'); end
148
+ end
149
+
129
150
  end
data/test/test_dmo.rb CHANGED
@@ -14,6 +14,7 @@ class TestDmo < Test::Unit::TestCase
14
14
  def setup
15
15
  @client = Orientdb4r.client
16
16
  @client.connect :database => DB, :user => 'admin', :password => 'admin'
17
+ @client.drop_class(CLASS) # just to be sure if the previous test failed
17
18
  @client.create_class(CLASS) do |c|
18
19
  c.property 'prop1', :integer
19
20
  c.property 'prop2', :string, :mandatory => true, :notnull => :true, :min => 1, :max => 99
@@ -56,21 +57,27 @@ class TestDmo < Test::Unit::TestCase
56
57
  ###
57
58
  # SELECT
58
59
  def test_query
59
- 1.upto(10) do |i|
60
+ 1.upto(25) do |i|
60
61
  @client.command "INSERT INTO #{CLASS} (prop1, prop2, friends) VALUES (#{i}, 'string#{i}', [#{@admin['@rid']}])"
61
62
  end
62
63
 
63
64
  entries = @client.query("SELECT FROM #{CLASS}")
65
+ assert_not_nil entries
64
66
  assert_instance_of Array, entries
65
- assert_equal 10, entries.size
67
+ assert_equal 20, entries.size # 20 is default limit
66
68
  entries.each { |doc| assert doc.kind_of? Orientdb4r::DocumentMetadata }
69
+ # limit
70
+ assert_equal 5, @client.query("SELECT FROM #{CLASS} LIMIT 5").size
71
+ entries = @client.query "SELECT FROM #{CLASS}", :limit => 100
72
+ assert_equal 25, entries.size
73
+ assert_raise ArgumentError do @client.query "SELECT FROM #{CLASS}", :unknown => 100; end
67
74
 
68
75
  assert_equal 1, @client.query("SELECT FROM #{CLASS} WHERE prop1 = 1").size
69
- assert_equal 0, @client.query("SELECT FROM #{CLASS} WHERE prop1 = 11").size
76
+ assert_equal 0, @client.query("SELECT FROM #{CLASS} WHERE prop1 = -1").size
70
77
  # graph
71
78
  rid = @client.query("SELECT FROM #{CLASS} WHERE prop1 = 1")[0]['@rid']
72
79
  gr = @client.query("SELECT FROM (TRAVERSE * FROM #{rid})")
73
- assert_equal 3, gr.size # # entries: testing, OUser, ORole
80
+ assert_equal 3, gr.size # entries: testing, OUser, ORole
74
81
  assert_equal 1, gr.select { |e| e if e['@class'] == CLASS }.size
75
82
  assert_equal 1, gr.select { |e| e if e['@class'] == 'OUser' }.size
76
83
  assert_equal 1, gr.select { |e| e if e['@class'] == 'ORole' }.size
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orientdb4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-01 00:00:00.000000000 Z
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client