Giter VIP home page Giter VIP logo

test-mysqld's Introduction

test-mysqld

Create real MySQL server instance for testing

Build Status

GoDoc

DESCRIPTION

By default importing github.com/lestrrat-go/test-mysqld will import package mysqltest

import (
    "database/sql"
    "log"
    "github.com/lestrrat-go/test-mysqld"
)

mysqld, err := mysqltest.NewMysqld(nil)
if err != nil {
   log.Fatalf("Failed to start mysqld: %s", err)
}
defer mysqld.Stop()

db, err := sql.Open("mysql", mysqld.Datasource("test", "", "", 0))
// Now use db, which is connected to a mysql db

go-test-mysqld is a port of Test::mysqld

When you create a new struct via NewMysqld() a new mysqld instance is automatically setup and launched. Don't forget to call Stop() on this struct to stop the launched mysqld

If you want to customize the configuration, create a new config and set each field on the struct:

config := mysqltest.NewConfig()
config.SkipNetworking = false
config.Port = 13306

// Starts mysqld listening on port 13306
mysqld, _ := mysqltest.NewMysqld(config)

Generating DSN

DSN strings can be generated using the DSN method:

// Use default
dsn := mysqld.DSN()

// Pass explicit parameters
dsn := mysqld.DSN(mysqltest.WithUser("foo"), mysqltest.WithPassword("passw0rd!"))

// Tell the mysql driver to parse time values
dsn := mysqld.DSN(mysqltest.WithParseTime(true))

// ...And pass the dsn to sql.Open
db, err := sql.Open("mysql", dsn)

Following is a list of possible parameters to DSN. I

Option Description Default
mysqltest.WithProto(string) Specifies the protocol ("unix" or "tcp") Depends on value of config.SkipNetworking
mysqltest.WithSocket(string) Specifies the path to the unix socket value of config.Socket
mysqltest.WithHost(string) Specifies the hostname value of config.BindAddress
mysqltest.WithPort(int) Specifies the port number value of config.Port
mysqltest.WithUser(string) Specifies the username "root"
mysqltest.WithPassword(string) Specifies the password ""
mysqltest.WithDbname(string) Specifies the database name to connect "test"
mysqltest.WithParseTime(bool) Specifies if mysql driver should parse time values to time.Time false

test-mysqld's People

Contributors

lestrrat avatar memememomo avatar songmu avatar srmor avatar tarao avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.