arangorb 0.1.0

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.
@@ -0,0 +1,138 @@
1
+ # === GRAPH VERTEX ===
2
+
3
+ # ==== DOCUMENT ====
4
+
5
+ class ArangoV < ArangoDoc
6
+ def initialize(key: nil, collection: @@collection, graph: @@graph, database: @@database, body: {})
7
+ if collection.is_a?(String)
8
+ @collection = collection
9
+ elsif collection.is_a?(ArangoC)
10
+ @collection = collection.collection
11
+ else
12
+ raise "collection should be a String or an ArangoC instance, not a #{collection.class}"
13
+ end
14
+
15
+ if graph.is_a?(String)
16
+ @graph = graph
17
+ elsif graph.is_a?(ArangoG)
18
+ @graph = graph.graph
19
+ else
20
+ raise "graph should be a String or an ArangoG instance, not a #{graph.class}"
21
+ end
22
+
23
+ if database.is_a?(String)
24
+ @database = database
25
+ else
26
+ raise "database should be a String, not a #{database.class}"
27
+ end
28
+
29
+ if key.is_a?(String) || key.nil?
30
+ @key = key
31
+ unless key.nil?
32
+ body["_key"] = @key
33
+ @id = "#{@collection}/#{@key}"
34
+ end
35
+ else
36
+ raise "key should be a String, not a #{key.class}"
37
+ end
38
+
39
+ if body.is_a?(Hash)
40
+ @body = body
41
+ else
42
+ raise "body should be a Hash, not a #{body.class}"
43
+ end
44
+ end
45
+
46
+ attr_reader :key, :id, :body, :database, :graph, :collection
47
+
48
+ # === GET ===
49
+
50
+ def retrieve
51
+ result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}").parsed_response
52
+ if @@verbose
53
+ @body = result["vertex"] unless result["error"]
54
+ result
55
+ else
56
+ if result["error"]
57
+ result["errorMessage"]
58
+ else
59
+ @body = result["vertex"]
60
+ self
61
+ end
62
+ end
63
+ end
64
+
65
+ # === POST ====
66
+
67
+ def create(body: @body, waitForSync: nil)
68
+ query = {"waitForSync" => waitForSync}.delete_if{|k,v| v.nil?}
69
+ body["_key"] = @key if body["_key"].nil? && !@key.nil?
70
+ new_Document = { :body => body.to_json, :query => query }
71
+ result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@collection}", new_Document).parsed_response
72
+ return_result(result, body)
73
+ end
74
+ alias create_vertex create
75
+
76
+ # === MODIFY ===
77
+
78
+ def replace(body: {}, waitForSync: nil)
79
+ query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
80
+ new_Document = { :body => body.to_json, :query => query }
81
+ result = self.class.put("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", new_Document).parsed_response
82
+ return_result(result, body)
83
+ end
84
+
85
+ def update(body: {}, waitForSync: nil, keepNull: nil)
86
+ query = {"waitForSync" => waitForSync, "keepNull" => keepNull}.delete_if{|k,v| v.nil?}
87
+ new_Document = { :body => body.to_json, :query => query }
88
+ result = self.class.patch("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", new_Document).parsed_response
89
+ if @@verbose
90
+ unless result["error"]
91
+ @key = result["_key"]
92
+ @id = "#{@collection}/#{@key}"
93
+ @body = body
94
+ end
95
+ result
96
+ else
97
+ if result["error"]
98
+ result["errorMessage"]
99
+ else
100
+ @key = result["_key"]
101
+ @id = "#{@collection}/#{@key}"
102
+ @body = @body.merge(body)
103
+ self
104
+ end
105
+ end
106
+ end
107
+
108
+ # === DELETE ===
109
+
110
+ def destroy(body: nil, waitForSync: nil)
111
+ query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
112
+ new_Document = { :query => query }
113
+ result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}").parsed_response
114
+ @@verbose ? result : result["error"] ? result["errorMessage"] : true
115
+ end
116
+
117
+ # === UTILITY ===
118
+
119
+ def return_result(result, body)
120
+ if @@verbose
121
+ unless result["error"]
122
+ @key = result["vertex"]["_key"]
123
+ @id = "#{@collection}/#{@key}"
124
+ @body = body
125
+ end
126
+ result
127
+ else
128
+ if result["error"]
129
+ result["errorMessage"]
130
+ else
131
+ @key = result["vertex"]["_key"]
132
+ @id = "#{@collection}/#{@key}"
133
+ @body = body
134
+ self
135
+ end
136
+ end
137
+ end
138
+ end
data/lib/arangorb.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "httparty"
2
+ require_relative "ArangoRB_Ser"
3
+ require_relative "ArangoRB_DB"
4
+ require_relative "ArangoRB_Col"
5
+ require_relative "ArangoRB_Doc"
6
+ require_relative "ArangoRB_Gra"
7
+ require_relative "ArangoRB_Ver"
8
+ require_relative "ArangoRB_Edg"
9
+ require_relative "ArangoRB_Tra"
10
+ require_relative "ArangoRB_AQL"
@@ -0,0 +1,9 @@
1
+ require "lib/arangoS_helper"
2
+ require "lib/arangoDB_helper"
3
+ require "lib/arangoC_helper"
4
+ require "lib/arangoDoc_helper"
5
+ require "lib/arangoG_helper"
6
+ require "lib/arangoV_helper"
7
+ require "lib/arangoE_helper"
8
+ require "lib/arangoT_helper"
9
+ require "lib/arangoAQL_helper"
@@ -0,0 +1,83 @@
1
+ require_relative './../spec_helper'
2
+
3
+ describe ArangoT do
4
+ before :all do
5
+ ArangoS.default_server user: "root", password: "tretretre", server: "localhost", port: "8529"
6
+ ArangoS.database = "MyDatabase"
7
+ ArangoS.collection = "MyCollection"
8
+ ArangoS.graph = "MyGraph"
9
+ ArangoDB.new.create
10
+ @myGraph = ArangoG.new.create
11
+ @myCollection = ArangoC.new.create
12
+ @myEdgeCollection = ArangoC.new(collection: "MyEdgeCollection").create_edge_collection
13
+ @myGraph.addEdgeCollection collection: "MyEdgeCollection", from: "MyCollection", to: "MyCollection"
14
+ @myAQL = ArangoAQL.new query: "FOR u IN MyCollection RETURN u.num"
15
+ @myDoc = @myCollection.create_document document: [{"num" => 1, "_key" => "FirstKey"}, {"num" => 1}, {"num" => 1}, {"num" => 1}, {"num" => 1}, {"num" => 1}, {"num" => 1}, {"num" => 2}, {"num" => 2}, {"num" => 2}, {"num" => 3}, {"num" => 2}, {"num" => 5}, {"num" => 2}]
16
+ @myEdgeCollection.create_edge from: [@myDoc[0].id, @myDoc[1].id, @myDoc[2].id, @myDoc[3].id, @myDoc[7].id], to: [@myDoc[4].id, @myDoc[5].id, @myDoc[6].id, @myDoc[8].id]
17
+ end
18
+
19
+ after :all do
20
+ ArangoDB.new.destroy
21
+ end
22
+
23
+ context "#new" do
24
+ it "create a new AQL instance" do
25
+ myAQL = ArangoAQL.new query: "FOR u IN MyCollection RETURN u.num"
26
+ expect(myAQL.query).to eq "FOR u IN MyCollection RETURN u.num"
27
+ end
28
+
29
+ it "instantiate size" do
30
+ @myAQL.size = 5
31
+ expect(@myAQL.size).to eq 5
32
+ end
33
+ end
34
+
35
+ context "#execute" do
36
+ it "execute Transaction" do
37
+ @myAQL.execute
38
+ expect(@myAQL.result.length).to eq 5
39
+ end
40
+
41
+ it "execute again Transaction" do
42
+ @myAQL.next
43
+ expect(@myAQL.result.length).to eq 5
44
+ end
45
+ end
46
+
47
+ context "#info" do
48
+ it "explain" do
49
+ expect(@myAQL.explain["cacheable"]).to be true
50
+ end
51
+
52
+ it "parse" do
53
+ expect(@myAQL.parse["parsed"]).to be true
54
+ end
55
+
56
+ it "properties" do
57
+ expect(@myAQL.properties["enabled"]).to be true
58
+ end
59
+
60
+ it "current" do
61
+ expect(@myAQL.current).to eq []
62
+ end
63
+
64
+ it "slow" do
65
+ expect(@myAQL.slow).to eq []
66
+ end
67
+ end
68
+
69
+ context "#delete" do
70
+ it "stopSlow" do
71
+ expect(@myAQL.stopSlow).to be true
72
+ end
73
+
74
+ it "kill" do
75
+ expect(@myAQL.kill.class).to be String
76
+ end
77
+
78
+ it "changeProperties" do
79
+ result = @myAQL.changeProperties maxSlowQueries: 65
80
+ expect(result["maxSlowQueries"]).to eq 65
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,149 @@
1
+ require_relative './../spec_helper'
2
+
3
+ describe ArangoC do
4
+ before :all do
5
+ ArangoS.default_server user: "root", password: "tretretre", server: "localhost", port: "8529"
6
+ ArangoS.database = "MyDatabase"
7
+ ArangoS.collection = "MyCollection"
8
+ ArangoDB.new.create
9
+ @myCollection = ArangoC.new
10
+ @myEdgeCollection = ArangoC.new collection: "MyEdgeCollection"
11
+ end
12
+
13
+ after :all do
14
+ ArangoDB.new.destroy
15
+ end
16
+
17
+ context "#new" do
18
+ it "create a new instance without global" do
19
+ myCollection = ArangoC.new collection: "MyCollection"
20
+ expect(myCollection.collection).to eq "MyCollection"
21
+ end
22
+
23
+ it "create a new instance with global" do
24
+ myCollection = ArangoC.new
25
+ expect(myCollection.collection).to eq "MyCollection"
26
+ end
27
+
28
+ it "create a new instance with type Edge" do
29
+ myCollection = ArangoC.new collection: "MyCollection", type: "Edge"
30
+ expect(myCollection.type).to eq "Edge"
31
+ end
32
+ end
33
+
34
+ context "#create" do
35
+ it "create a new Collection" do
36
+ myCollection = @myCollection.create
37
+ expect(myCollection.collection).to eq "MyCollection"
38
+ end
39
+
40
+ it "create a duplicate Collection" do
41
+ myCollection = @myCollection.create
42
+ expect(myCollection).to eq "cannot create collection: duplicate name"
43
+ end
44
+
45
+ it "create a new Edge Collection" do
46
+ myCollection = @myEdgeCollection.create_edge_collection
47
+ expect(myCollection.type).to eq "Edge"
48
+ end
49
+
50
+ it "create a new Document in the Collection" do
51
+ myDocument = @myCollection.create_document document: {"Hello" => "World", "num" => 1}
52
+ expect(myDocument.body["Hello"]).to eq "World"
53
+ end
54
+
55
+ it "create new Documents in the Collection" do
56
+ myDocument = @myCollection.create_document document: [{"Ciao" => "Mondo", "num" => 1}, {"Hallo" => "Welt", "num" => 2}]
57
+ expect(myDocument[0].body["Ciao"]).to eq "Mondo"
58
+ end
59
+
60
+ it "create a new Edge in the Collection" do
61
+ myDoc = @myCollection.create_document document: [{"A" => "B", "num" => 1}, {"C" => "D", "num" => 3}]
62
+ myEdge = @myEdgeCollection.create_edge from: myDoc[0].id, to: myDoc[1].id
63
+ expect(myEdge.body["_from"]).to eq myDoc[0].id
64
+ end
65
+ end
66
+
67
+ context "#info" do
68
+ it "retrieve the Collection" do
69
+ info = @myCollection.retrieve
70
+ expect(info.collection).to eq "MyCollection"
71
+ end
72
+
73
+ it "properties of the Collection" do
74
+ info = @myCollection.properties
75
+ expect(info["name"]).to eq "MyCollection"
76
+ end
77
+
78
+ it "documents in the Collection" do
79
+ info = @myCollection.count
80
+ expect(info).to eq 5
81
+ end
82
+
83
+ it "statistics" do
84
+ info = @myCollection.stats
85
+ expect(info["lastTick"]).to eq "0"
86
+ end
87
+
88
+ it "checksum" do
89
+ info = @myCollection.checksum
90
+ expect(info.class).to eq String
91
+ end
92
+
93
+ it "list Documents" do
94
+ info = @myCollection.allDocuments
95
+ expect(info.length).to eq 5
96
+ end
97
+
98
+ it "search Documents by match" do
99
+ info = @myCollection.documentsMatch match: {"num" => 1}
100
+ expect(info.length).to eq 3
101
+ end
102
+
103
+ it "search Document by match" do
104
+ info = @myCollection.documentMatch match: {"num" => 1}
105
+ expect(info.collection).to eq "MyCollection"
106
+ end
107
+
108
+ it "search random Document" do
109
+ info = @myCollection.random
110
+ expect(info.collection).to eq "MyCollection"
111
+ end
112
+ end
113
+
114
+ context "#modify" do
115
+ it "load" do
116
+ myCollection = @myCollection.load
117
+ expect(myCollection.collection).to eq "MyCollection"
118
+ end
119
+
120
+ it "unload" do
121
+ myCollection = @myCollection.unload
122
+ expect(myCollection.collection).to eq "MyCollection"
123
+ end
124
+
125
+ it "change" do
126
+ myCollection = @myCollection.change waitForSync: true
127
+ expect(myCollection.body["waitForSync"]).to be true
128
+ end
129
+
130
+ it "rename" do
131
+ myCollection = @myCollection.rename "MyCollection2"
132
+ expect(myCollection.collection).to eq "MyCollection2"
133
+ end
134
+ end
135
+
136
+ context "#truncate" do
137
+ it "truncate a Collection" do
138
+ myCollection = @myCollection.truncate
139
+ expect(myCollection.count).to eq 0
140
+ end
141
+ end
142
+
143
+ context "#destroy" do
144
+ it "delete a Collection" do
145
+ myCollection = @myCollection.destroy
146
+ expect(myCollection).to be true
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,119 @@
1
+ require_relative './../spec_helper'
2
+
3
+ describe ArangoDB do
4
+ before :all do
5
+ ArangoS.default_server user: "root", password: "tretretre", server: "localhost", port: "8529"
6
+ ArangoS.database = "MyDatabase"
7
+ @myDatabase = ArangoDB.new
8
+ end
9
+
10
+ context "#new" do
11
+ it "create a new instance without global" do
12
+ myDatabase = ArangoDB.new database: "MyDatabase"
13
+ expect(myDatabase.database).to eq "MyDatabase"
14
+ end
15
+
16
+ it "create a new instance with global" do
17
+ myDatabase = ArangoDB.new
18
+ expect(myDatabase.database).to eq "MyDatabase"
19
+ end
20
+ end
21
+
22
+ context "#create" do
23
+ it "create a new Database" do
24
+ myDatabase = @myDatabase.create
25
+ expect(myDatabase.database).to eq "MyDatabase"
26
+ end
27
+
28
+ it "create a duplicate Database" do
29
+ myDatabase = @myDatabase.create
30
+ expect(myDatabase).to eq "duplicate name"
31
+ end
32
+ end
33
+
34
+ context "#info" do
35
+ it "obtain general info" do
36
+ info = ArangoDB.info
37
+ expect(info["name"]).to eq "_system"
38
+ end
39
+
40
+ it "list databases" do
41
+ list = ArangoDB.databases
42
+ expect(list.length).to be >= 1
43
+ end
44
+
45
+ it "list collections" do
46
+ list = @myDatabase.collections
47
+ expect(list.length).to be 0
48
+ end
49
+
50
+ it "list graphs" do
51
+ list = @myDatabase.graphs
52
+ expect(list.length).to be 0
53
+ end
54
+ end
55
+
56
+ context "#query" do
57
+ it "properties" do
58
+ expect(@myDatabase.propertiesQuery["enabled"]).to be true
59
+ end
60
+
61
+ it "current" do
62
+ expect(@myDatabase.currentQuery).to eq []
63
+ end
64
+
65
+ it "slow" do
66
+ expect(@myDatabase.slowQuery).to eq []
67
+ end
68
+ end
69
+
70
+ context "#delete query" do
71
+ it "stopSlow" do
72
+ expect(@myDatabase.stopSlowQuery).to be true
73
+ end
74
+
75
+ it "kill" do
76
+ expect(@myDatabase.killQuery(id: "4353453463443").class).to be String
77
+ end
78
+
79
+ it "changeProperties" do
80
+ result = @myDatabase.changePropertiesQuery maxSlowQueries: 65
81
+ expect(result["maxSlowQueries"]).to eq 65
82
+ end
83
+ end
84
+
85
+ context "#cache" do
86
+ it "clear" do
87
+ expect(@myDatabase.clearCache).to be true
88
+ end
89
+
90
+ it "change Property Cache" do
91
+ @myDatabase.changePropertyCache maxResults: 130
92
+ expect(@myDatabase.propertyCache["maxResults"]).to eq 130
93
+ end
94
+ end
95
+
96
+ context "#function" do
97
+ it "create Function" do
98
+ result = @myDatabase.createFunction name: "myfunctions::temperature::celsiustofahrenheit", code: "function (celsius) { return celsius * 1.8 + 32; }"
99
+ expect(result.class).to eq Hash
100
+ end
101
+
102
+ it "list Functions" do
103
+ result = @myDatabase.functions
104
+ expect(result[0]["name"]).to eq "myfunctions::temperature::celsiustofahrenheit"
105
+ end
106
+
107
+ it "delete Function" do
108
+ result = @myDatabase.deleteFunction name: "myfunctions::temperature::celsiustofahrenheit"
109
+ expect(result).to be true
110
+ end
111
+ end
112
+
113
+ context "#destroy" do
114
+ it "delete a Database" do
115
+ myDatabase = @myDatabase.destroy
116
+ expect(myDatabase).to be true
117
+ end
118
+ end
119
+ end