<commit_before>import assert from 'assert';
import clone from 'clone';
import config from './config';


import * as schemaObjects from '../helper/schema-objects';
import * as schemas from '../helper/schemas';
import * as humansCollection from '../helper/humans-collection';

import AsyncTestUtil, { wait, waitUntil } from 'async-test-util';
import {
    createRxDatabase,
    RxDocument,
    isRxDocument,
    promiseWait,
    randomCouchString
} from '../../';

import {
    getRxStoragePouch
} from '../../plugins/pouchdb';

import {
    filter,
    map,
    first
} from 'rxjs/operators';
import { HumanDocumentType } from '../helper/schemas';

config.parallel('reactive-query.test.js', () => {
    config.parallel('positive', () => {
        it('get results of array when .subscribe() and filled array later', async () => {
            const c = await humansCollection.create(1);
            const query = c.find();
            let lastValue: any = null;
            let count = 0;
            query.$.subscribe(newResults => {
                count++;
                lastValue = newResults;
            });
            await AsyncTestUtil.waitUntil(() => count === 1);
            assert.ok(lastValue);
            assert.strictEqual(lastValue.length, 1);
            assert.strictEqual(count, 1);
            c.database.destroy();
        });
        it('get the updated docs on Collection.insert()', async () => {
            const c = await humansCollection.create(1);
            const query = c.find();
            let lastValue: any[] = [];
            const emitted: any[] = [];
            query.$.subscribe(newResults => {
                lastValue = newResults;
                emitted.push(newResults);
            });
            await waitUntil(() => emitted.length === 1);
            assert.strictEqual(lastValue.length, 1);

            const addHuman = schemaObjects.human();
            const newPromiseWait = AsyncTestUtil.waitResolveable(500);
            await c.insert(addHuman);
            await newPromiseWait.promise;
            assert.strictEqual(lastValue.length, 2);

            let isHere = false;
            lastValue.map(doc => {
                if (doc.get('passportId') === addHuman.passportId)
                    isHere = true;
            });
            assert.ok(isHere);
            c.database.destroy();
        });
        it('get the value twice when subscribing 2 times', async () => {
            const c = await humansCollection.create(1);
            const query = c.find();
            let lastValue: any[] = [];
            query.$.subscribe(newResults => {
                lastValue = newResults;
            });
            let lastValue2: any[] = [];
            query.$.subscribe(newResults => {
                lastValue2 = newResults;
            });
            await promiseWait(100);

            await AsyncTestUtil.waitUntil(() => lastValue2 && lastValue2.length === 1);
            assert.deepStrictEqual(lastValue, lastValue2);
            c.database.destroy();
        });
        it('get the base-value when subscribing again later', async () => {
            const c = await humansCollection.create(1);
            const query = c.find();
            let lastValue: any[] = [];
            query.$.subscribe(newResults => {
                lastValue = newResults;
            });
            await AsyncTestUtil.waitUntil(() => lastValue.length > 0);
            let lastValue2: any[] = [];
            query.$.subscribe(newResults => {
                lastValue2 = newResults;
            });
            await AsyncTestUtil.waitUntil(() => lastValue2.length > 0);
            await promiseWait(10);
            assert.strictEqual(lastValue2.length, 1);
            assert.deepStrictEqual(lastValue, lastValue2);
            c.database.destroy();
        });
        it('get new values on RxDocument.save', async () => {
            const c = await humansCollection.create(1);
            const doc: any = await c.findOne().exec(true);

            let values: any;
            const emitted: any[] = [];
            const querySub = c.find({
                selector: {
                    firstName: doc.get('firstName')
                }
            }).$.subscribe(newV => {
                values = newV;
                if (newV) {
                    emitted.push(newV);
                }
            });
            await waitUntil(() => emitted.length === 1);
            assert.strictEqual(values.length, 1);

            // change doc so query does not match
            const newPromiseWait = AsyncTestUtil.waitResolveable(500);
            await doc.atomicPatch({ firstName: 'foobar' });
            await newPromiseWait.promise;
            assert.strictEqual(values.length, 0);
            querySub.unsubscribe();
            c.database.destroy();
        });
        it('subscribing many times should not result in many database-requests', async () => {
            const c = await humansCollection.create(1);
            const query = c.find({
                selector: {
                    passportId: {
                        $ne: 'foobar'
                    }
                }
            });
            await query.exec();
            const countBefore = query._execOverDatabaseCount;
            await Promise.all(
                new Array(10).fill(0).map(() => {
                    return query.$.pipe(first()).toPromise();
                })
            );
            const countAfter = query._execOverDatabaseCount;

            assert.strictEqual(countBefore, countAfter);

            c.database.destroy();
        });
        it('changing many documents in one write should not lead to many query result emits', async () => {
            const c = await humansCollection.create(0);

            const emitted: any[] = [];
            const sub = c.find().$.subscribe(results => emitted.push(results));
            await waitUntil(() => emitted.length > 0);

            await c.bulkInsert(
                new Array(10).fill(0).map(() => schemaObjects.human())
            );
            await wait(config.isFastMode() ? 50 : 100);
            assert.strictEqual(emitted.length, 2);

            sub.unsubscribe();
            c.database.destroy();
        });<commit_msg>IMPROVE logs<commit_after>import assert from 'assert';
import clone from 'clone';
import config from './config';


import * as schemaObjects from '../helper/schema-objects';
import * as schemas from '../helper/schemas';
import * as humansCollection from '../helper/humans-collection';

import AsyncTestUtil, { wait, waitUntil } from 'async-test-util';
import {
    createRxDatabase,
    RxDocument,
    isRxDocument,
    promiseWait,
    randomCouchString
} from '../../';

import {
    getRxStoragePouch
} from '../../plugins/pouchdb';

import {
    filter,
    map,
    first
} from 'rxjs/operators';
import { HumanDocumentType } from '../helper/schemas';

config.parallel('reactive-query.test.js', () => {
    config.parallel('positive', () => {
        it('get results of array when .subscribe() and filled array later', async () => {
            const c = await humansCollection.create(1);
            const query = c.find();
            let lastValue: any = null;
            let count = 0;
            query.$.subscribe(newResults => {
                count++;
                lastValue = newResults;
            });
            await AsyncTestUtil.waitUntil(() => count === 1);
            assert.ok(lastValue);
            assert.strictEqual(lastValue.length, 1);
            assert.strictEqual(count, 1);
            c.database.destroy();
        });
        it('get the updated docs on Collection.insert()', async () => {
            const c = await humansCollection.create(1);
            const query = c.find();
            let lastValue: any[] = [];
            const emitted: any[] = [];
            query.$.subscribe(newResults => {
                lastValue = newResults;
                emitted.push(newResults);
            });
            await waitUntil(() => emitted.length === 1);
            assert.strictEqual(lastValue.length, 1);

            const addHuman = schemaObjects.human();
            const newPromiseWait = AsyncTestUtil.waitResolveable(500);
            await c.insert(addHuman);
            await newPromiseWait.promise;
            assert.strictEqual(lastValue.length, 2);

            let isHere = false;
            lastValue.map(doc => {
                if (doc.get('passportId') === addHuman.passportId)
                    isHere = true;
            });
            assert.ok(isHere);
            c.database.destroy();
        });
        it('get the value twice when subscribing 2 times', async () => {
            const c = await humansCollection.create(1);
            const query = c.find();
            let lastValue: any[] = [];
            query.$.subscribe(newResults => {
                lastValue = newResults;
            });
            let lastValue2: any[] = [];
            query.$.subscribe(newResults => {
                lastValue2 = newResults;
            });
            await promiseWait(100);

            await AsyncTestUtil.waitUntil(() => lastValue2 && lastValue2.length === 1);
            assert.deepStrictEqual(lastValue, lastValue2);
            c.database.destroy();
        });
        it('get the base-value when subscribing again later', async () => {
            const c = await humansCollection.create(1);
            const query = c.find();
            let lastValue: any[] = [];
            query.$.subscribe(newResults => {
                lastValue = newResults;
            });
            await AsyncTestUtil.waitUntil(() => lastValue.length > 0);
            let lastValue2: any[] = [];
            query.$.subscribe(newResults => {
                lastValue2 = newResults;
            });
            await AsyncTestUtil.waitUntil(() => lastValue2.length > 0);
            await promiseWait(10);
            assert.strictEqual(lastValue2.length, 1);
            assert.deepStrictEqual(lastValue, lastValue2);
            c.database.destroy();
        });
        it('get new values on RxDocument.save', async () => {
            const c = await humansCollection.create(1);
            const doc: any = await c.findOne().exec(true);

            let values: any;
            const emitted: any[] = [];
            const querySub = c.find({
                selector: {
                    firstName: doc.get('firstName')
                }
            }).$.subscribe(newV => {
                values = newV;
                if (newV) {
                    emitted.push(newV);
                }
            });
            await waitUntil(() => emitted.length === 1);
            assert.strictEqual(values.length, 1);

            // change doc so query does not match
            const newPromiseWait = AsyncTestUtil.waitResolveable(500);
            await doc.atomicPatch({ firstName: 'foobar' });
            await newPromiseWait.promise;
            assert.strictEqual(values.length, 0);
            querySub.unsubscribe();
            c.database.destroy();
        });
        it('subscribing many times should not result in many database-requests', async () => {
            const c = await humansCollection.create(1);
            const query = c.find({
                selector: {
                    passportId: {
                        $ne: 'foobar'
                    }
                }
            });
            await query.exec();
            const countBefore = query._execOverDatabaseCount;
            await Promise.all(
                new Array(10).fill(0).map(() => {
                    return query.$.pipe(first()).toPromise();
                })
            );
            const countAfter = query._execOverDatabaseCount;

            assert.strictEqual(countBefore, countAfter);

            c.database.destroy();
        });
        it('changing many documents in one write should not lead to many query result emits', async () => {
            const c = await humansCollection.create(0);

            const emitted: RxDocument<HumanDocumentType>[][] = [];
            const sub = c.find().$.subscribe(results => emitted.push(results));
            await waitUntil(() => emitted.length > 0);

            await c.bulkInsert(
                new Array(10).fill(0).map(() => schemaObjects.human())
            );
            await wait(config.isFastMode() ? 50 : 100);
            assert.strictEqual(
                emitted.length, 2,
                JSON.stringify(emitted.map(result => result.map(doc => doc.toJSON())), null, 4)
            );

            sub.unsubscribe();
            c.database.destroy();
        });
{'fraction_non_alphanumeric': 0.12761440478062588, 'fraction_numerical': 0.008885044818367669, 'mean_word_length': 1.6349699606380774, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 66, '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': '2248427', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3170, 'n_words': 743}