mattermost/server/public/model/map_test.go
Jesse Hallam 67568d558f
Some checks are pending
API / build (push) Waiting to run
Server CI / Compute Go Version (push) Waiting to run
Server CI / Check mocks (push) Blocked by required conditions
Server CI / Check go mod tidy (push) Blocked by required conditions
Server CI / check-style (push) Blocked by required conditions
Server CI / Check serialization methods for hot structs (push) Blocked by required conditions
Server CI / Vet API (push) Blocked by required conditions
Server CI / Check migration files (push) Blocked by required conditions
Server CI / Generate email templates (push) Blocked by required conditions
Server CI / Check store layers (push) Blocked by required conditions
Server CI / Check mmctl docs (push) Blocked by required conditions
Server CI / Postgres with binary parameters (push) Blocked by required conditions
Server CI / Postgres (push) Blocked by required conditions
Server CI / Postgres (FIPS) (push) Blocked by required conditions
Server CI / Generate Test Coverage (push) Blocked by required conditions
Server CI / Run mmctl tests (push) Blocked by required conditions
Server CI / Run mmctl tests (FIPS) (push) Blocked by required conditions
Server CI / Build mattermost server app (push) Blocked by required conditions
Web App CI / check-lint (push) Waiting to run
Web App CI / check-i18n (push) Waiting to run
Web App CI / check-types (push) Waiting to run
Web App CI / test (push) Waiting to run
Web App CI / build (push) Waiting to run
Introduce model.AssertNotSameMap (#34058)
When we last bumped dependencies in https://github.com/mattermost/mattermost/pull/30005, `assert.NotSame` for maps started failing because of the change in https://github.com/stretchr/testify/issues/1661. The reality was that the previous assertion was silently skipped, and just now reporting as much.

Here's an illustrative example:
```go
package main

import (
	"maps"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestClonedMapsAreNotSame(t *testing.T) {
	original := map[string]int{
		"a": 1,
		"b": 2,
		"c": 3,
	}

	cloned := maps.Clone(original)

	assert.NotSame(t, original, cloned)
}

func TestSameMaps(t *testing.T) {
	original := map[string]int{
		"a": 1,
		"b": 2,
		"c": 3,
	}

	cloned := original
	assert.Same(t, original, cloned)

	cloned["d"] = 4
	assert.Same(t, original, cloned)
}
```

which fails with the following after the original dependency update:
```
--- FAIL: TestClonedMapsAreNotSame (0.00s)
    main_test.go:19:
                Error Trace:    /Users/jesse/tmp/testify/main_test.go:19
                Error:          Both arguments must be pointers
                Test:           TestClonedMapsAreNotSame
--- FAIL: TestSameMaps (0.00s)
    main_test.go:30:
                Error Trace:    /Users/jesse/tmp/testify/main_test.go:30
                Error:          Both arguments must be pointers
                Test:           TestSameMaps
    main_test.go:33:
                Error Trace:    /Users/jesse/tmp/testify/main_test.go:33
                Error:          Both arguments must be pointers
                Test:           TestSameMaps
FAIL
FAIL    testassertequal 0.149s
FAIL
```

However, instead of fixing the underlying issue, we took the address of those variables and kept using `assert.Same`. This isn't meaningful, since it doesn't directly compare the underlying pointers of the map objects in question, just the address of the pointers to those maps. Here's the output after taking the address (e.g. `&original` and `&cloned`):

```
--- FAIL: TestSameMaps (0.00s)
    main_test.go:30:
                Error Trace:    /Users/jesse/tmp/testify/main_test.go:30
                Error:          Not same:
                                expected: 0x14000070170 &map[string]int{"a":1, "b":2, "c":3}
                                actual  : 0x14000070178 &map[string]int{"a":1, "b":2, "c":3}
                Test:           TestSameMaps
    main_test.go:33:
                Error Trace:    /Users/jesse/tmp/testify/main_test.go:33
                Error:          Not same:
                                expected: 0x14000070170 &map[string]int{"a":1, "b":2, "c":3, "d":4}
                                actual  : 0x14000070178 &map[string]int{"a":1, "b":2, "c":3, "d":4}
                Test:           TestSameMaps
FAIL
FAIL    testassertequal 0.157s
FAIL
```

They are obviously the same map, since modifying `cloned` modified the
original, yet `assert.Same` thinks they are different (because the
pointe values are indeed different). (`assert.NotSame` "passes", but for
the wrong reasons.)

To fix this, introduce `model.AssertNotSameMap` to check this correctly.
2025-10-27 13:16:59 -03:00

54 lines
1.1 KiB
Go

package model
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestAssertNotSameMap(t *testing.T) {
t.Run("both nil maps are the same", func(t *testing.T) {
mockT := &testing.T{}
var a, b map[string]int
AssertNotSameMap(mockT, a, b)
require.True(t, mockT.Failed())
})
t.Run("one nil map is not the same", func(t *testing.T) {
mockT := &testing.T{}
var a map[string]int
b := map[string]int{"key1": 1}
AssertNotSameMap(mockT, a, b)
require.False(t, mockT.Failed())
})
t.Run("different maps should not be the same", func(t *testing.T) {
mockT := &testing.T{}
a := map[string]int{"key1": 1}
b := map[string]int{"key1": 1}
AssertNotSameMap(mockT, a, b)
require.False(t, mockT.Failed())
})
t.Run("same map should be the same", func(t *testing.T) {
mockT := &testing.T{}
a := map[string]int{"key1": 1}
b := a
AssertNotSameMap(mockT, a, b)
require.True(t, mockT.Failed())
})
t.Run("same map should be the same after modification", func(t *testing.T) {
mockT := &testing.T{}
a := map[string]int{"key1": 1}
b := a
b["key2"] = 2
AssertNotSameMap(mockT, a, b)
require.True(t, mockT.Failed())
})
}