11package quickfix
22
33import (
4+ "fmt"
5+ "os"
6+ "path"
47 "testing"
58
69 "github.com/stretchr/testify/require"
710)
811
12+ func requireNotFileExists (t * testing.T , fname string ) {
13+ _ , err := os .Stat (fname )
14+ require .NotNil (t , err )
15+ require .True (t , os .IsNotExist (err ))
16+ }
17+
18+ func requireFileExists (t * testing.T , fname string ) {
19+ _ , err := os .Stat (fname )
20+ require .Nil (t , err )
21+ }
22+
923func TestSessionIDFilename_MinimallyQualifiedSessionID (t * testing.T ) {
1024 // When the session ID is
1125 sessionID := SessionID {BeginString : "FIX.4.4" , SenderCompID : "SENDER" , TargetCompID : "TARGET" }
@@ -30,3 +44,22 @@ func TestSessionIDFilename_FullyQualifiedSessionID(t *testing.T) {
3044 // Then the filename should be
3145 require .Equal (t , "FIX.4.4-A_B_C-D_E_F-G" , sessionIDFilenamePrefix (sessionID ))
3246}
47+
48+ func TestOpenOrCreateFile (t * testing.T ) {
49+ // When the file doesn't exist yet
50+ fname := path .Join (os .TempDir (), fmt .Sprintf ("TestOpenOrCreateFile-%d" , os .Getpid ()))
51+ requireNotFileExists (t , fname )
52+ defer os .Remove (fname )
53+
54+ // Then it should be created
55+ f , err := openOrCreateFile (fname , 0664 )
56+ requireFileExists (t , fname )
57+
58+ // When the file already exists
59+ f .Close ()
60+
61+ // Then it should be opened
62+ f , err = openOrCreateFile (fname , 0664 )
63+ require .Nil (t , err )
64+ require .Nil (t , f .Close ())
65+ }
0 commit comments