sequel-association-filtering 0.0.1 → 0.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 650d4f992d2994676afd8b8cab81c98522dcd3a7f3c7700702fe2eeab78f7f64
4
- data.tar.gz: dc67094e11cdefd1b267524de4cecc4fe7729025eb9bdcadd9cfd8aaf72e4794
3
+ metadata.gz: ff146b722b3e38c2309610aad40275960ac257b8cae86047fb25aa15b1c08e38
4
+ data.tar.gz: fd4d3faeef0debbcaabebc11418c5d4543ef17e46c709ca4e332d53eceab0790
5
5
  SHA512:
6
- metadata.gz: 8c07e15a8e8c2f0160cc98c3ccbc0292e6257d1d70ebeb914209d7d4288179f5030689ec6159d7f26ba52f1e449b38c3d3f2c03824d48e07030d2c6ffd0bf6c9
7
- data.tar.gz: 72490ad79e2f0b1a8cadb2150dccc0a9544f3c0ab018298407f4bce4deb549cd62935a505279c139b955c74b84fabb460e641fc2ada8d9e7025e95dff2e5fe3c
6
+ metadata.gz: c13509e8f28653f671101d17c861c4a03ff118c3f44233fb6a522423b5f7698535a56e712bacd4b22a250d82404cad96086caa5138089ac53bdfecd301257122
7
+ data.tar.gz: 4cc33a23a642c60143869391e8ca678d6cb6b3634695be26e607baa0818b2aa9ac4e6a130c67cb732d72078f0ff59710f55818c92de50509a57c64c9a05b76d6
@@ -49,7 +49,15 @@ module Sequel
49
49
  raise Error, "Unsupported reflection type: #{t}"
50
50
  end
51
51
 
52
- ds.where(remote_keys => local_keys).select(1)
52
+ local_keys = Array(local_keys)
53
+ remote_keys = Array(remote_keys)
54
+
55
+ ds.where(
56
+ remote_keys.
57
+ zip(local_keys).
58
+ map{|r,l| {r => l}}.
59
+ inject{|a,b| Sequel.&(a, b)}
60
+ ).select(1)
53
61
  end
54
62
  end
55
63
 
@@ -1,7 +1,7 @@
1
1
  module Sequel
2
2
  module Plugins
3
3
  module AssociationFiltering
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
6
6
  end
7
7
  end
@@ -1,14 +1,31 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class BasicSpec < AssociationFilteringSpecs
4
+ before do
5
+ DB.create_table :artists do
6
+ primary_key :id
7
+ end
8
+
9
+ DB.run <<-SQL
10
+ INSERT INTO artists SELECT i FROM generate_series(1, 10) i;
11
+ SQL
12
+
13
+ class Artist < Sequel::Model
14
+ end
15
+ end
16
+
17
+ after do
18
+ DB.drop_table? :artists
19
+ end
20
+
4
21
  describe "association_filter" do
5
22
  it "with an unknown association should throw an error" do
6
23
  error =
7
24
  assert_raises(Sequel::Plugins::AssociationFiltering::Error) do
8
- Album.association_filter(:widgets)
25
+ Artist.association_filter(:widgets)
9
26
  end
10
27
 
11
- assert_equal "association widgets not found on model Album", error.message
28
+ assert_equal "association widgets not found on model BasicSpec::Artist", error.message
12
29
  end
13
30
  end
14
31
  end
@@ -1,6 +1,50 @@
1
1
  require 'spec_helper'
2
2
 
3
- class AssociationFilteringSpec < AssociationFilteringSpecs
3
+ class ManyToManySpec < AssociationFilteringSpecs
4
+ before do
5
+ drop_tables
6
+
7
+ DB.create_table :albums do
8
+ primary_key :id
9
+ end
10
+
11
+ DB.create_table :genres do
12
+ primary_key :id
13
+ end
14
+
15
+ DB.create_table :album_genres do
16
+ primary_key :id
17
+ foreign_key :album_id, :albums
18
+ foreign_key :genre_id, :genres
19
+
20
+ unique [:album_id, :genre_id]
21
+ end
22
+
23
+ DB.run <<-SQL
24
+ INSERT INTO albums SELECT i FROM generate_series(1, 100) i;
25
+ INSERT INTO genres SELECT i FROM generate_series(1, 10) i;
26
+
27
+ INSERT INTO album_genres (album_id, genre_id)
28
+ SELECT DISTINCT ceil(random() * 100), ceil(random() * 10) FROM generate_series(1, 300);
29
+ SQL
30
+
31
+ class Album < Sequel::Model
32
+ many_to_many :genres, class: 'ManyToManySpec::Genre', join_table: :album_genres
33
+ end
34
+
35
+ class Genre < Sequel::Model
36
+ many_to_many :albums, class: 'ManyToManySpec::Album', join_table: :album_genres
37
+ end
38
+ end
39
+
40
+ after do
41
+ drop_tables
42
+ end
43
+
44
+ def drop_tables
45
+ DB.drop_table? :album_genres, :genres, :albums
46
+ end
47
+
4
48
  describe "association_filter through a many_to_many_association" do
5
49
  it "should support an empty filter that checks for existence" do
6
50
  expected_count = DB[:album_genres].distinct(:album_id).count
@@ -1,6 +1,40 @@
1
1
  require 'spec_helper'
2
2
 
3
- class AssociationFilteringSpec < AssociationFilteringSpecs
3
+ class ManyToOneSpec < AssociationFilteringSpecs
4
+ before do
5
+ drop_tables
6
+
7
+ DB.create_table :artists do
8
+ primary_key :id
9
+ end
10
+
11
+ DB.create_table :albums do
12
+ primary_key :id
13
+ foreign_key :artist_id, :artists
14
+ end
15
+
16
+ DB.run <<-SQL
17
+ INSERT INTO artists SELECT i FROM generate_series(1, 10) i;
18
+ INSERT INTO albums (artist_id) SELECT (i % 10) + 1 FROM generate_series(1, 100) i;
19
+ SQL
20
+
21
+ class Artist < Sequel::Model
22
+ one_to_many :albums, class: 'ManyToOneSpec::Album'
23
+ end
24
+
25
+ class Album < Sequel::Model
26
+ many_to_one :artist, class: 'ManyToOneSpec::Artist'
27
+ end
28
+ end
29
+
30
+ after do
31
+ drop_tables
32
+ end
33
+
34
+ def drop_tables
35
+ DB.drop_table? :albums, :artists
36
+ end
37
+
4
38
  describe "association_filter through a many_to_one association" do
5
39
  it "should support a simple filter" do
6
40
  ds = Album.association_filter(:artist){|a| a.where{mod(id, 5) =~ 0}}
@@ -1,23 +1,133 @@
1
1
  require 'spec_helper'
2
2
 
3
- class AssociationFilteringSpec < AssociationFilteringSpecs
4
- describe "association_filter through a one_to_many association" do
5
- it "should support a simple filter" do
6
- ds = Album.association_filter(:tracks){|t| t.where(id: 40)}
7
- assert_equal 1, ds.count
8
- assert_equal %(SELECT * FROM "albums" WHERE (EXISTS (SELECT 1 FROM "tracks" WHERE (("tracks"."album_id" = "albums"."id") AND ("id" = 40))))), ds.sql
3
+ class OneToManySpec < AssociationFilteringSpecs
4
+ describe "with a singular primary_key" do
5
+ before do
6
+ drop_tables
9
7
 
10
- record = ds.first!
11
- assert_includes record.tracks.map(&:id), 40
8
+ DB.create_table :artists do
9
+ primary_key :id
10
+ end
11
+
12
+ DB.create_table :albums do
13
+ primary_key :id
14
+ foreign_key :artist_id, :artists
15
+ end
16
+
17
+ DB.run <<-SQL
18
+ INSERT INTO artists SELECT i FROM generate_series(1, 10) i;
19
+ INSERT INTO albums (artist_id) SELECT (i % 10) + 1 FROM generate_series(1, 100) i;
20
+ SQL
21
+
22
+ class Artist < Sequel::Model
23
+ one_to_many :albums, class: 'OneToManySpec::Album'
24
+ end
25
+
26
+ class Album < Sequel::Model
27
+ many_to_one :artist, class: 'OneToManySpec::Artist'
28
+ end
29
+ end
30
+
31
+ after do
32
+ OneToManySpec.send(:remove_const, :Artist)
33
+ OneToManySpec.send(:remove_const, :Album)
34
+ drop_tables
35
+ end
36
+
37
+ def drop_tables
38
+ DB.drop_table? :albums, :artists
39
+ end
40
+
41
+ describe "association_filter through a one_to_many association" do
42
+ it "should support a simple filter" do
43
+ ds = Artist.association_filter(:albums){|t| t.where(id: 40)}
44
+ assert_equal 1, ds.count
45
+ assert_equal %(SELECT * FROM "artists" WHERE (EXISTS (SELECT 1 FROM "albums" WHERE (("albums"."artist_id" = "artists"."id") AND ("id" = 40))))), ds.sql
46
+
47
+ artist = ds.first!
48
+ assert_includes artist.albums.map(&:id), 40
49
+ end
50
+
51
+ it "should support an empty filter that checks for existence" do
52
+ ds = Artist.association_filter(:albums)
53
+ assert_equal 10, ds.count
54
+ assert_equal %(SELECT * FROM "artists" WHERE (EXISTS (SELECT 1 FROM "albums" WHERE ("albums"."artist_id" = "artists"."id")))), ds.sql
55
+
56
+ Album.where(artist_id: 5).delete
57
+ assert_equal 9, ds.count
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "with a composite primary key" do
63
+ before do
64
+ drop_tables
65
+
66
+ DB.create_table :artists do
67
+ integer :id_1, null: false
68
+ integer :id_2, null: false
69
+ serial :serial_column
70
+
71
+ primary_key [:id_1, :id_2]
72
+ end
73
+
74
+ DB.create_table :albums do
75
+ integer :id_1, null: false
76
+ integer :id_2, null: false
77
+ integer :id_3, null: false
78
+ serial :serial_column
79
+
80
+ primary_key [:id_1, :id_2, :id_3]
81
+ foreign_key [:id_1, :id_2], :artists
82
+ end
83
+
84
+ DB.run <<-SQL
85
+ INSERT INTO artists (id_1, id_2) SELECT i,j FROM generate_series(1, 10) i CROSS JOIN generate_series(1, 10) j;
86
+ INSERT INTO albums (id_1, id_2, id_3) SELECT i,j,k FROM generate_series(1, 10) i CROSS JOIN generate_series(1, 10) j CROSS JOIN generate_series(1, 10) k;
87
+ SQL
88
+
89
+ class Artist < Sequel::Model
90
+ set_primary_key [:id_1, :id_2]
91
+
92
+ one_to_many :albums, class: 'OneToManySpec::Album', key: [:id_1, :id_2]
93
+ end
94
+
95
+ class Album < Sequel::Model
96
+ set_primary_key [:id_1, :id_2, :id_3]
97
+
98
+ many_to_one :artist, class: 'OneToManySpec::Artist', key: [:id_1, :id_2]
99
+ end
100
+ end
101
+
102
+ after do
103
+ OneToManySpec.send(:remove_const, :Artist)
104
+ OneToManySpec.send(:remove_const, :Album)
105
+ drop_tables
106
+ end
107
+
108
+ def drop_tables
109
+ DB.drop_table? :albums, :artists
12
110
  end
13
111
 
14
- it "should support an empty filter that checks for existence" do
15
- ds = Album.association_filter(:tracks)
16
- assert_equal 100, ds.count
17
- assert_equal %(SELECT * FROM "albums" WHERE (EXISTS (SELECT 1 FROM "tracks" WHERE ("tracks"."album_id" = "albums"."id")))), ds.sql
112
+ describe "association_filter through a one_to_many association" do
113
+ it "should support a simple filter" do
114
+ ds = Artist.association_filter(:albums){|t| t.where(serial_column: 40)}
115
+
116
+ assert_equal 1, ds.count
117
+ assert_equal %(SELECT * FROM "artists" WHERE (EXISTS (SELECT 1 FROM "albums" WHERE (("albums"."id_1" = "artists"."id_1") AND ("albums"."id_2" = "artists"."id_2") AND ("serial_column" = 40))))), ds.sql
118
+
119
+ artist = ds.first!
120
+ assert_includes artist.albums.map(&:serial_column), 40
121
+ end
122
+
123
+ it "should support an empty filter that checks for existence" do
124
+ ds = Artist.association_filter(:albums)
125
+ assert_equal 100, ds.count
126
+ assert_equal %(SELECT * FROM "artists" WHERE (EXISTS (SELECT 1 FROM "albums" WHERE (("albums"."id_1" = "artists"."id_1") AND ("albums"."id_2" = "artists"."id_2"))))), ds.sql
18
127
 
19
- Track.where(album_id: 50).delete
20
- assert_equal 99, ds.count
128
+ Album.where(id_1: 5, id_2: 6).delete
129
+ assert_equal 99, ds.count
130
+ end
21
131
  end
22
132
  end
23
133
  end
@@ -6,63 +6,6 @@ Sequel::Model.plugin :association_filtering
6
6
 
7
7
  DB = Sequel.connect "postgres:///sequel-association-filtering-test"
8
8
 
9
- DB.drop_table? :album_genres, :genres, :tracks, :albums, :artists
10
-
11
- DB.create_table :artists do
12
- primary_key :id
13
- end
14
-
15
- DB.create_table :albums do
16
- primary_key :id
17
- foreign_key :artist_id, :artists
18
- end
19
-
20
- DB.create_table :tracks do
21
- primary_key :id
22
- foreign_key :album_id, :albums
23
- end
24
-
25
- DB.create_table :genres do
26
- primary_key :id
27
- end
28
-
29
- DB.create_table :album_genres do
30
- primary_key :id
31
- foreign_key :album_id, :albums
32
- foreign_key :genre_id, :genres
33
-
34
- unique [:album_id, :genre_id]
35
- end
36
-
37
- class Artist < Sequel::Model
38
- one_to_many :albums
39
- end
40
-
41
- class Album < Sequel::Model
42
- many_to_one :artist
43
- one_to_many :tracks
44
-
45
- many_to_many :genres, join_table: :album_genres
46
- end
47
-
48
- class Track < Sequel::Model
49
- many_to_one :album
50
- end
51
-
52
- class Genre < Sequel::Model
53
- many_to_many :albums, join_table: :album_genres
54
- end
55
-
56
- DB.run <<-SQL
57
- INSERT INTO artists SELECT i FROM generate_series(1, 10) i;
58
- INSERT INTO albums (artist_id) SELECT (i % 10) + 1 FROM generate_series(1, 100) i;
59
- INSERT INTO tracks (album_id) SELECT (i % 100) + 1 FROM generate_series(1, 1000) i;
60
- INSERT INTO genres SELECT i FROM generate_series(1, 10) i;
61
-
62
- INSERT INTO album_genres (album_id, genre_id)
63
- SELECT DISTINCT ceil(random() * 100), ceil(random() * 10) FROM generate_series(1, 300);
64
- SQL
65
-
66
9
  require 'pry'
67
10
  require 'minitest/autorun'
68
11
  require 'minitest/pride'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-association-filtering
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hanks
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-09 00:00:00.000000000 Z
11
+ date: 2018-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler