Skip to content

refactor mapreduce tests with async/await#9176

Draft
emilygilberts wants to merge 12 commits intoapache:masterfrom
emilygilberts:refactor-unit-test-mapreduce
Draft

refactor mapreduce tests with async/await#9176
emilygilberts wants to merge 12 commits intoapache:masterfrom
emilygilberts:refactor-unit-test-mapreduce

Conversation

@emilygilberts
Copy link
Copy Markdown
Contributor

@emilygilberts emilygilberts commented Feb 19, 2026

tests/unit

  • rewrote the unit tests for mapreduce.js-upsert using async/await
    (left the mapreduce.js-utils tests as they are, because they are more readable & straightforward with the current syntax)

tests/mapreduce (in process)

  • started with some ES6 refactoring of the tests by replacing var with const/let
  • rewrote promise chains to async/await
  • used arrow functions
  • rewrote mocha chai should syntax: (before: should.become to should.deep.equal for objects or should.be.true for boolean)
  • replaced .toString() with template literals (except on view map function declarations)

tests/mapreduce/test.mapreduce.js
in test Query result should include _conflicts and Views should include conflicts :

  • replaced the use of testUtils.fin() with try/finally block
  • replaced the use of promisify with simply awaiting the replication - so that testUtils.promisify(remote, db.replicate); becomes await db.replicate.from(remote);

in test Test view querying with custom reduce function:

  • in custom reduce function: replaced nested unrolling of values with values.flat()
  • remoed unneccesary line pulling out keyId[0]
  • ? can the comment //const id = keyId[1]; be deleted?

Copy link
Copy Markdown
Member

@janl janl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks good overall, I’m just wondering if it would make sense to DRY out the get and put functions a little bit, it’s three lines each when the only difference is the what the promise resolves with.

Spitballing here:

instead of

   const res = await upsert(
      {
        get: function () {
          return Promise.reject({ status: 404 });
        },
        put: function () {
          return Promise.resolve({ rev: 'abc' });
        },
      },
      'foo',
      function () {
        return { difference: "something" };
      },
    );
function makeFun (action, value) {
  return function() { return Promise[action](value) };
}

// [...]

    const res = await upsert(
      {
        get: makeFun('resolve', {status: 404}),
        put: makeFun('reject', { rev: 'abc' }),
      },
      'foo',
      function () { return { difference: "something" };
      },
    );

Might be too fancy, feel free to shoot me down :)

it('should throw an error if the doc errors', async function () {
try {
await upsert(
{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a style nit, but I’d be okay with having the opening curlies start with the method: upsert({ to save a mostly empty line

@emilygilberts
Copy link
Copy Markdown
Contributor Author

emilygilberts commented Feb 26, 2026

@janl hmm how about using arrow functions for the functions in get and put? I think it could look neat/be readable cause they are all one liners.

@emilygilberts emilygilberts force-pushed the refactor-unit-test-mapreduce branch from 3dd1915 to f1a1b9c Compare February 26, 2026 20:37
@emilygilberts emilygilberts marked this pull request as draft March 20, 2026 14:03
@emilygilberts emilygilberts changed the title rewrite mapreduce upsert tests with async/await refactor mapreduce upsert tests with async/await Mar 20, 2026
@emilygilberts emilygilberts changed the title refactor mapreduce upsert tests with async/await refactor mapreduce tests with async/await Mar 20, 2026
@emilygilberts
Copy link
Copy Markdown
Contributor Author

emilygilberts commented Mar 25, 2026

UPDATE: ahhh my local CouchDB wasn't properly connected ... nvm :)

I'm running the tests locally with TYPE=mapreduce npm run test-node. Any idea why they all pass for test.mapreduce.js-persisted, but for test.mapreduce.js-temp the first test executed jumps out with a timeout error?

Bildschirmfoto 2026-03-25 um 14 51 41

@emilygilberts
Copy link
Copy Markdown
Contributor Author

hmm wondering - in some tests we had promise chains that did multiple queries that returned res to then be evaluated with should expressions. I always reassigned res to keep the same names as before, but I'm guessing it would be better style to use const and res, res2,... ?

await db.bulkDocs(docs);

const queryFun = await createView(db, {
map: doc => emit(doc._id)
Copy link
Copy Markdown
Contributor Author

@emilygilberts emilygilberts Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, could it be a problem in some setups that I changes the map function to an arrow function? It will be converted to a string in the createView function:

const storableViewObj = {
          map: `${viewObj.map}`
        };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fine in tests, but lets leave them in their CouchDB-canonical traditional function form

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants