Documentation ¶
Overview ¶
Package test contains common functionality and data used by tests.
Index ¶
- Constants
- Variables
- func Caller() string
- func CallerDir() (string, error)
- func CloudBuild() bool
- func CompareDumpFileFlag(p string) string
- func CompareSongs(expected, actual []db.Song, order OrderPolicy) error
- func CopySongs(dir string, filenames ...string) error
- func Date(year int, month time.Month, day int, tm ...int) time.Time
- func DeleteSongs(dir string, filenames ...string) error
- func DumpedGainsFlag(p string) string
- func FindUnusedPorts(n int) ([]int, error)
- func ForceGlobFlag(glob string) string
- func HandleSignals(sigs []os.Signal, f func())
- func Must(t *testing.T, err error)
- func OutputDir(suite string) (dir string, keep bool, err error)
- func ServeFiles(dir string) *httptest.Server
- func SongsDir() (string, error)
- func WriteSongPathsFile(dir string, filenames ...string) (string, error)
- func WriteSongsToJSONFile(dir string, songs ...db.Song) (string, error)
- type DeletionPolicy
- type DevAppserver
- type DevAppserverOption
- type FlushType
- type OrderPolicy
- type StripPolicy
- type Tester
- func (t *Tester) ClearData()
- func (t *Tester) DeleteSong(songID string)
- func (t *Tester) DumpSongs(strip StripPolicy, flags ...string) []db.Song
- func (t *Tester) FlushCache(ft FlushType)
- func (t *Tester) ForceUpdateFailures(fail bool)
- func (t *Tester) GetNowFromServer() time.Time
- func (t *Tester) GetSongsForAndroid(minLastModified time.Time, deleted DeletionPolicy) []db.Song
- func (t *Tester) GetStats() db.Stats
- func (t *Tester) GetTags(requireCache bool) string
- func (t *Tester) ImportSongsFromJSONFile(songs []db.Song, flags ...string)
- func (t *Tester) MergeSongs(fromID, toID string, flags ...string)
- func (t *Tester) NewRequest(method, path string, body io.Reader) *http.Request
- func (t *Tester) PingServer()
- func (t *Tester) PostSongs(songs []db.Song, replaceUserData bool, updateDelay time.Duration)
- func (t *Tester) QuerySongs(params ...string) []db.Song
- func (t *Tester) RateAndTag(songID string, rating int, tags []string)
- func (t *Tester) ReindexSongs()
- func (t *Tester) ReportPlayed(songID string, startTime time.Time)
- func (t *Tester) ReportPlayedUnix(songID string, startTime time.Time)
- func (t *Tester) SongID(sha1 string) string
- func (t *Tester) UpdateSongs(flags ...string)
- func (t *Tester) UpdateSongsFromList(path string, flags ...string)
- func (t *Tester) UpdateSongsRaw(flags ...string) (stdout, stderr string, err error)
- func (t *Tester) UpdateStats()
- type TesterConfig
Constants ¶
const ( // Hardcoded gain info used for all songs. Instead of actually running mp3gain (which may not // even be installed) during testing, these values get passed to 'nup update' via its // -test-gain-info flag. TrackGain = -6.7 AlbumGain = -6.3 PeakAmp = 1.05 )
const ( // Username and Password are used for basic HTTP authentication by Tester. // The server must be configured to accept these credentials. Username = "testuser" Password = "testpass" )
const DeleteAfterMergeFlag = "-delete-after-merge"
const KeepUserDataFlag = "-import-user-data=false"
const UseFilenamesFlag = "-use-filenames"
Variables ¶
var ID3V1Song = db.Song{ SHA1: "fefac74a1d5928316d7131747107c8a61b71ffe4", Filename: "id3v1.mp3", Artist: "The Legacy Formats", Title: "Give It Up For ID3v1", Album: "UTF-8, Who Needs It?", Track: 0, Disc: 0, Date: Date(1992, 1, 1), Length: 0.026, TrackGain: TrackGain, AlbumGain: AlbumGain, PeakAmp: PeakAmp, }
var LegacySong1 = db.Song{ SHA1: "1977c91fea860245695dcceea0805c14cede7559", Filename: "arovane/atol_scrap/thaem_nue.mp3", Artist: "Arovane", Title: "Thaem Nue", Album: "Atol Scrap", Track: 1, Disc: 1, Length: 449, TrackGain: TrackGain, AlbumGain: AlbumGain, PeakAmp: PeakAmp, Rating: 4, Plays: []db.Play{ db.NewPlay(Date(2010, 6, 9, 4, 19, 30), "127.0.0.1"), db.NewPlay(Date(2011, 2, 10, 5, 48, 33), "1.2.3.4"), }, Tags: []string{"electronic", "instrumental"}, }
var LegacySong2 = db.Song{ SHA1: "b70984a4ac5084999b70478cdf163218b90cefdb", Filename: "gary_hoey/animal_instinct/motown_fever.mp3", Artist: "Gary Hoey", Title: "Motown Fever", Album: "Animal Instinct", Track: 7, Disc: 1, Length: 182, TrackGain: TrackGain, AlbumGain: AlbumGain, PeakAmp: PeakAmp, Rating: 3, Plays: []db.Play{db.NewPlay(Date(2014, 3, 14, 5, 12, 10), "8.8.8.8")}, Tags: []string{"instrumental", "rock"}, }
var Song0s = db.Song{ SHA1: "fefac74a1d5928316d7131747107c8a61b71ffe4", Filename: "0s.mp3", Artist: "First Artist", Title: "Zero Seconds", Album: "First Album", AlbumID: "1e477f68-c407-4eae-ad01-518528cedc2c", RecordingID: "392cea06-94c2-416b-80aa-f5b1e7d0fb1c", Track: 1, Disc: 1, Date: Date(1992, 1, 1), Length: 0.026, TrackGain: TrackGain, AlbumGain: AlbumGain, PeakAmp: PeakAmp, }
var Song0sUpdated = db.Song{ SHA1: Song0s.SHA1, Filename: "0s-updated.mp3", Artist: Song0s.Artist, Title: "Zero Seconds (Remix)", Album: Song0s.Album, AlbumID: Song0s.AlbumID, RecordingID: "271a81af-6c2d-44cf-a0b8-a25ad74c82f9", Track: Song0s.Track, Disc: Song0s.Disc, Date: Date(1995, 4, 3, 13, 17, 59), Length: Song0s.Length, TrackGain: TrackGain, AlbumGain: AlbumGain, PeakAmp: PeakAmp, }
var Song10s = db.Song{ SHA1: "dfc21dbdf2056184fa3bbe9688a2050f8f2c5dff", Filename: "10s.mp3", Artist: "Boring Artist", Title: "Ten Seconds", Album: "Music for Waiting Rooms", DiscSubtitle: "The Only Disc", Length: 10.031, TrackGain: TrackGain, AlbumGain: AlbumGain, PeakAmp: PeakAmp, }
var Song1s = db.Song{ SHA1: "c6e3230b4ed5e1f25d92dd6b80bfc98736bbee62", Filename: "1s.mp3", Artist: "Second Artist", Title: "One Second", Album: "First Album", AlbumArtist: "The Remixer", AlbumID: "1e477f68-c407-4eae-ad01-518528cedc2c", RecordingID: "5d7e41b2-ec4b-44dd-b25a-a576d7a08adb", Track: 2, Disc: 1, Date: Date(2004, 1, 1), Length: 1.071, TrackGain: TrackGain, AlbumGain: AlbumGain, PeakAmp: PeakAmp, }
var Song5s = db.Song{ SHA1: "63afdde2b390804562d54788865fff1bfd11cf94", Filename: "5s.mp3", Artist: "Third Artist", Title: "Five Seconds", Album: "Another Album (disc 3: The Third Disc)", AlbumArtist: "", AlbumID: "a1d2405b-afe0-4e28-a935-b5b256f68131", Track: 1, Disc: 2, Date: Date(2014, 1, 1), Length: 5.041, TrackGain: TrackGain, AlbumGain: AlbumGain, PeakAmp: PeakAmp, }
Functions ¶
func Caller ¶
func Caller() string
Caller walks down the call stack and returns the first test file that it sees as e.g. "foo_test.go:53".
func CloudBuild ¶
func CloudBuild() bool
CloudBuild returns true when running under Google Cloud Build.
func CompareDumpFileFlag ¶
func CompareSongs ¶
func CompareSongs(expected, actual []db.Song, order OrderPolicy) error
CompareSongs compares expected against actual. A descriptive error is returned if the songs don't match. TODO: Returning a multi-line error seems dumb.
func CopySongs ¶
CopySongs copies the provided songs (e.g. Song0s.Filename) from SongsDir into dir. The supplied directory is created if it doesn't already exist.
func Date ¶
Date is a convenience wrapper around time.Date that constructs a time.Time in UTC. Hour, minute, second, and nanosecond values are taken from tm if present.
func DeleteSongs ¶
DeleteSongs removes the provided songs (e.g. Song0s.Filename) from dir.
func DumpedGainsFlag ¶
func FindUnusedPorts ¶
FindUnusedPorts returns n unused TCP ports.
func ForceGlobFlag ¶
func HandleSignals ¶
HandleSignals installs a signal handler for sigs that sends SIGTERM to the current process group, runs f (in a goroutine) if non-nil, and then exits with 1.
func OutputDir ¶
OutputDir returns a directory where the named test suite (e.g. "e2e_test" or "web_test") should create output files.
If the OUTPUT_DIR environment variable is set, the returned directory is created within it and keep is true, indicating that the caller should preserve the returned directory. Otherwise, a temporary directory is created and keep is false, indicating that the caller can choose whether to keep the directory (e.g. on failure) or delete it (on success).
This function should only be called once per test suite.
func ServeFiles ¶
ServeFiles starts an httptest.Server for dir.
The server sets the Access-Control-Allow-Credentials and Access-Control-Allow-Origin headers to allow requests from any origin. It also sets the Cache-Control header to disable caching (but note that Chrome sometimes ignores this and caches MP3 files anyway).
func WriteSongPathsFile ¶
WriteSongPathsFile creates a file in dir listing filenames, suitable for passing to the `nup update -song-paths-file` flag.
Types ¶
type DeletionPolicy ¶
type DeletionPolicy int // controls whether GetSongsForAndroid gets deleted songs
const ( GetRegularSongs DeletionPolicy = iota // get only regular songs GetDeletedSongs // get only deleted songs )
type DevAppserver ¶
type DevAppserver struct {
// contains filtered or unexported fields
}
DevAppserver wraps a dev_appserver.py process.
func NewDevAppserver ¶
func NewDevAppserver(cfg *config.Config, storageDir string, out io.Writer, opts ...DevAppserverOption) (*DevAppserver, error)
NewDevAppserver starts a dev_appserver.py process using the supplied configuration.
storageDir is used to hold Datastore data and will be created if it doesn't exist. dev_appserver.py's noisy output will be sent to out (which may be nil). Close must be called later to kill the process.
func (*DevAppserver) Addr ¶
func (srv *DevAppserver) Addr() string
Addr returns the address of app's HTTP server, e.g. "localhost:8080".
func (*DevAppserver) Close ¶
func (srv *DevAppserver) Close() error
Close stops dev_appserver.py and cleans up its resources.
func (*DevAppserver) URL ¶
func (srv *DevAppserver) URL() string
URL returns the app's slash-terminated URL.
type DevAppserverOption ¶
type DevAppserverOption func(*DevAppserver)
DevAppserverOption can be passed to NewDevAppserver to configure dev_appserver.py.
func DevAppserverCreateIndexes ¶
func DevAppserverCreateIndexes(create bool) DevAppserverOption
DevAppserverCreateIndexes specifies whether dev_appserver.py should automatically create datastore indexes in index.yaml. By default, queries fail if they can not be satisfied using the existing indexes.
func DevAppserverPort ¶
func DevAppserverPort(port int) DevAppserverOption
DevAppserverPort sets the port that the app will listen on for HTTP requests. If this option is not supplied, an arbitrary open port will be used.
func DevAppserverWatchForChanges ¶
func DevAppserverWatchForChanges(watch bool) DevAppserverOption
DevAppserverWatchForChanges specifies whether dev_appserver.py should watch for changes to the app and rebuild it automatically. Defaults to false.
type FlushType ¶
type FlushType string
FlushType describes which caches should be flushed by FlushCache.
type OrderPolicy ¶
type OrderPolicy int
OrderPolicy specifies whether CompareSongs requires that songs appear in the specified order.
const ( CompareOrder OrderPolicy = iota IgnoreOrder )
type StripPolicy ¶
type StripPolicy int // controls whether DumpSongs removes data from songs
const ( StripIDs StripPolicy = iota // clear SongID KeepIDs // preserve SongID )
type Tester ¶
type Tester struct { T *testing.T // used to report errors (panic on errors if nil) MusicDir string // dir containing songs for 'nup update' CoverDir string // dir containing album art for 'nup update' // contains filtered or unexported fields }
Tester helps tests send HTTP requests to a development server and run the nup executable.
func NewTester ¶
func NewTester(tt *testing.T, serverURL, tempDir string, cfg TesterConfig) *Tester
NewTester creates a new tester for the development server at serverURL.
The supplied testing.T object will be used to report errors. If nil (e.g. if sharing a Tester between multiple tests), log.Panic will be called instead. The T field can be modified as tests start and stop.
The nup command must be in $PATH.
func (*Tester) DeleteSong ¶
DeleteSong deletes the specified song using 'nup update'.
func (*Tester) DumpSongs ¶
func (t *Tester) DumpSongs(strip StripPolicy, flags ...string) []db.Song
DumpSongs runs 'nup dump' with the supplied flags and returns unmarshaled songs.
func (*Tester) FlushCache ¶
FlushCache flushes the specified caches in the app server.
func (*Tester) ForceUpdateFailures ¶
ForceUpdateFailures configures the server to reject or allow updates.
func (*Tester) GetNowFromServer ¶
GetNowFromServer queries the server for the current time.
func (*Tester) GetSongsForAndroid ¶
GetSongsForAndroid exports songs from the server in a manner similar to that of the Android client.
func (*Tester) ImportSongsFromJSONFile ¶
ImportSongsFromJSON serializes the supplied songs to JSON and sends them to the server using 'nup update'.
func (*Tester) MergeSongs ¶
MergeSongs merges one song's user data into another song using 'nup update'.
func (*Tester) NewRequest ¶
NewRequest creates a new http.Request with the specified parameters. Tests should generally call helper methods like PostSongs or QuerySongs instead.
func (*Tester) PingServer ¶
func (t *Tester) PingServer()
PingServer fails the test if the server isn't serving the main page.
func (*Tester) QuerySongs ¶
QuerySongs issues a query with the supplied parameters to the server.
func (*Tester) RateAndTag ¶
RateAndTag sends a rating and/or tags update to the server. The rating is not sent if negative, and tags are not sent if nil.
func (*Tester) ReindexSongs ¶
func (t *Tester) ReindexSongs()
ReindexSongs asks the server to reindex all songs.
func (*Tester) ReportPlayed ¶
ReportPlayed sends a playback report to the server.
func (*Tester) ReportPlayedUnix ¶
ReportPlayedUnix is like ReportPlayed, but sends the time as fractional seconds since the Unix epoch instead.
func (*Tester) SongID ¶
SongID dumps all songs from the server and returns the ID of the song with the supplied SHA1. The test is failed if the song is not found.
func (*Tester) UpdateSongs ¶
UpdateSongs runs 'nup update' with the supplied flags.
func (*Tester) UpdateSongsFromList ¶
UpdateSongsFromList runs 'nup update' to import the songs listed in path.
func (*Tester) UpdateSongsRaw ¶
UpdateSongsRaw is similar to UpdateSongs but allows the caller to handle errors.
func (*Tester) UpdateStats ¶
func (t *Tester) UpdateStats()
UpdateStats instructs the server to update stats.
type TesterConfig ¶
type TesterConfig struct { // MusicDir is the directory 'nup update' will examine for song files. // If empty, a directory will be created within tempDir. MusicDir string // CoverDir is the directory 'nup update' will examine for album art image files. // If empty, a directory will be created within tempDir. CoverDir string }
TesterConfig contains optional configuration for Tester.