Skip to content

Commit b5470aa

Browse files
test: improve code coverage with comprehensive unit tests
1 parent c3c0300 commit b5470aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+7186
-513
lines changed

.github/copilot-instructions.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55
This is the Microsoft official Go MSSQL driver repository. This document provides comprehensive instructions for working effectively in this codebase, including build, test, lint, and validation processes.
66

7+
## Code Style
8+
- Minimal code, maximum modularity
9+
- Extract repeated logic into helpers
10+
- No verbose patterns
11+
- No redundant comments (e.g., `// TestX tests the X function` - the name says it)
12+
- Comments should add information, not repeat what the code already says
13+
14+
## Testing
15+
- Use `github.com/stretchr/testify/assert` for assertions
16+
- Let exceptions be exceptions: use `t.Fatal(err)` not `t.Skipf` when tests should fail
17+
- Table-driven tests with `t.Run` for multiple cases
18+
- No redundant test scaffolding - extract helpers when patterns repeat 3+ times
19+
720
## Working Effectively
821

922
### Bootstrap and Build the Repository
@@ -75,9 +88,23 @@ Always run these commands before committing changes:
7588
- `go test ./msdsn ./internal/... ./integratedauth ./azuread` - run unit tests (~1.5 seconds total)
7689
- If you have SQL Server available: `go test ./...` with 30+ minute timeout. NEVER CANCEL.
7790

91+
### Code Coverage Requirements
92+
**IMPORTANT**: This project enforces a strict **80% minimum code coverage** requirement.
93+
- All PRs must maintain project coverage at or above 80%
94+
- New code in PRs must also have at least 80% coverage
95+
- PRs that drop coverage below 80% will fail the Codecov status check
96+
- Coverage is configured in `codecov.yml` at the repository root
97+
98+
To check coverage locally:
99+
```bash
100+
go test -coverprofile=coverage.out ./...
101+
go tool cover -func=coverage.out | tail -1 # Shows total coverage
102+
```
103+
78104
The CI pipeline (.github/workflows/pr-validation.yml) runs:
79-
1. `go test -v ./...` against SQL Server 2019 and 2022 in Docker
80-
2. AppVeyor runs Windows-specific tests including named pipes and shared memory
105+
1. `go test -coverprofile=coverage.out -v ./...` against SQL Server 2019 and 2022 in Docker
106+
2. Uploads coverage to Codecov for enforcement
107+
3. AppVeyor runs Windows-specific tests including named pipes and shared memory
81108

82109
## Commit Message Format
83110

.github/workflows/pr-validation.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@ jobs:
2929
export HOST=.
3030
docker run -m 2GB -e ACCEPT_EULA=1 -d --name sqlserver -p:1433:1433 -e SA_PASSWORD=$SQLCMDPASSWORD mcr.microsoft.com/mssql/server:${{ matrix.sqlImage }}
3131
sleep 10
32-
go test -v ./...
32+
go test -coverprofile=coverage.out -covermode=atomic -v ./...
33+
- name: Upload coverage to Codecov
34+
uses: codecov/codecov-action@v4
35+
with:
36+
files: coverage.out
37+
flags: unittests
38+
name: go-${{ matrix.go }}-sql-${{ matrix.sqlImage }}
39+
fail_ci_if_error: false
40+
env:
41+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
42+

.github/workflows/release-please.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- main
7+
workflow_dispatch:
78

89
permissions:
910
contents: write
@@ -13,4 +14,7 @@ jobs:
1314
release-please:
1415
runs-on: ubuntu-latest
1516
steps:
16-
- uses: google-github-actions/release-please-action@v4
17+
- uses: googleapis/release-please-action@v4
18+
with:
19+
config-file: release-please-config.json
20+
manifest-file: .release-please-manifest.json

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@
66
*.log
77
*.swp
88
*~
9+
10+
# Coverage artifacts
911
coverage.json
1012
coverage.txt
1113
coverage.xml
14+
coverage.out
15+
*.out
16+
coverage/
17+
cov/
18+
cov*
19+
integ_cov*
20+
final.out
21+
coverage_all.txt
22+
1223
testresults.xml
1324
.azureconnstr
1425

CONTRIBUTING.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,33 @@ This project uses [Release Please](https://github.com/googleapis/release-please)
4343
-`feat!: remove deprecated connection parameters`
4444
-`chore: update dependencies`
4545
-`Update README` (should be `docs: update README`)
46-
-`Bug fix` (should be `fix: <description>`)
46+
-`Bug fix` (should be `fix: <description>`)
47+
48+
## Code Coverage Requirements
49+
50+
This project enforces a **strict 80% minimum code coverage** requirement to maintain code quality and ensure inclusion in the [awesome-go](https://github.com/avelino/awesome-go) directory.
51+
52+
### Requirements
53+
54+
- **Project coverage**: Must stay at or above 80%
55+
- **Patch coverage**: New code in PRs must be at least 80% covered
56+
57+
### Checking Coverage Locally
58+
59+
```bash
60+
# Run tests with coverage
61+
go test -coverprofile=coverage.out ./...
62+
63+
# View total coverage
64+
go tool cover -func=coverage.out | tail -1
65+
66+
# Generate HTML coverage report
67+
go tool cover -html=coverage.out -o coverage.html
68+
```
69+
70+
### Tips for Maintaining Coverage
71+
72+
1. Write unit tests for new functions and methods
73+
2. Test error paths, not just happy paths
74+
3. Use table-driven tests for comprehensive coverage
75+
4. Check coverage before submitting PRs

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Go Reference](https://pkg.go.dev/badge/github.com/microsoft/go-mssqldb.svg)](https://pkg.go.dev/github.com/microsoft/go-mssqldb)
44
[![Build status](https://ci.appveyor.com/api/projects/status/jrln8cs62wj9i0a2?svg=true)](https://ci.appveyor.com/project/microsoft/go-mssqldb)
5-
[![codecov](https://codecov.io/gh/microsoft/go-mssqldb/branch/master/graph/badge.svg)](https://codecov.io/gh/microsoft/go-mssqldb)
5+
[![codecov](https://codecov.io/gh/microsoft/go-mssqldb/branch/main/graph/badge.svg)](https://codecov.io/gh/microsoft/go-mssqldb)
66

77

88
## Install

accesstokenconnector_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"fmt"
1111
"strings"
1212
"testing"
13+
14+
"github.com/stretchr/testify/assert"
1315
)
1416

1517
func TestNewAccessTokenConnector(t *testing.T) {
@@ -91,3 +93,27 @@ func TestAccessTokenConnectorFailsToConnectIfNoAccessToken(t *testing.T) {
9193
t.Fatalf("expected error to contain %q, but got %q", errorText, err)
9294
}
9395
}
96+
97+
func TestNewAccessTokenConnector_TokenProviderCalled(t *testing.T) {
98+
// Test that the token provider is actually called and returns expected value
99+
dsn := "Server=server.database.windows.net;Database=db"
100+
expectedToken := "test-token-123"
101+
called := false
102+
103+
tp := func() (string, error) {
104+
called = true
105+
return expectedToken, nil
106+
}
107+
108+
connector, err := NewAccessTokenConnector(dsn, tp)
109+
assert.NoError(t, err, "NewAccessTokenConnector()")
110+
111+
c, ok := connector.(*Connector)
112+
assert.True(t, ok, "connector should be of type *Connector")
113+
114+
// Call the security token provider
115+
token, err := c.securityTokenProvider(context.Background())
116+
assert.NoError(t, err, "securityTokenProvider()")
117+
assert.True(t, called, "Token provider should be called")
118+
assert.Equal(t, expectedToken, token, "token value")
119+
}

0 commit comments

Comments
 (0)