Skip to content

Commit

Permalink
proof of concept working
Browse files Browse the repository at this point in the history
  • Loading branch information
richardrdev committed Mar 5, 2024
1 parent fbc80f7 commit 4718c83
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package main
import (
"database/sql"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -46,6 +48,20 @@ type TestCase struct {
validateFunc func(t *testing.T, response map[string]interface{})
}

type TempTestCaseNew struct {
description string

// Test input
query string
body string
headers map[string][]string

// Expected output
expectedCode int
expectedBody interface{}
expectedContentType string
}

func init() {
// Test on SQLite by default if DATABASE_DSN is not set
if _, exists := os.LookupEnv("DATABASE_DSN"); !exists {
Expand Down Expand Up @@ -158,6 +174,59 @@ func runTestCases(t *testing.T, tests []TestCase) {
}
}

func CompareTestInterfaces(a, b interface{}) (bool, error) {
if reflect.TypeOf(a) != reflect.TypeOf(b) {
return false, errors.New("types are different")
}

return a == b, nil
}

func TempRunTestsNew(t *testing.T, tests []TempTestCaseNew) {
responseFromApi := `{"title":"Not Found","detail":"Cannot GET /v1/i-dont-exist","status":404}`

test := tests[0]

resultBody := reflect.New(reflect.TypeOf(test.expectedBody)).Interface()

err := json.Unmarshal([]byte(responseFromApi), &resultBody)
if err != nil {
return
}

result, err := CompareTestInterfaces(test.expectedBody, reflect.ValueOf(resultBody).Elem().Interface())
if err != nil {
return
}

if !result {
t.Fail()
}
}

func TestApiNew(t *testing.T) {
type TestApiExpectedBody struct {
Title string
Detail string
Status int
}

expectedBody := TestApiExpectedBody{Title: "Not Found", Detail: "Cannot GET /v1/i-dont-exist", Status: 404}

tests := []TempTestCaseNew{
{
description: "non existing route",
query: "GET /v1/i-dont-exist",

expectedCode: 404,
expectedBody: expectedBody,
expectedContentType: "application/problem+json",
},
}

TempRunTestsNew(t, tests)
}

func TestApi(t *testing.T) {
tests := []TestCase{
{
Expand Down

0 comments on commit 4718c83

Please sign in to comment.