|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +//go:build integration |
| 4 | + |
| 5 | +package executor |
| 6 | + |
| 7 | +import ( |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/mendixlabs/mxcli/mdl/ast" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRoundtripExportMapping_NoSchema(t *testing.T) { |
| 15 | + env := setupTestEnv(t) |
| 16 | + defer env.teardown() |
| 17 | + |
| 18 | + // First create the entity that will be exported |
| 19 | + if err := env.executeMDL(`CREATE ENTITY ` + testModule + `.EMPet { |
| 20 | + Id: Integer; |
| 21 | + Name: String(200); |
| 22 | +};`); err != nil { |
| 23 | + t.Fatalf("CREATE ENTITY failed: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + createMDL := `CREATE EXPORT MAPPING ` + testModule + `.ExportPetBasic { |
| 27 | + ` + testModule + `.EMPet -> Root { |
| 28 | + Id -> id (Integer); |
| 29 | + Name -> name (String); |
| 30 | + }; |
| 31 | +};` |
| 32 | + |
| 33 | + env.assertContains(createMDL, []string{ |
| 34 | + "EXPORT MAPPING", |
| 35 | + "ExportPetBasic", |
| 36 | + "EMPet", |
| 37 | + "Root", |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +func TestRoundtripExportMapping_WithJsonStructureRef(t *testing.T) { |
| 42 | + env := setupTestEnv(t) |
| 43 | + defer env.teardown() |
| 44 | + |
| 45 | + if err := env.executeMDL(`CREATE ENTITY ` + testModule + `.EMOrder { |
| 46 | + OrderId: Integer; |
| 47 | + Total: Decimal(10,2); |
| 48 | +};`); err != nil { |
| 49 | + t.Fatalf("CREATE ENTITY failed: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + if err := env.executeMDL(`CREATE JSON STRUCTURE ` + testModule + `.EMOrderJS |
| 53 | +FROM '{"orderId": 1, "total": 99.99}';`); err != nil { |
| 54 | + t.Fatalf("CREATE JSON STRUCTURE failed: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + createMDL := `CREATE EXPORT MAPPING ` + testModule + `.ExportOrder |
| 58 | + TO JSON STRUCTURE ` + testModule + `.EMOrderJS |
| 59 | +{ |
| 60 | + ` + testModule + `.EMOrder -> Root { |
| 61 | + OrderId -> orderId (Integer); |
| 62 | + Total -> total (Decimal); |
| 63 | + }; |
| 64 | +};` |
| 65 | + |
| 66 | + env.assertContains(createMDL, []string{ |
| 67 | + "EXPORT MAPPING", |
| 68 | + "ExportOrder", |
| 69 | + "TO JSON STRUCTURE", |
| 70 | + "EMOrder", |
| 71 | + "orderId", |
| 72 | + "total", |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +func TestRoundtripExportMapping_NullValueOption(t *testing.T) { |
| 77 | + env := setupTestEnv(t) |
| 78 | + defer env.teardown() |
| 79 | + |
| 80 | + if err := env.executeMDL(`CREATE ENTITY ` + testModule + `.EMNullPet { |
| 81 | + Id: Integer; |
| 82 | +};`); err != nil { |
| 83 | + t.Fatalf("CREATE ENTITY failed: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + if err := env.executeMDL(`CREATE EXPORT MAPPING ` + testModule + `.ExportNullPet |
| 87 | + NULL VALUES SendAsNil |
| 88 | +{ |
| 89 | + ` + testModule + `.EMNullPet -> Root { |
| 90 | + Id -> id (Integer); |
| 91 | + }; |
| 92 | +};`); err != nil { |
| 93 | + t.Fatalf("CREATE EXPORT MAPPING failed: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + out, err := env.describeMDL(`DESCRIBE EXPORT MAPPING ` + testModule + `.ExportNullPet;`) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("DESCRIBE failed: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + if !strings.Contains(out, "SendAsNil") { |
| 102 | + t.Errorf("expected 'SendAsNil' in DESCRIBE output:\n%s", out) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestRoundtripExportMapping_Drop(t *testing.T) { |
| 107 | + env := setupTestEnv(t) |
| 108 | + defer env.teardown() |
| 109 | + |
| 110 | + if err := env.executeMDL(`CREATE ENTITY ` + testModule + `.EMDropPet { |
| 111 | + Id: Integer; |
| 112 | +};`); err != nil { |
| 113 | + t.Fatalf("CREATE ENTITY failed: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + if err := env.executeMDL(`CREATE EXPORT MAPPING ` + testModule + `.ToDropEM { |
| 117 | + ` + testModule + `.EMDropPet -> Root { |
| 118 | + Id -> id (Integer); |
| 119 | + }; |
| 120 | +};`); err != nil { |
| 121 | + t.Fatalf("CREATE EXPORT MAPPING failed: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + if _, err := env.describeMDL(`DESCRIBE EXPORT MAPPING ` + testModule + `.ToDropEM;`); err != nil { |
| 125 | + t.Fatalf("export mapping should exist before DROP: %v", err) |
| 126 | + } |
| 127 | + |
| 128 | + if err := env.executeMDL(`DROP EXPORT MAPPING ` + testModule + `.ToDropEM;`); err != nil { |
| 129 | + t.Fatalf("DROP EXPORT MAPPING failed: %v", err) |
| 130 | + } |
| 131 | + |
| 132 | + if _, err := env.describeMDL(`DESCRIBE EXPORT MAPPING ` + testModule + `.ToDropEM;`); err == nil { |
| 133 | + t.Error("export mapping should not exist after DROP") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestRoundtripExportMapping_ShowAppearsInList(t *testing.T) { |
| 138 | + env := setupTestEnv(t) |
| 139 | + defer env.teardown() |
| 140 | + |
| 141 | + if err := env.executeMDL(`CREATE ENTITY ` + testModule + `.EMListPet { |
| 142 | + Id: Integer; |
| 143 | +};`); err != nil { |
| 144 | + t.Fatalf("CREATE ENTITY failed: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + if err := env.executeMDL(`CREATE EXPORT MAPPING ` + testModule + `.ListableEM { |
| 148 | + ` + testModule + `.EMListPet -> Root { |
| 149 | + Id -> id (Integer); |
| 150 | + }; |
| 151 | +};`); err != nil { |
| 152 | + t.Fatalf("CREATE EXPORT MAPPING failed: %v", err) |
| 153 | + } |
| 154 | + |
| 155 | + env.output.Reset() |
| 156 | + if err := env.executeMDL(`SHOW EXPORT MAPPINGS IN ` + testModule + `;`); err != nil { |
| 157 | + t.Fatalf("SHOW failed: %v", err) |
| 158 | + } |
| 159 | + |
| 160 | + if !strings.Contains(env.output.String(), "ListableEM") { |
| 161 | + t.Errorf("expected 'ListableEM' in SHOW output:\n%s", env.output.String()) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// --- MX Check --- |
| 166 | + |
| 167 | +func TestMxCheck_ExportMapping_Basic(t *testing.T) { |
| 168 | + if !mxCheckAvailable() { |
| 169 | + t.Skip("mx command not available") |
| 170 | + } |
| 171 | + |
| 172 | + env := setupTestEnv(t) |
| 173 | + defer env.teardown() |
| 174 | + |
| 175 | + if err := env.executeMDL(`CREATE ENTITY ` + testModule + `.MxCheckEMPet { |
| 176 | + Id: Integer; |
| 177 | + Name: String(200); |
| 178 | +};`); err != nil { |
| 179 | + t.Fatalf("CREATE ENTITY failed: %v", err) |
| 180 | + } |
| 181 | + |
| 182 | + if err := env.executeMDL(`CREATE EXPORT MAPPING ` + testModule + `.MxCheckExportPet { |
| 183 | + ` + testModule + `.MxCheckEMPet -> Root { |
| 184 | + Id -> id (Integer); |
| 185 | + Name -> name (String); |
| 186 | + }; |
| 187 | +};`); err != nil { |
| 188 | + t.Fatalf("CREATE EXPORT MAPPING failed: %v", err) |
| 189 | + } |
| 190 | + |
| 191 | + env.executor.Execute(&ast.DisconnectStmt{}) |
| 192 | + |
| 193 | + output, err := runMxCheck(t, env.projectPath) |
| 194 | + assertMxCheckPassed(t, output, err) |
| 195 | +} |
0 commit comments