Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Fix handling of empty local state in TestResourcesDataApp #4699

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
324 changes: 170 additions & 154 deletions ledger/accountdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ func TestCompactResourceDeltas(t *testing.T) {

func TestResourcesDataApp(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

a := require.New(t)

Expand Down Expand Up @@ -1326,161 +1327,176 @@ func TestResourcesDataApp(t *testing.T) {
a.Equal(appParamsEmpty, rd.GetAppParams())
a.Equal(appLocalEmpty, rd.GetAppLocalState())

// check empty states + non-empty params
appParams := ledgertesting.RandomAppParams()
rd = resourcesData{}
rd.SetAppLocalState(appLocalEmpty)
rd.SetAppParams(appParams, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())
a.Equal(appParams, rd.GetAppParams())
a.Equal(appLocalEmpty, rd.GetAppLocalState())

appState := ledgertesting.RandomAppLocalState()
rd.SetAppLocalState(appState)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())
a.Equal(appParams, rd.GetAppParams())
a.Equal(appState, rd.GetAppLocalState())

// check ClearAppLocalState
rd.ClearAppLocalState()
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.False(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())
a.Equal(appParams, rd.GetAppParams())
a.Equal(appLocalEmpty, rd.GetAppLocalState())

// check ClearAppParams
rd.SetAppLocalState(appState)
rd.ClearAppParams()
a.True(rd.IsApp())
a.False(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())
a.Equal(appParamsEmpty, rd.GetAppParams())
a.Equal(appState, rd.GetAppLocalState())

// check both clear
rd.ClearAppLocalState()
a.False(rd.IsApp())
a.False(rd.IsOwning())
a.False(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.True(rd.IsEmpty())
a.Equal(appParamsEmpty, rd.GetAppParams())
a.Equal(appLocalEmpty, rd.GetAppLocalState())

// check params clear when non-empty params and empty holding
rd = resourcesData{}
rd.SetAppLocalState(appLocalEmpty)
rd.SetAppParams(appParams, true)
rd.ClearAppParams()
a.True(rd.IsApp())
a.False(rd.IsOwning())
a.True(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())
a.Equal(appParamsEmpty, rd.GetAppParams())
a.Equal(appLocalEmpty, rd.GetAppLocalState())

rd = resourcesData{}
rd.SetAppLocalState(appLocalEmpty)
a.True(rd.IsEmptyAppFields())
a.True(rd.IsApp())
a.False(rd.IsEmpty())
a.Equal(rd.ResourceFlags, resourceFlagsEmptyApp)
rd.ClearAppLocalState()
a.False(rd.IsApp())
a.True(rd.IsEmptyAppFields())
a.True(rd.IsEmpty())
a.Equal(rd.ResourceFlags, resourceFlagsNotHolding)

// check migration flow (accountDataResources)
// 1. both exist and empty
rd = makeResourcesData(0)
rd.SetAppLocalState(appLocalEmpty)
rd.SetAppParams(appParamsEmpty, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 2. both exist and not empty
rd = makeResourcesData(0)
rd.SetAppLocalState(appState)
rd.SetAppParams(appParams, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 3. both exist: holding not empty, param is empty
rd = makeResourcesData(0)
rd.SetAppLocalState(appState)
rd.SetAppParams(appParamsEmpty, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 4. both exist: holding empty, param is not empty
rd = makeResourcesData(0)
rd.SetAppLocalState(appLocalEmpty)
rd.SetAppParams(appParams, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 5. holding does not exist and params is empty
rd = makeResourcesData(0)
rd.SetAppParams(appParamsEmpty, false)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.False(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 6. holding does not exist and params is not empty
rd = makeResourcesData(0)
rd.SetAppParams(appParams, false)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.False(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 7. holding exist and not empty and params does not exist
rd = makeResourcesData(0)
rd.SetAppLocalState(appState)
a.True(rd.IsApp())
a.False(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 8. both do not exist
rd = makeResourcesData(0)
a.False(rd.IsApp())
a.False(rd.IsOwning())
a.False(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.True(rd.IsEmpty())
// Since some steps use randomly generated input, the test is run N times
// to cover a larger search space of inputs.
for i := 0; i < 1000; i++ {
// check empty states + non-empty params
jannotti marked this conversation as resolved.
Show resolved Hide resolved
appParams := ledgertesting.RandomAppParams()
rd = resourcesData{}
rd.SetAppLocalState(appLocalEmpty)
rd.SetAppParams(appParams, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())
a.Equal(appParams, rd.GetAppParams())
a.Equal(appLocalEmpty, rd.GetAppLocalState())

appState := ledgertesting.RandomAppLocalState()
rd.SetAppLocalState(appState)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())
a.Equal(appParams, rd.GetAppParams())
a.Equal(appState, rd.GetAppLocalState())

// check ClearAppLocalState
rd.ClearAppLocalState()
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.False(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())
a.Equal(appParams, rd.GetAppParams())
a.Equal(appLocalEmpty, rd.GetAppLocalState())

// check ClearAppParams
rd.SetAppLocalState(appState)
rd.ClearAppParams()
a.True(rd.IsApp())
a.False(rd.IsOwning())
a.True(rd.IsHolding())
if appState.Schema.NumEntries() == 0 {
a.True(rd.IsEmptyAppFields())
} else {
a.False(rd.IsEmptyAppFields())
}
a.False(rd.IsEmpty())
a.Equal(appParamsEmpty, rd.GetAppParams())
a.Equal(appState, rd.GetAppLocalState())

// check both clear
rd.ClearAppLocalState()
a.False(rd.IsApp())
a.False(rd.IsOwning())
a.False(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.True(rd.IsEmpty())
a.Equal(appParamsEmpty, rd.GetAppParams())
a.Equal(appLocalEmpty, rd.GetAppLocalState())

// check params clear when non-empty params and empty holding
rd = resourcesData{}
rd.SetAppLocalState(appLocalEmpty)
rd.SetAppParams(appParams, true)
rd.ClearAppParams()
a.True(rd.IsApp())
a.False(rd.IsOwning())
a.True(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())
a.Equal(appParamsEmpty, rd.GetAppParams())
a.Equal(appLocalEmpty, rd.GetAppLocalState())

rd = resourcesData{}
rd.SetAppLocalState(appLocalEmpty)
a.True(rd.IsEmptyAppFields())
a.True(rd.IsApp())
a.False(rd.IsEmpty())
a.Equal(rd.ResourceFlags, resourceFlagsEmptyApp)
rd.ClearAppLocalState()
a.False(rd.IsApp())
a.True(rd.IsEmptyAppFields())
a.True(rd.IsEmpty())
a.Equal(rd.ResourceFlags, resourceFlagsNotHolding)

// check migration flow (accountDataResources)
// 1. both exist and empty
rd = makeResourcesData(0)
rd.SetAppLocalState(appLocalEmpty)
rd.SetAppParams(appParamsEmpty, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 2. both exist and not empty
rd = makeResourcesData(0)
rd.SetAppLocalState(appState)
rd.SetAppParams(appParams, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 3. both exist: holding not empty, param is empty
rd = makeResourcesData(0)
rd.SetAppLocalState(appState)
rd.SetAppParams(appParamsEmpty, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
if appState.Schema.NumEntries() == 0 {
a.True(rd.IsEmptyAppFields())
} else {
a.False(rd.IsEmptyAppFields())
}
a.False(rd.IsEmpty())

// 4. both exist: holding empty, param is not empty
rd = makeResourcesData(0)
rd.SetAppLocalState(appLocalEmpty)
rd.SetAppParams(appParams, true)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.True(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 5. holding does not exist and params is empty
rd = makeResourcesData(0)
rd.SetAppParams(appParamsEmpty, false)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.False(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 6. holding does not exist and params is not empty
rd = makeResourcesData(0)
rd.SetAppParams(appParams, false)
a.True(rd.IsApp())
a.True(rd.IsOwning())
a.False(rd.IsHolding())
a.False(rd.IsEmptyAppFields())
a.False(rd.IsEmpty())

// 7. holding exist and not empty and params does not exist
rd = makeResourcesData(0)
rd.SetAppLocalState(appState)
a.True(rd.IsApp())
a.False(rd.IsOwning())
a.True(rd.IsHolding())
if appState.Schema.NumEntries() == 0 {
a.True(rd.IsEmptyAppFields())
} else {
a.False(rd.IsEmptyAppFields())
}
a.False(rd.IsEmpty())

// 8. both do not exist
rd = makeResourcesData(0)
a.False(rd.IsApp())
a.False(rd.IsOwning())
a.False(rd.IsHolding())
a.True(rd.IsEmptyAppFields())
a.True(rd.IsEmpty())
}
}

func TestResourcesDataAsset(t *testing.T) {
Expand Down