From e0d7f484d17ab1eafa248901c24c7eee87aed4f3 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 26 Dec 2019 13:54:18 +0800 Subject: [PATCH] test: add test for sql --- _fixtures/sql/hello.sql | 2 ++ core/adapter/sql/sql_identifier_app.go | 12 +++++++++++- core/adapter/sql/sql_identifier_app_test.go | 15 +++++++++++++++ core/adapter/sql/sql_identifier_listener.go | 13 +++++++++++++ core/domain/count/count_app.go | 6 +++--- .../refactor/unused_classes/unused_classes_app.go | 4 ++-- 6 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 _fixtures/sql/hello.sql create mode 100644 core/adapter/sql/sql_identifier_app_test.go diff --git a/_fixtures/sql/hello.sql b/_fixtures/sql/hello.sql new file mode 100644 index 0000000..4dc0295 --- /dev/null +++ b/_fixtures/sql/hello.sql @@ -0,0 +1,2 @@ +SELECT name, country FROM Websites; +SELECT * FROM Persons; diff --git a/core/adapter/sql/sql_identifier_app.go b/core/adapter/sql/sql_identifier_app.go index 54c7a70..e52c2a4 100644 --- a/core/adapter/sql/sql_identifier_app.go +++ b/core/adapter/sql/sql_identifier_app.go @@ -14,7 +14,11 @@ type SqlIdentifierApp struct { } -func (j *SqlIdentifierApp) AnalysisPath(codeDir string) { +func NewSqlIdentifierApp() SqlIdentifierApp { + return *&SqlIdentifierApp{} +} + +func (j *SqlIdentifierApp) AnalysisPath(codeDir string) []SqlNode { xmlFiles := (*SqlIdentifierApp)(nil).xmlFiles(codeDir) for _, xmlFile := range xmlFiles { xmlFile, err := os.Open(xmlFile) @@ -30,6 +34,7 @@ func (j *SqlIdentifierApp) AnalysisPath(codeDir string) { } } + var infos []SqlNode files := (*SqlIdentifierApp)(nil).sqlFiles(codeDir) for index := range files { file := files[index] @@ -40,7 +45,12 @@ func (j *SqlIdentifierApp) AnalysisPath(codeDir string) { listener := NewSqlIdentifierListener() antlr.NewParseTreeWalker().Walk(listener, context) + + info := listener.GetNodeInfo() + infos = append(infos, info) } + + return infos } func (j *SqlIdentifierApp) xmlFiles(codeDir string) []string { diff --git a/core/adapter/sql/sql_identifier_app_test.go b/core/adapter/sql/sql_identifier_app_test.go new file mode 100644 index 0000000..32fa7b7 --- /dev/null +++ b/core/adapter/sql/sql_identifier_app_test.go @@ -0,0 +1,15 @@ +package sql + +import ( + . "github.com/onsi/gomega" + "testing" +) + +func TestJavaIdentifierApp_AnalysisPath(t *testing.T) { + g := NewGomegaWithT(t) + + identApp := NewSqlIdentifierApp() + results := identApp.AnalysisPath("../../../_fixtures/sql") + + g.Expect(len(results)).To(Equal(1)) +} diff --git a/core/adapter/sql/sql_identifier_listener.go b/core/adapter/sql/sql_identifier_listener.go index de0ffa9..f610e0e 100644 --- a/core/adapter/sql/sql_identifier_listener.go +++ b/core/adapter/sql/sql_identifier_listener.go @@ -9,6 +9,15 @@ type SqlIdentifierListener struct { parser.BaseSqlListener } +type SqlNode struct { +} + +var sqlNode SqlNode + +func init() { + sqlNode = *&SqlNode{} +} + func NewSqlIdentifierListener() *SqlIdentifierListener { return &SqlIdentifierListener{} } @@ -40,3 +49,7 @@ func (s *SqlIdentifierListener) EnterSelect_core(ctx *parser.Select_coreContext) } } } + +func (s *SqlIdentifierListener) GetNodeInfo() SqlNode { + return sqlNode +} diff --git a/core/domain/count/count_app.go b/core/domain/count/count_app.go index e5c884f..51eca5a 100644 --- a/core/domain/count/count_app.go +++ b/core/domain/count/count_app.go @@ -2,9 +2,9 @@ package count import "github.com/phodal/coca/core/models" -func BuildCallMap(cparsedDeps []models.JClassNode) map[string]int { +func BuildCallMap(parserDeps []models.JClassNode) map[string]int { var projectMethods = make(map[string]bool) - for _, clz := range cparsedDeps { + for _, clz := range parserDeps { for _, method := range clz.Methods { projectMethods[clz.Package+"."+clz.Class+"."+method.Name] = true } @@ -12,7 +12,7 @@ func BuildCallMap(cparsedDeps []models.JClassNode) map[string]int { // TODO: support identify data class var callMap = make(map[string]int) - for _, clz := range cparsedDeps { + for _, clz := range parserDeps { for _, call := range clz.MethodCalls { callMethod := call.Package + "." + call.Class + "." + call.MethodName if projectMethods[callMethod] { diff --git a/core/domain/refactor/unused_classes/unused_classes_app.go b/core/domain/refactor/unused_classes/unused_classes_app.go index e60cef9..04b23ae 100644 --- a/core/domain/refactor/unused_classes/unused_classes_app.go +++ b/core/domain/refactor/unused_classes/unused_classes_app.go @@ -1,4 +1,4 @@ -package main +package unused_classes import ( "encoding/json" @@ -11,7 +11,7 @@ import ( var parsedDeps []JClassNode -func main() { +func Refactoring() { var analysisPackage = "" file := support.ReadFile("deps.json") if file == nil { -- GitLab