3
0

Post test tweaks and fixes

This commit is contained in:
Tomaž Jerman
2023-06-23 15:32:41 +02:00
parent 7c6fb592bc
commit 3eb709b90a
11 changed files with 331 additions and 100 deletions

View File

@@ -81,40 +81,31 @@ func (aa AlterationSet) Merge(bb AlterationSet) (cc AlterationSet) {
// * attribute A renamed to A' and then renamed to A''
// * attribute A deleted and then created
cc = make(AlterationSet, 0, len(aa)+len(bb))
skip := make(map[int]bool, (len(aa)+len(bb))/2)
aux := append(aa, bb...)
seen := make(map[int]bool, len(aux)/2)
// For each item in aa, check if it has a matching element in bb.
// If it does, mark the bb index as skipped, if it doesn't use the aa element.
// If a duplicate is found, the bb element is used (considered newer),
//
// This is sub-optimal but the slices are expected to be small and this
// won't be ran often.
for _, a := range aa {
found := false
for j, b := range bb {
if skip[j] {
continue
}
if a.compare(*b) {
skip[j] = true
cc = append(cc, b)
found = true
break
}
}
if !found {
cc = append(cc, a)
}
}
for j, b := range bb {
if skip[j] {
for i, a := range aux {
if seen[i] {
continue
}
cc = append(cc, b)
found := false
for j := i + 1; j < len(aux); j++ {
if seen[j] {
continue
}
if a.compare(*aux[j]) {
seen[j] = true
found = true
cc = append(cc, aux[j])
break
}
}
if !found {
cc = append(cc, a)
}
}
return

View File

@@ -69,6 +69,38 @@ func TestMerge(t *testing.T) {
},
},
},
{
name: "remove duplicates",
aa: AlterationSet{},
bb: AlterationSet{
&Alteration{
AttributeAdd: &AttributeAdd{
Attr: &Attribute{
Ident: "foo",
Type: &TypeJSON{Nullable: false},
},
},
},
&Alteration{
AttributeAdd: &AttributeAdd{
Attr: &Attribute{
Ident: "foo",
Type: &TypeJSON{Nullable: false},
},
},
},
},
cc: AlterationSet{
&Alteration{
AttributeAdd: &AttributeAdd{
Attr: &Attribute{
Ident: "foo",
Type: &TypeJSON{Nullable: false},
},
},
},
},
},
{
name: "un matching types",
aa: AlterationSet{

View File

@@ -1,29 +1,24 @@
package dal
type (
modelDiffType string
ModelModification string
modelDiffType string
// ModelDiff defines one identified missmatch between two models
ModelDiff struct {
Type modelDiffType
Modification ModelModification
Type modelDiffType
// Original will be nil when a new attribute is being added
Original *Attribute
// Inserted will be nil wen an existing attribute is being removed
Inserted *Attribute
// Asserted will be nil wen an existing attribute is being removed
Asserted *Attribute
}
ModelDiffSet []*ModelDiff
)
const (
AttributeMissing modelDiffType = "attributeMissing"
AttributeTypeMissmatch modelDiffType = "typeMissmatch"
AttributeSensitivityMismatch modelDiffType = "sensitivityMismatch"
AttributeCodecMismatch modelDiffType = "codecMismatch"
AttributeDeleted ModelModification = "deleted"
AttributeAdded ModelModification = "added"
AttributeChanged ModelModification = "changed"
AttributeMissing modelDiffType = "attributeMissing"
AttributeTypeMissmatch modelDiffType = "typeMissmatch"
AttributeSensitivityMismatch modelDiffType = "sensitivityMismatch"
AttributeCodecMismatch modelDiffType = "codecMismatch"
)
// Diff calculates the diff between models a and b where a is used as base
@@ -75,9 +70,8 @@ func (a *Model) Diff(b *Model) (out ModelDiffSet) {
attrBAux, ok := bIndex[attrA.Ident]
if !ok {
out = append(out, &ModelDiff{
Type: AttributeMissing,
Modification: AttributeDeleted,
Original: attrA,
Type: AttributeMissing,
Original: attrA,
})
continue
}
@@ -85,10 +79,9 @@ func (a *Model) Diff(b *Model) (out ModelDiffSet) {
// Typecheck
if attrA.Type.Type() != attrBAux.attr.Type.Type() {
out = append(out, &ModelDiff{
Type: AttributeTypeMissmatch,
Modification: AttributeChanged,
Original: attrA,
Inserted: attrBAux.attr,
Type: AttributeTypeMissmatch,
Original: attrA,
Asserted: attrBAux.attr,
})
}
@@ -96,18 +89,16 @@ func (a *Model) Diff(b *Model) (out ModelDiffSet) {
// @todo improve; for now it'll do
if attrA.SensitivityLevelID != attrBAux.attr.SensitivityLevelID {
out = append(out, &ModelDiff{
Type: AttributeSensitivityMismatch,
Modification: AttributeChanged,
Original: attrA,
Inserted: attrBAux.attr,
Type: AttributeSensitivityMismatch,
Original: attrA,
Asserted: attrBAux.attr,
})
}
if attrA.Store.Type() != attrBAux.attr.Store.Type() {
out = append(out, &ModelDiff{
Type: AttributeCodecMismatch,
Modification: AttributeChanged,
Original: attrA,
Inserted: attrBAux.attr,
Type: AttributeCodecMismatch,
Original: attrA,
Asserted: attrBAux.attr,
})
}
}
@@ -120,10 +111,9 @@ func (a *Model) Diff(b *Model) (out ModelDiffSet) {
_, ok := aIndex[attrB.Ident]
if !ok {
out = append(out, &ModelDiff{
Type: AttributeMissing,
Modification: AttributeAdded,
Original: nil,
Inserted: attrB,
Type: AttributeMissing,
Original: nil,
Asserted: attrB,
})
continue
}
@@ -140,7 +130,7 @@ func (dd ModelDiffSet) Alterations() (out []*Alteration) {
for _, d := range dd {
switch d.Type {
case AttributeMissing:
if d.Inserted == nil {
if d.Asserted == nil {
// @todo if this was the last attribute we can consider dropping this column
if d.Original.Store.Type() == AttributeCodecRecordValueSetJSON {
break
@@ -152,11 +142,11 @@ func (dd ModelDiffSet) Alterations() (out []*Alteration) {
},
})
} else {
if d.Inserted.Store.Type() == AttributeCodecRecordValueSetJSON {
if d.Asserted.Store.Type() == AttributeCodecRecordValueSetJSON {
add(&Alteration{
AttributeAdd: &AttributeAdd{
Attr: &Attribute{
Ident: d.Inserted.StoreIdent(),
Ident: d.Asserted.StoreIdent(),
Type: &TypeJSON{Nullable: false},
Store: &CodecPlain{},
},
@@ -165,7 +155,7 @@ func (dd ModelDiffSet) Alterations() (out []*Alteration) {
} else {
add(&Alteration{
AttributeAdd: &AttributeAdd{
Attr: d.Inserted,
Attr: d.Asserted,
},
})
}
@@ -180,16 +170,16 @@ func (dd ModelDiffSet) Alterations() (out []*Alteration) {
add(&Alteration{
AttributeReType: &AttributeReType{
Attr: d.Inserted,
To: d.Inserted.Type,
Attr: d.Asserted,
To: d.Asserted.Type,
},
})
case AttributeCodecMismatch:
add(&Alteration{
AttributeReEncode: &AttributeReEncode{
Attr: d.Inserted,
To: d.Inserted.Store,
Attr: d.Asserted,
To: d.Asserted.Store,
},
})
}

View File

@@ -38,7 +38,6 @@ func TestDiff_wrongAttrType(t *testing.T) {
dd := a.Diff(b)
require.Len(t, dd, 1)
require.Equal(t, AttributeTypeMissmatch, dd[0].Type)
require.Equal(t, AttributeChanged, dd[0].Modification)
}
func TestDiff_removedAttr(t *testing.T) {
@@ -64,9 +63,8 @@ func TestDiff_removedAttr(t *testing.T) {
dd := a.Diff(b)
require.Len(t, dd, 1)
require.Equal(t, AttributeMissing, dd[0].Type)
require.Equal(t, AttributeDeleted, dd[0].Modification)
require.NotNil(t, dd[0].Original)
require.Nil(t, dd[0].Inserted)
require.Nil(t, dd[0].Asserted)
}
func TestDiff_addedAttr(t *testing.T) {
@@ -92,9 +90,8 @@ func TestDiff_addedAttr(t *testing.T) {
dd := a.Diff(b)
require.Len(t, dd, 1)
require.Equal(t, AttributeMissing, dd[0].Type)
require.Equal(t, AttributeAdded, dd[0].Modification)
require.Nil(t, dd[0].Original)
require.NotNil(t, dd[0].Inserted)
require.NotNil(t, dd[0].Asserted)
}
func TestDiff_changedCodec(t *testing.T) {
@@ -116,5 +113,4 @@ func TestDiff_changedCodec(t *testing.T) {
dd := a.Diff(b)
require.Len(t, dd, 1)
require.Equal(t, AttributeCodecMismatch, dd[0].Type)
require.Equal(t, AttributeChanged, dd[0].Modification)
}