Exemplo n.º 1
0
func ExampleDataset_Index() {
	d1 := &storage.Dataset{Name: "Iris"}
	d2 := &storage.Dataset{Name: "Iris"}

	db, _ := redis.NewDB(config)
	storage := storage.New(db)

	storage.Clean()
	storage.Insert(d1)
	storage.Insert(d2)

	req, _ := http.NewRequest("GET", "/datasets/", nil)
	dataset := &api.DatasetHandler{storage}

	resp := httptest.NewRecorder()
	dataset.Index(resp, req, nil)

	datasets := []string{}
	unmarshaller.IoReader(resp.Body, &datasets)

	fmt.Printf("%d\n", resp.Code)
	fmt.Printf("Size %d", len(datasets))

	// Output:
	// 200
	// Size 2
}
Exemplo n.º 2
0
func TestStorage_Datasets(t *testing.T) {
	bucket, _ := redis.NewBucket(config)

	dataset1 := storage.NewDataset("posts", bucket)
	dataset2 := storage.NewDataset("users", bucket)

	storage := storage.New(bucket)
	storage.Clean()

	storage.Insert(dataset1)
	storage.Insert(dataset2)

	datasets := storage.Datasets()

	if len(datasets) != 2 {
		t.Errorf("Want 2 got %d\n", len(datasets))
	}
}
Exemplo n.º 3
0
func (handler *DatasetHandler) Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	puaro.HTTPLogger(r)

	storage := handler.Storage
	dataset := storage.NewDataset("")

	unmarshaller.IoReader(r.Body, dataset)

	err := storage.Insert(dataset)
	if err != nil {
		w.WriteHeader(400)
		fmt.Fprint(w, err)
		return
	}

	w.WriteHeader(200)
}
Exemplo n.º 4
0
//
// /nodes/ => POST => register new node
//
func ExampleFactories_Register() {
	fa := models.Factory{
		Name: "factory_1",
		Host: "localhost:9001",
	}

	body, _ := marshaller.IoReader(fa)

	req, err := http.NewRequest("POST", "http://example.com/nodes", body)

	if err != nil {
		log.Fatal(err)
	}

	w := httptest.NewRecorder()

	db, _ := redis.NewDB(config)
	storage := storage.New(db)

	if err != nil {
		fmt.Printf("%s\n", err)
		return
	}

	dataset := storage.NewDataset("puaro")
	storage.Insert(dataset)

	newCluster := cluster.NewCluster("test", db)

	factory := api.FactoryHandler{newCluster}
	factory.Register(w, req, nil)

	fmt.Printf("%d", w.Code)

	newCluster.Factories.Clean()

	// Output:
	// 200
}
Exemplo n.º 5
0
func ExampleFactories_Index() {
	factory1 := &models.Factory{Name: "factory_1"}
	factory2 := &models.Factory{Name: "factory_2"}
	factory3 := &models.Factory{Name: "factory_3"}

	body, _ := marshaller.IoReader(factory1)

	req, err := http.NewRequest("POST", "http://example.com/factories", body)
	if err != nil {
		log.Fatal(err)
	}

	w := httptest.NewRecorder()

	db, _ := redis.NewDB(config)
	storage := storage.New(db)

	dataset := storage.NewDataset("puaro")
	storage.Insert(dataset)

	newCluster := cluster.NewCluster("test", db)

	newCluster.Register(factory1)
	newCluster.Register(factory2)
	newCluster.Register(factory3)

	factory := api.FactoryHandler{newCluster}
	factory.Index(w, req, nil)

	factoriesResponse := []string{}

	json.Unmarshal([]byte(w.Body.String()), &factoriesResponse)

	fmt.Printf("%d - %d", w.Code, len(factoriesResponse))

	// Output:
	// 200 - 3
}
Exemplo n.º 6
0
func ExampleInstance_Create() {
	config, _ := puaro.ConfigFromFile("../../configs/test.json")
	ds := &storage.Dataset{Name: "Iris"}

	attrs := &instance.Attributes{
		"attr1": "value1",
		"attr2": "value2",
		"attr3": "value3",
	}

	ps := httprouter.Params{
		httprouter.Param{"dataset", "iris"},
		httprouter.Param{"collection", "satosa"},
	}

	params, _ := marshaller.IoReader(attrs)
	req, _ := http.NewRequest("POST", "/", params)

	db, _ := redis.NewDB(config)
	storage := storage.New(db)

	storage.Clean()

	if err := storage.Insert(ds); err != nil {
		fmt.Printf("%s\n", err)
		return
	}

	instance := api.InstanceHandler{storage}
	resp := httptest.NewRecorder()
	instance.Create(resp, req, ps)

	fmt.Printf("RESPONSE CODE: %d\n", resp.Code)

	// Output:
	// RESPONSE CODE: 200
}