Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [Add documentation for generated client](https://github.com/ballerina-platform/ballerina-library/issues/8467)
- [Support eager loading for get all methods in generated client](https://github.com/ballerina-platform/ballerina-library/issues/8488)
- [Add support for selecting tables in ballerina persist pull command](https://github.com/ballerina-platform/ballerina-library/issues/8468)
- [Add support for individual init parameters support in persist clients](https://github.com/ballerina-platform/ballerina-library/issues/8505)

### Changed
- [Fix an SQL script generation order issue when there are multiple associations](https://github.com/ballerina-platform/ballerina-library/issues/7921)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,56 @@ public void testAddWithEagerLoadingUnsupportedDatastore() throws ClassNotFoundEx
assertGeneratedSources("tool_test_add_21");
}

@Test
@Description("Test add command with --with-init-params flag for MySQL datastore")
public void testAddWithInitParamsMySQL() throws ClassNotFoundException, NoSuchMethodException,
InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> persistClass = Class.forName("io.ballerina.persist.cmd.Add");
Add persistCmd = (Add) persistClass.getDeclaredConstructor(String.class).newInstance(Paths
.get(GENERATED_SOURCES_DIRECTORY, "tool_test_add_22_init_params_mysql").toAbsolutePath().toString());
new CommandLine(persistCmd).parseArgs("--datastore", "mysql", "--module", "entities", "--with-init-params");
persistCmd.execute();
assertGeneratedSources("tool_test_add_22_init_params_mysql");
}

@Test
@Description("Test add command with --with-init-params flag for PostgreSQL datastore")
public void testAddWithInitParamsPostgreSQL() throws ClassNotFoundException, NoSuchMethodException,
InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> persistClass = Class.forName("io.ballerina.persist.cmd.Add");
Add persistCmd = (Add) persistClass.getDeclaredConstructor(String.class)
.newInstance(Paths.get(GENERATED_SOURCES_DIRECTORY, "tool_test_add_23_init_params_postgresql")
.toAbsolutePath().toString());
new CommandLine(persistCmd).parseArgs("--datastore", "postgresql", "--module", "entities",
"--with-init-params");
persistCmd.execute();
assertGeneratedSources("tool_test_add_23_init_params_postgresql");
}

@Test
@Description("Test add command with --with-init-params flag for MSSQL datastore")
public void testAddWithInitParamsMSSQL() throws ClassNotFoundException, NoSuchMethodException,
InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> persistClass = Class.forName("io.ballerina.persist.cmd.Add");
Add persistCmd = (Add) persistClass.getDeclaredConstructor(String.class).newInstance(Paths
.get(GENERATED_SOURCES_DIRECTORY, "tool_test_add_24_init_params_mssql").toAbsolutePath().toString());
new CommandLine(persistCmd).parseArgs("--datastore", "mssql", "--module", "entities", "--with-init-params");
persistCmd.execute();
assertGeneratedSources("tool_test_add_24_init_params_mssql");
}

@Test
@Description("Test add command with --with-init-params flag for H2 datastore")
public void testAddWithInitParamsH2() throws ClassNotFoundException, NoSuchMethodException,
InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> persistClass = Class.forName("io.ballerina.persist.cmd.Add");
Add persistCmd = (Add) persistClass.getDeclaredConstructor(String.class).newInstance(
Paths.get(GENERATED_SOURCES_DIRECTORY, "tool_test_add_25_init_params_h2").toAbsolutePath().toString());
new CommandLine(persistCmd).parseArgs("--datastore", "h2", "--module", "entities", "--with-init-params");
persistCmd.execute();
assertGeneratedSources("tool_test_add_25_init_params_h2");
}

private void executeCommand(String subDir) {
Class<?> persistClass;
Path sourcePath = Paths.get(GENERATED_SOURCES_DIRECTORY, subDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,80 @@ public void testGenerateWithEagerLoadingMSSQL() {
assertGeneratedSources("tool_test_generate_117");
}

@Test(enabled = true)
@Description("Test generate command with --with-init-params flag for MySQL datastore")
public void testGenerateWithInitParamsMySQL() {
updateOutputBallerinaToml("tool_test_generate_118_init_params_mysql");
executeGenerateCommand("tool_test_generate_118_init_params_mysql",
"--datastore", "mysql", "--module", "entities", "--with-init-params");
assertGeneratedSources("tool_test_generate_118_init_params_mysql");
}

@Test(enabled = true)
@Description("Test generate command with --with-init-params flag for PostgreSQL datastore")
public void testGenerateWithInitParamsPostgreSQL() {
updateOutputBallerinaToml("tool_test_generate_119_init_params_postgresql");
executeGenerateCommand("tool_test_generate_119_init_params_postgresql",
"--datastore", "postgresql", "--module", "entities", "--with-init-params");
assertGeneratedSources("tool_test_generate_119_init_params_postgresql");
}

@Test(enabled = true)
@Description("Test generate command with --with-init-params flag for MSSQL datastore")
public void testGenerateWithInitParamsMSSQL() {
updateOutputBallerinaToml("tool_test_generate_120_init_params_mssql");
executeGenerateCommand("tool_test_generate_120_init_params_mssql",
"--datastore", "mssql", "--module", "entities", "--with-init-params");
assertGeneratedSources("tool_test_generate_120_init_params_mssql");
}

@Test(enabled = true)
@Description("Test generate command with --with-init-params flag for H2 datastore")
public void testGenerateWithInitParamsH2() {
updateOutputBallerinaToml("tool_test_generate_121_init_params_h2");
executeGenerateCommand("tool_test_generate_121_init_params_h2",
"--datastore", "h2", "--module", "entities", "--with-init-params");
assertGeneratedSources("tool_test_generate_121_init_params_h2");
}

@Test(enabled = true)
@Description("Test generate command with --with-init-params flag for MySQL with relationships")
public void testGenerateWithInitParamsRelationships() {
updateOutputBallerinaToml("tool_test_generate_122_init_params_relationships");
executeGenerateCommand("tool_test_generate_122_init_params_relationships",
"--datastore", "mysql", "--module", "entities", "--with-init-params");
assertGeneratedSources("tool_test_generate_122_init_params_relationships");
}

@Test(enabled = true)
@Description("Test generate command with --with-init-params flag for PostgreSQL with multiple entities")
public void testGenerateWithInitParamsMultipleEntities() {
updateOutputBallerinaToml("tool_test_generate_123_init_params_multiple_entities");
executeGenerateCommand("tool_test_generate_123_init_params_multiple_entities",
"--datastore", "postgresql", "--module", "entities", "--with-init-params");
assertGeneratedSources("tool_test_generate_123_init_params_multiple_entities");
}

@Test(enabled = true)
@Description("Test generate command with --with-init-params flag when Config.toml exists " +
"(should ignore Config.toml)")
public void testGenerateWithInitParamsWithConfig() {
updateOutputBallerinaToml("tool_test_generate_124_init_params_with_config");
executeGenerateCommand("tool_test_generate_124_init_params_with_config",
"--datastore", "mysql", "--module", "entities", "--with-init-params");
assertGeneratedSources("tool_test_generate_124_init_params_with_config");
}

@Test(enabled = true)
@Description("Test generate command with --with-init-params flag with invalid model (missing readonly on key)")
public void testGenerateWithInitParamsInvalidModel() {
updateOutputBallerinaToml("tool_test_generate_125_init_params_negative_invalid_model");
assertGeneratedSourcesNegative("tool_test_generate_125_init_params_negative_invalid_model", GENERATE,
new String[]{"modules/entities/persist_client.bal", "modules/entities/persist_types.bal",
"modules/entities/script.sql"},
"--datastore", "mysql", "--module", "entities", "--with-init-params");
}

private void updateOutputBallerinaToml(String fileName) {
String tomlFileName = "Ballerina.toml";
Path filePath = Paths.get("src", "test", "resources", "test-src", "output", fileName, tomlFileName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_add_init_params_mysql"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type Product record {|
readonly int id;
string name;
decimal price;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_add_init_params_postgresql"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type Product record {|
readonly int id;
string name;
decimal price;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_add_init_params_mssql"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type Product record {|
readonly int id;
string name;
decimal price;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_add_init_params_h2"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type Product record {|
readonly int id;
string name;
decimal price;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_init_params_mysql"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type User record {|
readonly int id;
string name;
string email;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_init_params_postgresql"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type User record {|
readonly int id;
string name;
string email;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_init_params_mssql"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type User record {|
readonly int id;
string name;
string email;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_init_params_h2"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type User record {|
readonly int id;
string name;
string email;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_relationships_init_params"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import ballerina/persist as _;

type Customer record {|
readonly int id;
string name;
string email;
Order[] orders;
|};

type Order record {|
readonly int id;
string orderNumber;
decimal totalAmount;
Customer customer;
OrderItem[] items;
|};

type OrderItem record {|
readonly int id;
int quantity;
decimal price;
Order 'order;
Product product;
|};

type Product record {|
readonly int id;
string name;
string description;
decimal price;
OrderItem[] orderItems;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_multiple_entities"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import ballerina/persist as _;
import ballerina/time;

type Employee record {|
readonly string empNo;
string firstName;
string lastName;
time:Date birthDate;
string gender;
time:Date hireDate;
Department department;
Workspace? workspace;
|};

type Workspace record {|
readonly string workspaceId;
string workspaceType;
Building location;
Employee employee;
|};

type Building record {|
readonly string buildingCode;
string city;
string state;
string country;
string postalCode;
Workspace[] workspaces;
|};

type Department record {|
readonly string deptNo;
string deptName;
Employee[] employees;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_init_params_with_config"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This Config.toml should be ignored when --with-init-params is used
[test.persist_init_params_with_config]
host = "localhost"
port = 3306
user = "root"
password = "root"
database = "testdb"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type Product record {|
readonly int id;
string name;
decimal price;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "test"
name = "persist_init_params_negative_invalid"
version = "0.1.0"
distribution = "2201.13.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ballerina/persist as _;

// Invalid model - missing readonly on key field
type User record {|
int id;
string name;
string email;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
org = "test"
name = "persist_add_init_params_mysql"
version = "0.1.0"
distribution = "2201.13.0"

[[tool.persist]]
id = "generate-db-client"
targetModule = "persist_add_init_params_mysql.entities"
options.datastore = "mysql"
options.initParams = true
filePath = "persist/model.bal"


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/persist as _;

type Product record {|
readonly int id;
string name;
decimal price;
|};
Loading