relation_to_struct 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4917a1af7ed5ef3d71c3f31314f67cf7150f2903
4
- data.tar.gz: d1e99802577449b4b789a7b52476a4a7333b6b36
3
+ metadata.gz: f14569badb2ed3049d91f233ebc208c09bef0251
4
+ data.tar.gz: 3513ef3efd8d057645db818927a1939338e75f17
5
5
  SHA512:
6
- metadata.gz: 8bf5ac98b2a2945701453e84c48576ba6257a2be03109d5efa32dd1e4d1bb1d23abc57bb21f570798e8d919d19707ca8e71c2374aa53d3ea139e09a1a3804cbf
7
- data.tar.gz: d4cdf8c1d1d0de0b9cc9e3b40112888a5b20f6053a7c5e7faca2895cd4db7d14a8d3b769246fe112ab7516ebe88e3a1d9d7df121acfeef4b62a98540cc1c4c35
6
+ metadata.gz: 87b088344c4b07d36ef607bd145a6a4480b6397e07c47b3495e40462d7bf9a4ac564e4590d4addd01deae8353c7abd757ac60fbdd5ded1b2a22e3e71355fa37e
7
+ data.tar.gz: 70b3761d903aee08b8a36a3fd16dc53b8bfb416b23dc8445e26421d38ec289d9e6cc548f1dcda139fba0be407f12a4d90aaccb1632d142394a83954d3815818b
@@ -9,6 +9,10 @@ module RelationToStruct::ActiveRecordBaseExtension
9
9
  raise ArgumentError, 'Expected struct fields and columns lengths to be equal'
10
10
  end
11
11
 
12
+ if result.columns.size != result.column_types.size
13
+ raise ArgumentError, 'Expected unique column names count and column count to be equal'
14
+ end
15
+
12
16
  if result.columns.size == 1
13
17
  result.cast_values().map do |tuple|
14
18
  struct_class.new(tuple)
@@ -11,6 +11,10 @@ module RelationToStruct::ActiveRecordRelationExtension
11
11
  raise ArgumentError, 'Expected struct fields and columns lengths to be equal'
12
12
  end
13
13
 
14
+ if result.columns.size != result.column_types.size
15
+ raise ArgumentError, 'Expected unique column names count and column count to be equal'
16
+ end
17
+
14
18
  result.cast_values(klass.column_types)
15
19
 
16
20
  if result.columns.size == 1
@@ -1,3 +1,3 @@
1
1
  module RelationToStruct
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -42,7 +42,7 @@ describe RelationToStruct do
42
42
  expect(
43
43
  Economist
44
44
  .joins(:economic_school)
45
- .select('economists.name', 'economic_schools.name')
45
+ .select('economists.name', 'economic_schools.name as school_name')
46
46
  .to_structs(test_struct)
47
47
  ).to eq([test_struct.new(hayek.name, austrian.name)])
48
48
  end
@@ -81,6 +81,13 @@ describe RelationToStruct do
81
81
  Economist.select('id, name').to_structs(test_struct)
82
82
  end.to raise_error(ArgumentError, 'Expected struct fields and columns lengths to be equal')
83
83
  end
84
+
85
+ it '#to_structs should raise an error when column_type count does not match the column count' do
86
+ expect do
87
+ test_struct = Struct.new(:id, :id2)
88
+ Economist.select('id, id').to_structs(test_struct)
89
+ end.to raise_error(ArgumentError, 'Expected unique column names count and column count to be equal')
90
+ end
84
91
  end
85
92
 
86
93
  describe 'non-model specific querying' do
@@ -126,5 +133,12 @@ describe RelationToStruct do
126
133
  ActiveRecord::Base.structs_from_sql(test_struct, 'SELECT id, name FROM economists')
127
134
  end.to raise_error(ArgumentError, 'Expected struct fields and columns lengths to be equal')
128
135
  end
136
+
137
+ it 'structs_from_sql should raise an error when column_type count does not match the column count' do
138
+ expect do
139
+ test_struct = Struct.new(:id, :id2)
140
+ ActiveRecord::Base.structs_from_sql(test_struct, 'SELECT id, id FROM economists')
141
+ end.to raise_error(ArgumentError, 'Expected unique column names count and column count to be equal')
142
+ end
129
143
  end
130
144
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relation_to_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Coleman