From 72c68e22de662813f01140860de62dd18550169d Mon Sep 17 00:00:00 2001
From: Aaron L <aaron@bettercoder.net>
Date: Mon, 19 Dec 2016 19:29:11 -0800
Subject: [PATCH] Fix exists

- Add tests for exists
---
 templates_test/finishers.tpl                  | 46 ++++++++++++++++++-
 templates_test/singleton/boil_suites_test.tpl | 10 ++++
 2 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/templates_test/finishers.tpl b/templates_test/finishers.tpl
index fa8b129..b3ba672 100644
--- a/templates_test/finishers.tpl
+++ b/templates_test/finishers.tpl
@@ -95,6 +95,15 @@ func test{{$tableNamePlural}}Count(t *testing.T) {
 
 	tx := MustTx(boil.Begin())
 	defer tx.Rollback()
+	count, err := {{$tableNamePlural}}(tx).Count()
+	if err != nil {
+		t.Error(err)
+	}
+
+	if count != 0 {
+		t.Error("want 0 records found")
+	}
+
 	if err = {{$varNameSingular}}One.Insert(tx); err != nil {
 		t.Error(err)
 	}
@@ -102,7 +111,7 @@ func test{{$tableNamePlural}}Count(t *testing.T) {
 		t.Error(err)
 	}
 
-	count, err := {{$tableNamePlural}}(tx).Count()
+	count, err = {{$tableNamePlural}}(tx).Count()
 	if err != nil {
 		t.Error(err)
 	}
@@ -111,3 +120,38 @@ func test{{$tableNamePlural}}Count(t *testing.T) {
 		t.Error("want 2 records, got:", count)
 	}
 }
+
+func test{{$tableNamePlural}}ExistsFinisher(t *testing.T) {
+	t.Parallel()
+
+	var err error
+	seed := randomize.NewSeed()
+	{{$varNameSingular}}One := &{{$tableNameSingular}}{}
+	if err = randomize.Struct(seed, {{$varNameSingular}}One, {{$varNameSingular}}DBTypes, false, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
+		t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
+	}
+
+	tx := MustTx(boil.Begin())
+	defer tx.Rollback()
+	exists, err := {{$tableNamePlural}}(tx).Exists()
+	if err != nil {
+		t.Error(err)
+	}
+
+	if exists {
+		t.Error("the record should not exist")
+	}
+
+	if err = {{$varNameSingular}}One.Insert(tx); err != nil {
+		t.Error(err)
+	}
+
+	exists, err = {{$tableNamePlural}}(tx).Exists()
+	if err != nil {
+		t.Error(err)
+	}
+
+	if !exists {
+		t.Error("wanted record to exist")
+	}
+}
diff --git a/templates_test/singleton/boil_suites_test.tpl b/templates_test/singleton/boil_suites_test.tpl
index 8723da4..bf105a3 100644
--- a/templates_test/singleton/boil_suites_test.tpl
+++ b/templates_test/singleton/boil_suites_test.tpl
@@ -105,6 +105,16 @@ func TestCount(t *testing.T) {
   {{- end -}}
 }
 
+func TestExistsFinisher(t *testing.T) {
+  {{- range $index, $table := .Tables}}
+  {{- if $table.IsJoinTable -}}
+  {{- else -}}
+  {{- $tableName := $table.Name | plural | titleCase -}}
+  t.Run("{{$tableName}}", test{{$tableName}}ExistsFinisher)
+  {{end -}}
+  {{- end -}}
+}
+
 {{if not .NoHooks -}}
 func TestHooks(t *testing.T) {
   {{- range $index, $table := .Tables}}