relation_to_struct 0.0.4 → 0.0.5
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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f14569badb2ed3049d91f233ebc208c09bef0251
|
4
|
+
data.tar.gz: 3513ef3efd8d057645db818927a1939338e75f17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
@@ -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
|