Skip to content

Conversation

@mmeest
Copy link
Contributor

@mmeest mmeest commented Oct 31, 2025

This PR improves test coverage and refactors the test suite for better maintainability by splitting a large test file into smaller, focused test files.

Changes

Test Coverage Improvements

  • Added additional test cases to unicode_validator_test.rb to improve coverage from 45.45% to 100%
    • Added tests for multi-byte hex-escaped UTF-8 strings
    • Added tests for ArgumentError handling
    • Added tests for hex sequence unescaping logic

Test Structure Refactoring

  • Split test/whois_server_test.rb (449 lines, 70+ test methods) into three focused test files:
    • test/unit/whois_server_core_test.rb - Domain validation and regexp tests (6 tests)
    • test/unit/whois_server_methods_test.rb - Individual method tests (25 tests)
    • test/integration/whois_server_integration_test.rb - Integration flow tests (13 tests)
  • This improves maintainability and makes it easier to locate specific test categories

Configuration

  • Fixed SimpleCov initialization to start at the top of test_helper.rb for accurate coverage tracking

Test Results

  • ✅ All 108 tests passing
  • ✅ Coverage: 200/205 lines covered (97.56%)
  • ✅ 0 failures, 0 errors, 0 skips

@mmeest mmeest changed the title Upgrade test coverage from 24.6% to 97,56% Upgrade test coverage from 24.6% to 99,02% Oct 31, 2025
@vohmar vohmar requested a review from OlegPhenomenon October 31, 2025 14:01
end

def test_receive_data_with_invalid_data
stubs(:invalid_data?).returns(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added a stub to the invalid_data? method so it always returns true. Then you send data that’s obviously invalid? What if I decide to send valid data here: will the test fail, or will it just claim my valid data is invalid too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously this test would return same result despite sent data as it is focused on code behaviour in negative scenario. But for better handling test is now rewritten.

assert_equal 0, @sent_data.length
end

def test_receive_data_full_flow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is this test checking? You’ve mocked so many methods that I can’t tell what we’re actually testing here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was testing on positive scenario. It is rewritten: test_receive_data_returns_valid_whois_body

mock_record.verify
end

def test_receive_data_integration_full_flow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what are we testing in this one? Is it any different from the previous test, apart from the domain name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was very similar. And now it is removed to reduce duplication.

data = "\xFF\xFE".force_encoding('ASCII-8BIT')
ip = ['12345', '127.0.0.1']

send(:process_whois_request, data, ip)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process_whois_request is a private method. It’s generally considered bad practice to test a private method directly, except in rare cases. You should prepare test data that triggers this method indirectly, so we can verify how it behaves through its effects. A private method is meant to be hidden, no one outside the class should know about it. It’s a black box.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing private methods is removed.

data = 'invalid..domain.ee'
ip = ['12345', '127.0.0.1']

send(:process_whois_request, data, ip)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here. The issue was that when we ran code like printf 'täpiline.ee\r\n' | nc localhost 43, the WHOIS service would crash. That’s why we updated the process_whois_request method to add extra checks: specifically for cases when the invalid_data? method in the public scope doesn’t see anything suspicious.

Honestly, I’d slightly restructure and refactor this, but the problem is that if I move some of the logic from private methods up to a higher-level abstraction, for example, from process_whois_request into receive_data, the tests will start failing. And that’s exactly the point I’m making: you shouldn’t be testing private methods directly. Public methods should be the ones calling them as needed, depending on the data and conditions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All testing on focused for private methods have been removed.

assert_includes @sent_data.first, 'Policy error'
end

def test_process_whois_request_with_valid_domain_found
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what differents between this test and test_receive_data_full_flow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplication was a bit heavy in these tests. Now the duplication have been reduced by removing similar tests and rewritten test logic.

@mmeest mmeest changed the title Upgrade test coverage from 24.6% to 99,02% Upgrade test coverage from 24.6% to 94,12% Nov 12, 2025
@mmeest mmeest changed the title Upgrade test coverage from 24.6% to 94,12% Upgrade test coverage from 24.6% to 94.12% Nov 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants