提问人:zaphodbln_ 提问时间:10/15/2023 最后编辑:zaphodbln_ 更新时间:10/15/2023 访问量:59
不能在 RoR 夹具中使用哈希数组
Cannot use Array of Hashes in RoR fixtures
问:
我将 Ruby On Rails 与 PostgreSQL 数据库一起使用。出于性能原因,我使用带有哈希数组的 jsonb 字段,而不是将其提取到特定表中。在现实世界中,这个 blob 看起来像
[ { "row": 1, "price": 0, "column": 1, "status": 1, "index_row": 1, "contingency": 0, "index_column": 1 }, { "row": 1, "price": 0, "column": 2, "status": 1, "index_row": 1, "contingency": 0, "index_column": 2 }... ]
并且工作正常。
现在,我试图增加测试覆盖率,并注意到,我无法使用标准夹具方法创建哈希数组。假设以下情况:
CREATE TABLE IF NOT EXISTS public.circles
(
id bigint NOT NULL DEFAULT nextval('circles_id_seq'::regclass),
blob jsonb
)
以及包含以下内容的夹具文件circles.yml:
templatecircle_1:
blob: <%= [1, 2] %>
这在运行 bin/rails 测试时按预期工作。
templatecircle_1:
blob: {"abc": "def"}
这也按预期工作。
然而
templatecircle_1:
blob: <%= [1, {"abc": "def"}] %>
产生 ActiveRecord::Fixture::FormatError:
请注意,YAML 必须始终使用空格缩进。不允许使用制表符。错误:():在第 2 行第 11 列解析流节点时未找到预期的节点内容
我什至尝试使用以下解决方法:
templatecircle_1:
blob: <%= JSON.parse("[1, {\"abc\": \"def\"}]") %>
这给出了相同的错误。
有没有人知道,如何在 RoR 夹具中创建哈希数组?
答:
若要在 YAML 固定器文件中定义“哈希数组”字段,请对每个数组元素和每个哈希键/值对使用表示法。不需要 ERB 语法或一些 JSON 解析。就是这样的结构:-
:
# test/fixtures/circles.yml
templatecircle_1:
blob:
- row: 1
price: 0
column: 1
status: 1
index_row: 1
contingency: 0
index_column: 1
- row: 1
price: 0
column: 2
status: 1
index_row: 1
contingency: 0
index_column: 2
之后,您可以在测试中调用
Circle.first.blob
# => [{
# "row"=>1,
# "price"=>0,
# "column"=>1,
# "status"=>1,
# "index_row"=>1,
# "contingency"=>0,
# "index_column"=>1
# },
# "row"=>1,
# "price"=>0,
# "column"=>2,
# "status"=>1,
# "index_row"=>1,
# "contingency"=>0,
# "index_column"=>2
# }]
如您所见,它将被自动解析
因此,对于结构将是:[1, {"abc": "def"}]
# test/fixtures/circles.yml
templatecircle_1:
blob:
- 1
- abc: def
在 Ruby 中:
Circle.first.blob
# => [1, {"abc"=>"def"}]
也可以使用这样的语法(只需从原始尝试中删除 ERB):
# test/fixtures/circles.yml
templatecircle_1:
blob: [1, {"abc": "def"}]
在 Ruby 中:
Circle.first.blob
# => [1, {"abc"=>"def"}]
如果您由于某些原因仍然需要 ERB,您可以使用方法to_json
# test/fixtures/circles.yml
templatecircle_1:
blob: <%= [1, { "abc": "def" }].to_json %>
在 Ruby 中:
Circle.first.blob
# => [1, {"abc"=>"def"}]
评论
to_json
templatecircle_1: blob: <% 100.times do |i| %> - row: <%= (1 + i / 10) %> price: 0 column: <%= (1 + i % 10) %> status: <%= Circle::SeatOccupancyFree %> index_row: <%= (1 + i / 10).to_s %> contingency: 0 index_column: <%= (1 + i % 10).to_s %> <% end %>
评论