Commit ed4b62b

Richard Luby <richluby@gmail.com>
2017-01-26 14:16:03
server can build record array provided a blueprint
1 parent 2cc02ce
serverHandlers.go
@@ -36,13 +36,18 @@ func randomizeArray(array []Record) []Record {
 }
 
 // buildRecordArray builds an array of Records based on the parameter criteria
+// will return numQuestions, or the available questions, whichever is less
 func buildRecordArray(numQuestions int, selectedCategories []string) ([]Record, error) {
 	recordArray := []Record{}
 	var err error
 	if len(selectedCategories) > 0 {
-		recordArray, err = returnForQuery(selectedCategories, numQuestions)
+		recordArray, err = returnForQuery(selectedCategories)
 	} else {
-		recordArray, err = returnForQuery(categories, numQuestions)
+		recordArray, err = returnForQuery(categories)
+	}
+	recordArray = randomizeArray(recordArray)
+	if len(recordArray) > numQuestions {
+		return recordArray[0:numQuestions], err
 	}
 	return recordArray, err
 }
@@ -304,3 +309,5 @@ func init() {
 	}
 	userTests = map[string][]ClientTest{}
 }
+
+//TODO: ensure category cannot contain regex operators
serverHandlers_test.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"strings"
 	"testing"
 )
 
@@ -29,3 +30,26 @@ func TestBuildingBlueprint(t *testing.T) {
 		}
 	}
 }
+
+// TestBuildRecordArray builds the array for giving
+// a test to a client
+func TestBuildRecordArray(t *testing.T) {
+	numQuestions := 23
+	testArray := []string{"linux"}
+	serverConfig.DB_ADDRESS = "localhost"
+	serverConfig.DB_PORT = 27017
+	records, err := buildRecordArray(numQuestions, testArray)
+	if err != nil {
+		t.Errorf("Error building array: %+v", err)
+		t.FailNow()
+	}
+	if len(records) != numQuestions {
+		t.Errorf("Incorrect number of records returned (%d): %+v", len(records), records)
+		t.Fail()
+	}
+	for _, record := range records {
+		if !strings.HasPrefix(record.Path, testArray[0]) {
+			t.Errorf("Improper record selected: %+v", record)
+		}
+	}
+}