|
| 1 | +package master_data_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "connectrpc.com/connect" |
| 11 | + "github.com/averak/hbaas/app/registry" |
| 12 | + "github.com/averak/hbaas/protobuf/api" |
| 13 | + "github.com/averak/hbaas/protobuf/api/api_errors" |
| 14 | + "github.com/averak/hbaas/protobuf/api/apiconnect" |
| 15 | + "github.com/averak/hbaas/protobuf/resource" |
| 16 | + "github.com/averak/hbaas/testutils" |
| 17 | + "github.com/averak/hbaas/testutils/bdd" |
| 18 | + "github.com/averak/hbaas/testutils/fixture/builder/system_builder" |
| 19 | + "github.com/averak/hbaas/testutils/fixture/setupper/systemup" |
| 20 | + "github.com/averak/hbaas/testutils/testconnect" |
| 21 | + "github.com/google/uuid" |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 25 | +) |
| 26 | + |
| 27 | +func Test_handler_GetV1(t *testing.T) { |
| 28 | + mux, err := registry.InitializeAPIServerMux(context.Background()) |
| 29 | + if err != nil { |
| 30 | + t.Fatal(err) |
| 31 | + } |
| 32 | + server := httptest.NewServer(mux) |
| 33 | + defer server.Close() |
| 34 | + |
| 35 | + now := time.Now().Truncate(time.Millisecond) |
| 36 | + |
| 37 | + type given struct { |
| 38 | + systemData system_builder.Data |
| 39 | + } |
| 40 | + type when struct { |
| 41 | + req *api.MasterDataServiceGetV1Request |
| 42 | + } |
| 43 | + type then = func(*testing.T, *connect.Response[api.MasterDataServiceGetV1Response], error) |
| 44 | + tests := []bdd.Testcase[given, when, then]{ |
| 45 | + { |
| 46 | + Name: "有効なリビジョンが存在する状態で", |
| 47 | + Given: given{ |
| 48 | + systemData: system_builder.New(). |
| 49 | + MasterData( |
| 50 | + system_builder.NewMasterDataBuilder(1). |
| 51 | + Content([]byte("v1")). |
| 52 | + IsActive(true). |
| 53 | + Comment("c1"). |
| 54 | + CreatedAt(now.Add(-1 * time.Hour)). |
| 55 | + Build(), |
| 56 | + ). |
| 57 | + MasterData( |
| 58 | + system_builder.NewMasterDataBuilder(2). |
| 59 | + Content([]byte("v2")). |
| 60 | + IsActive(false). |
| 61 | + Comment("c2"). |
| 62 | + CreatedAt(now). |
| 63 | + Build(), |
| 64 | + ). |
| 65 | + Build(), |
| 66 | + }, |
| 67 | + Behaviors: []bdd.Behavior[when, then]{ |
| 68 | + { |
| 69 | + Name: "有効なリビジョンのマスターデータを取得できる", |
| 70 | + When: when{ |
| 71 | + req: &api.MasterDataServiceGetV1Request{}, |
| 72 | + }, |
| 73 | + Then: func(t *testing.T, got *connect.Response[api.MasterDataServiceGetV1Response], err error) { |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + // 最新のリビジョンではなく、有効なリビジョンが取得される。 |
| 77 | + want := &api.MasterDataServiceGetV1Response{ |
| 78 | + MasterData: &resource.MasterData{ |
| 79 | + Revision: 1, |
| 80 | + Content: []byte("v1"), |
| 81 | + IsActive: true, |
| 82 | + Comment: "c1", |
| 83 | + CreatedAt: timestamppb.New(now.Add(-1 * time.Hour)), |
| 84 | + }, |
| 85 | + } |
| 86 | + assert.EqualExportedValues(t, want, got.Msg) |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + Name: "有効なリビジョンが存在しない状態で", |
| 93 | + Given: given{ |
| 94 | + systemData: system_builder.New(). |
| 95 | + MasterData(system_builder.NewMasterDataBuilder(1).IsActive(false).Build()). |
| 96 | + Build(), |
| 97 | + }, |
| 98 | + Behaviors: []bdd.Behavior[when, then]{ |
| 99 | + { |
| 100 | + Name: "エラーを返す", |
| 101 | + When: when{ |
| 102 | + req: &api.MasterDataServiceGetV1Request{}, |
| 103 | + }, |
| 104 | + Then: func(t *testing.T, got *connect.Response[api.MasterDataServiceGetV1Response], err error) { |
| 105 | + testconnect.AssertErrorCode(t, api_errors.ErrorCode_METHOD_RESOURCE_NOT_FOUND, err) |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + for _, tt := range tests { |
| 112 | + tt.Run(t, func(t *testing.T, given given, when when, then then) { |
| 113 | + systemup.Setup(t, context.Background(), given.systemData) |
| 114 | + defer testutils.Teardown(t) |
| 115 | + |
| 116 | + got, err := testconnect.MethodInvoke( |
| 117 | + apiconnect.NewMasterDataServiceClient(http.DefaultClient, server.URL).GetV1, |
| 118 | + when.req, |
| 119 | + testconnect.WithSpoofingUserID(uuid.New()), |
| 120 | + ) |
| 121 | + then(t, got, err) |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments