github.com/mssola/todo@v0.0.0-20181029153210-d25348dc3f48/app/db_test.go (about)

     1  // Copyright (C) 2014-2017 Miquel Sabaté Solà <mikisabate@gmail.com>
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, v. 2.0. If a copy of the MPL was not distributed with this
     5  // file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7  package app
     8  
     9  import (
    10  	"fmt"
    11  	"io/ioutil"
    12  	"log"
    13  	"os"
    14  	"testing"
    15  
    16  	"github.com/mssola/todo/lib"
    17  )
    18  
    19  // Initialize the database before running an unit test.
    20  func initTestDB() {
    21  	log.SetOutput(ioutil.Discard)
    22  
    23  	lib.InitSession()
    24  	lib.ViewsDir = "../views"
    25  
    26  	_ = os.Setenv("TODO_ENV", "test")
    27  	InitDB()
    28  
    29  	_ = Db.TruncateTables()
    30  }
    31  
    32  // Use this in the end of every unit test.
    33  func closeTestDB() {
    34  	_ = Db.TruncateTables()
    35  	CloseDB()
    36  }
    37  
    38  func TestConfigURL(t *testing.T) {
    39  	dbname := EnvOrElse("TODO_DB_NAME", "todo-dev")
    40  
    41  	str := configURL()
    42  	exp := fmt.Sprintf("user=postgres host=localhost port=5432 dbname=%s sslmode=disable", dbname)
    43  	if str != exp {
    44  		t.Fatalf("Got: '%s'; Expected: '%s'", str, exp)
    45  	}
    46  
    47  	exp = fmt.Sprintf("user=postgres host=localhost port=5432 dbname=%s sslmode=disable password=1234", dbname)
    48  	os.Setenv("TODO_DB_PASSWORD", "1234")
    49  	str = configURL()
    50  	os.Setenv("TODO_DB_PASSWORD", "")
    51  	if str != exp {
    52  		t.Fatalf("Got: '%s'; Expected: '%s'", str, exp)
    53  	}
    54  }
    55  
    56  func TestExists(t *testing.T) {
    57  	initTestDB()
    58  	defer closeTestDB()
    59  
    60  	// Does not exist.
    61  	if Exists("users", "1") {
    62  		t.Fatal("Expected to be false")
    63  	}
    64  
    65  	// Exists!
    66  	createUser("u", "1234")
    67  	var u User
    68  	err := Db.SelectOne(&u, "select * from users")
    69  	if err != nil {
    70  		t.Fatalf("Should've executed correctly, but: %v", err)
    71  	}
    72  	if !Exists("users", u.ID) {
    73  		t.Fatal("Expected to be true")
    74  	}
    75  }
    76  
    77  func TestCount(t *testing.T) {
    78  	initTestDB()
    79  	defer closeTestDB()
    80  
    81  	// Try to count a non-existing table.
    82  	count := Count("doesnotexist")
    83  	if count != 0 {
    84  		t.Fatalf("Wrong count: %v; expected: %v", count, 0)
    85  	}
    86  
    87  	// Counting.
    88  	count = Count("topics")
    89  	if count != 0 {
    90  		t.Fatalf("Wrong count: %v; expected: %v", count, 0)
    91  	}
    92  	createTopic("t1")
    93  	createTopic("t2")
    94  	count = Count("topics")
    95  	if count != 2 {
    96  		t.Fatalf("Wrong count: %v; expected: %v", count, 2)
    97  	}
    98  	count = Count("users")
    99  	if count != 0 {
   100  		t.Fatalf("Wrong count: %v; expected: %v", count, 0)
   101  	}
   102  }