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

test: Add Unit tests for the PreTranslateAction.java class #664

Closed
wants to merge 10 commits into from

Conversation

Sweetdevil144
Copy link
Contributor

@Sweetdevil144 Sweetdevil144 commented Oct 12, 2023

Fixes #659 .

  • @andrii-bodnar . I haven't touched private METHODS for security reasons, so the code coverage will be a bit less, around 30%. If you want me to run tests for them too, then I surely can do It.

@codecov
Copy link

codecov bot commented Oct 12, 2023

Codecov Report

Merging #664 (befd827) into main (2a59e03) will increase coverage by 0.36%.
The diff coverage is n/a.

❗ Current head befd827 differs from pull request most recent head ab7ff05. Consider uploading reports for the commit ab7ff05 to get more accurate results

@@             Coverage Diff              @@
##               main     #664      +/-   ##
============================================
+ Coverage     63.66%   64.01%   +0.36%     
- Complexity     1380     1385       +5     
============================================
  Files           210      209       -1     
  Lines          5648     5643       -5     
  Branches        851      849       -2     
============================================
+ Hits           3595     3612      +17     
+ Misses         1594     1570      -24     
- Partials        459      461       +2     

see 5 files with indirect coverage changes

@andrii-bodnar andrii-bodnar added the hacktoberfest This issue welcomes contributions for Hacktoberfest label Oct 13, 2023
@andrii-bodnar
Copy link
Member

@Sweetdevil144 could you please specify which private classes you mean?

I guess it should be possible to cover the other methods in this class by passing different values to the act method and mocking all the client methods' calls.

@Sweetdevil144
Copy link
Contributor Author

@Sweetdevil144 could you please specify which private classes you mean?

Oh Sorry. I mistakenly types classes. Updated the comment

@Sweetdevil144
Copy link
Contributor Author

I guess it should be possible to cover the other methods in this class by passing different values to the act method and mocking all the client methods' calls.

Sure. let me try that too.

@andrii-bodnar andrii-bodnar self-requested a review October 13, 2023 08:21
@andrii-bodnar
Copy link
Member

@Sweetdevil144 it looks like the GlossaryDownloadActionTest and TmDownloadActionTest are good examples in this case

@Sweetdevil144
Copy link
Contributor Author

Hey @andrii-bodnar . I tried adjusting the files as you recommeded. However I'm getting a lot of NullPointerExceptions at lines like ->

action.act(out, properties, client);

for test cases like

@Test
    void testPrepareFileIds_SomeSourcesDoNotExistInProjectPaths() {
        Language english = new Language();
        english.setId("en");
        when(project.getProjectLanguages(false)).thenReturn(Collections.singletonList(english));
        when(project.getDirectories()).thenReturn(new HashMap<>());
        when(project.getBranches()).thenReturn(new HashMap<>());
        when(project.getFileInfos()).thenReturn(Collections.emptyList());

        action.act(out, properties, client);

        verify(out, times(1)).println(anyString());
    }

@andrii-bodnar
Copy link
Member

Hey @Sweetdevil144, probably you need to mock the Crowdin client.

Like in this test - GlossaryDownloadActionTest.java

@Sweetdevil144
Copy link
Contributor Author

Sorry @andrii-bodnar . This seems unbelievably 'Out' of my reach :- |

@Sweetdevil144
Copy link
Contributor Author

This is what it says in each test case
Screenshot 2023-10-16 at 10 41 47 PM
a Runtine Exception

@Sweetdevil144
Copy link
Contributor Author

I'm still encountring RuntimeError Exceptions which is demonstrated by the recent commit

@andrii-bodnar
Copy link
Member

andrii-bodnar commented Oct 17, 2023

@Sweetdevil144 it says "Language 'en' doesn't exist in the project". It means that your project mock doesn't return this language.

Probably, you'll need to update this mock:

when(project.getProjectLanguages(false)).thenReturn(Collections.<Language>emptyList());

to return a list of languages instead of an empty list.

@Sweetdevil144
Copy link
Contributor Author

Sorry @andrii-bodnar , I cant seem to get a hold of this. I would like to close this PR!
Thanks!!

@Sweetdevil144 Sweetdevil144 marked this pull request as draft October 18, 2023 17:15
@andrii-bodnar
Copy link
Member

@Sweetdevil144 I just looked closer at this and prepared an almost working example based on your code:

package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ProjectBuilder;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder;
import com.crowdin.cli.properties.PropertiesWithFiles;
import com.crowdin.cli.client.CrowdinProjectFull;
import com.crowdin.cli.utils.Utils;
import com.crowdin.client.translations.model.PreTranslationStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Collections;

import static org.mockito.Mockito.*;

class PreTranslateActionTest {

    @Mock
    private Outputter out;

    @Mock
    private ProjectClient client;

    @Mock
    private PreTranslationStatus preTranslationStatus;

    private PreTranslateAction action;

    private PropertiesWithFiles pb;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.initMocks(this);

        action = new PreTranslateAction(
            Collections.singletonList("ua"), null, 1L, null, null, false, false, false, false, false, false, false, null
        );

        NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
            .minimalBuiltPropertiesBean("android.xml", "%two_letters_code%.xml")
            .setBasePath(Utils.PATH_SEPARATOR);
        pb = pbBuilder.build();

        CrowdinProjectFull project = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
            .addFile("android.xml", "android", 101L, null, null, "%two_letters_code%.xml")
            .build();

        when(client.downloadFullProject(null)).thenReturn(project);
        when(client.startPreTranslation(any())).thenReturn(preTranslationStatus);
        when(preTranslationStatus.getStatus()).thenReturn("finished");
    }

    @Test
    void testAct_FailureWhenPreTranslationFails() {
        when(preTranslationStatus.getStatus()).thenReturn("processing", "failed");
        when(client.checkPreTranslation(anyString())).thenReturn(preTranslationStatus);

        action.act(out, pb, client);
    }
}

Currently, this is failing with the Couldn't find any files to Pre-Translate in the current project error since the prepareFileIds method returns an empty list. This is happening because the SourcesUtils.getFiles tries to find files in the filesystem and I didn't find a way to mock this method.

Maybe we can create some temporary files locally during the test execution? These files should match the source pattern in the NewPropertiesWithFilesUtilBuilder configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest This issue welcomes contributions for Hacktoberfest invalid tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Unit tests for the PreTranslateAction.java class
2 participants