Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: bal test required to provide values to configurables which are not used in the testcases #41792

Closed
daneshk opened this issue Nov 29, 2023 · 4 comments
Assignees
Labels
Area/Configurable Runtime configurable related issues Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug

Comments

@daneshk
Copy link
Member

daneshk commented Nov 29, 2023

Description

The Ballerina project contains the initializeClient function which is used to initialize MySQL client.

final db:Client dbClient = check initializeClient();

function initializeClient() returns db:Client|error {
   return new ();
}

To initialize the client, we need to set the following set of configurable,

[mock_with_h2.db]
hostname = "localhost"
port1 = 3307
username = ""
pwd = ""
db = "hospitaldb"

In the test cases, we try to mock the initializeClient and mock the MySQL client with the H2 client like below,

@test:Mock {functionName: "initializeClient"}
isolated function getMockClient() returns db:Client|error {
    return test:mock(db:Client, check new Client("jdbc:h2:./test", "sa", "", options = {}));         
}

For the H2 client, we are setting the following values in the Config.toml file,

url = "jdbc:h2:./test"
user = "sa"
password = ""

When we add only H2 client-related configuration, the bal test fails and asks for values for the MySQL configuration which is not used in the test cases.

Steps to Reproduce

Project Source: https://github.com/daneshk/persist-test-samples/blob/main/mock_with_h2/tests/Config.toml

Affected Version(s)

Ballerina Swan Lake

@daneshk daneshk added Type/Bug Area/Configurable Runtime configurable related issues labels Nov 29, 2023
@ballerina-bot ballerina-bot added the Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime label Nov 29, 2023
@gabilang gabilang self-assigned this Dec 7, 2023
@gabilang
Copy link
Contributor

gabilang commented Dec 11, 2023

As per the current behavior, Ballerina test execution requires to have the config values inside the test module's Config.toml file as well regardless of whether those config values are used by the test code.
Consider the following scenario,
main.bal file

import ballerina/io;

configurable int intVar = ?;

public function main() {
    io:println(intVar);
}

Config.toml file

[bal_test_project]
intVar = 42

inside test module

import ballerina/test;

@test:Config{}
function test_main() {
}

If we are not providing the config value via the Config.toml file inside the test module, we will get an error: value not provided for required configurable variable 'intVar'

As per the current design, it's expected the config values are required for test execution even they need to be given for an imported module which is not actually required for the tests. We need to give some dummy values to make the things work.

@warunalakshitha
Copy link
Contributor

Since configurable variables are global and if they do not have any default values (mandatory configurable variable) they requires value to be assign through Config.toml during initialisation. Actually all config values will be used at program startup. As @gabilang mentioned we need to use some dummy values to initialise them.

@warunalakshitha
Copy link
Contributor

I am closing this issue this is the correct behaviour. Please feel free to reopen if you have further queries on this.

Copy link

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Configurable Runtime configurable related issues Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Projects
Archived in project
Development

No branches or pull requests

4 participants