<commit_before>package com.conveyal.gtfs.graphql;

import com.conveyal.gtfs.TestUtils;
import com.conveyal.gtfs.loader.FeedLoadResult;
import graphql.ExecutionInput;
import org.apache.commons.io.IOUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.conveyal.gtfs.GTFS.createDataSource;
import static com.conveyal.gtfs.GTFS.load;
import static com.conveyal.gtfs.GTFS.validate;
import static com.conveyal.gtfs.TestUtils.getResourceFileName;
import static com.zenika.snapshotmatcher.SnapshotMatcher.matchesSnapshot;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * A test suite for all things related to fetching objects with GraphQL.
 * Important note: Snapshot testing is heavily used in this test suite and can potentially result in false negatives
 *   in cases where the order of items in a list is not important.
 */
public class GTFSGraphQLTest {
    private static String testDBName;
    private static DataSource testDataSource;
    private static String testNamespace;

    private static String testInjectionDBName;
    private static DataSource testInjectionDataSource;
    private static String testInjectionNamespace;
    private static String badCalendarDateNamespace;

    @BeforeClass
    public static void setUpClass() throws IOException {
        // create a new database
        testDBName = TestUtils.generateNewDB();
        String dbConnectionUrl = String.format("jdbc:postgresql://localhost/%s", testDBName);
        testDataSource = createDataSource(dbConnectionUrl, null, null);
        // zip up test folder into temp zip file
        String zipFileName = TestUtils.zipFolderFiles("fake-agency", true);
        // load feed into db
        FeedLoadResult feedLoadResult = load(zipFileName, testDataSource);
        testNamespace = feedLoadResult.uniqueIdentifier;
        // validate feed to create additional tables
        validate(testNamespace, testDataSource);

        // Zip up test folder into temp zip file for loading into database.
        FeedLoadResult feed2LoadResult = load(
            TestUtils.zipFolderFiles("fake-agency-bad-calendar-date", true),
            testDataSource
        );
        badCalendarDateNamespace = feed2LoadResult.uniqueIdentifier;
        // validate feed to create additional tables
        validate(badCalendarDateNamespace, testDataSource);

        // create a separate injection database to use in injection tests
        // create a new database
        testInjectionDBName = TestUtils.generateNewDB();
        String injectionDbConnectionUrl = String.format("jdbc:postgresql://localhost/%s", testInjectionDBName);
        testInjectionDataSource = createDataSource(injectionDbConnectionUrl, null, null);
        // load feed into db
        FeedLoadResult injectionFeedLoadResult = load(zipFileName, testInjectionDataSource);
        testInjectionNamespace = injectionFeedLoadResult.uniqueIdentifier;
        // validate feed to create additional tables
        validate(testInjectionNamespace, testInjectionDataSource);
    }

    @AfterClass
    public static void tearDownClass() {
        TestUtils.dropDB(testDBName);
        TestUtils.dropDB(testInjectionDBName);
    }

    /** Tests that the graphQL schema can initialize. */
    @Test(timeout=5000)
    public void canInitialize() {
        GTFSGraphQL.initialize(testDataSource);
        GTFSGraphQL.getGraphQl();
    }

    /** Tests that the root element of a feed can be fetched. */
    @Test(timeout=5000)
    public void canFetchFeed() throws IOException {
        assertThat(queryGraphQL("feed.txt"), matchesSnapshot());
    }

    /** Tests that the row counts of a feed can be fetched. */
    @Test(timeout=5000)
    public void canFetchFeedRowCounts() throws IOException {<commit_msg>test(postgres): fix GTFSGraphQLTest db user/pass<commit_after>package com.conveyal.gtfs.graphql;

import com.conveyal.gtfs.TestUtils;
import com.conveyal.gtfs.loader.FeedLoadResult;
import graphql.ExecutionInput;
import org.apache.commons.io.IOUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.conveyal.gtfs.GTFS.createDataSource;
import static com.conveyal.gtfs.GTFS.load;
import static com.conveyal.gtfs.GTFS.validate;
import static com.conveyal.gtfs.TestUtils.PG_TEST_PASSWORD;
import static com.conveyal.gtfs.TestUtils.PG_TEST_USER;
import static com.conveyal.gtfs.TestUtils.getResourceFileName;
import static com.zenika.snapshotmatcher.SnapshotMatcher.matchesSnapshot;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * A test suite for all things related to fetching objects with GraphQL.
 * Important note: Snapshot testing is heavily used in this test suite and can potentially result in false negatives
 *   in cases where the order of items in a list is not important.
 */
public class GTFSGraphQLTest {
    private static String testDBName;
    private static DataSource testDataSource;
    private static String testNamespace;

    private static String testInjectionDBName;
    private static DataSource testInjectionDataSource;
    private static String testInjectionNamespace;
    private static String badCalendarDateNamespace;

    @BeforeClass
    public static void setUpClass() throws IOException {
        // create a new database
        testDBName = TestUtils.generateNewDB();
        String dbConnectionUrl = String.format("jdbc:postgresql://localhost/%s", testDBName);
        testDataSource = createDataSource(dbConnectionUrl, PG_TEST_USER, PG_TEST_PASSWORD);
        // zip up test folder into temp zip file
        String zipFileName = TestUtils.zipFolderFiles("fake-agency", true);
        // load feed into db
        FeedLoadResult feedLoadResult = load(zipFileName, testDataSource);
        testNamespace = feedLoadResult.uniqueIdentifier;
        // validate feed to create additional tables
        validate(testNamespace, testDataSource);

        // Zip up test folder into temp zip file for loading into database.
        FeedLoadResult feed2LoadResult = load(
            TestUtils.zipFolderFiles("fake-agency-bad-calendar-date", true),
            testDataSource
        );
        badCalendarDateNamespace = feed2LoadResult.uniqueIdentifier;
        // validate feed to create additional tables
        validate(badCalendarDateNamespace, testDataSource);

        // create a separate injection database to use in injection tests
        // create a new database
        testInjectionDBName = TestUtils.generateNewDB();
        String injectionDbConnectionUrl = String.format("jdbc:postgresql://localhost/%s", testInjectionDBName);
        testInjectionDataSource = createDataSource(injectionDbConnectionUrl, PG_TEST_USER, PG_TEST_PASSWORD);
        // load feed into db
        FeedLoadResult injectionFeedLoadResult = load(zipFileName, testInjectionDataSource);
        testInjectionNamespace = injectionFeedLoadResult.uniqueIdentifier;
        // validate feed to create additional tables
        validate(testInjectionNamespace, testInjectionDataSource);
    }

    @AfterClass
    public static void tearDownClass() {
        TestUtils.dropDB(testDBName);
        TestUtils.dropDB(testInjectionDBName);
    }

    /** Tests that the graphQL schema can initialize. */
    @Test(timeout=5000)
    public void canInitialize() {
        GTFSGraphQL.initialize(testDataSource);
        GTFSGraphQL.getGraphQl();
    }

    /** Tests that the root element of a feed can be fetched. */
    @Test(timeout=5000)
    public void canFetchFeed() throws IOException {
        assertThat(queryGraphQL("feed.txt"), matchesSnapshot());
    }

    /** Tests that the row counts of a feed can be fetched. */
    @Test(timeout=5000)
    public void canFetchFeedRowCounts() throws IOException {
{'fraction_non_alphanumeric': 0.07689517591585056, 'fraction_numerical': 0.0033853222101317857, 'mean_word_length': 3.743119266055046, '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': '7148973', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2129, 'n_words': 616}