Esempio n. 1
0
func TestValidFiles(t *testing.T) {
	inputs := testfiles.Glob("bson_test/input*.go")
	for _, input := range inputs {
		b, err := ioutil.ReadFile(input)
		if err != nil {
			t.Fatal(err)
		}
		out, err := generateCode(string(b), "MyType")
		if err != nil {
			fmt.Fprintf(os.Stderr, "%v\n", err)
			return
		}
		want, err := ioutil.ReadFile(strings.Replace(input, "input", "output", 1))
		if err != nil {
			t.Fatal(err)
		}
		// goimports is flaky. So, let's not test that part.
		d, err := diff(skip_imports(want), skip_imports(out))
		if len(d) != 0 {
			t.Errorf("Unexpected output for %s:\n%s", input, string(d))
			if testing.Verbose() {
				t.Logf("%s:\n%s", input, out)
			}
		}
	}
}
Esempio n. 2
0
func TestValidFiles(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping integration test in short mode.")
	}

	inputs := testfiles.Glob("bson_test/input*.go")
	for _, input := range inputs {
		b, err := ioutil.ReadFile(input)
		if err != nil {
			t.Fatalf("ioutil.ReadFile error: %v", err)
		}
		want, err := ioutil.ReadFile(strings.Replace(input, "input", "output", 1))
		if err != nil {
			t.Fatalf("ioutil.ReadFile error: %v", err)
		}

		out, err := generateCode(string(b), "MyType")
		if err != nil {
			t.Fatalf("generateCode error: %v", err)
		}

		// goimports is flaky. So, let's not test that part.
		want, err = skipImports(want)
		if err != nil {
			t.Fatalf("skipImports error: %v", err)
		}
		out, err = skipImports(out)
		if err != nil {
			t.Fatalf("skipImports error: %v", err)
		}

		d, err := diff(want, out)
		if err != nil {
			t.Fatalf("diff error: %v", err)
		}
		if len(d) != 0 {
			t.Errorf("Unexpected output for %s:\n%s", input, string(d))
			if testing.Verbose() {
				t.Logf("%s:\n%s", input, out)
			}
		}
	}
}
Esempio n. 3
0
func TestCustom(t *testing.T) {
	testSchemas := testfiles.Glob("sqlparser_test/*_schema.json")
	if len(testSchemas) == 0 {
		t.Log("No schemas to test")
		return
	}
	for _, schemFile := range testSchemas {
		schem := loadSchema(schemFile)
		t.Logf("Testing schema %s", schemFile)
		files, err := filepath.Glob(strings.Replace(schemFile, "schema.json", "*.txt", -1))
		if err != nil {
			log.Fatal(err)
		}
		if len(files) == 0 {
			t.Fatalf("No test files for %s", schemFile)
		}
		getter := func(name string) (*schema.Table, bool) {
			r, ok := schem[name]
			return r, ok
		}
		for _, file := range files {
			t.Logf("Testing file %s", file)
			for tcase := range iterateExecFile(file) {
				plan, err := GetExecPlan(tcase.input, getter)
				var out string
				if err != nil {
					out = err.Error()
				} else {
					bout, err := json.Marshal(plan)
					if err != nil {
						panic(fmt.Sprintf("Error marshalling %v: %v", plan, err))
					}
					out = string(bout)
				}
				if out != tcase.output {
					t.Errorf("File: %s: Line:%v\n%s\n%s", file, tcase.lineno, tcase.output, out)
				}
			}
		}
	}
}
Esempio n. 4
0
func iterateFiles(pattern string) (testCaseIterator chan testCase) {
	names := testfiles.Glob(pattern)
	testCaseIterator = make(chan testCase)
	go func() {
		defer close(testCaseIterator)
		for _, name := range names {
			fd, err := os.OpenFile(name, os.O_RDONLY, 0)
			if err != nil {
				panic(fmt.Sprintf("Could not open file %s", name))
			}

			r := bufio.NewReader(fd)
			lineno := 0
			for {
				line, err := r.ReadString('\n')
				lines := strings.Split(strings.TrimRight(line, "\n"), "#")
				lineno++
				if err != nil {
					if err != io.EOF {
						panic(fmt.Sprintf("Error reading file %s: %s", name, err.Error()))
					}
					break
				}
				input := lines[0]
				output := ""
				if len(lines) > 1 {
					output = lines[1]
				}
				if input == "" {
					continue
				}
				testCaseIterator <- testCase{name, lineno, input, output}
			}
		}
	}()
	return testCaseIterator
}