<commit_before>package com.brettonw.db;

import com.brettonw.bag.BagArray;
import com.brettonw.bag.BagObject;
import com.brettonw.bag.formats.MimeType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

import java.util.Map;

import static com.brettonw.db.BagMongo.*;
import static org.junit.Assert.*;

public class BagMongo_Test {
    private static final Logger log = LogManager.getLogger (BagMongo_Test.class);

    private static final String TEST_COLLECTION_NAME = "Test";
    private static final String TEST_NAME = "Test.Test";
    private BagArray testBagArray;
    private String queryJson;
    private String queryManyJson;

    public BagMongo_Test () throws Exception {

        testBagArray = new BagArray ()
                .add (new BagObject ()
                        .put ("id", 1)
                        .put ("key", "value 1" )
                        .put ("payload", "full" )
                )
                .add (new BagObject ()
                        .put ("id", 2)
                        .put ("key", "value 2" )
                        .put ("payload", "medium" )
                )
                .add (new BagObject ()
                        .put ("id", 3)
                        .put ("key", "value 3" )
                        .put ("payload", "medium" )
                )
                .add (new BagObject ()
                        .put ("id", 4)
                        .put ("key", "value 4" )
                        .put ("payload", "empty" )
                );

        queryJson = new BagObject ().put ("id", 2).toString (MimeType.JSON);
        queryManyJson = new BagObject ().put ("payload", "medium" ).toString (MimeType.JSON);

        // ensure the primary test database is fresh
        close (open ());
    }

    private BagDbInterface open () {
        BagMongo bagMongo = BagMongo.connectLocal (TEST_COLLECTION_NAME);
        assertNotEquals (null, bagMongo);
        return bagMongo;
    }

    private void close (BagDbInterface bagDb) throws Exception {
        bagDb.deleteAll ();
        assertEquals (0, bagDb.getCount ());
        bagDb.drop ();
    }

    @Test
    public void testGetNames () throws Exception {
        BagMongo bagMongo = BagMongo.connectLocal (TEST_COLLECTION_NAME);
        assertEquals (TEST_COLLECTION_NAME, bagMongo.getDatabaseName ());
        assertEquals (TEST_COLLECTION_NAME, bagMongo.getCollectionName ());
        assertEquals (TEST_NAME, bagMongo.getName ());
        close (bagMongo);
    }

    @Test
    public void testPutWithGet () throws Exception {
        BagDbInterface bagDb = open ()
                .put (testBagArray.getBagObject (0))
                .put (testBagArray.getBagObject (1))
                .put (testBagArray.getBagObject (2));
        assertEquals (3, bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagObject result = bagDb.get (queryJson);
        assertEquals (testBagArray.getBagObject (1), result);

        close (bagDb);
    }

    @Test
    public void testPutArrayWithGet () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagObject result = bagDb.get (queryJson);
        assertEquals (testBagArray.getBagObject (1), result);

        close (bagDb);
    }

    @Test
    public void testDelete () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        bagDb.delete (queryJson);
        BagObject result = bagDb.get (queryJson);
        assertEquals (null, result);
        assertEquals (testBagArray.getCount () - 1, bagDb.getCount ());

        close (bagDb);
    }

    @Test
    public void testGetAll () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagArray bagArray = bagDb.getAll ();
        assertNotEquals (null, bagArray);
        assertEquals (testBagArray.getCount (), bagArray.getCount ());
        assertEquals (testBagArray, bagArray);

        close (bagDb);
    }

    @Test
    public void testGetMany () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagArray bagArray = bagDb.getMany (queryManyJson);
        assertNotEquals (null, bagArray);
        assertEquals (2, bagArray.getCount ());
        assertEquals (testBagArray.getBagObject (1), bagArray.getBagObject (0));
        assertEquals (testBagArray.getBagObject (2), bagArray.getBagObject (1));

        close (bagDb);
    }

    @Test
    public void testDeleteMany () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        bagDb.deleteMany (queryManyJson);
        assertEquals (2, bagDb.getCount ());

        BagArray bagArray = bagDb.getMany (queryManyJson);
        assertNotEquals (null, bagArray);
        assertEquals (0, bagArray.getCount ());

        close (bagDb);
    }

    @Test
    public void testGetWithMultipleMatchFields () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagObject result = bagDb.get (new BagObject ().put ("id", 3).put ("payload", "medium").toString (MimeType.JSON));
        assertEquals (testBagArray.getBagObject (2), result);

        close (bagDb);
    }

    @Test
    public void testGetManyWithNull () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagArray bagArray = bagDb.getMany (null);
        assertNotEquals (null, bagArray);
        assertEquals (testBagArray.getCount (), bagArray.getCount ());
        assertEquals (testBagArray, bagArray);

        close (bagDb);
    }

    @Test
    public void testConnectWithBadConnectionStringFails () {
        Map<String, BagMongo> collections = BagMongo.connect ("bongo", "bongo", "bongo");
        assertEquals (null, collections);
    }

    @Test
    public void testConnectWithInvalidAddressFails () {
        Map<String, BagMongo> collections = BagMongo.connect ("mongodb://bongo", "bongo", "bongo");
        assertEquals (null, collections);
    }

    @Test
    public void testConnectLocalWithNullFails () {
        BagMongo bagMongo = BagMongo.connectLocal (null);
        assertEquals (null, bagMongo);
    }

    @Test
    public void testConnectLocalWithNullFails2 () {
        Map<String, BagMongo> collections = BagMongo.connectLocal ("xxx", (String[])null);
        assertEquals (null, collections);
    }

    @Test
    public void testConnectLocalWithNullDatabaseNameFails () {
        Map<String, BagMongo> collections = BagMongo.connectLocal (null, "xxx");
        assertEquals (null, collections);
    }

    @Test
    public void testMinimumConfiguration () {
        // minimum required configuration
        BagObject configuration = BagObject.open (COLLECTION_NAME, "bongo");
        Map<String, BagMongo> collections = BagMongo.connect (configuration);
        assertNotEquals (null, collections);
        try (BagMongo bagMongo = collections.get ("bongo")) {
            bagMongo.put (BagObject.open ("xxx", "yyy"));
            assertTrue (bagMongo.getCount () == 1);
            bagMongo.drop ();
        } catch (Exception exception) {}
    }

    @Test
    public void testMinimumConfiguration2 () {
        // minimum required configuration
        BagObject configuration = BagObject.open (DATABASE_NAME, "bongo");
        Map<String, BagMongo> collections = BagMongo.connect (configuration);
        assertNotEquals (null, collections);
        try (BagMongo bagMongo = collections.get ("bongo")) {
            bagMongo.put (BagObject.open ("xxx", "yyy"));
            assertTrue (bagMongo.getCount () == 1);
            bagMongo.drop ();
        } catch (Exception exception) {}
    }

    @Test
    public void testConfiguration () throws Exception {
        BagObject configuration = BagObject
                .open (DATABASE_NAME, "mvn-test")
                .put (COLLECTION_NAMES, BagArray.open ("bongo"));

        Map<String, BagMongo> collections = BagMongo.connect (configuration);
        assertNotEquals (null, collections);
        try (BagMongo bagMongo = collections.get ("bongo")) {
            bagMongo.put (BagObject.open ("xxx", "yyy"));
            assertTrue (bagMongo.getCount () == 1);
            bagMongo.drop ();
        } catch (Exception exception) {
            throw (exception);
        }
    }

    @Test
    public void testBadConfiguration () {
        BagObject configuration = BagObject.open (CONNECTION_STRING, "xxx");
        Map<String, BagMongo> collections = BagMongo.connect (configuration);
        assertEquals (null, collections);
    }
}
<commit_msg>BagMongo tests<commit_after>package com.brettonw.db;

import com.brettonw.bag.BagArray;
import com.brettonw.bag.BagObject;
import com.brettonw.bag.formats.MimeType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

import java.util.Map;

import static com.brettonw.db.BagMongo.*;
import static org.junit.Assert.*;

public class BagMongo_Test {
    private static final Logger log = LogManager.getLogger (BagMongo_Test.class);

    private static final String TEST_COLLECTION_NAME = "Test";
    private static final String TEST_NAME = "Test.Test";
    private BagArray testBagArray;
    private String queryJson;
    private String queryManyJson;

    public BagMongo_Test () throws Exception {

        testBagArray = new BagArray ()
                .add (new BagObject ()
                        .put ("id", 1)
                        .put ("key", "value 1" )
                        .put ("payload", "full" )
                )
                .add (new BagObject ()
                        .put ("id", 2)
                        .put ("key", "value 2" )
                        .put ("payload", "medium" )
                )
                .add (new BagObject ()
                        .put ("id", 3)
                        .put ("key", "value 3" )
                        .put ("payload", "medium" )
                )
                .add (new BagObject ()
                        .put ("id", 4)
                        .put ("key", "value 4" )
                        .put ("payload", "empty" )
                );

        queryJson = new BagObject ().put ("id", 2).toString (MimeType.JSON);
        queryManyJson = new BagObject ().put ("payload", "medium" ).toString (MimeType.JSON);

        // ensure the primary test database is fresh
        close (open ());
    }

    private BagDbInterface open () {
        BagMongo bagMongo = BagMongo.connectLocal (TEST_COLLECTION_NAME);
        assertNotEquals (null, bagMongo);
        return bagMongo;
    }

    private void close (BagDbInterface bagDb) throws Exception {
        bagDb.deleteAll ();
        assertEquals (0, bagDb.getCount ());
        bagDb.drop ();
    }

    @Test
    public void testGetNames () throws Exception {
        BagMongo bagMongo = BagMongo.connectLocal (TEST_COLLECTION_NAME);
        assertEquals (TEST_COLLECTION_NAME, bagMongo.getDatabaseName ());
        assertEquals (TEST_COLLECTION_NAME, bagMongo.getCollectionName ());
        assertEquals (TEST_NAME, bagMongo.getName ());
        close (bagMongo);
    }

    @Test
    public void testGetWithNoMatches () throws Exception {
        // ensure the primary test database is fresh
        close (open ());
        BagDbInterface bagDb = open ();
        BagObject result = bagDb.get (queryJson);
        assertEquals (result, null);
        close (bagDb);
    }

    @Test
    public void testPutWithGet () throws Exception {
        BagDbInterface bagDb = open ()
                .put (testBagArray.getBagObject (0))
                .put (testBagArray.getBagObject (1))
                .put (testBagArray.getBagObject (2));
        assertEquals (3, bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagObject result = bagDb.get (queryJson);
        assertEquals (testBagArray.getBagObject (1), result);

        close (bagDb);
    }

    @Test
    public void testPutArrayWithGet () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagObject result = bagDb.get (queryJson);
        assertEquals (testBagArray.getBagObject (1), result);

        close (bagDb);
    }

    @Test
    public void testDelete () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        bagDb.delete (queryJson);
        BagObject result = bagDb.get (queryJson);
        assertEquals (null, result);
        assertEquals (testBagArray.getCount () - 1, bagDb.getCount ());

        close (bagDb);
    }

    @Test
    public void testGetAll () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagArray bagArray = bagDb.getAll ();
        assertNotEquals (null, bagArray);
        assertEquals (testBagArray.getCount (), bagArray.getCount ());
        assertEquals (testBagArray, bagArray);

        close (bagDb);
    }

    @Test
    public void testGetMany () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagArray bagArray = bagDb.getMany (queryManyJson);
        assertNotEquals (null, bagArray);
        assertEquals (2, bagArray.getCount ());
        assertEquals (testBagArray.getBagObject (1), bagArray.getBagObject (0));
        assertEquals (testBagArray.getBagObject (2), bagArray.getBagObject (1));

        close (bagDb);
    }

    @Test
    public void testDeleteMany () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        bagDb.deleteMany (queryManyJson);
        assertEquals (2, bagDb.getCount ());

        BagArray bagArray = bagDb.getMany (queryManyJson);
        assertNotEquals (null, bagArray);
        assertEquals (0, bagArray.getCount ());

        close (bagDb);
    }

    @Test
    public void testGetWithMultipleMatchFields () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagObject result = bagDb.get (new BagObject ().put ("id", 3).put ("payload", "medium").toString (MimeType.JSON));
        assertEquals (testBagArray.getBagObject (2), result);

        close (bagDb);
    }

    @Test
    public void testGetManyWithNull () throws Exception {
        BagDbInterface bagDb = open ().putMany (testBagArray);
        assertEquals (testBagArray.getCount (), bagDb.getCount ());
        assertEquals (TEST_NAME, bagDb.getName ());

        BagArray bagArray = bagDb.getMany (null);
        assertNotEquals (null, bagArray);
        assertEquals (testBagArray.getCount (), bagArray.getCount ());
        assertEquals (testBagArray, bagArray);

        close (bagDb);
    }

    @Test
    public void testConnectWithBadConnectionStringFails () {
        Map<String, BagMongo> collections = BagMongo.connect ("bongo", "bongo", "bongo");
        assertEquals (null, collections);
    }

    @Test
    public void testConnectWithInvalidAddressFails () {
        Map<String, BagMongo> collections = BagMongo.connect ("mongodb://bongo", "bongo", "bongo");
        assertEquals (null, collections);
    }

    @Test
    public void testConnectLocalWithNullFails () {
        BagMongo bagMongo = BagMongo.connectLocal (null);
        assertEquals (null, bagMongo);
    }

    @Test
    public void testConnectLocalWithNullFails2 () {
        Map<String, BagMongo> collections = BagMongo.connectLocal ("xxx", (String[])null);
        assertEquals (null, collections);
    }

    @Test
    public void testConnectLocalWithNullDatabaseNameFails () {
        Map<String, BagMongo> collections = BagMongo.connectLocal (null, "xxx");
        assertEquals (null, collections);
    }

    @Test
    public void testMinimumConfiguration () {
        // minimum required configuration
        BagObject configuration = BagObject.open (COLLECTION_NAME, "bongo");
        Map<String, BagMongo> collections = BagMongo.connect (configuration);
        assertNotEquals (null, collections);
        try (BagMongo bagMongo = collections.get ("bongo")) {
            bagMongo.put (BagObject.open ("xxx", "yyy"));
            assertTrue (bagMongo.getCount () == 1);
            bagMongo.drop ();
        } catch (Exception exception) {}
    }

    @Test
    public void testMinimumConfiguration2 () {
        // minimum required configuration
        BagObject configuration = BagObject.open (DATABASE_NAME, "bongo");
        Map<String, BagMongo> collections = BagMongo.connect (configuration);
        assertNotEquals (null, collections);
        try (BagMongo bagMongo = collections.get ("bongo")) {
            bagMongo.put (BagObject.open ("xxx", "yyy"));
            assertTrue (bagMongo.getCount () == 1);
            bagMongo.drop ();
        } catch (Exception exception) {}
    }

    @Test
    public void testConfiguration () throws Exception {
        BagObject configuration = BagObject
                .open (DATABASE_NAME, "mvn-test")
                .put (COLLECTION_NAMES, BagArray.open ("bongo"));

        Map<String, BagMongo> collections = BagMongo.connect (configuration);
        assertNotEquals (null, collections);
        try (BagMongo bagMongo = collections.get ("bongo")) {
            bagMongo.put (BagObject.open ("xxx", "yyy"));
            assertTrue (bagMongo.getCount () == 1);
            bagMongo.drop ();
        } catch (Exception exception) {
            throw (exception);
        }
    }

    @Test
    public void testBadConfiguration () {
        BagObject configuration = BagObject.open (CONNECTION_STRING, "xxx");
        Map<String, BagMongo> collections = BagMongo.connect (configuration);
        assertEquals (null, collections);
    }
}

{'fraction_non_alphanumeric': 0.11428274320178859, 'fraction_numerical': 0.0034316019341756356, 'mean_word_length': 2.4000353544281423, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5929329', 'n_tokens_mistral': 5398, 'n_tokens_neox': 5194, 'n_words': 1308}