rom-sql 2.0.0.beta2 → 2.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -0
  3. data/lib/rom/plugins/relation/sql/postgres/explain.rb +54 -0
  4. data/lib/rom/sql.rb +1 -1
  5. data/lib/rom/sql/attribute.rb +17 -18
  6. data/lib/rom/sql/errors.rb +3 -0
  7. data/lib/rom/sql/extensions/mysql.rb +1 -1
  8. data/lib/rom/sql/extensions/mysql/type_builder.rb +28 -0
  9. data/lib/rom/sql/extensions/postgres.rb +3 -1
  10. data/lib/rom/sql/extensions/postgres/commands.rb +30 -13
  11. data/lib/rom/sql/extensions/postgres/{attributes_inferrer.rb → type_builder.rb} +24 -28
  12. data/lib/rom/sql/extensions/postgres/type_serializer.rb +39 -0
  13. data/lib/rom/sql/extensions/postgres/types.rb +24 -477
  14. data/lib/rom/sql/extensions/postgres/types/array.rb +163 -0
  15. data/lib/rom/sql/extensions/postgres/types/geometric.rb +135 -0
  16. data/lib/rom/sql/extensions/postgres/types/json.rb +235 -0
  17. data/lib/rom/sql/extensions/postgres/types/network.rb +15 -0
  18. data/lib/rom/sql/extensions/sqlite.rb +1 -1
  19. data/lib/rom/sql/extensions/sqlite/{attributes_inferrer.rb → type_builder.rb} +5 -5
  20. data/lib/rom/sql/extensions/sqlite/types.rb +8 -3
  21. data/lib/rom/sql/foreign_key.rb +17 -0
  22. data/lib/rom/sql/function.rb +86 -8
  23. data/lib/rom/sql/gateway.rb +26 -26
  24. data/lib/rom/sql/index.rb +4 -0
  25. data/lib/rom/sql/migration.rb +3 -3
  26. data/lib/rom/sql/migration/inline_runner.rb +9 -83
  27. data/lib/rom/sql/migration/migrator.rb +35 -12
  28. data/lib/rom/sql/migration/recorder.rb +21 -0
  29. data/lib/rom/sql/migration/runner.rb +115 -0
  30. data/lib/rom/sql/migration/schema_diff.rb +108 -53
  31. data/lib/rom/sql/migration/writer.rb +61 -0
  32. data/lib/rom/sql/relation.rb +2 -1
  33. data/lib/rom/sql/relation/reading.rb +63 -3
  34. data/lib/rom/sql/relation/writing.rb +38 -0
  35. data/lib/rom/sql/schema.rb +9 -3
  36. data/lib/rom/sql/schema/attributes_inferrer.rb +3 -119
  37. data/lib/rom/sql/schema/inferrer.rb +99 -18
  38. data/lib/rom/sql/schema/type_builder.rb +94 -0
  39. data/lib/rom/sql/type_dsl.rb +30 -0
  40. data/lib/rom/sql/type_extensions.rb +11 -6
  41. data/lib/rom/sql/type_serializer.rb +46 -0
  42. data/lib/rom/sql/types.rb +12 -0
  43. data/lib/rom/sql/version.rb +1 -1
  44. metadata +26 -244
  45. data/.codeclimate.yml +0 -15
  46. data/.gitignore +0 -17
  47. data/.rspec +0 -3
  48. data/.travis.yml +0 -39
  49. data/.yardopts +0 -2
  50. data/Gemfile +0 -33
  51. data/Guardfile +0 -24
  52. data/LICENSE.txt +0 -22
  53. data/Rakefile +0 -19
  54. data/circle.yml +0 -10
  55. data/lib/rom/sql/extensions/mysql/attributes_inferrer.rb +0 -10
  56. data/lib/rom/sql/relation/sequel_api.rb +0 -133
  57. data/log/.gitkeep +0 -0
  58. data/rom-sql.gemspec +0 -29
  59. data/spec/extensions/postgres/attribute_spec.rb +0 -217
  60. data/spec/extensions/postgres/integration_spec.rb +0 -59
  61. data/spec/extensions/postgres/types_spec.rb +0 -252
  62. data/spec/extensions/sqlite/types_spec.rb +0 -11
  63. data/spec/fixtures/migrations/20150403090603_create_carrots.rb +0 -8
  64. data/spec/integration/associations/many_to_many/custom_fks_spec.rb +0 -76
  65. data/spec/integration/associations/many_to_many/from_view_spec.rb +0 -88
  66. data/spec/integration/associations/many_to_many_spec.rb +0 -162
  67. data/spec/integration/associations/many_to_one/custom_fks_spec.rb +0 -64
  68. data/spec/integration/associations/many_to_one/from_view_spec.rb +0 -84
  69. data/spec/integration/associations/many_to_one/self_ref_spec.rb +0 -53
  70. data/spec/integration/associations/many_to_one_spec.rb +0 -117
  71. data/spec/integration/associations/one_to_many/custom_fks_spec.rb +0 -54
  72. data/spec/integration/associations/one_to_many/from_view_spec.rb +0 -57
  73. data/spec/integration/associations/one_to_many/self_ref_spec.rb +0 -54
  74. data/spec/integration/associations/one_to_many_spec.rb +0 -86
  75. data/spec/integration/associations/one_to_one_spec.rb +0 -69
  76. data/spec/integration/associations/one_to_one_through_spec.rb +0 -92
  77. data/spec/integration/auto_migrations/errors_spec.rb +0 -31
  78. data/spec/integration/auto_migrations/indexes_spec.rb +0 -253
  79. data/spec/integration/auto_migrations/managing_columns_spec.rb +0 -156
  80. data/spec/integration/auto_migrations/postgres/column_types_spec.rb +0 -63
  81. data/spec/integration/combine_with_spec.rb +0 -43
  82. data/spec/integration/commands/create_spec.rb +0 -304
  83. data/spec/integration/commands/delete_spec.rb +0 -84
  84. data/spec/integration/commands/update_spec.rb +0 -90
  85. data/spec/integration/commands/upsert_spec.rb +0 -83
  86. data/spec/integration/gateway_spec.rb +0 -107
  87. data/spec/integration/migration_spec.rb +0 -55
  88. data/spec/integration/plugins/associates/many_to_many_spec.rb +0 -69
  89. data/spec/integration/plugins/associates_spec.rb +0 -250
  90. data/spec/integration/plugins/auto_restrictions_spec.rb +0 -74
  91. data/spec/integration/relation_schema_spec.rb +0 -271
  92. data/spec/integration/schema/call_spec.rb +0 -24
  93. data/spec/integration/schema/inferrer/mysql_spec.rb +0 -45
  94. data/spec/integration/schema/inferrer/postgres_spec.rb +0 -203
  95. data/spec/integration/schema/inferrer/sqlite_spec.rb +0 -37
  96. data/spec/integration/schema/inferrer_spec.rb +0 -390
  97. data/spec/integration/schema/prefix_spec.rb +0 -16
  98. data/spec/integration/schema/qualified_spec.rb +0 -16
  99. data/spec/integration/schema/rename_spec.rb +0 -21
  100. data/spec/integration/schema/view_spec.rb +0 -29
  101. data/spec/integration/sequel_api_spec.rb +0 -36
  102. data/spec/integration/setup_spec.rb +0 -26
  103. data/spec/integration/support/active_support_notifications_spec.rb +0 -24
  104. data/spec/integration/support/rails_log_subscriber_spec.rb +0 -30
  105. data/spec/integration/wrap_spec.rb +0 -91
  106. data/spec/shared/accounts.rb +0 -48
  107. data/spec/shared/database_setup.rb +0 -70
  108. data/spec/shared/notes.rb +0 -23
  109. data/spec/shared/posts.rb +0 -34
  110. data/spec/shared/puppies.rb +0 -15
  111. data/spec/shared/relations.rb +0 -8
  112. data/spec/shared/users.rb +0 -32
  113. data/spec/shared/users_and_tasks.rb +0 -50
  114. data/spec/spec_helper.rb +0 -122
  115. data/spec/support/env_helper.rb +0 -25
  116. data/spec/support/helpers.rb +0 -24
  117. data/spec/support/oracle/create_users.sql +0 -7
  118. data/spec/support/oracle/set_sys_passwords.sql +0 -2
  119. data/spec/support/test_configuration.rb +0 -16
  120. data/spec/unit/attribute_spec.rb +0 -104
  121. data/spec/unit/function_spec.rb +0 -48
  122. data/spec/unit/gateway_spec.rb +0 -70
  123. data/spec/unit/logger_spec.rb +0 -14
  124. data/spec/unit/migration_tasks_spec.rb +0 -111
  125. data/spec/unit/migrator_spec.rb +0 -25
  126. data/spec/unit/order_dsl_spec.rb +0 -43
  127. data/spec/unit/plugin/associates_spec.rb +0 -94
  128. data/spec/unit/plugin/pagination_spec.rb +0 -91
  129. data/spec/unit/plugin/timestamp_spec.rb +0 -117
  130. data/spec/unit/projection_dsl_spec.rb +0 -110
  131. data/spec/unit/relation/assoc_spec.rb +0 -87
  132. data/spec/unit/relation/associations_spec.rb +0 -27
  133. data/spec/unit/relation/avg_spec.rb +0 -11
  134. data/spec/unit/relation/by_pk_spec.rb +0 -62
  135. data/spec/unit/relation/dataset_spec.rb +0 -50
  136. data/spec/unit/relation/distinct_spec.rb +0 -15
  137. data/spec/unit/relation/exclude_spec.rb +0 -11
  138. data/spec/unit/relation/exist_predicate_spec.rb +0 -25
  139. data/spec/unit/relation/exists_spec.rb +0 -18
  140. data/spec/unit/relation/fetch_spec.rb +0 -21
  141. data/spec/unit/relation/group_spec.rb +0 -61
  142. data/spec/unit/relation/having_spec.rb +0 -22
  143. data/spec/unit/relation/inner_join_spec.rb +0 -158
  144. data/spec/unit/relation/inspect_spec.rb +0 -11
  145. data/spec/unit/relation/instrument_spec.rb +0 -45
  146. data/spec/unit/relation/invert_spec.rb +0 -11
  147. data/spec/unit/relation/left_join_spec.rb +0 -55
  148. data/spec/unit/relation/lock_spec.rb +0 -93
  149. data/spec/unit/relation/map_spec.rb +0 -16
  150. data/spec/unit/relation/max_spec.rb +0 -11
  151. data/spec/unit/relation/min_spec.rb +0 -11
  152. data/spec/unit/relation/order_spec.rb +0 -51
  153. data/spec/unit/relation/pluck_spec.rb +0 -11
  154. data/spec/unit/relation/prefix_spec.rb +0 -29
  155. data/spec/unit/relation/primary_key_spec.rb +0 -27
  156. data/spec/unit/relation/project_spec.rb +0 -24
  157. data/spec/unit/relation/qualified_columns_spec.rb +0 -30
  158. data/spec/unit/relation/qualified_spec.rb +0 -25
  159. data/spec/unit/relation/read_spec.rb +0 -25
  160. data/spec/unit/relation/rename_spec.rb +0 -23
  161. data/spec/unit/relation/right_join_spec.rb +0 -57
  162. data/spec/unit/relation/select_append_spec.rb +0 -21
  163. data/spec/unit/relation/select_spec.rb +0 -40
  164. data/spec/unit/relation/sum_spec.rb +0 -11
  165. data/spec/unit/relation/union_spec.rb +0 -19
  166. data/spec/unit/relation/unique_predicate_spec.rb +0 -18
  167. data/spec/unit/relation/where_spec.rb +0 -133
  168. data/spec/unit/restriction_dsl_spec.rb +0 -34
  169. data/spec/unit/schema_spec.rb +0 -25
  170. data/spec/unit/types_spec.rb +0 -65
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'rom/lint/spec'
4
-
5
- RSpec.describe ROM::SQL::Gateway, :postgres do
6
- include_context 'users and tasks'
7
-
8
- let(:gateway) { container.gateways[:default] }
9
-
10
- it_behaves_like 'a rom gateway' do
11
- let(:identifier) { :sql }
12
- let(:gateway) { ROM::SQL::Gateway }
13
- end
14
-
15
- describe 'sqlite with a file db', :sqlite, postgres: false do
16
- before do
17
- Tempfile.new('test.sqlite')
18
- end
19
-
20
- it 'establishes an sqlite connection' do
21
- gateway = ROM::SQL::Gateway.new(uri)
22
- expect(gateway).to be_instance_of(ROM::SQL::Gateway)
23
- end
24
- end
25
-
26
- describe '#dataset?' do
27
- it 'returns true if a table exists' do
28
- expect(gateway.dataset?(:users)).to be(true)
29
- end
30
-
31
- it 'returns false if a table does not exist' do
32
- expect(gateway.dataset?(:not_here)).to be(false)
33
- end
34
- end
35
-
36
- describe 'using options' do
37
- it 'allows custom sequel-specific options' do
38
- migrator = double('migrator')
39
-
40
- expect(Sequel).to receive(:connect)
41
- .with(uri, host: '127.0.0.1', migrator: migrator)
42
- .and_return(conn)
43
-
44
- gateway = ROM::SQL::Gateway.new(uri, migrator: migrator, host: '127.0.0.1')
45
-
46
- expect(gateway.options).to eql(migrator: migrator, host: '127.0.0.1')
47
- end
48
-
49
- it 'allows extensions' do
50
- extensions = [:pg_array, :pg_array_ops]
51
- connection = Sequel.connect uri
52
-
53
- expect(connection).to receive(:extension).with(:pg_array, :pg_json, :pg_enum, :pg_hstore, :pg_array_ops)
54
- expect(connection).to receive(:extension).with(:freeze_datasets) unless RUBY_ENGINE == 'rbx'
55
-
56
- ROM::SQL::Gateway.new(connection, extensions: extensions)
57
- end
58
- end
59
-
60
- describe '#disconnect' do
61
- let(:gateway) { ROM::SQL::Gateway.new(uri) }
62
-
63
- it 'disconnects via sequel connection' do
64
- # FIXME: no idea how to test it in a different way
65
- # FIXME: we are leaking connection here
66
- expect(gateway.connection).to receive(:disconnect)
67
- gateway.disconnect
68
- end
69
- end
70
- end
@@ -1,14 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe 'Logger', :postgres do
4
- include_context 'database setup'
5
-
6
- it 'sets up a logger for sequel' do
7
- gateway = container.gateways[:default]
8
-
9
- gateway.use_logger(LOGGER)
10
-
11
- expect(gateway.logger).to be(LOGGER)
12
- expect(conn.loggers).to include(LOGGER)
13
- end
14
- end
@@ -1,111 +0,0 @@
1
- require 'spec_helper'
2
-
3
- namespace :db do
4
- task :setup do
5
- #noop
6
- end
7
- end
8
-
9
- RSpec.describe 'MigrationTasks', :postgres, skip_tables: true do
10
- include_context 'database setup'
11
-
12
- let(:migrator) { container.gateways[:default].migrator }
13
-
14
- before do
15
- ROM::SQL::Gateway.instance = nil
16
- ROM::SQL::RakeSupport.env = nil
17
- conf
18
- end
19
-
20
- context 'db:reset' do
21
- it 'calls proper commands' do
22
- expect(migrator).to receive(:run).with(target: 0)
23
- expect(migrator).to receive(:run)
24
-
25
- expect {
26
- Rake::Task["db:reset"].invoke
27
- }.to output("<= db:reset executed\n").to_stdout
28
- end
29
- end
30
-
31
- context 'db:migrate' do
32
- context 'with VERSION' do
33
- it 'calls proper commands' do
34
- expect(migrator).to receive(:run).with(target: 1)
35
-
36
- expect {
37
- Rake::Task["db:migrate"].invoke(1)
38
- }.to output("<= db:migrate version=[1] executed\n").to_stdout
39
- end
40
- end
41
-
42
- context 'without VERSION' do
43
- it 'calls proper commands' do
44
- expect(migrator).to receive(:run)
45
-
46
- expect {
47
- Rake::Task["db:migrate"].execute
48
- }.to output("<= db:migrate executed\n").to_stdout
49
- end
50
- end
51
-
52
- it 'raises an error on missing both env and Gateway.instance' do
53
- ROM::SQL::RakeSupport.env = nil
54
- ROM::SQL::Gateway.instance = nil
55
-
56
- expect {
57
- Rake::Task["db:migrate"].execute
58
- }.to raise_error(ROM::SQL::RakeSupport::MissingEnv)
59
- end
60
- end
61
-
62
- context 'db:clean' do
63
- it 'calls proper commands' do
64
- expect(migrator).to receive(:run).with(target: 0)
65
-
66
- expect {
67
- Rake::Task["db:clean"].invoke
68
- }.to output("<= db:clean executed\n").to_stdout
69
- end
70
- end
71
-
72
- context 'db:create_migration' do
73
- context 'without NAME' do
74
- it 'exit without creating any file' do
75
- expect(File).to_not receive(:write)
76
-
77
- expect {
78
- expect {
79
- Rake::Task["db:create_migration"].execute
80
- }.to output(/No NAME specified/).to_stdout
81
- }.to raise_error(SystemExit)
82
- end
83
- end
84
-
85
- context 'with NAME' do
86
- let(:dirname) { 'tmp/db/migrate' }
87
- let(:name) { 'foo_bar' }
88
- let(:version) { '001' }
89
- let(:filename) { "#{version}_#{name}.rb" }
90
- let(:path) { File.join(dirname, filename) }
91
-
92
- it 'calls proper commands with default VERSION' do
93
- expect(migrator).to receive(:create_file).with(name).and_return(path)
94
-
95
- expect {
96
- Rake::Task["db:create_migration"].execute(
97
- Rake::TaskArguments.new([:name], [name]))
98
- }.to output("<= migration file created #{path}\n").to_stdout
99
- end
100
-
101
- it 'calls proper commands with manualy set VERSION' do
102
- expect(migrator).to receive(:create_file).with(name, version).and_return(path)
103
-
104
- expect {
105
- Rake::Task["db:create_migration"].execute(
106
- Rake::TaskArguments.new([:name, :version], [name, version]))
107
- }.to output("<= migration file created #{path}\n").to_stdout
108
- end
109
- end
110
- end
111
- end
@@ -1,25 +0,0 @@
1
- RSpec.describe ROM::SQL::Migration::Migrator, :postgres, skip_tables: true do
2
- include_context 'database setup'
3
-
4
- subject(:migrator) { ROM::SQL::Migration::Migrator.new(conn, options) }
5
-
6
- let(:options) { { path: TMP_PATH.join('test/migrations') } }
7
-
8
- describe '#create_file' do
9
- it 'creates a migration file under configured path with specified version and name' do
10
- file_path = migrator.create_file('create_users', 1)
11
-
12
- expect(file_path).to eql(migrator.path.join('1_create_users.rb'))
13
- expect(File.exist?(file_path)).to be(true)
14
- expect(File.read(file_path)).to eql(migrator.migration_file_content)
15
- end
16
-
17
- it 'auto-generates version when it is not provided' do
18
- file_path = migrator.create_file('create_users')
19
-
20
- expect(file_path.to_s).to match(/.(\d+)_create_users\.rb/)
21
- expect(File.exist?(file_path)).to be(true)
22
- expect(File.read(file_path)).to eql(migrator.migration_file_content)
23
- end
24
- end
25
- end
@@ -1,43 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe ROM::SQL::OrderDSL, :postgres, helpers: true do
4
- include_context 'database setup'
5
-
6
- subject(:dsl) do
7
- ROM::SQL::OrderDSL.new(schema)
8
- end
9
-
10
- let(:schema) do
11
- define_schema(:users, id: ROM::SQL::Types::Serial, name: ROM::SQL::Types::String)
12
- end
13
-
14
- let(:ds) do
15
- conn[:users]
16
- end
17
-
18
- describe '#call' do
19
- it 'returns an array with ordered expressions' do
20
- expect(ds.literal(dsl.call { id }.first)).to eql('"id"')
21
- end
22
- end
23
-
24
- describe '#method_missing' do
25
- it 'responds to methods matching attribute names' do
26
- expect(dsl.id.name).to be(:id)
27
- expect(dsl.name.name).to be(:name)
28
- end
29
-
30
- it 'delegates to sequel virtual row' do
31
- expect(ds.literal(dsl.call { nullif(id.qualified, Sequel.lit("''")).desc }.first)).
32
- to eql(%(NULLIF("users"."id", '') DESC))
33
- end
34
-
35
- it 'allows to set nulls first/last' do
36
- expect(ds.literal(dsl.call { id.desc(nulls: :first) }.first)).
37
- to eql(%("id" DESC NULLS FIRST))
38
-
39
- expect(ds.literal(dsl.call { id.desc(nulls: :last) }.first)).
40
- to eql(%("id" DESC NULLS LAST))
41
- end
42
- end
43
- end
@@ -1,94 +0,0 @@
1
- require 'ostruct'
2
- require 'rom/sql/commands'
3
-
4
- RSpec.describe ROM::SQL::Plugin::Associates do
5
- subject(:command) do
6
- command_class.build(posts).with_association(:tags)
7
- end
8
-
9
- let(:posts) do
10
- instance_double(Class.new(ROM::SQL::Relation), schema?: false, associations: associations)
11
- end
12
-
13
- let(:tags) do
14
- instance_double(ROM::SQL::Relation, associations: associations)
15
- end
16
-
17
- let(:join_relation) do
18
- instance_double(ROM::SQL::Relation)
19
- end
20
-
21
- let(:command_class) do
22
- Class.new(ROM::SQL::Commands::Create) do
23
- use :associates, tags: []
24
- end
25
- end
26
-
27
- let(:associations) do
28
- Hash.new { |h, k| h.fetch(k.to_sym) }.update(posts: posts_assoc)
29
- end
30
-
31
- let(:tags_assoc) do
32
- ROM::SQL::Associations::ManyToMany.new(double(:definition).as_null_object, {})
33
- end
34
-
35
- let(:posts_assoc) do
36
- ROM::SQL::Associations::ManyToMany.new(double(:definition).as_null_object, {})
37
- end
38
-
39
- before do
40
- allow(associations).to receive(:[]).and_return(tags_assoc)
41
- allow(tags_assoc).to receive(:join_keys).and_return({})
42
- end
43
-
44
- shared_context 'associates result' do
45
- it 'inserts join tuples and returns child tuples with combine keys' do
46
- expect(tags_assoc).to receive(:persist).with(post_tuples, tag_tuples)
47
- expect(tags_assoc).to receive(:parent_combine_keys).and_return(%i[name tag])
48
-
49
- result = command.associate(post_tuples, tag_tuples, assoc: tags_assoc, keys: {})
50
-
51
- expect(result).
52
- to match_array([
53
- { title: 'post 1', tag: 'red' }, { title: 'post 1', tag: 'green'},
54
- { title: 'post 2', tag: 'red' }, { title: 'post 2', tag: 'green'}
55
- ])
56
- end
57
- end
58
-
59
- describe '#associate' do
60
- context 'with plain hash tuples' do
61
- let(:post_tuples) do
62
- [{ title: 'post 1' }, { title: 'post 2' }]
63
- end
64
-
65
- let(:tag_tuples) do
66
- [{ name: 'red' }, { name: 'green' }]
67
- end
68
-
69
- include_context 'associates result'
70
- end
71
-
72
- context 'with tuples coercible to a hash' do
73
- before do
74
- module Test
75
- class Post < OpenStruct
76
- def to_hash
77
- { title: title }
78
- end
79
- end
80
- end
81
- end
82
-
83
- let(:post_tuples) do
84
- [Test::Post.new(title: 'post 1'), Test::Post.new(title: 'post 2')]
85
- end
86
-
87
- let(:tag_tuples) do
88
- [{ name: 'red' }, { name: 'green' }]
89
- end
90
-
91
- include_context 'associates result'
92
- end
93
- end
94
- end
@@ -1,91 +0,0 @@
1
- require 'rom/sql/plugin/pagination'
2
-
3
- RSpec.describe 'Plugin / Pagination', seeds: false do
4
- include_context 'users'
5
-
6
- with_adapters do
7
- before do
8
- 9.times { |i| conn[:users].insert(name: "User #{i}") }
9
-
10
- conf.relation(:users) do
11
- use :pagination
12
-
13
- per_page 4
14
- end
15
- end
16
-
17
- describe '#page' do
18
- it 'allow to call with stringify number' do
19
- expect {
20
- container.relations[:users].page('5')
21
- }.to_not raise_error
22
- end
23
-
24
- it 'preserves existing modifiers' do
25
- expect(
26
- container.relations[:users].send(:where, name: 'User 2').page(1).to_a.size
27
- ).to be(1)
28
- end
29
- end
30
-
31
- describe '#per_page' do
32
- it 'allow to call with stringify number' do
33
- expect {
34
- container.relations[:users].per_page('5')
35
- }.to_not raise_error
36
- end
37
-
38
- it 'returns paginated relation with provided limit' do
39
- users = container.relations[:users].page(2).per_page(5)
40
-
41
- expect(users.dataset.opts[:offset]).to eql(5)
42
- expect(users.dataset.opts[:limit]).to eql(5)
43
-
44
- expect(users.pager.current_page).to eql(2)
45
-
46
- expect(users.pager.total).to be(9)
47
- expect(users.pager.total_pages).to be(2)
48
-
49
- expect(users.pager.next_page).to be(nil)
50
- expect(users.pager.prev_page).to be(1)
51
- expect(users.pager.limit_value).to eql(5)
52
- end
53
- end
54
-
55
- describe '#total_pages' do
56
- it 'returns a single page when elements are a perfect fit' do
57
- users = container.relations[:users].page(1).per_page(3)
58
- expect(users.pager.total_pages).to eql(3)
59
- end
60
-
61
- it 'returns the exact number of pages to accommodate all elements' do
62
- users = container.relations[:users].per_page(9)
63
- expect(users.pager.total_pages).to eql(1)
64
- end
65
- end
66
-
67
- describe '#pager' do
68
- it 'returns a pager with pagination meta-info' do
69
- users = container.relations[:users].page(1)
70
-
71
- expect(users.pager.total).to be(9)
72
- expect(users.pager.total_pages).to be(3)
73
-
74
- expect(users.pager.current_page).to be(1)
75
- expect(users.pager.next_page).to be(2)
76
- expect(users.pager.prev_page).to be(nil)
77
-
78
- users = container.relations[:users].page(2)
79
-
80
- expect(users.pager.current_page).to be(2)
81
- expect(users.pager.next_page).to be(3)
82
- expect(users.pager.prev_page).to be(1)
83
-
84
- users = container.relations[:users].page(3)
85
-
86
- expect(users.pager.next_page).to be(nil)
87
- expect(users.pager.prev_page).to be(2)
88
- end
89
- end
90
- end
91
- end