Пример #1
0
func TestImportWithInvalidData(t *testing.T) {
	product = exchange.NewResource(&Product{}, exchange.Config{PrimaryField: "Code"})
	product.Meta(exchange.Meta{Name: "Code"})
	product.Meta(exchange.Meta{Name: "Name"})
	product.Meta(exchange.Meta{Name: "Price"})

	product.AddValidator(func(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
		if f, err := strconv.ParseFloat(fmt.Sprint(metaValues.Get("Price").Value), 64); err == nil {
			if f == 0 {
				return errors.New("product's price can't be env")
			}
			return nil
		} else {
			return err
		}
	})

	if err := product.Import(csv_adaptor.New("fixtures/products.csv"), newContext()); err != nil {
		t.Errorf("Failed to import product, get error", err)
	}

	if err := product.Import(csv_adaptor.New("fixtures/invalid_price_products.csv"), newContext()); err == nil {
		t.Errorf("should get error when import products with invalid price")
	}
}
Пример #2
0
func TestExportCSV(t *testing.T) {
	product.Import(csv_adaptor.New("fixtures/products.csv"), newContext())

	if err := product.Export(csv_adaptor.New("fixtures/products2.csv"), newContext()); err != nil {
		t.Fatalf("Failed to export csv, get error %v", err)
	}
}
Пример #3
0
func TestImportCSV(t *testing.T) {
	if err := product.Import(csv_adaptor.New("fixtures/products.csv"), newContext()); err != nil {
		t.Fatalf("Failed to import csv, get error %v", err)
	}

	checkProduct(t, "fixtures/products.csv")

	if err := product.Import(csv_adaptor.New("fixtures/products_update.csv"), newContext()); err != nil {
		t.Fatalf("Failed to import csv, get error %v", err)
	}

	checkProduct(t, "fixtures/products_update.csv")
}
Пример #4
0
func TestProcessImportedData(t *testing.T) {
	product = exchange.NewResource(&Product{}, exchange.Config{PrimaryField: "Code"})
	product.Meta(exchange.Meta{Name: "Code"})
	product.Meta(exchange.Meta{Name: "Name"})
	product.Meta(exchange.Meta{Name: "Price"})

	product.AddProcessor(func(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
		product := result.(*Product)
		product.Price = float64(int(product.Price * 1.1)) // Add 10% Tax
		return nil
	})

	if err := product.Import(csv_adaptor.New("fixtures/products.csv"), newContext()); err != nil {
		t.Errorf("Failed to import product, get error", err)
	}

	checkProduct(t, "fixtures/products_with_tax.csv")
}