Coder Social home page Coder Social logo

mysql.server's Introduction

MySql.Server Build Status Nuget

MySql standalone server for C# unit tests

Use

Download with NuGet, or download the release and include Mysql.Server.dll as a reference in your project.

How it works

Mysql.Server is simply running a minimal instance of MySql (currently version 5.6.26). Necessary data and log files are created at run time (and are cleaned up afterwards).

Mysql.Server makes it possible to create and run unit tests on a real MySql server without spending time on server setup.

Examples

Create server, table and data.

See Example.cs for a complete example.

        //Get an instance
        MySqlServer dbServer = MySqlServer.Instance;
        
        //Start the server
        dbServer.StartServer();
        
        //Create a database and use it
        MySqlHelper.ExecuteNonQuery(dbServer.GetConnectionString(), "CREATE DATABASE testserver;");
        
        //Insert data
        MySqlHelper.ExecuteNonQuery(dbServer.GetConnectionString("testserver"), "INSERT INTO testTable (`id`, `value`) VALUES (2, 'test value')"); 
        
        //Shut down server
        dbServer.ShutDown();

A test

        //Concrete test. Writes data and reads it again.
        [TestMethod]
        public void TestMethod()
        {
            MySqlServer database = MySqlServer.Instance;

            MySqlHelper.ExecuteNonQuery(database.GetConnectionString("testserver"), "INSERT INTO testTable (`id`, `value`) VALUES (2, 'test value')");

            using (MySqlDataReader reader = MySqlHelper.ExecuteReader(database.GetConnectionString("testserver"), "SELECT * FROM testTable WHERE id = 2"))
            {
                reader.Read();

                Assert.AreEqual("test value", reader.GetString("value"), "Inserted and read string should match");
            }
        }

API

  • MySqlServer.Instance: Retrieves an Instance of the server API.

  • MySqlServer.StartServer(): Starts the server.

  • MySqlServer.StartServer(int serverPort): Starts the server at a specified port. Nice to have if you have a real MySql server running on the test machine.

  • MySqlServer.ShutDown(): Shuts down the server.

  • MySqlServer.GetConnectionString(): Returns a connection string to be used when connecting to the server.

  • MySqlServer.GetConnectionString(string databasename): Returns a connection string to be used when connecting to the server and a specific database. This method can only be used if a database is already created.

  • MySqlServer.ProcessId: Returns the process id of the server. Returns -1 if the process has exited.

  • MySqlServer.ServerPort: Returns the server port of the instance.

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.