|
| 1 | +package usage |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "slices" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + "github.com/openmcp-project/controller-utils/pkg/logging" |
| 10 | + |
| 11 | + k8s "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("Database Module", func() { |
| 15 | + BeforeEach(func() { |
| 16 | + os.Setenv("USAGE_DB_PATH", "") |
| 17 | + }) |
| 18 | + Context("When getting the Database", func() { |
| 19 | + It("should return a database entity", func() { |
| 20 | + _, err := GetDB() |
| 21 | + Ω(err).Should(Succeed()) |
| 22 | + }) |
| 23 | + }) |
| 24 | + Context("When initializing the database", func() { |
| 25 | + It("should successfully initialize the database correctly", func(ctx SpecContext) { |
| 26 | + log, err := logging.GetLogger() |
| 27 | + Ω(err).Should(Succeed()) |
| 28 | + |
| 29 | + err = InitDB(ctx, &log) |
| 30 | + Ω(err).Should(Succeed()) |
| 31 | + }) |
| 32 | + It("should have created the tables correctly", func(ctx SpecContext) { |
| 33 | + db, err := GetDB() |
| 34 | + Ω(err).Should(Succeed()) |
| 35 | + |
| 36 | + query := "SELECT distinct(table_name) FROM information_schema.tables" |
| 37 | + rows, err := db.Query(query) |
| 38 | + Ω(err).Should(Succeed()) |
| 39 | + |
| 40 | + expectedTables := []string{"hourly_usage", "mcp"} |
| 41 | + for rows.Next() { |
| 42 | + var tableName string |
| 43 | + err = rows.Scan(&tableName) |
| 44 | + Ω(err).Should(Succeed()) |
| 45 | + |
| 46 | + Ω(len(expectedTables)).ShouldNot(Equal(0)) |
| 47 | + expectedTables = slices.DeleteFunc(expectedTables, func(expected string) bool { |
| 48 | + return expected == tableName |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + err = db.Close() |
| 53 | + Ω(err).Should(Succeed()) |
| 54 | + }) |
| 55 | + |
| 56 | + }) |
| 57 | + |
| 58 | + var resourceEntry HourlyUsageEntry |
| 59 | + var expectedResourceName = "test-project-test-workspace-test-mcp" |
| 60 | + Context("Database Entities", Ordered, func() { |
| 61 | + BeforeAll(func() { |
| 62 | + resourceEntry = HourlyUsageEntry{ |
| 63 | + Project: "test-project", |
| 64 | + Workspace: "test-workspace", |
| 65 | + Name: "test-mcp", |
| 66 | + } |
| 67 | + }) |
| 68 | + It("should return the correct ResourceName", func() { |
| 69 | + Ω(resourceEntry.ResourceName()).Should(Equal(expectedResourceName)) |
| 70 | + }) |
| 71 | + It("should return the correct ObjectKey", func() { |
| 72 | + Ω(resourceEntry.ObjectKey()).Should(Equal(k8s.ObjectKey{ |
| 73 | + Name: expectedResourceName, |
| 74 | + })) |
| 75 | + }) |
| 76 | + }) |
| 77 | +}) |
0 commit comments