use Exec instead of Query for inserts and assert every insert error

This commit is contained in:
pascal
2026-08-05 16:33:48 +02:00
parent c81103dfa6
commit ec3e37456e
3 changed files with 25 additions and 19 deletions

View File

@@ -13,7 +13,7 @@ import (
func TestGetAccountSettings(t *testing.T) {
ctx := context.TODO()
_, err := pgstore.Pool.Query(ctx,
_, err := pgstore.Pool.Exec(ctx,
`insert into accounts (id, settings_peer_login_expiration_enabled, settings_peer_login_expiration, settings_peer_inactivity_expiration_enabled,
settings_peer_inactivity_expiration, settings_dns_domain, settings_ipv6_enabled_groups, settings_routing_peer_dns_resolution_enabled,
settings_lazy_connection_enabled, settings_auto_update_version, settings_auto_update_always, settings_metrics_push_enabled)

View File

@@ -52,7 +52,7 @@ func TestGetGroupsWithoutExpectedFields(t *testing.T) {
"insert into accounts (id) VALUES($1)", acctId)
assert.NoError(t, err)
_, err = s.Pool.Query(ctx,
_, err = s.Pool.Exec(ctx,
"insert into groups (id, account_id) VALUES('g2-test-group-id-1',$1)", acctId)
assert.NoError(t, err)

View File

@@ -5,22 +5,23 @@ import (
"database/sql"
"testing"
networkmap_pgsql "github.com/netbirdio/netbird/management/internals/network_map_db/pgsql"
"github.com/stretchr/testify/assert"
networkmap_pgsql "github.com/netbirdio/netbird/management/internals/network_map_db/pgsql"
)
func TestGetPrivateServicesViaPgxConnection(t *testing.T) {
ctx := context.TODO()
_, err := pgstore.Pool.Query(ctx,
_, err := pgstore.Pool.Exec(ctx,
`insert into services (id, account_id, enabled, private, access_groups, proxy_cluster, domain)
values('service-1','account-1',true,true,'["group-one-resource-id"]','test-1.com','test-2.com')`)
assert.NoError(t, err)
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into services (id, account_id, enabled, private, access_groups, proxy_cluster, domain)
values('service-2','account-1',true,true,'["group-one-resource-id","group-two-resources-id"]','test-3.com','test-4.com')`)
assert.NoError(t, err)
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into services (id, account_id, enabled, private, access_groups, proxy_cluster, domain)
values('service-3','account-1',null,null,null,null,null)`)
assert.NoError(t, err)
@@ -53,56 +54,61 @@ func TestGetPrivateServicesViaPgxConnection(t *testing.T) {
func TestGetProxyTargetedDomainResourceIDsViaPgxConnection(t *testing.T) {
ctx := context.TODO()
_, err := pgstore.Pool.Query(ctx,
_, err := pgstore.Pool.Exec(ctx,
`insert into services (id, account_id, enabled, terminated)
values('service-4','account-1',true,false)`)
assert.NoError(t, err)
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into targets (target_id, account_id, service_id, enabled, target_type)
values('target-1','account-1','service-4',true,'domain')`)
assert.NoError(t, err)
// id shouldn't be returned as the taget_type is not "domain"
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into targets (target_id, account_id, service_id, enabled, target_type)
values('target-2','account-1','service-4',true,'cluster')`)
assert.NoError(t, err)
// id shouldn't be included as the target is disabled
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into targets (target_id, account_id, service_id, enabled, target_type)
values('target-3','account-1','service-4',false,'domain')`)
assert.NoError(t, err)
// id shouldn't be included as the service is disabled
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into services (id, account_id, enabled, terminated)
values('service-5','account-1',false,false)`)
_, err = pgstore.Pool.Query(ctx,
assert.NoError(t, err)
_, err = pgstore.Pool.Exec(ctx,
`insert into targets (target_id, account_id, service_id, enabled, target_type)
values('target-4','account-1','service-5',false,'domain')`)
assert.NoError(t, err)
// id shouldn't be included as the service is terminated (explicitly)
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into services (id, account_id, enabled, terminated)
values('service-6','account-1',true,true)`)
_, err = pgstore.Pool.Query(ctx,
assert.NoError(t, err)
_, err = pgstore.Pool.Exec(ctx,
`insert into targets (target_id, account_id, service_id, enabled, target_type)
values('target-5','account-1','service-6',true,'domain')`)
assert.NoError(t, err)
// id shouldn't be included as the service is terminated (implicitly)
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into services (id, account_id, enabled, terminated)
values('service-7','account-1',true,null)`)
_, err = pgstore.Pool.Query(ctx,
assert.NoError(t, err)
_, err = pgstore.Pool.Exec(ctx,
`insert into targets (target_id, account_id, service_id, enabled, target_type)
values('target-6','account-1','service-7',true,'domain')`)
assert.NoError(t, err)
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into services (id, account_id, enabled, terminated)
values('service-8','account-1',true,false)`)
_, err = pgstore.Pool.Query(ctx,
assert.NoError(t, err)
_, err = pgstore.Pool.Exec(ctx,
`insert into targets (target_id, account_id, service_id, enabled, target_type)
values('target-7','account-1','service-8',true,'domain')`)
assert.NoError(t, err)
// id shouldn't be returned as the taget_id is null
_, err = pgstore.Pool.Query(ctx,
_, err = pgstore.Pool.Exec(ctx,
`insert into targets (target_id, account_id, service_id, enabled, target_type)
values(null,'account-1','service-4',true,'cluster')`)
assert.NoError(t, err)