github.com/ravendb/ravendb-go-client@v0.0.0-20240229102137-4474ee7aa0fa/tests/facet_test_base_test.go (about) 1 package tests 2 3 import ( 4 "math/rand" 5 "time" 6 7 ravendb "github.com/ravendb/ravendb-go-client" 8 ) 9 10 var ( 11 FEATURES = []string{"Image Stabilizer", "Tripod", "Low Light Compatible", "Fixed Lens", "LCD"} 12 MANUFACTURERS = []string{"Sony", "Nikon", "Phillips", "Canon", "Jessops"} 13 MODELS = []string{"Model1", "Model2", "Model3", "Model4", "Model5"} 14 RANDOM = rand.New(rand.NewSource(time.Now().UnixNano())) 15 ) 16 17 func NewCameraCostIndex() *ravendb.IndexCreationTask { 18 res := ravendb.NewIndexCreationTask("CameraCost") 19 m := `from camera in docs.Cameras select new { camera.manufacturer, 20 camera.model, 21 camera.cost, 22 camera.dateOfListing, 23 camera.megapixels }` 24 res.Maps = []string{m} 25 return res 26 } 27 28 func facetTestBaseCreateCameraTestIndex(store *ravendb.DocumentStore) error { 29 index := NewCameraCostIndex() 30 indexDef := index.CreateIndexDefinition() 31 op := ravendb.NewPutIndexesOperation(indexDef) 32 return store.Maintenance().Send(op) 33 } 34 35 func facetTestBaseInsertCameraData(store *ravendb.DocumentStore, cameras []*Camera, shouldWaitForIndexing bool) error { 36 session, err := store.OpenSession("") 37 if err != nil { 38 return err 39 } 40 defer session.Close() 41 for _, camera := range cameras { 42 err = session.Store(camera) 43 if err != nil { 44 return err 45 } 46 } 47 48 err = session.SaveChanges() 49 if err != nil { 50 return err 51 } 52 53 if !shouldWaitForIndexing { 54 return nil 55 } 56 57 return waitForIndexing(store, "", 0) 58 } 59 60 func facetTestBaseGetFacets() []ravendb.FacetBase { 61 facet1 := ravendb.NewFacet() 62 facet1.FieldName = "manufacturer" 63 64 costRangeFacet := ravendb.NewRangeFacet(nil) 65 costRangeFacet.Ranges = []string{ 66 "cost <= 200", 67 "cost >= 200 and cost <= 400", 68 "cost >= 400 and cost <= 600", 69 "cost >= 600 and cost <= 800", 70 "cost >= 800", 71 } 72 73 megaPixelsRangeFacet := ravendb.NewRangeFacet(nil) 74 megaPixelsRangeFacet.Ranges = []string{ 75 "megapixels <= 3", 76 "megapixels >= 3 and megapixels <= 7", 77 "megapixels >= 7 and megapixels <= 10", 78 "megapixels >= 10", 79 } 80 return []ravendb.FacetBase{facet1, costRangeFacet, megaPixelsRangeFacet} 81 } 82 83 func facetTestBaseGetCameras(numCameras int) []*Camera { 84 var cameraList []*Camera 85 for i := 1; i <= numCameras; i++ { 86 camera := &Camera{} 87 y := 1980 + RANDOM.Intn(30) 88 m := time.Month(RANDOM.Intn(12)) 89 d := RANDOM.Intn(27) 90 t := time.Date(y, m, d, 0, 0, 0, 0, time.Local) 91 camera.DateOfListing = ravendb.Time(t) 92 camera.Manufacturer = MANUFACTURERS[RANDOM.Intn(len(MANUFACTURERS))] 93 camera.Model = MODELS[RANDOM.Intn(len(MODELS))] 94 camera.Cost = RANDOM.Float64()*900 + 100 95 camera.Zoom = RANDOM.Intn(10) + 1 96 camera.Megapixels = RANDOM.Float64()*10 + 1.0 97 camera.ImageStabilizer = RANDOM.Float64() > 0.6 98 camera.AdvancedFeatures = []string{"??"} 99 100 cameraList = append(cameraList, camera) 101 } 102 103 return cameraList 104 } 105 106 // json tags to match what Java sends 107 type Camera struct { 108 ID string 109 110 DateOfListing ravendb.Time `json:"dateOfListing"` 111 Manufacturer string `json:"manufacturer"` 112 Model string `json:"model"` 113 Cost float64 `json:"cost"` 114 115 Zoom int `json:"zoom"` 116 Megapixels float64 `json:"megapixels"` 117 ImageStabilizer bool `json:"imageStabilizer"` 118 AdvancedFeatures []string `json:"advancedFeatures"` 119 }