IndexCreators { public static void writeIndexes(Writer body, CollectionName collection, IObject options) throws Exception { try { if (options == null) { return; } for (String indexType : INDEX_WRITERS.keySet()) { writeCreateIndex(indexType, body, collection, options); } } catch (ReadValueException e) { } } private  IndexCreators(); static void writeIndexes(Writer body, CollectionName collection, IObject options);  }
@Test public void testOrderedIndex() throws Exception { IObject options = new DSObject("{ \"ordered\": [ \"a\", \"b\" ] }"); IndexCreators.writeIndexes(body, collection, options); assertEquals("CREATE INDEX ON test_collection USING BTREE ((document#>'{a}'));\n" + "CREATE INDEX ON test_collection USING BTREE ((document#>'{b}'));\n", body.toString()); }
@Test public void testOrderedIndexForOneField() throws Exception { IObject options = new DSObject("{ \"ordered\": \"a\" }"); IndexCreators.writeIndexes(body, collection, options); assertEquals("CREATE INDEX ON test_collection USING BTREE ((document#>'{a}'));\n", body.toString()); }
@Test public void testDatetimeIndex() throws Exception { IObject options = new DSObject("{ \"datetime\": [ \"a\", \"b\" ] }"); IndexCreators.writeIndexes(body, collection, options); assertEquals("CREATE INDEX ON test_collection USING BTREE ((parse_timestamp_immutable(document#>'{a}')));\n" + "CREATE INDEX ON test_collection USING BTREE ((parse_timestamp_immutable(document#>'{b}')));\n", body.toString()); }
@Test public void testDatetimeIndexForOneField() throws Exception { IObject options = new DSObject("{ \"datetime\": \"a\" }"); IndexCreators.writeIndexes(body, collection, options); assertEquals("CREATE INDEX ON test_collection USING BTREE ((parse_timestamp_immutable(document#>'{a}')));\n", body.toString()); }
@Test public void testTagsIndex() throws Exception { IObject options = new DSObject("{ \"tags\": [ \"a\", \"b\" ] }"); IndexCreators.writeIndexes(body, collection, options); assertEquals("CREATE INDEX ON test_collection USING GIN ((document#>'{a}'));\n" + "CREATE INDEX ON test_collection USING GIN ((document#>'{b}'));\n", body.toString()); }
@Test public void testTagsIndexForOneField() throws Exception { IObject options = new DSObject("{ \"tags\": \"a\" }"); IndexCreators.writeIndexes(body, collection, options); assertEquals("CREATE INDEX ON test_collection USING GIN ((document#>'{a}'));\n", body.toString()); }
@Test public void testFulltextIndex() throws Exception { IObject options = new DSObject("{ " + "\"fulltext\": [ \"a\", \"b\" ]," + "\"language\": \"russian\"" + "}"); IndexCreators.writeIndexes(body, collection, options); assertEquals("CREATE INDEX ON test_collection USING GIN (fulltext);\n" + "CREATE FUNCTION test_collection_fulltext_update_trigger() RETURNS trigger AS $$\n" + "begin\n" + "new.fulltext := " + "to_tsvector('russian', coalesce((new.document#>'{a}')::text,'') || ' ' || coalesce((new.document#>'{b}')::text,''));\n" + "return new;\n" + "end\n" + "$$ LANGUAGE plpgsql;\n" + "CREATE TRIGGER test_collection_fulltext_update_trigger BEFORE INSERT OR UPDATE " + "ON test_collection FOR EACH ROW EXECUTE PROCEDURE " + "test_collection_fulltext_update_trigger();\n", body.toString()); }
@Test public void testFulltextIndexForOneField() throws Exception { IObject options = new DSObject("{ " + "\"fulltext\": \"text\"," + "\"language\": \"russian\"" + "}"); IndexCreators.writeIndexes(body, collection, options); assertEquals("CREATE INDEX ON test_collection USING GIN (fulltext);\n" + "CREATE FUNCTION test_collection_fulltext_update_trigger() RETURNS trigger AS $$\n" + "begin\n" + "new.fulltext := " + "to_tsvector('russian', coalesce((new.document#>'{text}')::text,''));\n" + "return new;\n" + "end\n" + "$$ LANGUAGE plpgsql;\n" + "CREATE TRIGGER test_collection_fulltext_update_trigger BEFORE INSERT OR UPDATE " + "ON test_collection FOR EACH ROW EXECUTE PROCEDURE " + "test_collection_fulltext_update_trigger();\n", body.toString()); }
@Test(expected = Exception.class) public void testFullTextIndexWithoutLanguage() throws Exception { IObject options = new DSObject("{ \"fulltext\": \"text\" }"); IndexCreators.writeIndexes(body, collection, options); }
@Test(expected = Exception.class) public void testWrongIndexFormat() throws Exception { IObject options = new DSObject("{ \"ordered\": 123 }"); IndexCreators.writeIndexes(body, collection, options); }