github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/db/sql/sql_test.go (about) 1 // Copyright (C) 2015 NTT Innovation Institute, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package sql_test 17 18 import ( 19 "fmt" 20 "os" 21 "strings" 22 23 "github.com/cloudwan/gohan/db" 24 . "github.com/cloudwan/gohan/db/sql" 25 "github.com/cloudwan/gohan/db/transaction" 26 "github.com/cloudwan/gohan/schema" 27 28 . "github.com/onsi/ginkgo" 29 . "github.com/onsi/gomega" 30 ) 31 32 var _ = Describe("Sql", func() { 33 34 var conn string 35 var tx transaction.Transaction 36 37 BeforeEach(func() { 38 var dbType string 39 if os.Getenv("MYSQL_TEST") == "true" { 40 conn = "root@/gohan_test" 41 dbType = "mysql" 42 } else { 43 conn = "./test.db" 44 dbType = "sqlite3" 45 } 46 47 manager := schema.GetManager() 48 dbc, err := db.ConnectDB(dbType, conn) 49 Expect(err).ToNot(HaveOccurred()) 50 Expect(manager.LoadSchemasFromFiles( 51 "../../etc/schema/gohan.json", "../../tests/test_schema.yaml")).To(Succeed()) 52 db.InitDBWithSchemas(dbType, conn, true, false) 53 54 // Insert fixture data 55 fixtureDB, err := db.ConnectDB("json", "test_fixture.json") 56 Expect(err).ToNot(HaveOccurred()) 57 db.CopyDBResources(fixtureDB, dbc) 58 59 tx, err = dbc.Begin() 60 Expect(err).ToNot(HaveOccurred()) 61 }) 62 63 AfterEach(func() { 64 schema.ClearManager() 65 if os.Getenv("MYSQL_TEST") != "true" { 66 os.Remove(conn) 67 } 68 }) 69 70 Describe("Query", func() { 71 var s *schema.Schema 72 73 BeforeEach(func() { 74 manager := schema.GetManager() 75 var ok bool 76 s, ok = manager.Schema("test") 77 Expect(ok).To(BeTrue()) 78 }) 79 80 Context("Without place holders", func() { 81 It("Returns resources", func() { 82 query := fmt.Sprintf( 83 "SELECT %s FROM %s", 84 strings.Join(MakeColumns(s, false), ", "), 85 s.GetDbTableName(), 86 ) 87 results, err := tx.Query(s, query, []interface{}{}) 88 Expect(err).ToNot(HaveOccurred()) 89 Expect(results[0].Get("tenant_id")).To(Equal("tenant0")) 90 Expect(results[0].Get("test_string")).To(Equal("obj0")) 91 Expect(results[2].Get("tenant_id")).To(Equal("tenant1")) 92 Expect(results[2].Get("test_string")).To(Equal("obj2")) 93 Expect(len(results)).To(Equal(4)) 94 }) 95 }) 96 97 Context("With a place holder", func() { 98 It("Replace the place holder and returns resources", func() { 99 query := fmt.Sprintf( 100 "SELECT %s FROM %s WHERE tenant_id = ?", 101 strings.Join(MakeColumns(s, false), ", "), 102 s.GetDbTableName(), 103 ) 104 results, err := tx.Query(s, query, []interface{}{"tenant0"}) 105 Expect(err).ToNot(HaveOccurred()) 106 Expect(results[0].Get("tenant_id")).To(Equal("tenant0")) 107 Expect(results[0].Get("test_string")).To(Equal("obj0")) 108 Expect(results[1].Get("tenant_id")).To(Equal("tenant0")) 109 Expect(results[1].Get("test_string")).To(Equal("obj1")) 110 Expect(len(results)).To(Equal(2)) 111 112 }) 113 }) 114 115 Context("With place holders", func() { 116 It("Replace the place holders and returns resources", func() { 117 query := fmt.Sprintf( 118 "SELECT %s FROM %s WHERE tenant_id = ? AND test_string = ?", 119 strings.Join(MakeColumns(s, false), ", "), 120 s.GetDbTableName(), 121 ) 122 results, err := tx.Query(s, query, []interface{}{"tenant0", "obj1"}) 123 Expect(err).ToNot(HaveOccurred()) 124 Expect(results[0].Get("tenant_id")).To(Equal("tenant0")) 125 Expect(results[0].Get("test_string")).To(Equal("obj1")) 126 Expect(len(results)).To(Equal(1)) 127 }) 128 }) 129 }) 130 })