Skip to content

Commit a89973a

Browse files
committed
Added some unit tests along with Travis CI config
1 parent 8b4190b commit a89973a

File tree

12 files changed

+119
-28
lines changed

12 files changed

+119
-28
lines changed

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: go
2+
3+
go:
4+
- 1.9.x
5+
- release
6+
7+
script:
8+
- go test -v ./...
9+
10+
env:
11+
- DEP_VERSION="0.3.2"
12+
13+
before_install:
14+
# Download the binary to bin folder in $GOPATH
15+
- curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep
16+
# Make the binary executable
17+
- chmod +x $GOPATH/bin/dep
18+
19+
install:
20+
- dep ensure

Gopkg.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@
2828
[[constraint]]
2929
branch = "v2"
3030
name = "gopkg.in/yaml.v2"
31+
32+
[[constraint]]
33+
name = "github.com/stretchr/testify"
34+
version = "1.2.0"

commands/list_tables.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,41 @@ package commands
22

33
import (
44
"fmt"
5-
"github.com/Persata/mysqldumptablepurger/io"
5+
"github.com/Persata/mysqldumptablepurger/iowrapper"
66
"github.com/Persata/mysqldumptablepurger/parser"
77
)
88

99
func ListTables(path string) int {
10-
s, f := io.GetScanner(path)
10+
fmt.Println(fmt.Sprintf("Scanning for tables in %s...\n", path))
11+
12+
tables := GetTablesList(path)
13+
14+
for _, table := range tables {
15+
fmt.Println(table)
16+
}
17+
18+
fmt.Println("\nScan complete!")
19+
20+
return 0
21+
}
22+
23+
func GetTablesList(path string) []string {
24+
s, f := iowrapper.GetScanner(path)
1125
defer f.Close()
1226

13-
fmt.Println(fmt.Sprintf("Scanning for tables in %s...\n", path))
27+
var tables []string
1428

1529
for s.Scan() {
1630
match := parser.TableStructureRegex.FindStringSubmatch(s.Text())
1731

1832
if len(match) > 0 {
19-
fmt.Println(match[1])
33+
tables = append(tables, match[1])
2034
}
2135
}
2236

2337
if s.Err() != nil {
2438
fmt.Println(fmt.Sprintf("An error occurred scanning the file: %s", s.Err()))
2539
}
2640

27-
fmt.Println("\nScan complete!")
28-
29-
return 0
41+
return tables
3042
}

commands/list_tables_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package commands_test
2+
3+
import (
4+
"testing"
5+
"github.com/stretchr/testify/assert"
6+
"github.com/Persata/mysqldumptablepurger/commands"
7+
)
8+
9+
func TestListTables(t *testing.T) {
10+
tables := commands.GetTablesList("../test/data/test_dump.sql")
11+
assert.Len(t, tables, 2)
12+
assert.Contains(t, tables, "test_table_1")
13+
assert.Contains(t, tables, "test_table_2")
14+
}

commands/remove_tables.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
"os"
66
"bufio"
77
"strings"
8-
"github.com/Persata/mysqldumptablepurger/io"
8+
"github.com/Persata/mysqldumptablepurger/iowrapper"
99
"github.com/Persata/mysqldumptablepurger/parser"
1010
"regexp"
1111
)
1212

1313
func RemoveTables(inputPath string, outputPath string, tables []string) int {
14-
fmt.Println("The following tables will be removed: \n")
14+
fmt.Println("The following tables will be removed:")
15+
fmt.Println()
1516

1617
for _, table := range tables {
1718
fmt.Println(fmt.Sprintf("- %s", table))
@@ -32,13 +33,14 @@ func RemoveTables(inputPath string, outputPath string, tables []string) int {
3233
fmt.Println("Exiting, no action performed")
3334
return 0
3435
} else {
35-
fmt.Println("Processing...\n")
36+
fmt.Println("Processing...")
37+
fmt.Println()
3638
}
3739

38-
s, fr := io.GetScanner(inputPath)
40+
s, fr := iowrapper.GetScanner(inputPath)
3941
defer fr.Close()
4042

41-
w, fw := io.GetWriter(outputPath)
43+
w, fw := iowrapper.GetWriter(outputPath)
4244
defer fw.Close()
4345

4446
replaceRegex := regexp.MustCompile(fmt.Sprintf(parser.TableStructureRegexReplace, strings.Join(tables, "|")))

commands/remove_tables_by_conf.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ type RemoveConf struct {
1212
}
1313

1414
func RemoveTablesByConf(inputPath string, configFilePath string) int {
15+
16+
configFileData := GetConfigFileData(configFilePath)
17+
18+
if len(configFileData.OutputFile) == 0 {
19+
fmt.Println(fmt.Sprintf("No output file found in the given config file - got '%s'.", configFileData.OutputFile))
20+
return 1
21+
}
22+
23+
if len(configFileData.Tables) == 0 {
24+
fmt.Println("Specified config file does not contain any tables to remove.")
25+
return 1
26+
}
27+
28+
return RemoveTables(inputPath, configFileData.OutputFile, configFileData.Tables)
29+
}
30+
31+
func GetConfigFileData(configFilePath string) RemoveConf {
1532
configFileBuffer, err := ioutil.ReadFile(configFilePath)
1633

1734
if err != nil {
@@ -26,15 +43,5 @@ func RemoveTablesByConf(inputPath string, configFilePath string) int {
2643
panic(err)
2744
}
2845

29-
if len(configFileData.OutputFile) == 0 {
30-
fmt.Println(fmt.Sprintf("No output file found in the given config file - got '%s'.", configFileData.OutputFile))
31-
return 1
32-
}
33-
34-
if len(configFileData.Tables) == 0 {
35-
fmt.Println("Specified config file does not contain any tables to remove.")
36-
return 1
37-
}
38-
39-
return RemoveTables(inputPath, configFileData.OutputFile, configFileData.Tables)
46+
return configFileData
4047
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package commands_test
2+
3+
import (
4+
"testing"
5+
"github.com/stretchr/testify/assert"
6+
"github.com/Persata/mysqldumptablepurger/commands"
7+
)
8+
9+
func TestGetConfigFileDataSingle(t *testing.T) {
10+
configFileData := commands.GetConfigFileData("../test/conf/test_single_table.yml")
11+
assert.Equal(t, configFileData.OutputFile, "test_dump_cleaned.sql")
12+
assert.Len(t, configFileData.Tables, 1)
13+
}
14+
15+
16+
func TestGetConfigFileDataTwo(t *testing.T) {
17+
configFileData := commands.GetConfigFileData("../test/conf/test_two_tables.yml")
18+
assert.Equal(t, configFileData.OutputFile, "test_dump_cleaned.sql")
19+
assert.Len(t, configFileData.Tables, 2)
20+
}

io/get_scanner.go renamed to iowrapper/get_scanner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io
1+
package iowrapper
22

33
import (
44
"bufio"

io/get_writer.go renamed to iowrapper/get_writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io
1+
package iowrapper
22

33
import (
44
"bufio"

0 commit comments

Comments
 (0)