|
| 1 | +package youtube |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestSimpleCache(t *testing.T) { |
| 9 | + type args struct { |
| 10 | + setVideoID string |
| 11 | + getVideoID string |
| 12 | + operations []DecipherOperation |
| 13 | + expiredAt string |
| 14 | + getCacheAt string |
| 15 | + } |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + args args |
| 19 | + want []DecipherOperation |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "Get cache data with video id", |
| 23 | + args: args{ |
| 24 | + setVideoID: "test", |
| 25 | + getVideoID: "test", |
| 26 | + operations: []DecipherOperation{func(bytes []byte) []byte { return nil }}, |
| 27 | + expiredAt: "2021-01-01 00:01:00", |
| 28 | + getCacheAt: "2021-01-01 00:00:00", |
| 29 | + }, |
| 30 | + want: []DecipherOperation{func(bytes []byte) []byte { return nil }}, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "Get nil when cache is expired", |
| 34 | + args: args{ |
| 35 | + setVideoID: "test", |
| 36 | + getVideoID: "test", |
| 37 | + operations: []DecipherOperation{func(bytes []byte) []byte { return nil }}, |
| 38 | + expiredAt: "2021-01-01 00:00:00", |
| 39 | + getCacheAt: "2021-01-01 00:00:00", |
| 40 | + }, |
| 41 | + want: nil, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Get nil when video id is not cached", |
| 45 | + args: args{ |
| 46 | + setVideoID: "test", |
| 47 | + getVideoID: "not test", |
| 48 | + operations: []DecipherOperation{func(bytes []byte) []byte { return nil }}, |
| 49 | + expiredAt: "2021-01-01 00:00:01", |
| 50 | + getCacheAt: "2021-01-01 00:00:00", |
| 51 | + }, |
| 52 | + want: nil, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + s := NewSimpleCache() |
| 59 | + timeFormat := "2006-01-02 15:04:05" |
| 60 | + expiredAt, _ := time.Parse(timeFormat, tt.args.expiredAt) |
| 61 | + s.setWithExpiredTime(tt.args.setVideoID, tt.args.operations, expiredAt) |
| 62 | + getCacheAt, _ := time.Parse(timeFormat, tt.args.getCacheAt) |
| 63 | + if got := s.GetCacheBefore(tt.args.getVideoID, getCacheAt); len(got) != len(tt.want) { |
| 64 | + t.Errorf("GetCacheBefore() = %v, want %v", got, tt.want) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
0 commit comments