-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add index test and use t.TempDir instead of ioutil
- Loading branch information
1 parent
6c9bcbb
commit d733152
Showing
2 changed files
with
92 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package histories | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"predictor/env" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestHistoryIndex(t *testing.T) { | ||
// Write some dummy histories in the cache. | ||
tempDir := t.TempDir() | ||
exampleCycle := HistoryCycle{ | ||
StartTime: time.Unix(0, 0), | ||
EndTime: time.Unix(10, 0), // Usually cycles are longer, but suffices for testing | ||
Phases: []HistoryPhaseEvent{ | ||
// Green at 0 seconds | ||
{ | ||
Time: time.Unix(0, 0), | ||
Color: 3, | ||
}, | ||
// Red at 5 seconds | ||
{ | ||
Time: time.Unix(5, 0), | ||
Color: 1, | ||
}, | ||
}, | ||
} | ||
history := History{ | ||
Cycles: []HistoryCycle{ | ||
exampleCycle, | ||
exampleCycle, | ||
}, | ||
} | ||
jsonData, err := json.Marshal(history) | ||
if err != nil { | ||
t.Errorf("failed to marshal json data: %s", err.Error()) | ||
t.FailNow() | ||
} | ||
historyFilePath := fmt.Sprintf("%s/1337_1.json", tempDir) | ||
historyFile, err := os.Create(historyFilePath) | ||
if err != nil { | ||
t.Errorf("history file could not be created: %s", err.Error()) | ||
t.FailNow() | ||
} | ||
defer historyFile.Close() | ||
_, err = historyFile.Write(jsonData) | ||
if err != nil { | ||
t.Errorf("could not write into history file: %s", err.Error()) | ||
t.FailNow() | ||
} | ||
cache.Store(historyFilePath, history) | ||
|
||
env.StaticPath = tempDir | ||
UpdateHistoryIndex() | ||
|
||
indexFilePath := fmt.Sprintf("%s/index.json", tempDir) | ||
indexFile, err := os.OpenFile(indexFilePath, os.O_RDONLY, 0644) | ||
if err != nil { | ||
t.Errorf("could not open index file: %s", err.Error()) | ||
t.FailNow() | ||
} | ||
defer indexFile.Close() | ||
|
||
decoder := json.NewDecoder(indexFile) | ||
var unmarshaledIndex []IndexEntry | ||
err = decoder.Decode(&unmarshaledIndex) | ||
if err != nil { | ||
t.Errorf("could not unmarshal data from index file: %s", err.Error()) | ||
t.FailNow() | ||
} | ||
|
||
expectedIndex := []IndexEntry{ | ||
{ | ||
File: "1337_1.json", | ||
LastUpdated: time.Unix(10, 0), | ||
CycleCount: 2, | ||
}, | ||
} | ||
|
||
// Using deep equals here is fine | ||
if !reflect.DeepEqual(unmarshaledIndex, expectedIndex) { | ||
t.Errorf("expected index does not correspond with unmarshaled index") | ||
} | ||
} |