はじめに
hkob の雑記録の第457回目(通算30日目)は、昨日設計変更を実施して作成することにした ViewProperty のテストを作成していきます。
view_property_spec.rb のテスト
昨日、修正した title_property_spec.rb は全て削除し、新しく作成した view_property_spec.rb の方に移しました。とりあえず TitleProperty で TableView のものをテストしますが、この後でリファクタリングして他の Property や 別の View についても確認していきます。
# frozen_string_literal: true module NotionRubyMapping RSpec.describe ViewProperty do tc = TestConnection.instance context "when created by new" do let(:target) { described_class.new "tp", width: 100 } it_behaves_like "has name as", "tp" it_behaves_like "will not update" it_behaves_like "property view json", {"property_name" => "tp", "visible" => true, "wrap" => true, "width" => 100} describe "width=" do before { target.width = 200 } it_behaves_like "will update" it_behaves_like "update property view json", {"property_name" => "tp", "visible" => true, "wrap" => true, "width" => 200} end end context "when created from json" do let(:target) { ViewProperty.create_from_json "tp", tc.read_json("title_view_property_object") } it_behaves_like "has name as", "tp" it_behaves_like "will not update" it_behaves_like "property view json", {"property_id" => "title", "property_name" => "tp", "visible" => true, "width" => 129, "wrap" => true} it_behaves_like "update property view json", {} end end end
property view json と update property view json の shared_example は以下のようにしました。
shared_examples_for "property view json" do |json| it do expect(target.property_view_json).to eq json end end shared_examples_for "update property view json" do |json| it do expect(target.update_property_view_json).to eq json end end
まず、ViewProperty のクラス雛形だけ作成します。また、notion_ruby_mapping.rb の中で require_relative にも追加しておきます。
# frozen_string_literal: true class ViewProperty end
これでテストは実行されましたが、 spec/fixtures/title_view_property_object.json が見つからないというエラーになりました。以下のファイルを格納しておきます。
{ "property_id": "title", "property_name": "Title", "visible": true, "width": 129, "wrap": true }
view_property.rb の実装
あとはこれらが通るように実装するだけです。
# frozen_string_literal: true class ViewProperty def initialize(name, property_id: nil, will_update: false, width: nil, visible: true, wrap: true) @name = name @property_id = property_id @will_update = will_update @width = width @visible = visible @wrap = wrap end attr_reader :name, :width, :will_update def width=(width) @width = width @will_update = true end def property_view_json { "property_name" => @name, "visible" => @visible, "wrap" => @wrap, }.tap do |h| h["width"] = @width if @width h["property_id"] = @property_id if @property_id end end def update_property_view_json @will_update ? property_view_json : {} end def self.create_from_json(name, json) new name, property_id: json["property_id"], width: json["width"], visible: json["visible"], wrap: json["wrap"] end end
おわりに
最低限の ViewProperty のテストと実装を記載したので、Retreive a view API で取得した JSON の configuration ViewProperty を取得する処理を記載したいと思います。その後、filter, sorts, group by, subtasks などのプロパティ設定も確認するため、retrieve_view_table_view.json はこれらの設定を TableView に設定した後で取得し直すかもしれません。