Skip to content

Commit 7c8b9e9

Browse files
committed
Separate testdata and add parsing test
1 parent 5aba679 commit 7c8b9e9

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2025 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
package crds_test
17+
18+
import (
19+
"bufio"
20+
"bytes"
21+
"testing"
22+
23+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/autogen/translate/crds"
24+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/autogen/translate/testdata"
25+
"github.com/stretchr/testify/assert"
26+
"github.com/stretchr/testify/require"
27+
)
28+
29+
func TestParse(t *testing.T) {
30+
scanner := bufio.NewScanner(bytes.NewBuffer(testdata.SampleCRDs))
31+
for _ = range 2 { // CRDs sample file has at least 2 CRDs
32+
def, err := crds.Parse(scanner)
33+
require.NoError(t, err)
34+
assert.NotNil(t, def)
35+
}
36+
}
File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package testdata
16+
17+
import _ "embed"
18+
19+
//go:embed crds.yaml
20+
var SampleCRDs []byte

internal/autogen/translate/translate_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package translate_test
1818
import (
1919
"bufio"
2020
"bytes"
21-
_ "embed"
2221
"fmt"
2322
"testing"
2423
"time"
@@ -35,6 +34,7 @@ import (
3534
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/autogen/translate/crds"
3635
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/autogen/translate/refs"
3736
v1 "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/autogen/translate/samples/v1"
37+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/autogen/translate/testdata"
3838
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
3939
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/k8s"
4040
)
@@ -47,9 +47,6 @@ const (
4747
testProjectID = "6098765432109876"
4848
)
4949

50-
//go:embed samples/crds.yaml
51-
var crdsYAMLBytes []byte
52-
5350
func TestFromAPI(t *testing.T) {
5451
for _, tc := range []struct {
5552
name string
@@ -413,7 +410,7 @@ func TestFromAPI(t *testing.T) {
413410
}
414411

415412
func testFromAPI[S any, T any, P refs.PtrClientObj[T]](t *testing.T, kind string, target P, input *S, want []client.Object) {
416-
crdsYML := bytes.NewBuffer(crdsYAMLBytes)
413+
crdsYML := bytes.NewBuffer(testdata.SampleCRDs)
417414
crd, err := extractCRD(kind, bufio.NewScanner(crdsYML))
418415
require.NoError(t, err)
419416
tr, err := translate.NewTranslator(crd, version, sdkVersion)
@@ -583,7 +580,7 @@ func TestToAPIAllRefs(t *testing.T) {
583580
},
584581
} {
585582
t.Run(tc.name, func(t *testing.T) {
586-
crdsYML := bytes.NewBuffer(crdsYAMLBytes)
583+
crdsYML := bytes.NewBuffer(testdata.SampleCRDs)
587584
crd, err := extractCRD(tc.crd, bufio.NewScanner(crdsYML))
588585
require.NoError(t, err)
589586
tr, err := translate.NewTranslator(crd, version, sdkVersion)
@@ -2026,7 +2023,7 @@ func TestToAPI(t *testing.T) {
20262023
}
20272024

20282025
func testToAPI[T any](t *testing.T, kind string, input client.Object, objs []client.Object, target, want *T) {
2029-
crdsYML := bytes.NewBuffer(crdsYAMLBytes)
2026+
crdsYML := bytes.NewBuffer(testdata.SampleCRDs)
20302027
crd, err := extractCRD(kind, bufio.NewScanner(crdsYML))
20312028
require.NoError(t, err)
20322029
tr, err := translate.NewTranslator(crd, version, sdkVersion)

0 commit comments

Comments
 (0)