FrankYang0529 commented on code in PR #73277: URL: https://github.com/apache/airflow/pull/73277#discussion_r4052069561
########## go-sdk/airflow/context_test.go: ########## @@ -0,0 +1,171 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package airflow + +import ( + "context" + "io" + "log/slog" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/apache/airflow/go-sdk/sdk" +) + +type probeKey struct{} + +// fakeClient satisfies sdk.Client without implementing any of it. +// These tests only check which value the accessor hands back. +type fakeClient struct { + sdk.Client +} + +func testValues() (*slog.Logger, *fakeClient, TaskInstance, DagRun) { + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + client := &fakeClient{} + ti := TaskInstance{DagID: "py_etl", RunID: "run1", TaskID: "transform", TryNumber: 2} + dagRun := DagRun{DagID: "py_etl", RunID: "run1"} + return logger, client, ti, dagRun +} + +func TestNewContextAccessors(t *testing.T) { + logger, client, ti, dagRun := testValues() + + base := context.WithValue(context.Background(), probeKey{}, "probe-value") + actx := NewContext(base, logger, client, ti, dagRun) + + assert.Same(t, logger, actx.Logger()) + assert.Same(t, client, actx.Client()) + assert.Equal(t, ti, actx.TaskInstance()) + assert.Equal(t, dagRun, actx.DagRun()) + assert.Equal( + t, + "probe-value", + actx.Value(probeKey{}), + "context behaviour must delegate to the base context", + ) +} + +// Fail at construction rather than hand out a Context whose accessors return nil. +func TestNewContextRejectsNilArgs(t *testing.T) { + logger, client, ti, dagRun := testValues() + + cases := map[string]func(){ + "nil context": func() { NewContext(nil, logger, client, ti, dagRun) }, Review Comment: nit: for `nil` context, even if we don't have the check, it still fails in `context.WithValue`. To make sure the test case can cover `ctx == nil`, we can change the test case like following: ```diff --- a/go-sdk/airflow/context_test.go +++ b/go-sdk/airflow/context_test.go @@ -69,13 +69,19 @@ logger, client, ti, dagRun := testValues() cases := map[string]func(){ - "nil context": func() { NewContext(nil, logger, client, ti, dagRun) }, - "nil logger": func() { NewContext(context.Background(), nil, client, ti, dagRun) }, - "nil client": func() { NewContext(context.Background(), logger, nil, ti, dagRun) }, + "airflow.NewContext: nil context.Context": func() { + NewContext(nil, logger, client, ti, dagRun) + }, + "airflow.NewContext: nil logger": func() { + NewContext(context.Background(), nil, client, ti, dagRun) + }, + "airflow.NewContext: nil client": func() { + NewContext(context.Background(), logger, nil, ti, dagRun) + }, } - for name, build := range cases { - t.Run(name, func(t *testing.T) { - assert.Panics(t, build) + for want, build := range cases { + t.Run(want, func(t *testing.T) { + assert.PanicsWithValue(t, want, build) }) } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
