Commit ee90299
Changed files (4)
question.go
@@ -32,6 +32,7 @@ func buildTagPath(tags []string) int {
Value: tags[tagIndex]}
categories = append(categories, newCat)
currentChosen = len(categories) - 1
+ categories[newCat.Parent].Children = append(categories[newCat.Parent].Children, currentChosen)
tagIndex++
}
}
serverHandlers.go
@@ -22,16 +22,52 @@ var userTestsLock = sync.RWMutex{}
// getRecordForCategory returns a record for the specified category
// it moves the now used record to the usedRecordMap, and updates
// recordMap to reflect the change
-func getRecordForCategory(category string) Record {
+func getRecordForCategory(categoryIndex int) Record {
+ if categoryIndex >= len(categories) {
+ return Record{}
+ }
mapLock.Lock()
defer mapLock.Unlock()
-
- return Record{}
+ var record Record
+ for record.Answer == "" {
+ if len(categories[categoryIndex].RecordArray) > 0 {
+ recordIndex := rand.Intn(len(categories[categoryIndex].RecordArray))
+ //TODO: remove category from array
+ return categories[categoryIndex].RecordArray[recordIndex]
+ } else if len(categories[categoryIndex].UsedRecordArray) > 0 {
+ categories[categoryIndex].RecordArray = categories[categoryIndex].UsedRecordArray[0:]
+ } else { // choose random sub-category because this is not a leaf
+ if len(categories[categoryIndex].Children) > 0 {
+ childChosen := rand.Intn(len(categories[categoryIndex].Children))
+ categoryIndex = categories[categoryIndex].Children[childChosen]
+ } else {
+ return record
+ }
+ }
+ }
+ return record
}
// buildRecordArray builds an array of Records based on the parameter criteria
func buildRecordArray(numQuestions int, categoryIndices []int) []Record {
- return nil
+ recordArray := []Record{}
+ numCats := len(categoryIndices)
+ totalCats := len(categories)
+ for len(recordArray) < numQuestions {
+ var catChosen int
+ log.Printf("numCats: %d\ttotalCats: %d", numCats, totalCats)
+ if numCats > 0 {
+ catChosen = categoryIndices[rand.Intn(numCats)]
+ } else {
+ catChosen = rand.Intn(totalCats)
+ }
+ log.Printf("catChosen: %d", catChosen)
+ record := getRecordForCategory(catChosen)
+ if record.Answer != "" {
+ recordArray = append(recordArray, record)
+ }
+ }
+ return recordArray
}
// handleRequestForTest provides a JSON formatted test for the client
serverHandlers_test.go
@@ -0,0 +1,30 @@
+package main
+
+import (
+ "log"
+ "testing"
+)
+
+// tests the handlers for the server
+
+// TestBuildRecordArray ensures that the records arrays
+// returned follow the correct blueprinting options
+// passed
+func TestBuildRecordArray(t *testing.T) {
+ numQ := 5
+ recordCat := 3
+ t.Run("Setting up test env...", TestBuildTree)
+ categories[3].RecordArray = []Record{Record{Answer: "answer", Question: "question", Category: recordCat}}
+ recordArray := buildRecordArray(numQ, []int{2, recordCat, 5})
+ if len(recordArray) != numQ {
+ t.Errorf("Record array wrong length: %d should be %d\n%s", len(recordArray), numQ, recordArray)
+ t.FailNow()
+ }
+ if len(categories[categories[recordCat].Parent].Children) != 1 {
+ log.Printf("Wrong children: %s", categories[categories[recordCat].Parent].Children)
+ }
+ if len(categories[recordCat].RecordArray) != 1 {
+ log.Printf("Record not added: %+v", categories[recordCat])
+ }
+ log.Printf("records: %v", recordArray)
+}
structures_test.go
@@ -20,6 +20,11 @@ func TestTreeLinkage(t *testing.T) {
t.Errorf("Cannot find parent. Recevied: %+v for: %v", categories[cat].FullCategoryPath(),
catPath[0:len(catPath)])
}
+ if len(categories[cat].Children) != 1 {
+ t.Errorf("Wrong children: %v", categories[cat].Children)
+ } else if categories[cat].Children[0] != 3 {
+ t.Errorf("Wrong child: %v", categories[cat].Children[0])
+ }
})
}