<commit_before>package org.exist.xquery;

import junit.framework.TestCase;
import junit.textui.TestRunner;

import org.exist.storage.DBBroker;
import org.exist.xmldb.DatabaseInstanceManager;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XQueryService;

public class DocumentUpdateTest extends TestCase {

	private static final String TEST_COLLECTION_NAME = "testup";
    
    private Database database;
    private Collection testCollection;
    
    /**
     * Test if the doc, collection and document functions are correctly
     * notified upon document updates. Call a function once on the empty collection, 
     * then call it again after a document was added, and compare the results.
     */
    public void testUpdate() {
    	String imports = 
    		"import module namespace xdb='http://exist-db.org/xquery/xmldb';\n" + 
    		"import module namespace util='http://exist-db.org/xquery/util';\n";
    	
		try {
			System.out.println("-- TEST 1: doc() function --");
			String query = imports +
	    		"declare function local:get-doc($path as xs:string) {\n" + 
	    		"    if (doc-available($path)) then doc($path) else ()\n" + 
	    		"};\n" +
	    		"let $col := xdb:create-collection('/db', 'testup')\n" + 
	    		"let $path := '/db/testup/test1.xml'\n" +
	    		"let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" +
	    		"let $d1 := local:get-doc($path)\n" +
	    		"let $remove := xdb:remove('/db/testup', 'test1.xml')\n" +
	    		"return string-join((string(count(local:get-doc($path))), string(doc-available($path))), ' ')";
			String result = execQuery(query);
			assertEquals(result, "0 false");
			
			System.out.println("-- TEST 2: xmldb:document() function --");
			query = imports +
	    		"declare function local:get-doc($path as xs:string) {\n" + 
	    		"    if (doc-available($path)) then xmldb:document($path) else ()\n" + 
	    		"};\n" +
	    		"let $col := xdb:create-collection('/db', 'testup')\n" + 
	    		"let $path := '/db/testup/test1.xml'\n" +
	    		"let $d1 := local:get-doc($path)\n" +
	    		"let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" + 
	    		"return string-join((string(count(local:get-doc($path))), string(doc-available($path))), ' ')";
			result = execQuery(query);
			assertEquals(result, "1 true");
			
			System.out.println("-- TEST 3: collection() function --");
			query = imports +
				"declare function local:xpath($collection as xs:string) {\n" + 
	    		"    for $c in collection($collection) return $c//n\n" + 
	    		"};\n" +
	    		"let $col := xdb:create-collection('/db', 'testup')\n" + 
	    		"let $path := '/db/testup'\n" +
	    		"let $d1 := local:xpath($path)//n/text()\n" +
	    		"let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" + 
	    		"return local:xpath($path)//n/text()";
			result = execQuery(query);
			assertEquals(result, "1");
		
			System.out.println("-- TEST 4: 'update insert' statement --");
			query = imports +
				"declare function local:xpath($collection as xs:string) {\n" + 
	    		"    collection($collection)\n" + <commit_msg>[ignore] fix test:  let $a := <test><n>1</n></test> return $a//n//n/text()<commit_after>package org.exist.xquery;

import junit.framework.TestCase;
import junit.textui.TestRunner;

import org.exist.storage.DBBroker;
import org.exist.xmldb.DatabaseInstanceManager;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XQueryService;

public class DocumentUpdateTest extends TestCase {

	private static final String TEST_COLLECTION_NAME = "testup";
    
    private Database database;
    private Collection testCollection;
    
    /**
     * Test if the doc, collection and document functions are correctly
     * notified upon document updates. Call a function once on the empty collection, 
     * then call it again after a document was added, and compare the results.
     */
    public void testUpdate() {
    	String imports = 
    		"import module namespace xdb='http://exist-db.org/xquery/xmldb';\n" + 
    		"import module namespace util='http://exist-db.org/xquery/util';\n";
    	
		try {
			System.out.println("-- TEST 1: doc() function --");
			String query = imports +
	    		"declare function local:get-doc($path as xs:string) {\n" + 
	    		"    if (doc-available($path)) then doc($path) else ()\n" + 
	    		"};\n" +
	    		"let $col := xdb:create-collection('/db', 'testup')\n" + 
	    		"let $path := '/db/testup/test1.xml'\n" +
	    		"let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" +
	    		"let $d1 := local:get-doc($path)\n" +
	    		"let $remove := xdb:remove('/db/testup', 'test1.xml')\n" +
	    		"return string-join((string(count(local:get-doc($path))), string(doc-available($path))), ' ')";
			String result = execQuery(query);
			assertEquals(result, "0 false");
			
			System.out.println("-- TEST 2: xmldb:document() function --");
			query = imports +
	    		"declare function local:get-doc($path as xs:string) {\n" + 
	    		"    if (doc-available($path)) then xmldb:document($path) else ()\n" + 
	    		"};\n" +
	    		"let $col := xdb:create-collection('/db', 'testup')\n" + 
	    		"let $path := '/db/testup/test1.xml'\n" +
	    		"let $d1 := local:get-doc($path)\n" +
	    		"let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" + 
	    		"return string-join((string(count(local:get-doc($path))), string(doc-available($path))), ' ')";
			result = execQuery(query);
			assertEquals(result, "1 true");
			
			System.out.println("-- TEST 3: collection() function --");
			query = imports +
				"declare function local:xpath($collection as xs:string) {\n" + 
	    		"    for $c in collection($collection) return $c//n\n" + 
	    		"};\n" +
	    		"let $col := xdb:create-collection('/db', 'testup')\n" + 
	    		"let $path := '/db/testup'\n" +
	    		"let $d1 := local:xpath($path)//n/text()\n" +
	    		"let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" + 
	    		"return local:xpath($path)/text()";
			result = execQuery(query);
			assertEquals(result, "1");
		
			System.out.println("-- TEST 4: 'update insert' statement --");
			query = imports +
				"declare function local:xpath($collection as xs:string) {\n" + 
	    		"    collection($collection)\n" + 
{'fraction_non_alphanumeric': 0.18377731218198146, 'fraction_numerical': 0.005836575875486381, 'mean_word_length': 3.132962275819419, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 34}, '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': '365472', 'n_tokens_mistral': 2546, 'n_tokens_neox': 2336, 'n_words': 483}