<commit_before># Writing HiveQLUnit Tests #

This user guide explains how to write a unit test using the tools provided by HiveQLUnit. HiveQLUnit unit tests are built just like any other JUnit unit tests, with a dedicated testing class containing test methods annotated with @Test. HiveQLUnit provides a number of TestRules which integrate with the JUnit framework along with other utilities for writing JUnit tests focused around Hive.

## Resources ##

HiveQLUnit provides utilities to access scripts and data needed during testing.

### TextResource ###

The TextResource interface abstracts access to resources like scripts and testing data needed in a unit test. Resources can be pulled from the src/test/resources folder bundled into the jar, from the local file system, or passed as raw text programatically.

    public interface TextResource {
    
        public String resourceText();
    }

    new TextLiteralResource("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)")
    new LocalFileResource("C:/cygwin64/home/K00001/testdata.txt")
    new ResourceFolderResource("/testdata.txt")

Many other tools provided by HiveQLUnit take a TextResource as an input. Because of this, theses tools can work with many different data sources.

### HqlScript ###

The HqlScript interface abstracts how to parse and run an hql script on a hive server. All implimentations of HqlScript take in a TextResource object containing the script to execute.

    new MultiExpressionScript(
        new TextLiteralResource("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)")
    )

    new SingleExpressionScript(
        new TextLiteralResource("DROP TABLE IF EXISTS src")
    )

## JUnit Integration ##

    @ClassRule
    public static TestHiveServer hiveServer = new TestHiveServer();

    @Rule
    public TestDataLoader loader = new TestDataLoader(hiveServer);

    @Rule
    public SetUpHql prepSrc =
            new SetUpHql(
                    hiveServer,
                    new MultiExpressionScript(
                            new TextLiteralResource("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)")<commit_msg>Update WritingHiveQLUnitTests.md<commit_after># Writing HiveQLUnit Tests #

This user guide explains how to write a unit test using the tools provided by HiveQLUnit. HiveQLUnit unit tests are built just like any other JUnit unit tests, with a dedicated testing class containing test methods annotated with @Test. HiveQLUnit provides a number of TestRules which integrate with the JUnit framework along with other utilities for writing JUnit tests focused around Hive.

## Resources ##

HiveQLUnit provides utilities to access scripts and data needed during testing.

### TextResource ###

The TextResource interface abstracts access to resources like scripts and testing data needed in a unit test. Resources can be pulled from the src/test/resources folder bundled into the jar, from the local file system, or passed as raw text programatically.

    public interface TextResource {
    
        public String resourceText();
    }

    new TextLiteralResource("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)")
    new LocalFileResource("C:/cygwin64/home/K00001/testdata.txt")
    new ResourceFolderResource("/testdata.txt")

Many other tools provided by HiveQLUnit take a TextResource as an input. Because of this, these tools can work with many different data sources.

### HqlScript ###

The HqlScript interface abstracts how to parse and run an hql script on a hive server. All implimentations of HqlScript take in a TextResource object containing the script to execute.

    new MultiExpressionScript(
        new TextLiteralResource("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)")
    )

    new SingleExpressionScript(
        new TextLiteralResource("DROP TABLE IF EXISTS src")
    )

## JUnit Integration ##

    @ClassRule
    public static TestHiveServer hiveServer = new TestHiveServer();

    @Rule
    public TestDataLoader loader = new TestDataLoader(hiveServer);

    @Rule
    public SetUpHql prepSrc =
            new SetUpHql(
                    hiveServer,
                    new MultiExpressionScript(
                            new TextLiteralResource("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)")
{'fraction_non_alphanumeric': 0.05013992537313433, 'fraction_numerical': 0.0032649253731343282, 'mean_word_length': 3.5482502651113466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1542115', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1033, 'n_words': 502}