-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,22 +7,34 @@ import ( | |
) | ||
|
||
var ( | ||
jsonConfig Config | ||
jsonConfig Config | ||
dropAndCreateTable = "DROP TABLE IF EXISTS `wp_options`;\n" + | ||
"/*!40101 SET @saved_cs_client = @@character_set_client */;\n" + | ||
"/*!40101 SET character_set_client = utf8 */;\n" + | ||
"CREATE TABLE `wp_options` (\n" + | ||
"`option_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n" + | ||
"`option_name` varchar(191) NOT NULL DEFAULT '',\n" + | ||
"`option_value` longtext NOT NULL,\n" + | ||
"`autoload` varchar(20) NOT NULL DEFAULT 'yes',\n" + | ||
"PRIMARY KEY (`option_id`),\n" + | ||
"UNIQUE KEY `option_name` (`option_name`)\n" + | ||
") ENGINE=InnoDB AUTO_INCREMENT=123 DEFAULT CHARSET=utf8mb4;\n" + | ||
"/*!40101 SET character_set_client = @saved_cs_client */;" | ||
|
||
// Don't forget to escape \ because it'll translate to a newline and not pass | ||
// the comparison test | ||
multilineQuery = `INSERT INTO wp_usermeta VALUES | ||
(1,1,'first_name','John'),(2,1,'last_name','Doe'), | ||
(3,1,'foobar','bazquz'), | ||
(4,1,'nickname','Jim'), | ||
(5,1,'description','Lorum ipsum.'); | ||
` | ||
multilineQueryRecompiled = "insert into wp_usermeta values (1, 1, 'first_name', 'Nat'), (2, 1, 'last_name', 'Hermiston'), (3, 1, 'foobar', 'bazquz'), (4, 1, 'nickname', 'Treva'), (5, 1, 'description', 'Enim odio nihil.');\n" | ||
commentsQuery = "INSERT INTO `wp_comments` VALUES (1,1,'A WordPress Commenter','[email protected]','https://wordpress.org/','','2019-06-12 00:59:19','2019-06-12 00:59:19','Hi, this is a comment.\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\nCommenter avatars come from <a href=\"https://gravatar.com\">Gravatar</a>.',0,'1','','',0,0);\n" | ||
// Don't forget to escape \ because it'll translate to a newline and not pass | ||
// the comparison test | ||
commentsQueryRecompiled = "insert into wp_comments values (1, 1, 'sam_harvey', '[email protected]', 'http://balistreriwiegand.name/sunny', '', '2019-06-12 00:59:19', '2019-06-12 00:59:19', 'Hi, this is a comment.\\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\\nCommenter avatars come from <a href=\\\"https://gravatar.com\\\">Gravatar</a>.', 0, '1', '', '', 0, 0);\n" | ||
usersQuery = "INSERT INTO `wp_users` VALUES (1,'username','user_pass','username','[email protected]','','2019-06-12 00:59:19','',0,'username'),(2,'username','user_pass','username','[email protected]','http://notreal.com/username','2019-06-12 00:59:19','',0,'username');\n" | ||
usersQueryRecompiled = "insert into wp_users values (1, 'fatima.fisher', 'abOSwkVS', 'lillian', '[email protected]', '', '2019-06-12 00:59:19', '', 0, 'Retta Bailey'), (2, 'juwan.kassulke', 'zgtEQA3nm4Wlro', 'evalyn', '[email protected]', 'http://dickensmurphy.info/ophelia', '2019-06-12 00:59:19', '', 0, 'Rick Fahey III');\n" | ||
userMetaQuery = "INSERT INTO `wp_usermeta` VALUES (1,1,'first_name','John'),(2,1,'last_name','Doe'),(3,1,'foobar','bazquz'),(4,1,'nickname','Jim'),(5,1,'description','Lorum ipsum.'),(6,2,'first_name','Janet'),(7,2,'last_name','Doe'),(8,2,'foobar','bazquz'),(9,2,'nickname','Jane'),(10,2,'description','Lorum ipsum.');\n" | ||
userMetaQueryRecompiled = "insert into wp_usermeta values (1, 1, 'first_name', 'Ed'), (2, 1, 'last_name', 'Koelpin'), (3, 1, 'foobar', 'bazquz'), (4, 1, 'nickname', 'Watson'), (5, 1, 'description', 'Qui voluptatum est.'), (6, 2, 'first_name', 'Olen'), (7, 2, 'last_name', 'Williamson'), (8, 2, 'foobar', 'bazquz'), (9, 2, 'nickname', 'Kamren'), (10, 2, 'description', 'Eveniet repellat in.');\n" | ||
(5,1,'description','Lorum ipsum.');` | ||
multilineQueryRecompiled = "insert into wp_usermeta values (1, 1, 'first_name', 'Jazmyn'), (2, 1, 'last_name', 'Reynolds'), (3, 1, 'foobar', 'bazquz'), (4, 1, 'nickname', 'Sherman'), (5, 1, 'description', 'Vel at et.');\n" | ||
commentsQuery = "INSERT INTO `wp_comments` VALUES (1,1,'A WordPress Commenter','[email protected]','https://wordpress.org/','','2019-06-12 00:59:19','2019-06-12 00:59:19','Hi, this is a comment.\\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\\nCommenter avatars come from <a href=\\\"https://gravatar.com\\\">Gravatar</a>.',0,'1','','',0,0);\n" | ||
commentsQueryRecompiled = "insert into wp_comments values (1, 1, 'kamren.ohara', '[email protected]', 'http://ebert.com/korey_keeling', '', '2019-06-12 00:59:19', '2019-06-12 00:59:19', 'Hi, this is a comment.\\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\\nCommenter avatars come from <a href=\\\"https://gravatar.com\\\">Gravatar</a>.', 0, '1', '', '', 0, 0);\n" | ||
usersQuery = "INSERT INTO `wp_users` VALUES (1,'username','user_pass','username','[email protected]','','2019-06-12 00:59:19','',0,'username'),(2,'username','user_pass','username','[email protected]','http://notreal.com/username','2019-06-12 00:59:19','',0,'username');\n" | ||
usersQueryRecompiled = "insert into wp_users values (1, 'treva_cremin', 'NjaK5HeMAMuv', 'hailey', '[email protected]', '', '2019-06-12 00:59:19', '', 0, 'Kylie Rice'), (2, 'eduardo', 'J3JRQ4XoIxXX6A', 'albert.okeefe', '[email protected]', 'http://pfannerstill.net/brando', '2019-06-12 00:59:19', '', 0, 'Ardella Jenkins PhD');\n" | ||
userMetaQuery = "INSERT INTO `wp_usermeta` VALUES (1,1,'first_name','John'),(2,1,'last_name','Doe'),(3,1,'foobar','bazquz'),(4,1,'nickname','Jim'),(5,1,'description','Lorum ipsum.'),(6,2,'first_name','Janet'),(7,2,'last_name','Doe'),(8,2,'foobar','bazquz'),(9,2,'nickname','Jane'),(10,2,'description','Lorum ipsum.');\n" | ||
userMetaQueryRecompiled = "insert into wp_usermeta values (1, 1, 'first_name', 'Stephania'), (2, 1, 'last_name', 'Hamill'), (3, 1, 'foobar', 'bazquz'), (4, 1, 'nickname', 'Noah'), (5, 1, 'description', 'Dolorum nostrum alias.'), (6, 2, 'first_name', 'Ed'), (7, 2, 'last_name', 'Koelpin'), (8, 2, 'foobar', 'bazquz'), (9, 2, 'nickname', 'Watson'), (10, 2, 'description', 'Qui voluptatum est.');\n" | ||
) | ||
|
||
func init() { | ||
|
@@ -38,50 +50,53 @@ func BenchmarkProcessLine(b *testing.B) { | |
} | ||
} | ||
|
||
func TestProcessFile(t *testing.T) { | ||
input := bytes.NewBufferString(multilineQuery) | ||
|
||
lines := setupAndProcessInput(jsonConfig, input) | ||
|
||
var result string | ||
for line := range lines { | ||
result = <-line | ||
} | ||
|
||
if result != multilineQueryRecompiled { | ||
t.Error("\nExpected:\n", multilineQueryRecompiled, "\nActual:\n", result) | ||
} | ||
} | ||
|
||
func TestApplyConfigToQuery(t *testing.T) { | ||
func TestSetupAndProcessInput(t *testing.T) { | ||
|
||
var tests = []struct { | ||
testName string | ||
line string | ||
query string | ||
wants string | ||
}{ | ||
{ | ||
testName: "users query", | ||
line: usersQuery, | ||
query: usersQuery, | ||
wants: usersQueryRecompiled, | ||
}, | ||
{ | ||
testName: "usermeta query", | ||
line: userMetaQuery, | ||
query: userMetaQuery, | ||
wants: userMetaQueryRecompiled, | ||
}, | ||
{ | ||
testName: "comments query", | ||
line: commentsQuery, | ||
query: commentsQuery, | ||
wants: commentsQueryRecompiled, | ||
}, | ||
{ | ||
testName: "multiline query", | ||
query: multilineQuery, | ||
wants: multilineQueryRecompiled, | ||
}, | ||
{ | ||
testName: "table creation", | ||
query: dropAndCreateTable, | ||
wants: dropAndCreateTable + "\n", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.testName, func(t *testing.T) { | ||
line := processLine(test.line, jsonConfig) | ||
if line != test.wants { | ||
t.Error("\nExpected:\n", test.wants, "\nActual:\n", line) | ||
|
||
input := bytes.NewBufferString(test.query) | ||
lines := setupAndProcessInput(jsonConfig, input) | ||
|
||
var result string | ||
for line := range lines { | ||
result += <-line | ||
} | ||
|
||
if result != test.wants { | ||
t.Error("\nExpected:\n", test.wants, "\nActual:\n", result) | ||
} | ||
}) | ||
} | ||
|