NotionRubyMapping のアップデート(15) : hkob の雑記録 (171)

はじめに

hkob の雑記録の第171回目は、画像ブロックへの File Upload object の更新のテストを追加します。

update_block_image_file_upload

今日は ImageBlock へ file_upload_object= メソッドのテストを行います。まずは、before で file_upload_object= を実行し、updata_block_json が正しいことを確認します。実は昨日の FileBlock のテストと異なるのは describe の中の文字列だけです。JSON は type という変数で記述しているので、コード部分は完全に同じです。

    describe "update_block_file_file_upload" do
      let(:target) { described_class.new "abc", id: "20bd8e4e-98ab-8081-ad29-dd62726e4525" }
      let(:file_upload_object) do
        instance_double FileUploadObject, id: TestConnection::FILE_UPLOAD_IMAGE_ID, fname: "test.png"
      end

      before do
        allow(file_upload_object).to receive(:is_a?).with(FileUploadObject).and_return(true)
        target.file_upload_object = file_upload_object
      end

      it {
        expect(target.update_block_json).to eq(
          {
            type => {
              file_upload: {
                id: file_upload_object.id,
              },
            },
          },
        )
      }

ImageObject は type の設定以外は全て FileBaseBlock に丸投げです。FileBaseBlock は昨日修正しているので、実装の修正部分はありません。

# frozen_string_literal: true

module NotionRubyMapping
  # Notion block
  class ImageBlock < FileBaseBlock
    # @return [Symbol]
    def type
      :image
    end
  end
end

これにより、テストが通過しました。

  update_block_image_file_upload
    is expected to eq {image: {file_upload: {id: "20cd8e4e98ab81aa973b00b23083c115"}}}

続いて、dry_run のテストを追加します。これもテストコードは全く同じです。

      context "when dry_run" do
        let(:dry_run) { target.save dry_run: true }

        it_behaves_like "dry run", :patch, :block_path, use_id: true, json_method: :update_block_json
      end

こちらも payload は image にちゃんと変わっていますが、問題なくテストが通過します。

    when dry_run
      behaves like dry run
        is expected to eq "#!/bin/sh\ncurl -X PATCH 'https://api.notion.com/v1/blocks/20bd8e4e98ab8081ad29dd62726e4525' \\\n  -...on/json' \\\n  --data '{\"image\":{\"file_upload\":{\"id\":\"20cd8e4e98ab81aa973b00b23083c115\"}}}'"       

最後に実際の save のテストを記述します。

      context "when save" do
        it {

save の方は WebMock の stub が存在しないので、以下のようなエラーになりました。

  1) NotionRubyMapping::ImageBlock update_block_image_file_upload when save 
     Failure/Error:
       response = @client.send(method) do |request|
         request.headers["Authorization"] = "Bearer #{@notion_token}"                                                               
         case method
         when :get, :delete
           request.url path, options
         when :post, :put, :patch
           request.headers["Content-Type"] = "application/json"
           request.path = path
           request.body = options.to_json unless options.empty?
         else
     
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Please stub: PATCH https://api.notion.com/v1/blocks/20bd8e4e98ab8081ad29dd62726e4525 with body '{"image":{"file_upload":{"id":"20bd8e4e98ab802193bef79f70225b06"}}}' with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer secret_J08RBf9SlofiMhuIeJZc6AnI5qHXwDOaq4RolJFaaZ0', 'Content-Type'=>'application/json', 'Notion-Version'=>'2022-06-28', 'User-Agent'=>'Faraday v2.13.1'} 

こちらも dry_run に書かれている payload に対する stub が必要となります。昨日と同様に spec_helper にて登録を行います。upload_block の下に stubs を一括作成するメソッドを用意しているので、そこに payload を追加します。ImageBlock の id のページに、{} 以下の payload を入れた時に、200 の response code を返し、update_block_file_file_upload.json を返すという stub が追加されます。

    def update_block
      generate_stubs_sub :patch, __method__, :block_path, {
        (中略)
        image_file_upload: [
          "20bd8e4e98ab802193bef79f70225b06", 200,
          {
            image: {
              file_upload: {
                id: TestConnection::FILE_UPLOAD_IMAGE_ID,
              },
            },
          }
        ],
      }
    end

これによって、save も無事にテストが通過しました。

    when save
      is expected to eq "https://prod-files-secure.s3.us-west-2.amazonaws.com/2b7b01f0-67a8-40f8-acd4-88dd2805f216/bf91dfb5-7...a5969d427c9edeecad80a93fdf6a8fccd6a9d2791cb03667bb647e70ad6&X-Amz-SignedHeaders=host&x-id=GetObject"                                    

Finished in 0.0734 seconds (files took 0.3478 seconds to load)
16 examples, 0 failures

おわりに

FileBlock が動作したので、ImageBlock は実装を何も修正することなくテストを記載しただけで動作しました。明日は cover 画像の設定か、大きなファイルのアップロードのどちらかを実装したいと思います。

hkob.notion.site