Merge pull request #20 from 30x/ISSUE-65205927

[ISSUE-65205927] Use transaction for writing, add row close
diff --git a/apidAnalytics_suite_test.go b/apidAnalytics_suite_test.go
index 983a987..669b18c 100644
--- a/apidAnalytics_suite_test.go
+++ b/apidAnalytics_suite_test.go
@@ -119,7 +119,10 @@
 }
 
 func createTables(db apid.DB) {
-	_, err := db.Exec(`
+	tx, err := db.Begin()
+	Expect(err).Should(Succeed())
+	defer tx.Rollback()
+	_, err = tx.Exec(`
 		CREATE TABLE IF NOT EXISTS kms_api_product (
 		    id text,
 		    tenant_id text,
@@ -186,13 +189,16 @@
 		    PRIMARY KEY (appcred_id, app_id, apiprdt_id,tenant_id)
 		);
 	`)
-	if err != nil {
-		panic("Unable to initialize DB " + err.Error())
-	}
+	Expect(err).Should(Succeed())
+	err = tx.Commit()
+	Expect(err).Should(Succeed())
 }
 
 func createApidClusterTables(db apid.DB) {
-	_, err := db.Exec(`
+	txn, err := db.Begin()
+	Expect(err).Should(Succeed())
+	defer txn.Rollback()
+	_, err = txn.Exec(`
 		CREATE TABLE edgex_apid_cluster (
 		    id text,
 		    instance_id text,
@@ -222,16 +228,17 @@
 		    PRIMARY KEY (id)
 		);
 	`)
-	if err != nil {
-		panic("Unable to initialize DB " + err.Error())
-	}
+	Expect(err).Should(Succeed())
+	err = txn.Commit()
+	Expect(err).Should(Succeed())
 }
 
 func insertTestData(db apid.DB) {
 
 	txn, err := db.Begin()
-	Expect(err).ShouldNot(HaveOccurred())
-	txn.Exec("INSERT INTO kms_app_credential_apiproduct_mapper (tenant_id,"+
+	Expect(err).Should(Succeed())
+	defer txn.Rollback()
+	_, err = txn.Exec("INSERT INTO kms_app_credential_apiproduct_mapper (tenant_id,"+
 		" appcred_id, app_id, apiprdt_id, status, _change_selector) "+
 		"VALUES"+
 		"($1,$2,$3,$4,$5,$6)",
@@ -242,8 +249,8 @@
 		"APPROVED",
 		"12345",
 	)
-
-	txn.Exec("INSERT INTO kms_app (id, tenant_id, name, developer_id) "+
+	Expect(err).Should(Succeed())
+	_, err = txn.Exec("INSERT INTO kms_app (id, tenant_id, name, developer_id) "+
 		"VALUES"+
 		"($1,$2,$3,$4)",
 		"testappid",
@@ -251,16 +258,16 @@
 		"testapp",
 		"testdeveloperid",
 	)
-
-	txn.Exec("INSERT INTO kms_api_product (id, tenant_id, name) "+
+	Expect(err).Should(Succeed())
+	_, err = txn.Exec("INSERT INTO kms_api_product (id, tenant_id, name) "+
 		"VALUES"+
 		"($1,$2,$3)",
 		"testproductid",
 		"tenantid",
 		"testproduct",
 	)
-
-	txn.Exec("INSERT INTO kms_developer (id, tenant_id, username, email) "+
+	Expect(err).Should(Succeed())
+	_, err = txn.Exec("INSERT INTO kms_developer (id, tenant_id, username, email) "+
 		"VALUES"+
 		"($1,$2,$3,$4)",
 		"testdeveloperid",
@@ -268,8 +275,8 @@
 		"testdeveloper",
 		"testdeveloper@test.com",
 	)
-
-	txn.Exec("INSERT INTO edgex_data_scope (id, _change_selector, "+
+	Expect(err).Should(Succeed())
+	_, err = txn.Exec("INSERT INTO edgex_data_scope (id, _change_selector, "+
 		"apid_cluster_id, scope, org, env) "+
 		"VALUES"+
 		"($1,$2,$3,$4,$5,$6)",
@@ -280,7 +287,9 @@
 		"testorg",
 		"testenv",
 	)
-	txn.Commit()
+	Expect(err).Should(Succeed())
+	err = txn.Commit()
+	Expect(err).Should(Succeed())
 }
 
 var _ = AfterSuite(func() {
diff --git a/common_helper.go b/common_helper.go
index 05805bf..262546a 100644
--- a/common_helper.go
+++ b/common_helper.go
@@ -186,7 +186,7 @@
 	db := getDB()
 	rows, err := db.Query("SELECT 1 FROM edgex_data_scope"+
 		" where org = ? and env = ?", tenant.Org, tenant.Env)
-
+	defer rows.Close()
 	if !rows.Next() {
 		if err == nil {
 			reason := "No tenant found for this org: " + tenant.Org + " and env:" + tenant.Env
diff --git a/uploader.go b/uploader.go
index d645a95..c37a257 100644
--- a/uploader.go
+++ b/uploader.go
@@ -165,7 +165,7 @@
 func getDateFromDirTimestamp(timestamp string) string {
 	dateTime, _ := time.Parse(timestampLayout, timestamp)
 	date := dateTime.Format("2006-01-02") // same as YYYY-MM-dd
-	time := dateTime.Format("15-04")      // same as HH-mm
+	time := dateTime.Format("15-04-05")   // same as HH-mm-ss
 	dateHourTS := "date=" + date + "/time=" + time
 	return dateHourTS
 }
diff --git a/uploader_test.go b/uploader_test.go
index 03f1ce2..72d87ea 100644
--- a/uploader_test.go
+++ b/uploader_test.go
@@ -30,7 +30,7 @@
 
 		By("valid tenant")
 		tenant := "testorg~testenv"
-		relativeFilePath := "/date=2006-01-02/time=15-04/fakefile.txt.gz"
+		relativeFilePath := "/date=2006-01-02/time=15-04-05/fakefile.txt.gz"
 
 		status, err := uploadFile(tenant, relativeFilePath, fp)
 		Expect(err).ShouldNot(HaveOccurred())
@@ -38,7 +38,7 @@
 
 		By("invalid tenant")
 		tenant = "o~e"
-		relativeFilePath = "/date=2006-01-02/time=15-04/fakefile.txt.gz"
+		relativeFilePath = "/date=2006-01-02/time=15-04-05/fakefile.txt.gz"
 
 		status, err = uploadFile(tenant, relativeFilePath, fp)
 		Expect(err).Should(HaveOccurred())
@@ -81,7 +81,7 @@
 	Context("invalid tenant", func() {
 		It("should return error", func() {
 			tenant := "org~env"
-			relativeFilePath := "/date=2016-01-01/time=22-45/a.txt.gz"
+			relativeFilePath := "/date=2016-01-01/time=22-45-05/a.txt.gz"
 
 			_, err := getSignedUrl(tenant, relativeFilePath)
 			Expect(err).Should(HaveOccurred())
@@ -91,7 +91,7 @@
 	Context("valid tenant", func() {
 		It("should return signed url", func() {
 			tenant := "testorg~testenv"
-			relativeFilePath := "/date=2016-01-01/time=22-45/a.txt.gz"
+			relativeFilePath := "/date=2016-01-01/time=22-45-05/a.txt.gz"
 
 			url, err := getSignedUrl(tenant, relativeFilePath)
 			Expect(err).ShouldNot(HaveOccurred())
@@ -150,6 +150,6 @@
 	It("should return date/time formatted timestamp", func() {
 		timestamp := "20060102150405"
 		dateHourTS := getDateFromDirTimestamp(timestamp)
-		Expect(dateHourTS).To(Equal("date=2006-01-02/time=15-04"))
+		Expect(dateHourTS).To(Equal("date=2006-01-02/time=15-04-05"))
 	})
 })