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

Adding test_parse_file_with_missing_index_on_first_line #81

Merged
merged 1 commit into from
Mar 2, 2022
Merged

Adding test_parse_file_with_missing_index_on_first_line #81

merged 1 commit into from
Mar 2, 2022

Conversation

Lucas-C
Copy link
Contributor

@Lucas-C Lucas-C commented Feb 28, 2022

My original problem had already been spotted in #51:
some .srt files I found do not contain any initial index for the first block.

Hence I started working on adding support to the srt lib for parsing those files,
but I realized that it works totally fine with srt.parse(subs, ignore_errors=True).

Hence this PR only adds an explicit unit test,
to make srt behaviour for those kind of files explicit.

@cdown
Copy link
Owner

cdown commented Mar 1, 2022

Thanks! The concept is fine, but please use a Hypothesis test instead of using a file fixture :-)

See test_can_parse_index_trailing_ws and the like for an example of how to do that. In your case, you want to compose some subtitles and remove the first line.

@cdown
Copy link
Owner

cdown commented Mar 1, 2022

I'm also up for adding support to parse SRT files without an index, by the way. Those subtitles can get index=None, which can then be handled by to_srt() and the comparison checks.

@Lucas-C
Copy link
Contributor Author

Lucas-C commented Mar 1, 2022

I adapted the test to use hypothesis.
I find the resulting unit test a lot less readable, but I also see the point of using hypothesis to strengthen the tests.

I'm also up for adding support to parse SRT files without an index, by the way

I'll let you handle that 😊

tests/test_srt.py Outdated Show resolved Hide resolved
@cdown
Copy link
Owner

cdown commented Mar 1, 2022

Thanks! I see you pushed a new commit. But I guess I was asking why do the loop over input_subs at all? Usually we only need to do that when we need to mutate Subtitle object content. Do we handle the case where there's a single errant sub differently from if there's a trailing valid one?

@cdown
Copy link
Owner

cdown commented Mar 1, 2022

To be clear, this is what I'm wondering about (untested):

@given(st.lists(subtitles(), min_size=1))
def test_parse_file_with_missing_index_on_first_line(input_subs):  # cf. issue #51
    block = srt.compose(input_subs)
    block = block[block.find("\n") + 1:]
    with pytest.raises(srt.SRTParseError):
	list(srt.parse(block))
    assert list(srt.parse(block, ignore_errors=True)) == input_subs[1:]

@Lucas-C
Copy link
Contributor Author

Lucas-C commented Mar 1, 2022

I adopted the version of the code that you suggested.

@cdown
Copy link
Owner

cdown commented Mar 1, 2022

Sorry for another round of review, but I'm confused again. Why if block instead of using min_length=1? Why len() check instead of just comparing the lists directly?

I promise we're getting somewhere with this PR, I'm just confused at the changes from the suggested code :-)

@Lucas-C
Copy link
Contributor Author

Lucas-C commented Mar 1, 2022

Why if block instead of using min_length=1?

I am simply not very familiar with hypothesis API / features :)
I switched to using min_size=1

Why len() check instead of just comparing the lists directly?

Because the Subtitle.index change in some cases.
I'll let you test it if you want proof ;)

@cdown
Copy link
Owner

cdown commented Mar 1, 2022

Because the Subtitle.index change in some cases.

Ah, for that, pass reindex=False to srt.compose.

@Lucas-C
Copy link
Contributor Author

Lucas-C commented Mar 1, 2022

Ok, done!

@cdown
Copy link
Owner

cdown commented Mar 1, 2022

Looks like tests failed because black format check failed. Want me to just rebase and fix up? Otherwise feel free to run black on tests/test_srt.py and squash into one commit. :-)

https://github.com/cdown/srt/runs/5382080756?check_suite_focus=true

@Lucas-C
Copy link
Contributor Author

Lucas-C commented Mar 1, 2022

I prettified the code with black and squashed my commits.
Note that squashing can be done through GitHub by maintainers when they merge a PR.

@cdown
Copy link
Owner

cdown commented Mar 1, 2022

Yeah sure, I just wasn't sure if you'd prefer I just blacken the code and fix it up myself without your intervention (although that won't show as "merged" on GitHub UI). I'll run CI and merge. Thanks!

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling c3b5ed9 on Lucas-C:test_parse_file_with_missing_index_on_first_line into e518e28 on cdown:develop.

@cdown cdown merged commit 132c110 into cdown:develop Mar 2, 2022
@cdown
Copy link
Owner

cdown commented Mar 2, 2022

Thanks again!

cdown added a commit that referenced this pull request Mar 2, 2022
This came up as part of #51, and more concretely in #81. Some subtitles,
especially release group subtitles, have no index. In
ignore_errors=False mode, this results in a failure parsing.

Accept missing indexes in parsing and Subtitle object storage, and
render them as 0 on Subtitle.to_srt().
cdown added a commit that referenced this pull request Mar 2, 2022
This came up as part of #51, and more concretely in #81. Some subtitles,
especially release group subtitles, have no index. In
ignore_errors=False mode, this results in a failure parsing.

Accept missing indexes in parsing and Subtitle object storage, and
render them as 0 on Subtitle.to_srt().
@cdown
Copy link
Owner

cdown commented Mar 2, 2022

And thanks again for bringing this general case up, 745d5ee now supports it officially.

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