|
| 1 | +package filestore |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/vim-jp/slacklog-generator/internal/store" |
| 12 | + "github.com/vim-jp/slacklog-generator/internal/testassert" |
| 13 | +) |
| 14 | + |
| 15 | +func jsonToChannel(t *testing.T, s string) *store.Channel { |
| 16 | + t.Helper() |
| 17 | + var c store.Channel |
| 18 | + err := json.Unmarshal([]byte(s), &c) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("failed to parse as Channel: %s", err) |
| 21 | + } |
| 22 | + return &c |
| 23 | +} |
| 24 | + |
| 25 | +func TestChannelStore_Get(t *testing.T) { |
| 26 | + cs := &channelStore{dir: "testdata/channel_read"} |
| 27 | + |
| 28 | + for _, tc := range []struct { |
| 29 | + id string |
| 30 | + exp string |
| 31 | + }{ |
| 32 | + {"CXXXX0001", `{"id":"CXXXX0001","name":"channel01"}`}, |
| 33 | + {"CXXXX0002", `{"id":"CXXXX0002","name":"channel02"}`}, |
| 34 | + {"CXXXX0003", `{"id":"CXXXX0003","name":"channel03"}`}, |
| 35 | + {"CXXXX0004", `{"id":"CXXXX0004","name":"channel04"}`}, |
| 36 | + {"CXXXX0005", `{"id":"CXXXX0005","name":"channel05"}`}, |
| 37 | + } { |
| 38 | + act, err := cs.Get(tc.id) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("failed to get(%s): %s", tc.id, err) |
| 41 | + } |
| 42 | + exp := jsonToChannel(t, tc.exp) |
| 43 | + testassert.Equal(t, exp, act, "id:"+tc.id) |
| 44 | + } |
| 45 | + |
| 46 | + c, err := cs.Get("CXXXX9999") |
| 47 | + if err == nil { |
| 48 | + t.Fatalf("should fail to get unknown ID: %+v", c) |
| 49 | + } |
| 50 | + if !strings.HasPrefix(err.Error(), "channel not found, ") { |
| 51 | + t.Fatalf("unexpected error for getting unknown ID: %s", err) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestChannelStore_Iterate(t *testing.T) { |
| 56 | + cs := &channelStore{dir: "testdata/channel_read"} |
| 57 | + |
| 58 | + var act []*store.Channel |
| 59 | + err := cs.Iterate(store.ChannelIterateFunc(func(c *store.Channel) bool { |
| 60 | + act = append(act, c) |
| 61 | + return true |
| 62 | + })) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("iteration failed: %s", err) |
| 65 | + } |
| 66 | + exp := []*store.Channel{ |
| 67 | + jsonToChannel(t, `{"id":"CXXXX0001","name":"channel01"}`), |
| 68 | + jsonToChannel(t, `{"id":"CXXXX0002","name":"channel02"}`), |
| 69 | + jsonToChannel(t, `{"id":"CXXXX0003","name":"channel03"}`), |
| 70 | + jsonToChannel(t, `{"id":"CXXXX0004","name":"channel04"}`), |
| 71 | + jsonToChannel(t, `{"id":"CXXXX0005","name":"channel05"}`), |
| 72 | + } |
| 73 | + testassert.Equal(t, exp, act, "simple iteration") |
| 74 | +} |
| 75 | + |
| 76 | +func TestChannelStore_Iterate_Break(t *testing.T) { |
| 77 | + cs := &channelStore{dir: "testdata/channel_read"} |
| 78 | + |
| 79 | + i := 0 |
| 80 | + err := cs.Iterate(store.ChannelIterateFunc(func(_ *store.Channel) bool { |
| 81 | + i++ |
| 82 | + return i > 2 |
| 83 | + })) |
| 84 | + if !errors.Is(err, store.ErrIterateAbort) { |
| 85 | + t.Fatalf("iterate should be failed with:%s got:%s", store.ErrIterateAbort, err) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestChannelStore_Write(t *testing.T) { |
| 90 | + dir, err := ioutil.TempDir("testdata", "channel_write*") |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("failed to TempDir: %s", err) |
| 93 | + } |
| 94 | + t.Cleanup(func() { |
| 95 | + os.RemoveAll(dir) |
| 96 | + }) |
| 97 | + cs := &channelStore{dir: dir} |
| 98 | + |
| 99 | + for i, s := range []string{ |
| 100 | + `{"id":"W0001","name":"channel01"}`, |
| 101 | + `{"id":"W0009","name":"channel09"}`, |
| 102 | + `{"id":"W0005","name":"channel05"}`, |
| 103 | + `{"id":"W0001","name":"channel01a"}`, |
| 104 | + } { |
| 105 | + _, err := cs.Upsert(*jsonToChannel(t, s)) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("upsert failed #%d: %s", i, err) |
| 108 | + } |
| 109 | + } |
| 110 | + err = cs.Commit() |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("commit failed: %s", err) |
| 113 | + } |
| 114 | + |
| 115 | + cs2 := &channelStore{dir: dir} |
| 116 | + err = cs2.assureLoad() |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("assureLoad failed: %s", err) |
| 119 | + } |
| 120 | + testassert.Equal(t, []store.Channel{ |
| 121 | + *jsonToChannel(t, `{"id":"W0001","name":"channel01a"}`), |
| 122 | + *jsonToChannel(t, `{"id":"W0005","name":"channel05"}`), |
| 123 | + *jsonToChannel(t, `{"id":"W0009","name":"channel09"}`), |
| 124 | + }, cs2.channels, "wrote channels.json") |
| 125 | +} |
| 126 | + |
| 127 | +func TestChannelStore_Upsert_NoID(t *testing.T) { |
| 128 | + dir, err := ioutil.TempDir("testdata", "channel_write*") |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("failed to TempDir: %s", err) |
| 131 | + } |
| 132 | + t.Cleanup(func() { |
| 133 | + os.RemoveAll(dir) |
| 134 | + }) |
| 135 | + cs := &channelStore{dir: dir} |
| 136 | + _, err = cs.Upsert(*jsonToChannel(t, `{"name":"foobar"}`)) |
| 137 | + if err == nil { |
| 138 | + t.Fatal("upsert without ID should be failed") |
| 139 | + } |
| 140 | + if err.Error() != "empty ID is forbidden" { |
| 141 | + t.Fatalf("unexpected failure: %s", err) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func TestChannelStore_Commit_Empty(t *testing.T) { |
| 146 | + // 空のCommitは channels.json を作らない |
| 147 | + dir, err := ioutil.TempDir("testdata", "channel_write*") |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("failed to TempDir: %s", err) |
| 150 | + } |
| 151 | + t.Cleanup(func() { |
| 152 | + os.RemoveAll(dir) |
| 153 | + }) |
| 154 | + cs := &channelStore{dir: dir} |
| 155 | + |
| 156 | + err = cs.Commit() |
| 157 | + if err != nil { |
| 158 | + t.Fatalf("unexpted failure: %s", err) |
| 159 | + } |
| 160 | + fi, err := os.Stat(cs.path()) |
| 161 | + if err == nil { |
| 162 | + t.Fatalf("channels.json created unexpectedly: %s", fi.Name()) |
| 163 | + } |
| 164 | + if !os.IsNotExist(err) { |
| 165 | + t.Fatalf("unexpected failure: %s", err) |
| 166 | + } |
| 167 | +} |
0 commit comments