Skip to content

Commit

Permalink
test(it): Add integration tests for test and coverage reports
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Aug 15, 2024
1 parent 265eba8 commit 22a7b1a
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,32 @@ class MetricsTest {
// Size
assertAll(
// Size
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "ncloc")).isEqualTo(22) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "lines")).isEqualTo(23) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "files")).isEqualTo(3) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "statements")).isEqualTo(4) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "ncloc")).isEqualTo(32) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "lines")).isEqualTo(33) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "files")).isEqualTo(4) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "statements")).isEqualTo(7) },
// Documentation
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "comment_lines")).isEqualTo(1) },
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "comment_lines_density")).isEqualTo(4.3) },
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "comment_lines_density")).isEqualTo(3.0) },
{ assertThat(getMeasureAsString(PROJECT_KEY, "public_documented_api_density")).isNull() },
// Duplication
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "duplicated_lines")).isZero },
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "duplicated_blocks")).isZero },
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "duplicated_files")).isZero },
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "duplicated_lines_density")).isZero },
// Rules
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "violations")).isZero }
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "violations")).isZero },
// Tests
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "tests")).isEqualTo(6) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "skipped_tests")).isEqualTo(1) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "test_failures")).isEqualTo(1) },
{ assertThat(getMeasureAsInteger(PROJECT_KEY, "test_errors")).isEqualTo(1) },
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "test_success_density")).isEqualTo(66.7) },
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "test_execution_time")).isEqualTo(15.0) },
// Coverage
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "coverage")).isEqualTo(50.0) },
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "line_coverage")).isEqualTo(50.0) },
{ assertThat(getMeasureAsDouble(PROJECT_KEY, "branch_coverage")).isNull() },
)
}

Expand All @@ -69,7 +80,58 @@ class MetricsTest {
{ assertThat(getMeasureAsInteger(FILE_NAME, "duplicated_files")).isZero },
{ assertThat(getMeasureAsDouble(FILE_NAME, "duplicated_lines_density")).isZero },
// Rules
{ assertThat(getMeasureAsInteger(FILE_NAME, "violations")).isZero }
{ assertThat(getMeasureAsInteger(FILE_NAME, "violations")).isZero },
)
}

@Test
fun covered_file() {
assertAll(
// Size
{ assertThat(getMeasureAsInteger(COVERED_FILE_NAME, "ncloc")).isEqualTo(10) },
{ assertThat(getMeasureAsInteger(COVERED_FILE_NAME, "lines")).isEqualTo(10) },
{ assertThat(getMeasureAsInteger(COVERED_FILE_NAME, "statements")).isEqualTo(3) },
// Documentation
{ assertThat(getMeasureAsInteger(COVERED_FILE_NAME, "comment_lines")).isZero },
{ assertThat(getMeasureAsDouble(COVERED_FILE_NAME, "comment_lines_density")).isZero },
// Duplication
{ assertThat(getMeasureAsInteger(COVERED_FILE_NAME, "duplicated_lines")).isZero },
{ assertThat(getMeasureAsInteger(COVERED_FILE_NAME, "duplicated_blocks")).isZero },
{ assertThat(getMeasureAsInteger(COVERED_FILE_NAME, "duplicated_files")).isZero },
{ assertThat(getMeasureAsDouble(COVERED_FILE_NAME, "duplicated_lines_density")).isZero },
// Rules
{ assertThat(getMeasureAsInteger(COVERED_FILE_NAME, "violations")).isZero },
// Coverage
{ assertThat(getMeasureAsDouble(COVERED_FILE_NAME, "coverage")).isEqualTo(100.0) },
{ assertThat(getMeasureAsDouble(COVERED_FILE_NAME, "line_coverage")).isEqualTo(100.0) },
{ assertThat(getMeasureAsDouble(COVERED_FILE_NAME, "branch_coverage")).isNull() },
)
}

@Test
fun test_file() {
assertAll(
// Size
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "ncloc")).isNull() },
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "lines")).isNull() },
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "statements")).isNull() },
// Documentation
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "comment_lines")).isNull() },
{ assertThat(getMeasureAsDouble(TEST_FILE_NAME, "comment_lines_density")).isNull() },
// Duplication
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "duplicated_lines")).isZero },
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "duplicated_blocks")).isZero },
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "duplicated_files")).isZero },
{ assertThat(getMeasureAsDouble(TEST_FILE_NAME, "duplicated_lines_density")).isZero },
// Rules
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "violations")).isZero },
// Tests
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "tests")).isEqualTo(6) },
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "skipped_tests")).isEqualTo(1) },
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "test_failures")).isEqualTo(1) },
{ assertThat(getMeasureAsInteger(TEST_FILE_NAME, "test_errors")).isEqualTo(1) },
{ assertThat(getMeasureAsDouble(TEST_FILE_NAME, "test_success_density")).isEqualTo(66.7) },
{ assertThat(getMeasureAsDouble(TEST_FILE_NAME, "test_execution_time")).isEqualTo(15.0) },
)
}

Expand Down Expand Up @@ -108,6 +170,8 @@ class MetricsTest {

private const val PROJECT_KEY = "metrics"
private const val FILE_NAME = "$PROJECT_KEY:src/source1.sql"
private const val COVERED_FILE_NAME = "$PROJECT_KEY:src/betwnstr.sql"
private const val TEST_FILE_NAME = "$PROJECT_KEY:test/test_betwnstr.sql"

@JvmStatic
@BeforeAll
Expand All @@ -121,7 +185,11 @@ class MetricsTest {
.setProjectName(PROJECT_KEY)
.setProjectVersion("1.0")
.setSourceDirs("src")
.setTestDirs("test")
.setProperty("sonar.sourceEncoding", "UTF-8")
.setProperty("sonar.zpa.tests.reportPaths", "test_results.xml")
.setProperty("sonar.zpa.coverage.reportPaths", "coverage.xml")
.setDebugLogs(true)
orchestrator.executeBuild(build)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage version="1">
<file path="function ut3_demo.betwnstr">
<lineToCover lineNumber="2" covered="true"/>
<lineToCover lineNumber="4" covered="true"/>
<lineToCover lineNumber="5" covered="true"/>
<lineToCover lineNumber="7" covered="true"/>
</file>
</coverage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE OR REPLACE FUNCTION BETWNSTR(A_STRING VARCHAR2, A_START_POS INTEGER, A_END_POS INTEGER) RETURN VARCHAR2 IS
L_START_POS PLS_INTEGER := A_START_POS;
BEGIN
IF L_START_POS = 0 THEN
L_START_POS := 1;
END IF;
RETURN SUBSTR(A_STRING, L_START_POS, A_END_POS - L_START_POS + 1);
END;
/
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
create or replace package test_betwnstr as
-- %suite(Between string function)

-- %test(Returns substring from start position to end position)
procedure normal_case;

-- %test(Returns substring when start position is zero)
procedure zero_start_position;

-- %test(Returns string until end if end position is greater than string length)
procedure big_end_position;

-- %test(Returns null for null input string value)
procedure null_string;

-- %test(A demo of test raising runtime exception)
procedure bad_params;

-- %test(A demo of failing test)
procedure bad_test;

-- %test(Demo of a disabled test)
-- %disabled
procedure disabled_test;

end;
/
create or replace package body test_betwnstr as

procedure normal_case is
begin
ut.expect(betwnstr('1234567', 2, 5)).to_equal('2345');
end;

procedure zero_start_position is
begin
ut.expect(betwnstr('1234567', 0, 5)).to_(equal('12345'));
end;

procedure big_end_position is
begin
ut.expect(betwnstr('1234567', 0, 500)).to_(equal('1234567'));
end;

procedure null_string is
begin
ut.expect(betwnstr(null, 2, 5)).to_(be_null());
end;

procedure bad_params is
begin
ut.expect(betwnstr('1234567', 'a', 'b')).to_(be_null());
end;

procedure bad_test
is
begin
ut.expect(betwnstr('1234567', 0, 500)).to_(equal('1'));
end;

procedure disabled_test is
begin
ut.expect(betwnstr(null, null, null)).not_to(be_null);
end;

end;
/
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<testExecutions version="1">
<file path="test_betwnstr">
<testCase name="normal_case" duration="2">
</testCase>
<testCase name="zero_start_position" duration="2">
</testCase>
<testCase name="big_end_position" duration="2">
</testCase>
<testCase name="null_string" duration="2">
</testCase>
<testCase name="bad_params" duration="2">
<error message="encountered errors">
<![CDATA[
ORA-06502: PL/SQL: erro: erro de conversão de caractere em número numérico ou de valor
ORA-06512: em "UT3_DEMO.TEST_BETWNSTR", line 25
ORA-06512: em "UT3_DEMO.TEST_BETWNSTR", line 25
ORA-06512: em line 6
]]>
</error>
</testCase>
<testCase name="bad_test" duration="5">
<failure message="some expectations have failed">
<![CDATA[
Actual: '1234567' (varchar2) was expected to equal: '1' (varchar2)
at "UT3_DEMO.TEST_BETWNSTR.BAD_TEST", line 31 ut.expect( betwnstr( '1234567', 0, 500 ) ).to_( equal('1') );
]]>
</failure>
</testCase>
<testCase name="disabled_test" duration="0">
<skipped message="skipped"/>
</testCase>
</file>
</testExecutions>

0 comments on commit 22a7b1a

Please sign in to comment.