perpetuity-postgres 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +3 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +34 -0
  6. data/Rakefile +1 -0
  7. data/lib/perpetuity/postgres/boolean_value.rb +17 -0
  8. data/lib/perpetuity/postgres/connection.rb +78 -0
  9. data/lib/perpetuity/postgres/connection_pool.rb +39 -0
  10. data/lib/perpetuity/postgres/expression.rb +21 -0
  11. data/lib/perpetuity/postgres/json_array.rb +31 -0
  12. data/lib/perpetuity/postgres/json_hash.rb +55 -0
  13. data/lib/perpetuity/postgres/json_string_value.rb +17 -0
  14. data/lib/perpetuity/postgres/negated_query.rb +21 -0
  15. data/lib/perpetuity/postgres/nil_query.rb +9 -0
  16. data/lib/perpetuity/postgres/null_value.rb +9 -0
  17. data/lib/perpetuity/postgres/numeric_value.rb +17 -0
  18. data/lib/perpetuity/postgres/query.rb +34 -0
  19. data/lib/perpetuity/postgres/query_attribute.rb +33 -0
  20. data/lib/perpetuity/postgres/query_expression.rb +90 -0
  21. data/lib/perpetuity/postgres/query_intersection.rb +26 -0
  22. data/lib/perpetuity/postgres/query_union.rb +26 -0
  23. data/lib/perpetuity/postgres/serialized_data.rb +63 -0
  24. data/lib/perpetuity/postgres/serializer.rb +179 -0
  25. data/lib/perpetuity/postgres/sql_select.rb +71 -0
  26. data/lib/perpetuity/postgres/sql_update.rb +28 -0
  27. data/lib/perpetuity/postgres/sql_value.rb +40 -0
  28. data/lib/perpetuity/postgres/table/attribute.rb +60 -0
  29. data/lib/perpetuity/postgres/table.rb +36 -0
  30. data/lib/perpetuity/postgres/table_name.rb +21 -0
  31. data/lib/perpetuity/postgres/text_value.rb +14 -0
  32. data/lib/perpetuity/postgres/timestamp_value.rb +68 -0
  33. data/lib/perpetuity/postgres/value_with_attribute.rb +19 -0
  34. data/lib/perpetuity/postgres/version.rb +5 -0
  35. data/lib/perpetuity/postgres.rb +170 -0
  36. data/perpetuity-postgres.gemspec +26 -0
  37. data/spec/perpetuity/postgres/boolean_value_spec.rb +15 -0
  38. data/spec/perpetuity/postgres/connection_pool_spec.rb +55 -0
  39. data/spec/perpetuity/postgres/connection_spec.rb +30 -0
  40. data/spec/perpetuity/postgres/expression_spec.rb +13 -0
  41. data/spec/perpetuity/postgres/json_array_spec.rb +31 -0
  42. data/spec/perpetuity/postgres/json_hash_spec.rb +39 -0
  43. data/spec/perpetuity/postgres/json_string_value_spec.rb +11 -0
  44. data/spec/perpetuity/postgres/negated_query_spec.rb +39 -0
  45. data/spec/perpetuity/postgres/null_value_spec.rb +11 -0
  46. data/spec/perpetuity/postgres/numeric_value_spec.rb +12 -0
  47. data/spec/perpetuity/postgres/query_attribute_spec.rb +48 -0
  48. data/spec/perpetuity/postgres/query_expression_spec.rb +110 -0
  49. data/spec/perpetuity/postgres/query_intersection_spec.rb +23 -0
  50. data/spec/perpetuity/postgres/query_spec.rb +23 -0
  51. data/spec/perpetuity/postgres/query_union_spec.rb +23 -0
  52. data/spec/perpetuity/postgres/serialized_data_spec.rb +69 -0
  53. data/spec/perpetuity/postgres/serializer_spec.rb +216 -0
  54. data/spec/perpetuity/postgres/sql_select_spec.rb +51 -0
  55. data/spec/perpetuity/postgres/sql_update_spec.rb +22 -0
  56. data/spec/perpetuity/postgres/sql_value_spec.rb +38 -0
  57. data/spec/perpetuity/postgres/table/attribute_spec.rb +82 -0
  58. data/spec/perpetuity/postgres/table_name_spec.rb +15 -0
  59. data/spec/perpetuity/postgres/table_spec.rb +43 -0
  60. data/spec/perpetuity/postgres/text_value_spec.rb +15 -0
  61. data/spec/perpetuity/postgres/timestamp_value_spec.rb +28 -0
  62. data/spec/perpetuity/postgres/value_with_attribute_spec.rb +34 -0
  63. data/spec/perpetuity/postgres_spec.rb +163 -0
  64. data/spec/support/test_classes/book.rb +16 -0
  65. data/spec/support/test_classes/person.rb +11 -0
  66. metadata +207 -0
@@ -0,0 +1,163 @@
1
+ require 'perpetuity/postgres'
2
+ require 'perpetuity/postgres/table/attribute'
3
+ require 'perpetuity/attribute'
4
+ require 'perpetuity/postgres/serialized_data'
5
+
6
+ module Perpetuity
7
+ describe Postgres do
8
+ let(:postgres) { Postgres.new(db: 'perpetuity_gem_test') }
9
+
10
+ describe 'initialization params' do
11
+ context 'with params' do
12
+ let(:host) { double('host') }
13
+ let(:port) { double('port') }
14
+ let(:db) { double('db') }
15
+ let(:pool_size) { 5 }
16
+ let(:username) { double('username') }
17
+ let(:password) { double('password') }
18
+ let(:postgres) do
19
+ Postgres.new(
20
+ host: host,
21
+ port: port,
22
+ db: db,
23
+ pool_size: pool_size,
24
+ username: username,
25
+ password: password
26
+ )
27
+ end
28
+ subject { postgres }
29
+
30
+ its(:host) { should == host }
31
+ its(:port) { should == port }
32
+ its(:db) { should == db }
33
+ its(:pool_size) { should == pool_size }
34
+ its(:username) { should == username }
35
+ its(:password) { should == password }
36
+ end
37
+
38
+ context 'default values' do
39
+ let(:postgres) { Postgres.new(db: 'my_db') }
40
+ subject { postgres }
41
+
42
+ its(:host) { should == 'localhost' }
43
+ its(:port) { should == 5432 }
44
+ its(:pool_size) { should == 5 }
45
+ its(:username) { should == ENV['USER'] }
46
+ its(:password) { should be_nil }
47
+ end
48
+ end
49
+
50
+ it 'creates and drops tables' do
51
+ postgres.create_table 'Article', [
52
+ Postgres::Table::Attribute.new('title', String, max_length: 40),
53
+ Postgres::Table::Attribute.new('body', String),
54
+ Postgres::Table::Attribute.new('author', Object)
55
+ ]
56
+ postgres.should have_table('Article')
57
+
58
+ postgres.drop_table 'Article'
59
+ postgres.should_not have_table 'Article'
60
+ end
61
+
62
+ it 'converts values into something that works with the DB' do
63
+ postgres.postgresify("string").should == "'string'"
64
+ postgres.postgresify(1).should == '1'
65
+ postgres.postgresify(true).should == 'TRUE'
66
+ end
67
+
68
+ describe 'working with data' do
69
+ let(:attributes) { [Attribute.new(:name, String)] }
70
+ let(:data) { [Postgres::SerializedData.new([:name], ["'Jamie'"])] }
71
+
72
+ it 'inserts data and finds by id' do
73
+ id = postgres.insert('User', data, attributes).first
74
+ result = postgres.find('User', id)
75
+
76
+ id.should =~ /[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}/
77
+ result['name'].should == 'Jamie'
78
+ end
79
+
80
+ it 'returns the ids of all items saved' do
81
+ self.data << Postgres::SerializedData.new([:name], ["'Jessica'"]) <<
82
+ Postgres::SerializedData.new([:name], ["'Kevin'"])
83
+ ids = postgres.insert('User', data, attributes)
84
+ ids.should be_a Array
85
+ ids.should have(3).items
86
+ end
87
+
88
+ it 'counts objects' do
89
+ expect { postgres.insert 'User', data, attributes }.to change { postgres.count('User') }.by 1
90
+ end
91
+
92
+ it 'counts objects with a string query' do
93
+ insert = proc { postgres.insert 'User', data, attributes }
94
+ expect(&insert).to change { postgres.count('User', "name = 'Jamie'") }.by 1
95
+ expect(&insert).not_to change { postgres.count('User', "name = 'Jessica'") }.by 1
96
+ end
97
+
98
+ it 'returns a count of 0 when the table does not exist' do
99
+ postgres.drop_table 'Article'
100
+ postgres.count('Article').should == 0
101
+ end
102
+
103
+ it 'returns no rows when the table does not exist' do
104
+ postgres.drop_table 'Article'
105
+ postgres.retrieve('Article', 'TRUE').should == []
106
+ end
107
+
108
+ it 'deletes all records' do
109
+ postgres.insert 'User', data, attributes
110
+ postgres.delete_all 'User'
111
+ postgres.count('User').should == 0
112
+ end
113
+
114
+ it 'updates a specific record' do
115
+ id = postgres.insert('User', data, attributes).first
116
+ postgres.update 'User', id, name: 'foo'
117
+
118
+ retrieved = postgres.retrieve 'User', "id = '#{id}'"
119
+ retrieved.first['name'].should == 'foo'
120
+ end
121
+
122
+ it 'deletes a record with a specific id' do
123
+ id = postgres.insert('User', data, attributes).first
124
+ expect { postgres.delete id, 'User' }.to change { postgres.count 'User' }.by -1
125
+ end
126
+ end
127
+
128
+ describe 'query generation' do
129
+ it 'creates SQL queries with a block' do
130
+ postgres.query { |o| o.name == 'foo' }.to_db.should ==
131
+ "name = 'foo'"
132
+ end
133
+
134
+ it 'does not allow SQL injection' do
135
+ query = postgres.query { |o| o.name == "' OR 1; --" }.to_db
136
+ query.should == "name = ''' OR 1; --'"
137
+ end
138
+
139
+ it 'limits results' do
140
+ query = postgres.query
141
+ sql = postgres.select(from: 'Article', where: query, limit: 2)
142
+ sql.should == %Q{SELECT * FROM "Article" WHERE TRUE LIMIT 2}
143
+ end
144
+
145
+ describe 'ordering results' do
146
+ it 'orders results without a qualifier' do
147
+ sql = postgres.select(from: 'Article', order: :title)
148
+ sql.should == %Q{SELECT * FROM "Article" ORDER BY title}
149
+ end
150
+
151
+ it 'orders results with asc' do
152
+ sql = postgres.select(from: 'Article', order: { title: :asc })
153
+ sql.should == %Q{SELECT * FROM "Article" ORDER BY title ASC}
154
+ end
155
+
156
+ it 'reverse-orders results' do
157
+ sql = postgres.select(from: 'Article', order: { title: :desc })
158
+ sql.should == %Q{SELECT * FROM "Article" ORDER BY title DESC}
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,16 @@
1
+ class Book
2
+ attr_accessor :title, :authors, :main_character
3
+
4
+ def initialize title, authors=[], main_character=nil
5
+ @title = title
6
+ @authors = authors
7
+ @main_character = main_character
8
+ end
9
+
10
+ def == other
11
+ other.is_a?(Book) &&
12
+ other.title == title &&
13
+ other.authors == authors &&
14
+ other.main_character == main_character
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ class Person
2
+ attr_reader :name
3
+
4
+ def initialize name
5
+ @name = name
6
+ end
7
+
8
+ def == other
9
+ other.is_a?(self.class) && other.name == name
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,207 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: perpetuity-postgres
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jamie Gaskins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: perpetuity
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0.beta
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0.beta
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: PostgreSQL adapter for Perpetuity
84
+ email:
85
+ - jgaskins@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/perpetuity/postgres.rb
96
+ - lib/perpetuity/postgres/boolean_value.rb
97
+ - lib/perpetuity/postgres/connection.rb
98
+ - lib/perpetuity/postgres/connection_pool.rb
99
+ - lib/perpetuity/postgres/expression.rb
100
+ - lib/perpetuity/postgres/json_array.rb
101
+ - lib/perpetuity/postgres/json_hash.rb
102
+ - lib/perpetuity/postgres/json_string_value.rb
103
+ - lib/perpetuity/postgres/negated_query.rb
104
+ - lib/perpetuity/postgres/nil_query.rb
105
+ - lib/perpetuity/postgres/null_value.rb
106
+ - lib/perpetuity/postgres/numeric_value.rb
107
+ - lib/perpetuity/postgres/query.rb
108
+ - lib/perpetuity/postgres/query_attribute.rb
109
+ - lib/perpetuity/postgres/query_expression.rb
110
+ - lib/perpetuity/postgres/query_intersection.rb
111
+ - lib/perpetuity/postgres/query_union.rb
112
+ - lib/perpetuity/postgres/serialized_data.rb
113
+ - lib/perpetuity/postgres/serializer.rb
114
+ - lib/perpetuity/postgres/sql_select.rb
115
+ - lib/perpetuity/postgres/sql_update.rb
116
+ - lib/perpetuity/postgres/sql_value.rb
117
+ - lib/perpetuity/postgres/table.rb
118
+ - lib/perpetuity/postgres/table/attribute.rb
119
+ - lib/perpetuity/postgres/table_name.rb
120
+ - lib/perpetuity/postgres/text_value.rb
121
+ - lib/perpetuity/postgres/timestamp_value.rb
122
+ - lib/perpetuity/postgres/value_with_attribute.rb
123
+ - lib/perpetuity/postgres/version.rb
124
+ - perpetuity-postgres.gemspec
125
+ - spec/perpetuity/postgres/boolean_value_spec.rb
126
+ - spec/perpetuity/postgres/connection_pool_spec.rb
127
+ - spec/perpetuity/postgres/connection_spec.rb
128
+ - spec/perpetuity/postgres/expression_spec.rb
129
+ - spec/perpetuity/postgres/json_array_spec.rb
130
+ - spec/perpetuity/postgres/json_hash_spec.rb
131
+ - spec/perpetuity/postgres/json_string_value_spec.rb
132
+ - spec/perpetuity/postgres/negated_query_spec.rb
133
+ - spec/perpetuity/postgres/null_value_spec.rb
134
+ - spec/perpetuity/postgres/numeric_value_spec.rb
135
+ - spec/perpetuity/postgres/query_attribute_spec.rb
136
+ - spec/perpetuity/postgres/query_expression_spec.rb
137
+ - spec/perpetuity/postgres/query_intersection_spec.rb
138
+ - spec/perpetuity/postgres/query_spec.rb
139
+ - spec/perpetuity/postgres/query_union_spec.rb
140
+ - spec/perpetuity/postgres/serialized_data_spec.rb
141
+ - spec/perpetuity/postgres/serializer_spec.rb
142
+ - spec/perpetuity/postgres/sql_select_spec.rb
143
+ - spec/perpetuity/postgres/sql_update_spec.rb
144
+ - spec/perpetuity/postgres/sql_value_spec.rb
145
+ - spec/perpetuity/postgres/table/attribute_spec.rb
146
+ - spec/perpetuity/postgres/table_name_spec.rb
147
+ - spec/perpetuity/postgres/table_spec.rb
148
+ - spec/perpetuity/postgres/text_value_spec.rb
149
+ - spec/perpetuity/postgres/timestamp_value_spec.rb
150
+ - spec/perpetuity/postgres/value_with_attribute_spec.rb
151
+ - spec/perpetuity/postgres_spec.rb
152
+ - spec/support/test_classes/book.rb
153
+ - spec/support/test_classes/person.rb
154
+ homepage: https://github.com/jgaskins/perpetuity-postgres
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - '>='
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubyforge_project:
174
+ rubygems_version: 2.0.6
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: PostgreSQL adapter for Perpetuity
178
+ test_files:
179
+ - spec/perpetuity/postgres/boolean_value_spec.rb
180
+ - spec/perpetuity/postgres/connection_pool_spec.rb
181
+ - spec/perpetuity/postgres/connection_spec.rb
182
+ - spec/perpetuity/postgres/expression_spec.rb
183
+ - spec/perpetuity/postgres/json_array_spec.rb
184
+ - spec/perpetuity/postgres/json_hash_spec.rb
185
+ - spec/perpetuity/postgres/json_string_value_spec.rb
186
+ - spec/perpetuity/postgres/negated_query_spec.rb
187
+ - spec/perpetuity/postgres/null_value_spec.rb
188
+ - spec/perpetuity/postgres/numeric_value_spec.rb
189
+ - spec/perpetuity/postgres/query_attribute_spec.rb
190
+ - spec/perpetuity/postgres/query_expression_spec.rb
191
+ - spec/perpetuity/postgres/query_intersection_spec.rb
192
+ - spec/perpetuity/postgres/query_spec.rb
193
+ - spec/perpetuity/postgres/query_union_spec.rb
194
+ - spec/perpetuity/postgres/serialized_data_spec.rb
195
+ - spec/perpetuity/postgres/serializer_spec.rb
196
+ - spec/perpetuity/postgres/sql_select_spec.rb
197
+ - spec/perpetuity/postgres/sql_update_spec.rb
198
+ - spec/perpetuity/postgres/sql_value_spec.rb
199
+ - spec/perpetuity/postgres/table/attribute_spec.rb
200
+ - spec/perpetuity/postgres/table_name_spec.rb
201
+ - spec/perpetuity/postgres/table_spec.rb
202
+ - spec/perpetuity/postgres/text_value_spec.rb
203
+ - spec/perpetuity/postgres/timestamp_value_spec.rb
204
+ - spec/perpetuity/postgres/value_with_attribute_spec.rb
205
+ - spec/perpetuity/postgres_spec.rb
206
+ - spec/support/test_classes/book.rb
207
+ - spec/support/test_classes/person.rb