vagas-orientdb4r 0.5.2

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.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +26 -0
  4. data/.travis.yml +18 -0
  5. data/Gemfile +9 -0
  6. data/LICENSE +202 -0
  7. data/README.rdoc +124 -0
  8. data/Rakefile +43 -0
  9. data/changelog.txt +60 -0
  10. data/ci/initialize-ci.sh +36 -0
  11. data/fstudy/design_v1.dia +0 -0
  12. data/fstudy/design_v1.png +0 -0
  13. data/fstudy/domain_model.dia +0 -0
  14. data/fstudy/domain_model.png +0 -0
  15. data/fstudy/flat_class_perf.rb +56 -0
  16. data/fstudy/sample1_object_diagram.dia +0 -0
  17. data/fstudy/sample1_object_diagram.png +0 -0
  18. data/fstudy/study_case.rb +87 -0
  19. data/fstudy/technical_feasibility.rb +256 -0
  20. data/lib/orientdb4r.rb +115 -0
  21. data/lib/orientdb4r/bin/client.rb +86 -0
  22. data/lib/orientdb4r/bin/connection.rb +29 -0
  23. data/lib/orientdb4r/bin/constants.rb +20 -0
  24. data/lib/orientdb4r/bin/io.rb +38 -0
  25. data/lib/orientdb4r/bin/protocol28.rb +101 -0
  26. data/lib/orientdb4r/bin/protocol_factory.rb +25 -0
  27. data/lib/orientdb4r/chained_error.rb +37 -0
  28. data/lib/orientdb4r/client.rb +364 -0
  29. data/lib/orientdb4r/load_balancing.rb +113 -0
  30. data/lib/orientdb4r/node.rb +42 -0
  31. data/lib/orientdb4r/rest/client.rb +517 -0
  32. data/lib/orientdb4r/rest/excon_node.rb +115 -0
  33. data/lib/orientdb4r/rest/model.rb +159 -0
  34. data/lib/orientdb4r/rest/node.rb +43 -0
  35. data/lib/orientdb4r/rest/restclient_node.rb +77 -0
  36. data/lib/orientdb4r/rid.rb +54 -0
  37. data/lib/orientdb4r/utils.rb +203 -0
  38. data/lib/orientdb4r/version.rb +39 -0
  39. data/orientdb4r.gemspec +37 -0
  40. data/test/bin/test_client.rb +21 -0
  41. data/test/readme_sample.rb +38 -0
  42. data/test/test_client.rb +93 -0
  43. data/test/test_database.rb +261 -0
  44. data/test/test_ddo.rb +237 -0
  45. data/test/test_dmo.rb +115 -0
  46. data/test/test_document_crud.rb +184 -0
  47. data/test/test_gremlin.rb +52 -0
  48. data/test/test_helper.rb +10 -0
  49. data/test/test_loadbalancing.rb +81 -0
  50. data/test/test_utils.rb +67 -0
  51. metadata +136 -0
@@ -0,0 +1,203 @@
1
+ module Orientdb4r
2
+
3
+ module Utils
4
+
5
+ def verify_options(options, pattern)
6
+ raise ArgumentError, 'options cannot be nil' if options.nil?
7
+
8
+ # unknown key?
9
+ options.keys.each do |k|
10
+ raise ArgumentError, "unknow option: #{k}" unless pattern.keys.include? k
11
+ end
12
+ # missing mandatory option?
13
+ pattern.each do |k,v|
14
+ raise ArgumentError, "missing mandatory option: #{k}" if v == :mandatory and !options.keys.include? k
15
+ end
16
+ # option in a set of allowed values
17
+ pattern.each do |k,v|
18
+ if v.instance_of? Array
19
+ raise ArgumentError, "value '#{options[k]}' not in #{v.inspect}, key=#{k}" unless v.include?(options[k])
20
+ end
21
+ end
22
+
23
+ options
24
+ end
25
+
26
+ def verify_and_sanitize_options(options, pattern)
27
+ verify_options(options, pattern)
28
+
29
+ # set default values if missing in options
30
+ pattern.each do |k,v|
31
+ options[k] = v if !v.nil? and :optional != v and !options.keys.include? k
32
+ end
33
+ options
34
+ end
35
+
36
+
37
+ ###
38
+ # Checks if a given string is either 'nil' or empty string.
39
+ def blank?(str)
40
+ str.nil? or (str.is_a? String and str.strip.empty?)
41
+ end
42
+
43
+ ###
44
+ # Generates a random string with given length.
45
+ def random_string(len=8)
46
+ (0...len).map{65.+(rand(25)).chr}.join
47
+ end
48
+
49
+ ###
50
+ # Compares two given versions.
51
+ # ==== Returns
52
+ # * 1 if first > second
53
+ # * 0 if first == second
54
+ # * -1 if first < second
55
+ def compare_versions(first, second)
56
+ raise ArgumentError, "bad version format, version=#{first}" unless first =~ Orientdb4r::Client::SERVER_VERSION_PATTERN
57
+ raise ArgumentError, "bad version format, version=#{second}" unless second =~ Orientdb4r::Client::SERVER_VERSION_PATTERN
58
+
59
+ firstv = /^(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)/.match(first)[0]
60
+ secondv = /^(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)/.match(second)[0]
61
+
62
+ rslt = 0
63
+ rslt = 1 if firstv > secondv
64
+ rslt = -1 if firstv < secondv
65
+
66
+ yield rslt if block_given?
67
+
68
+ rslt
69
+ end
70
+
71
+ class Proxy
72
+
73
+ attr_reader :target, :context
74
+
75
+ def initialize(target, context)
76
+ @target = target
77
+ @context = context
78
+ end
79
+
80
+ def proxy(&block)
81
+ block.call
82
+ end
83
+
84
+ end # Proxy
85
+
86
+ end # Utils
87
+
88
+
89
+ # TODO extend it to work with already defined methods ('before :foo, :baz' after method definition) [BF #21]
90
+ module Aop2
91
+
92
+ def self.included(base)
93
+ base.extend ClassMethods2
94
+ base.init_aop_extension
95
+ end
96
+
97
+ def aop_context
98
+ Thread.current[:aop2_context]
99
+ end
100
+ def aop_context=(ctx={})
101
+ Thread.current[:aop2_context] = ctx
102
+ end
103
+
104
+ module ClassMethods2
105
+
106
+ def init_aop_extension
107
+ @hooks = {}
108
+ [:before, :after, :around].each { |where| @hooks[where] = Hash.new { |hash, key| hash[key] = [] }}
109
+ # will be like this:
110
+ # {:before=>{:disconnect=>[:assert_connected], :query=>[:assert_connected], :command=>[:assert_connected]}, :after=>{}, :around=>{}}
111
+ class << self
112
+ attr_reader :hooks
113
+ end
114
+ @@redefining = false # flag whether the process of method redefining is running
115
+ end
116
+ def method_added(method)
117
+ unless @@redefining # avoid recursion
118
+ redefine_method(method) if is_hooked?(method)
119
+ end
120
+ end
121
+
122
+ #----------------------------------------------------------------- Helpers
123
+
124
+ def is_hooked?(method)
125
+ # look into array of keys (method names) in 2nd level hashs (see above)
126
+ hooks.values.map(&:keys).flatten.uniq.include? method
127
+ end
128
+
129
+
130
+
131
+ def before(original_method, *hooks)
132
+ add_hook(:before, original_method, *hooks)
133
+ end
134
+ def after(original_method, *hooks)
135
+ add_hook(:after, original_method, *hooks)
136
+ end
137
+ def around(original_method, *hooks)
138
+ add_hook(:around, original_method, *hooks)
139
+ end
140
+
141
+
142
+ def add_hook(type, original_method, *hooks) #!!
143
+ Array(original_method).each do |method|
144
+ store_hook(type, method, *hooks)
145
+ end
146
+ end
147
+ def store_hook(type, method_name, *hook_methods) #!!
148
+ hooks[type.to_sym][method_name.to_sym] += hook_methods.flatten.map(&:to_sym)
149
+ end
150
+
151
+ def redefine_method(orig_method)
152
+ @@redefining = true
153
+
154
+ arity = instance_method(orig_method.to_sym).arity
155
+ params = ''
156
+ fixed_cnt = arity.abs
157
+ fixed_cnt -= 1 if arity < 0
158
+ # build up a list of params
159
+ 1.upto(fixed_cnt).each {|x| params << "p#{x},"}
160
+ params << "*argv" if arity < 0
161
+ params.gsub!(/,$/, '') # remove last ','
162
+
163
+ alias_method "#{orig_method}_aop2_orig".to_sym, orig_method.to_sym
164
+
165
+ class_eval <<-FILTER,__FILE__,__LINE__ + 1
166
+ def #{orig_method}(#{params})
167
+ self.aop_context = { :method => '#{orig_method}', :class => self.class }
168
+ begin
169
+ self.class.invoke_hooks(self, :before, :#{orig_method})
170
+ rslt = self.class.invoke_arround_hooks(self, :#{orig_method}, self.class.hooks[:around][:#{orig_method}].clone) {
171
+ #{orig_method}_aop2_orig(#{params})
172
+ }
173
+ self.class.invoke_hooks(self, :after, :#{orig_method})
174
+ # rescue Exception => e
175
+ # # TODO use logging
176
+ # $stderr.puts '' << e.class.name << ': ' << e.message
177
+ # $stderr.puts e.backtrace.inspect
178
+ # raise e
179
+ ensure
180
+ self.aop_context = nil
181
+ end
182
+ rslt
183
+ end
184
+ FILTER
185
+
186
+ @@redefining = false
187
+ end
188
+
189
+ def invoke_hooks(obj, hook_type, method_name)
190
+ hooks[hook_type][method_name.to_sym].each { |hook| obj.send hook }
191
+ end
192
+ def invoke_arround_hooks(obj, method_name, hooks, &block)
193
+ hook = hooks.slice! 0
194
+ return block.call if hook.nil? # call original method if no more hook
195
+
196
+ # invoke the hook with lambda containing recursion
197
+ obj.send(hook.to_sym) { invoke_arround_hooks(obj, method_name, hooks, &block); }
198
+ end
199
+
200
+ end # ClassMethods2
201
+ end # Aop2
202
+
203
+ end
@@ -0,0 +1,39 @@
1
+ module Orientdb4r
2
+
3
+ # Version history.
4
+ VERSION_HISTORY = [
5
+ ['0.5.2', '2016-09-17', "Compatible with OrientDB v2.2.x, PR #44, PR #45"],
6
+ ['0.5.1', '2015-03-26', "Enh #39, PR #40"],
7
+ ['0.5.0', '2015-03-03', "Compatible with OrientDB v2.x, PR #31, PR #32, PR #33"],
8
+ ['0.4.1', '2013-09-03', "Enh #24, Enh #26, Enh #27"],
9
+ ['0.4.0', '2013-08-14', "Closed gap between this driver and OrientDB v1.4.0+; Enh #20, BF #25"],
10
+ ['0.3.3', '2012-12-16', "Enh #18, Enh #19"],
11
+ ['0.3.2', '2012-11-02', "Enh #13, Enh #16"],
12
+ ['0.3.1', '2012-08-27', "Timeout for reuse of dirty nodes in load balancing; BF #14, BF #15"],
13
+ ['0.3.0', '2012-08-01', "Added support for cluster of distributed servers + load balancing"],
14
+ ['0.2.10', '2012-07-21', "Experimental support for Excon HTTP library with Keep-Alive connection"],
15
+ ['0.2.9', '2012-07-18', "Added feature Client#delete_database, New class Rid"],
16
+ ['0.2.8', '2012-07-16', "New exception handling, added feature Client#create_class(:properties)"],
17
+ ['0.2.7', '2012-07-07', "Added method Client#class_exists?"],
18
+ ['0.2.6', '2012-07-03', "BF #8, BF #6"],
19
+ ['0.2.5', '2012-07-01', "Added 'get_database' into database CRUD"],
20
+ # v-- https://groups.google.com/forum/?fromgroups#!topic/orient-database/5MAMCvFavTc
21
+ ['0.2.4', '2012-06-26', "Added session management"],
22
+ ['0.2.3', '2012-06-24', "Documents received by a query are kind of Orientdb4r::DocumentMetadata"],
23
+ # v-- https://groups.google.com/forum/?fromgroups#!topic/orient-database/jK4EZd068AE
24
+ # v-- https://groups.google.com/forum/?fromgroups#!topic/orient-database/nJOAsgwSnKI
25
+ ['0.2.2', '2012-06-23', "Added support for server version detection [r5913]"],
26
+ ['0.2.1', '2012-06-19', "Fixed linked property definition"],
27
+ ['0.2.0', '2012-06-12', "Introduces document's CRUD operations"],
28
+ ['0.1.2', '2012-06-10', 'Introduces new OClass module'],
29
+ ['0.1.1', '2012-06-08', 'First working version (including unit tests) released at github.com'],
30
+ ['0.1.0', '2012-06-02', 'Initial version on Ruby-1.9.3p194 and OrientDB-1.0.0']
31
+ ]
32
+
33
+ # Current version.
34
+ VERSION = VERSION_HISTORY[0][0]
35
+
36
+ # Driver identification.
37
+ DRIVER_NAME = 'Orientdb4r Ruby Client'
38
+
39
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.unshift File.expand_path('../lib', __FILE__)
4
+ require 'orientdb4r/version'
5
+
6
+ Gem::Specification.new do |s|
7
+
8
+ s.name = %q{vagas-orientdb4r}
9
+ s.version = Orientdb4r::VERSION
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new('> 1.3.1') if s.respond_to? :required_rubygems_version=
12
+ s.authors = ['Vaclav Sykora', 'Ronie Uliana']
13
+ s.date = Orientdb4r::VERSION_HISTORY[0][1]
14
+ s.description = %q{Orientdb4r provides a simple interface on top of OrientDB's RESTful HTTP API (VAGAS.com version)}
15
+ s.email = %q{vaclav.sykora@gmail.com ronie.uliana@gmail.com}
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.extra_rdoc_files = [
23
+ "LICENSE",
24
+ "README.rdoc"
25
+ ]
26
+ s.homepage = %q{http://github.com/vagascom/orientdb4r}
27
+ s.rdoc_options = ['--charset=UTF-8']
28
+ s.require_paths = ['lib']
29
+ s.rubygems_version = %q{1.3.7}
30
+ s.summary = %q{Ruby binding for Orient DB.}
31
+ s.license = 'Apache License, v2.0'
32
+
33
+ s.add_dependency(%q<rest-client>, ["~> 1.7"])
34
+
35
+ s.add_development_dependency "rake", "~> 10.3"
36
+
37
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ ###
4
+ # This class tests communication with OrientDB cluster and load balancing.
5
+ # Note: Test of binary communication.
6
+ class TestBinDatabase < Test::Unit::TestCase
7
+
8
+ DB = 'temp'
9
+ #Orientdb4r::logger.level = Logger::DEBUG
10
+
11
+ def setup
12
+ @client = Orientdb4r.client :binary => true
13
+ end
14
+
15
+ ###
16
+ # CONNECT
17
+ def test_connect
18
+ assert_nothing_thrown do @client.connect :database => DB, :user => 'admin', :password => 'admin'; end
19
+ end
20
+
21
+ end
@@ -0,0 +1,38 @@
1
+ $: << File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
2
+ require 'orientdb4r'
3
+
4
+ CLASS = 'myclass'
5
+
6
+ client = Orientdb4r.client # equivalent for :host => 'localhost', :port => 2480, :ssl => false
7
+
8
+ client.connect :database => 'temp', :user => 'admin', :password => 'admin'
9
+
10
+ unless client.class_exists? CLASS
11
+ client.create_class(CLASS) do |c|
12
+ c.property 'prop1', :integer, :notnull => true, :min => 1, :max => 99
13
+ c.property 'prop2', :string, :mandatory => true
14
+ c.link 'users', :linkset, 'OUser' # by default: :mandatory => false, :notnull => false
15
+ end
16
+ end
17
+
18
+ admin = client.query("SELECT FROM OUser WHERE name = 'admin'")[0]
19
+ 1.upto(5) do |i|
20
+ # insert link to admin only to first two
21
+ client.command "INSERT INTO #{CLASS} (prop1, prop2, users) VALUES (#{i}, 'text#{i}', [#{admin['@rid'] if i<3}])"
22
+ end
23
+
24
+ puts client.query "SELECT FROM #{CLASS}"
25
+ #>
26
+
27
+ puts client.query "SELECT count(*) FROM #{CLASS}"
28
+ #>
29
+
30
+ puts client.query "SELECT max(prop1) FROM #{CLASS}"
31
+ #>
32
+
33
+ puts client.query "TRAVERSE any() FROM (SELECT FROM #{CLASS} WHERE prop1 = 1)"
34
+ #>
35
+
36
+
37
+ client.drop_class CLASS
38
+ client.disconnect
@@ -0,0 +1,93 @@
1
+ require 'test_helper'
2
+
3
+ ###
4
+ # This class tests communication with OrientDB cluster and load balancing.
5
+ class TestClient < Test::Unit::TestCase
6
+
7
+ PROXY_URL = 'http://bad.domain.com'
8
+
9
+ ###
10
+ # Test inintialization of corresponding client type
11
+ def test_client_type
12
+ client = Orientdb4r.client :instance => :new
13
+ assert_instance_of Orientdb4r::RestClient, client
14
+ client = Orientdb4r.client :instance => :new, :binary => true
15
+ assert_instance_of Orientdb4r::Binary::BinClient, client
16
+ end
17
+
18
+ ###
19
+ # Test inintialization of single node.
20
+ def test_one_node_initialization
21
+ client = Orientdb4r.client :instance => :new
22
+ assert_not_nil client.nodes
23
+ assert_instance_of Array, client.nodes
24
+ assert_equal 1, client.nodes.size
25
+ assert_equal 2480, client.nodes[0].port
26
+ assert_equal false, client.nodes[0].ssl
27
+ end
28
+
29
+ ###
30
+ # Test inintialization of more nodes.
31
+ def test_nodes_initialization
32
+ client = Orientdb4r.client :nodes => [{}, {:port => 2481}], :instance => :new
33
+ assert_not_nil client.nodes
34
+ assert_instance_of Array, client.nodes
35
+ assert_equal 2, client.nodes.size
36
+ assert_equal 2480, client.nodes[0].port
37
+ assert_equal 2481, client.nodes[1].port
38
+ assert_equal false, client.nodes[0].ssl
39
+ assert_equal false, client.nodes[1].ssl
40
+
41
+ client = Orientdb4r.client :nodes => [{}, {:port => 2481}, {:port => 2482}], :instance => :new
42
+ assert_equal 3, client.nodes.size
43
+ end
44
+
45
+ ###
46
+ # Tests initialization of connection library.
47
+ def test_connection_library
48
+ # restclient
49
+ client = Orientdb4r.client :instance => :new
50
+ assert_equal :restclient, client.connection_library
51
+ assert_instance_of Orientdb4r::RestClientNode, client.nodes[0]
52
+
53
+ # excon
54
+ if Gem::Specification::find_all_by_name('excon').any?
55
+ client = Orientdb4r.client :connection_library => :excon, :instance => :new
56
+ assert_equal :excon, client.connection_library
57
+ assert_instance_of Orientdb4r::ExconNode, client.nodes[0]
58
+ end
59
+ end
60
+
61
+ ###
62
+ # Tests initialization of proxy.
63
+ def test_proxy
64
+ # no proxy - resclient
65
+ client = Orientdb4r.client :instance => :new
66
+ assert_nil client.proxy
67
+ assert_nil RestClient.proxy
68
+
69
+ # proxy - restclient
70
+ client = Orientdb4r.client :proxy => PROXY_URL, :instance => :new
71
+ assert_equal PROXY_URL, client.proxy
72
+ assert_equal PROXY_URL, RestClient.proxy
73
+ assert_raise Orientdb4r::ConnectionError do
74
+ client.connect :database => 'temp', :user => 'admin', :password => 'admin'
75
+ end
76
+ RestClient.proxy = nil # restore no setting
77
+
78
+ if Gem::Specification::find_all_by_name('excon').any?
79
+ # no proxy - excon
80
+ client = Orientdb4r.client :connection_library => :excon, :instance => :new
81
+ assert_nil client.proxy
82
+ assert_nil client.nodes[0].proxy
83
+
84
+ # proxy - excon
85
+ client = Orientdb4r.client :connection_library => :excon, :proxy => PROXY_URL, :instance => :new
86
+ assert_equal PROXY_URL, client.proxy
87
+ assert_equal PROXY_URL, client.nodes[0].proxy
88
+ assert_raise Orientdb4r::ConnectionError do
89
+ client.connect :database => 'temp', :user => 'admin', :password => 'admin'
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,261 @@
1
+ require 'test_helper'
2
+
3
+ ###
4
+ # This class tests DB management.
5
+ class TestDatabase < Test::Unit::TestCase
6
+
7
+ DB = 'temp'
8
+
9
+ def setup
10
+ @client = Orientdb4r.client
11
+ end
12
+
13
+ def teardown
14
+ if @client.database_exists? :database => 'UniT', :user => 'root', :password => DB_ROOT_PASS
15
+ @client.delete_database :database => 'UniT', :user => 'root', :password => DB_ROOT_PASS
16
+ end
17
+ @client.disconnect
18
+ end
19
+
20
+ ###
21
+ # CONNECT
22
+ def test_connect
23
+ assert_nothing_thrown do @client.connect :database => DB, :user => 'admin', :password => 'admin'; end
24
+ rslt = @client.connect :database => DB, :user => 'admin', :password => 'admin'
25
+ assert_instance_of TrueClass, rslt
26
+
27
+ assert_equal 'admin', @client.user
28
+ assert_equal 'admin', @client.password
29
+ assert_equal DB, @client.database
30
+
31
+ # connection refused
32
+ client = Orientdb4r.client :port => 2840, :instance => :new
33
+ assert_raise Orientdb4r::ConnectionError do
34
+ client.connect :database => DB, :user => 'admin', :password => 'admin'
35
+ end
36
+
37
+ # bad DB name
38
+ assert_raise Orientdb4r::UnauthorizedError do
39
+ @client.connect :database => 'unknown_db', :user => 'admin', :password => 'admin'
40
+ end
41
+ # !!! curl -v --user admin:adminX http://localhost:2480/connect/foo/bar
42
+ # # bad DB name with '/' => wrong REST resource
43
+ # assert_raise Orientdb4r::ServerError do
44
+ # @client.connect :database => 'temp/temp', :user => 'admin', :password => 'admin'
45
+ # end
46
+ # bad credentials
47
+ assert_raise Orientdb4r::UnauthorizedError do
48
+ @client.connect :database => DB, :user => 'admin1', :password => 'admin'
49
+ end
50
+ end
51
+
52
+
53
+ ###
54
+ # DISCONNECT
55
+ def test_disconnect
56
+ @client.connect :database => DB, :user => 'admin', :password => 'admin'
57
+ assert @client.connected?
58
+ assert_nothing_thrown do @client.disconnect; end
59
+ assert !@client.connected?
60
+ # unable to query after disconnect
61
+ assert_raise Orientdb4r::ConnectionError do @client.query 'SELECT FROM OUser'; end
62
+
63
+ assert_nil @client.user
64
+ assert_nil @client.password
65
+ assert_nil @client.database
66
+ end
67
+
68
+
69
+ ###
70
+ # CREATE DATABASE
71
+ def test_create_database
72
+ # bad options
73
+ assert_raise ArgumentError do @client.create_database(:database => 'UniT', :storage => :foo); end
74
+ assert_raise ArgumentError do @client.create_database(:database => 'UniT', :type => :foo); end
75
+
76
+ assert_nothing_thrown do
77
+ @client.create_database :database => 'UniT', :user => 'root', :password => DB_ROOT_PASS
78
+ end
79
+ assert_nothing_thrown do
80
+ @client.get_database :database => 'UniT', :user => 'admin', :password => 'admin'
81
+ end
82
+ # creating an existing DB
83
+ assert_raise Orientdb4r::StateConflictError do
84
+ @client.create_database :database => 'UniT', :user => 'root', :password => DB_ROOT_PASS
85
+ end
86
+ # insufficient rights
87
+ assert_raise Orientdb4r::UnauthorizedError do
88
+ @client.create_database :database => 'UniT1', :user => 'admin', :password => 'admin'
89
+ end
90
+
91
+ # By convention 3 users are always created by default every time you create a new database.
92
+ # Default users are: admin, reader, writer
93
+ assert_nothing_thrown do
94
+ @client.connect :database => 'UniT', :user => 'admin', :password => 'admin'
95
+ end
96
+ @client.delete_database({:database => 'UniT', :user => 'root', :password => DB_ROOT_PASS})
97
+
98
+ # create non-default DB: storage=local;type=graph
99
+ assert_nothing_thrown do
100
+ @client.create_database :database => 'UniT', :user => 'root', :password => DB_ROOT_PASS, :storage => :plocal, :type => :graph
101
+ @client.delete_database :database => 'UniT', :user => 'root', :password => DB_ROOT_PASS
102
+ end
103
+ end
104
+
105
+
106
+ ###
107
+ # GET DATABASE
108
+ def test_get_database
109
+ # not connected - allowed with additional authentication
110
+ assert_nothing_thrown do @client.get_database :database => DB, :user => 'admin', :password => 'admin' ; end
111
+ assert_raise Orientdb4r::ConnectionError do @client.get_database; end
112
+ # connected
113
+ @client.connect :database => DB, :user => 'admin', :password => 'admin'
114
+ assert_nothing_thrown do @client.get_database; end # gets info about connected DB
115
+
116
+ rslt = @client.get_database
117
+ assert_not_nil rslt
118
+ assert_instance_of Hash, rslt
119
+ assert !rslt.empty?
120
+ # server
121
+ assert rslt.include? 'server'
122
+ assert_instance_of Hash, rslt['server']
123
+ assert !rslt['server'].empty?
124
+ assert rslt['server'].include? 'version'
125
+ # classes
126
+ assert rslt.include? 'classes'
127
+ assert_instance_of Array, rslt['classes']
128
+ assert !rslt['classes'].empty?
129
+
130
+ # bad databases
131
+ assert_raise Orientdb4r::UnauthorizedError do @client.get_database :database => 'UnknownDB'; end
132
+ assert_raise Orientdb4r::ServerError do @client.get_database :database => 'temp/admin'; end # bad REST resource
133
+
134
+
135
+ # database_exists?
136
+ assert @client.database_exists?(:database => DB, :user => 'admin', :password => 'admin')
137
+ assert @client.database_exists?(:database => DB) # use credentials of logged in user
138
+ assert !@client.database_exists?(:database => 'UnknownDB')
139
+ assert !@client.database_exists?(:database => 'temp/admin')
140
+ end
141
+
142
+
143
+ ###
144
+ # DELETE DATABASE
145
+ def test_delete_database
146
+ @client.create_database :database => 'UniT', :user => 'root', :password => DB_ROOT_PASS
147
+
148
+ # deleting non-existing DB
149
+ assert_raise Orientdb4r::ServerError do
150
+ @client.delete_database :database => 'UniT1', :user => 'root', :password => DB_ROOT_PASS
151
+ end
152
+ # insufficient rights
153
+ assert_raise Orientdb4r::UnauthorizedError do
154
+ @client.delete_database :database => 'UniT', :user => 'admin', :password => 'admin'
155
+ end
156
+
157
+ assert_nothing_thrown do
158
+ @client.delete_database({:database => 'UniT', :user => 'root', :password => DB_ROOT_PASS})
159
+ end
160
+ end
161
+
162
+
163
+ ###
164
+ # SERVER info
165
+ def xtest_server
166
+ # admin/admin has not 'server.info' resource access in standard installation
167
+ assert_raise Orientdb4r::OrientdbError do @client.server :user => 'admin', :password => 'admin'; end
168
+
169
+ assert_nothing_thrown do @client.server :user => 'root', :password => DB_ROOT_PASS; end
170
+ rslt = @client.server :user => 'root', :password => DB_ROOT_PASS
171
+ assert_instance_of Hash, rslt
172
+ assert rslt.include? 'connections'
173
+ assert_not_nil rslt['connections']
174
+ end
175
+
176
+
177
+ ###
178
+ # GET List Databases
179
+ # Retrieves the available databases.
180
+ def test_list_databases
181
+ dbs = @client.list_databases :user => 'root', :password => DB_ROOT_PASS
182
+ assert_not_nil dbs
183
+ assert_instance_of Array, dbs
184
+ assert !dbs.empty?
185
+ assert dbs.include? DB
186
+ end
187
+
188
+
189
+ ###
190
+ # Test of :assert_connected before advice.
191
+ def test_assert_connected
192
+ @client.disconnect if @client.connected? # to be sure it this method is called as first in this TestCase and thread local client is still connected
193
+ assert_raise Orientdb4r::ConnectionError do @client.query 'SELECT FROM OUser'; end
194
+ assert_raise Orientdb4r::ConnectionError do @client.command "INSERT INTO OUser(name) VALUES('x')"; end
195
+ assert_raise Orientdb4r::ConnectionError do @client.gremlin("g.addVertex('class:X', 'prop1', 1, 'prop2', 'string1')"); end
196
+ assert_raise Orientdb4r::ConnectionError do @client.batch({:transaction => true, :operations => []}); end
197
+ #BF #21 assert_raise Orientdb4r::ConnectionError do @client.create_class 'x'; end
198
+ assert_raise Orientdb4r::ConnectionError do @client.create_property 'x', 'prop', :boolean; end
199
+ assert_raise Orientdb4r::ConnectionError do @client.class_exists? 'x'; end
200
+ assert_raise Orientdb4r::ConnectionError do @client.get_class 'x'; end
201
+ assert_raise Orientdb4r::ConnectionError do @client.drop_class 'x'; end
202
+ assert_raise Orientdb4r::ConnectionError do @client.create_document({ '@class' => 'x', :prop => 1 }); end
203
+ assert_raise Orientdb4r::ConnectionError do @client.get_document('#1:0'); end
204
+ assert_raise Orientdb4r::ConnectionError do @client.update_document({}); end
205
+ assert_raise Orientdb4r::ConnectionError do @client.delete_document('#1:0'); end
206
+ end
207
+
208
+
209
+ ###
210
+ # Tests using of session ID.
211
+ def test_session_id
212
+ client = Orientdb4r.client :instance => :new
213
+ assert_nil client.nodes[0].session_id
214
+ client.connect :database => DB, :user => 'admin', :password => 'admin'
215
+ session_id = client.nodes[0].session_id
216
+ assert_not_nil session_id
217
+ client.query 'SELECT count(*) FROM OUser'
218
+ assert_equal session_id, client.nodes[0].session_id
219
+ client.get_class 'OUser'
220
+ assert_equal session_id, client.nodes[0].session_id
221
+ client.disconnect
222
+ assert_nil client.nodes[0].session_id
223
+ end
224
+
225
+
226
+ ###
227
+ # EXPORT
228
+ def test_export
229
+ client = Orientdb4r.client :instance => :new
230
+
231
+ # export of connected database
232
+ client.connect :database => DB, :user => 'admin', :password => 'admin'
233
+ rslt = client.export
234
+ assert File.exist? './temp.gz'
235
+ assert File.file? './temp.gz'
236
+ assert 'temp.gz', rslt
237
+ File.delete './temp.gz'
238
+
239
+ # export with given file
240
+ given_filename = "#{Dir.tmpdir}/TEMP.gz"
241
+ client.export :file => given_filename
242
+ assert File.exist? given_filename
243
+ assert File.file? given_filename
244
+ assert given_filename, rslt
245
+
246
+ # explicit given DB
247
+ client.disconnect
248
+ assert_nothing_thrown do
249
+ client.export :database => DB, :user => 'admin', :password => 'admin', :file => given_filename
250
+ end
251
+ # unknow DB
252
+ assert_raise Orientdb4r::UnauthorizedError do
253
+ client.export :database => 'unknown', :user => 'admin', :password => 'admin'
254
+ end
255
+ # bad password
256
+ assert_raise Orientdb4r::UnauthorizedError do
257
+ client.export :database => DB, :user => 'admin', :password => 'unknown'
258
+ end
259
+ end
260
+
261
+ end