package handlers import ( "os" "path/filepath" "testing" "go.uber.org/fx/fxtest" "sneak.berlin/go/pixa/internal/config" "sneak.berlin/go/pixa/internal/database" "sneak.berlin/go/pixa/internal/globals" "sneak.berlin/go/pixa/internal/logger" ) // TestDiskCacheOffOnlyForExplicitZeroCacheMaxBytes starts the handlers // once with cache_max_bytes omitted and once with cache_max_bytes: 0, // and checks by whether the cache directories were created that the // disk cache is on in the first case and off in the second. func TestDiskCacheOffOnlyForExplicitZeroCacheMaxBytes(t *testing.T) { t.Parallel() cases := []struct { name string cacheMaxBytesExplicit bool wantDiskCache bool }{ {name: "cache_max_bytes omitted", cacheMaxBytesExplicit: false, wantDiskCache: true}, {name: "cache_max_bytes: 0", cacheMaxBytesExplicit: true, wantDiskCache: false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { t.Parallel() stateDir := t.TempDir() cfg := &config.Config{ SigningKey: testSigningKey, StateDir: stateDir, DBURL: "file:" + filepath.Join(stateDir, "state.sqlite3"), CacheMaxBytes: 0, CacheMaxBytesExplicit: tc.cacheMaxBytesExplicit, } lc := fxtest.NewLifecycle(t) log, err := logger.New(lc, logger.Params{Globals: &globals.Globals{}}) if err != nil { t.Fatalf("logger.New() error = %v", err) } db, err := database.New(lc, database.Params{Logger: log, Config: cfg}) if err != nil { t.Fatalf("database.New() error = %v", err) } _, err = New(lc, Params{Logger: log, Database: db, Config: cfg}) if err != nil { t.Fatalf("New() error = %v", err) } lc.RequireStart() t.Cleanup(lc.RequireStop) _, err = os.Stat(filepath.Join(stateDir, "cache", "variants")) gotDiskCache := err == nil if gotDiskCache != tc.wantDiskCache { t.Errorf("cache directories created = %v, want %v", gotDiskCache, tc.wantDiskCache) } }) } }