From b453f6cb2302a689f8cb8f152df9a5cba5f653f2 Mon Sep 17 00:00:00 2001 From: Michael McGuinness Date: Thu, 16 Jan 2025 18:25:30 +0000 Subject: [PATCH] Merged in feature/scriptclean (pull request #25) Scripting Clean Up * fixupscripts * addtests --- README.md | 42 ++++++++++++++++++- internal/api/listener.go | 2 +- internal/api/listener_test.go | 1 - internal/database/migrationsprivate_test.go | 6 ++- .../database/repository/querytype_test.go | 4 ++ mocks/repository/mock_DBTX.go | 2 +- scripts/Taskfile.yml | 12 +++++- scripts/dependencies.yml | 6 ++- scripts/tests.yml | 11 +++-- 9 files changed, 72 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 67fc44a3..e1cb48c7 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,48 @@ To find new packages: `https://search.nixos.org/packages` For regular usage, please use `task` scripts in order run the most appropriate configuration. -# Environment Variables +## Environment Variables When using environment variables, there is a hiearchy that will be respected. (First will override the next) 1. Within the devbox.json file, a variable added to "env" 2. Within the devbox.json file, a variable exported in "init_hook" -3. A variable added to .env \ No newline at end of file +3. A variable added to .env + +## Testing + +For testing integrations such as queries against a database, or interactions with an SQS queue, this repository employs the use of docker testcontainers. + +`https://testcontainers.com/` + +This enables tests to be fully self-contained, thus allowing allowing tests to be reliable and replicable. + +### Execution + +The simplest way of ensuring the validity of the full project is by running `task fullsuite` when in the devbox shell. + +This will: + +1. Generate the latest version of the specs. +2. Run the linting commands. +3. Run the unit tests. +4. Run the integration tests. + +## Naming Conventions + +**Service** - This term is used internally in order to refer to commands that get deployed as APIs. +**Runner** - This term is used internally in order to refer to commands that get deployed as consumers of a queue. + +### Creating a new Service + +1. Add a file named `.yml`. The service name is important, as it will be used as a reference throughout the codebase. The service name must be in the format `Service`. E.g. queryService, ClientService +2. Run `task openapi:generate`. This will generate the following: + - A location to implement the server-side functions in `./api//`, as well as generated functions, models and swagger docs. + - A location with the client-side code in `./pkg//` +3. Implement the controllers in `./api//` +4. Create a command in `./cmd//main.go` which creates a new instance from `./internal/api` + +### Creating a new Runner + +1. Implement the controller in `./api//` which implements the `Controller` interface in `./internal/queue`. The runner name must be in the format `Runner`. E.g. queryRunner, csvExportRunner +2. Create a command in `./cmd//main.go` which creates a new instance from `./internal/queue` diff --git a/internal/api/listener.go b/internal/api/listener.go index 5d924bde..d1ca176b 100644 --- a/internal/api/listener.go +++ b/internal/api/listener.go @@ -89,6 +89,6 @@ func (s *Server) Listen() { err := s.server.Start(s.address) if err != nil { - s.server.Logger.Fatal(err) + s.server.Logger.Panic(err) } } diff --git a/internal/api/listener_test.go b/internal/api/listener_test.go index 195271f0..9096262a 100644 --- a/internal/api/listener_test.go +++ b/internal/api/listener_test.go @@ -38,7 +38,6 @@ func TestNew(t *testing.T) { } func TestListen(t *testing.T) { - t.Skip("Must test appropriately") server := Server{} assert.Panics(t, func() { server.Listen() }) diff --git a/internal/database/migrationsprivate_test.go b/internal/database/migrationsprivate_test.go index ed30f131..2875d043 100644 --- a/internal/database/migrationsprivate_test.go +++ b/internal/database/migrationsprivate_test.go @@ -2,10 +2,12 @@ package database import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestCreateDB(t *testing.T) { - t.Skip("Must test appropriately") + t.Setenv("DB_HOST", "") - createDB() + assert.Panics(t, func() { createDB() }) } diff --git a/internal/database/repository/querytype_test.go b/internal/database/repository/querytype_test.go index 3bec35dd..1a8359f0 100644 --- a/internal/database/repository/querytype_test.go +++ b/internal/database/repository/querytype_test.go @@ -35,3 +35,7 @@ func TestNullQueryTypeValue(t *testing.T) { assert.Nil(t, err) assert.Equal(t, stringType, val) } + +func TestQueryTypeValid(t *testing.T) { + assert.True(t, repository.QuerytypeContextFull.Valid()) +} diff --git a/mocks/repository/mock_DBTX.go b/mocks/repository/mock_DBTX.go index b94d7e42..10dceec2 100644 --- a/mocks/repository/mock_DBTX.go +++ b/mocks/repository/mock_DBTX.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.50.0. DO NOT EDIT. +// Code generated by mockery v2.46.3. DO NOT EDIT. package repository diff --git a/scripts/Taskfile.yml b/scripts/Taskfile.yml index 711dfb00..60366109 100644 --- a/scripts/Taskfile.yml +++ b/scripts/Taskfile.yml @@ -33,15 +33,23 @@ tasks: - touch .env fullsuite: cmds: - - task generate + - task build - task lint - - task test:unit:coverage + - task test:unit - task test:integration + build: + deps: + - generate + cmds: + - task docker:build generate: deps: - db:generate - openapi:generate + - test:mocks:generate lint: + deps: + - generate cmds: - task go:lint - task db:lint diff --git a/scripts/dependencies.yml b/scripts/dependencies.yml index 7186c5a4..c56b41ad 100644 --- a/scripts/dependencies.yml +++ b/scripts/dependencies.yml @@ -7,7 +7,11 @@ tasks: download: cmds: - go mod download - tidy: + tidy:novendor: cmds: - go mod tidy + tidy: + deps: + - tidy:novendor + cmds: - go mod vendor diff --git a/scripts/tests.yml b/scripts/tests.yml index 1a57b92a..cac9514a 100644 --- a/scripts/tests.yml +++ b/scripts/tests.yml @@ -7,10 +7,12 @@ vars: COVERAGE_FILE: "{{.OUT_DIR}}/coverage.out" tasks: - mocks: + mocks:generate: cmds: - mockery - unit: + unit:nocoverage: + deps: + - mocks:generate vars: INTERNAL: "./internal/..." API: "./api/..." @@ -21,9 +23,9 @@ tasks: go test {{.INTERNAL}} {{.API}} {{.PKG}} \ -coverpkg={{.INTERNAL}},{{.API}},{{.PKG}} \ -coverprofile={{.COVERAGE_FILE}} - unit:coverage: + unit: deps: - - unit + - unit:nocoverage vars: TMP_FILE: "{{.OUT_DIR}}/coverage.tmp" FILTER_COVERAGE_FILE: "{{.OUT_DIR}}/filtered_coverage.out" @@ -44,6 +46,7 @@ tasks: silent: true integration: cmds: + - task generate - task docker:build - task test:integration:nobuild integration:nocache: