datastax / astra-db-ts

Typescript client for Astra DB Vector
https://npmjs.com/@datastax/astra-db-ts
Apache License 2.0
15 stars 6 forks source link

Massive test suite overhaul [Phase 1] #66

Closed toptobes closed 2 months ago

toptobes commented 2 months ago

Major changes to the astra-db-ts test suite

This is the first stage (of two) of major test overhauls to the astra-db-ts test suite. The second will come as tests for Data API Tables are added.

Rationale

Beyond testing itself, astra-db-ts, along with the other Data API clients, serve as the last line of defense when it comes to testing the Data API—thus, it's important to have a comprehensive test suite that covers as much surface area as it can.

However, in our particular case, its's difficult to do so in an efficient, clean, and scalable manner with the default tooling Mocha provides out of the box. What this update does is write a light wrapper around the basic Mocha API to provide some baked-in functionality focused around the following things:

While admittedly adding in some getting-started-complexity, by virtue of having to learn a somewhat-new testing API, these changes have eliminated hundreds of lines of boilerplate throughout the actual test code, and, most importantly, has made the full astra-db-ts test ~500% faster, in a manner that is very generic and easily appliable to any and all future tests.

image

image

(The few fewer tests are because some duplicate/extraneous tests were removed)

Changes to the test suite itself

Preface

Before getting into it, here are some "common terms" that may be used throughout the rest of my rambling in this section

Mocha-API-inspired functions

The most prominent changes are the introduction of 5 new Mocha-API-esque functions (two of which are overhauls)

These are not globals like Mocha's—rather, they are imported, like so:

import { background, describe, it, parallel } from '@/tests/testlib';

Examples

You can find examples of usages of each in most, if not all, test files, such as:

Custom test filtering*

Before even implementing any of this, Mocha's native test filtering was always iffy with dynamically generated test cases (i.e. for vectorize), and with the addition of custom test description blocks, native test filtering would've been even more broken.

Instead, astra-db-ts now has a Mocha-inspired, yet enhanced, test-filtering implementation.

Usage is like so:

scripts/test.sh [~f/F <match_filter>] [~g/G <regex_filter] [-fand | -for] # (-fand is default)

You can use multiple filters, with -fand (the default) requiring them all to be true for a test to run, or -for requiring just one. You can also negate a filter (requiring it to NOT match) using ~f instead of -f. ~ takes precedence over - if filters overlap.

Examples:

scripts/test.sh -f VECTORIZE -F openai # Run only vectorize tests, except for openai
scripts/test.sh -f createCollection -f dropCollection -for # Run all tests for either createCollection or dropCollection

*Test filtering refers to filtering tests by their name—tag filtering is an astra-db-ts-specific concept that refers specifically to filtering by tags in the name, such as (LONG) createCollection tests

Custom error reporter

A custom error reporter, which extends the default spec error reporter, was also added, which logs the complete errors thrown in each test run to the ./etc/test-reports directory in md format. This uses util.inspect on the error to print the full error object for the most possible debuggability.

Changes to the test script

Preface

There have been so many changes to the test script, there's no point trying to list them out. So I'll just give a quick overview of the new API.

One important thing though, is the test script is no longer an alternative to using the Mocha command directly—you need to use the test script, as it performs some operations Mocha simply can't/won't.

Script commands

There are two ways to use the script:

# Running the test cases
scripts/test.sh [-all | -light | -coverage] [-fand | -for] [-f/F <filter>]+ [-g/G <regex>]+ [-w/W <vectorize_whitelist>] [-b | -bail] [-R | -no-report] [-c <http_client>] [-e <environment>]

or

# Linting + typechecking
scripts/test.sh [-lint] [-tc]

You can check out the DEVGUIDE for a full explanation of the above.

Changes to the env vars

The env vars names' have all been changed, the most prominent change of which is them all being prefixed with CLIENT_ now. Check the .env.example file for the new names.

That being said, beyond the CLIENT_APPLICATION_URI and CLIENT_APPLICATION_TOKEN, the .env.example file shouldn't be used much except for setting default values for certain env vars—using flags on the test script is more recommended.

Other changes

There are likely quite a few smaller changes I've missed or not covered here, but in general, reading the DEVGUIDE should tell you everything you need to know to run and write your own astra-db-ts tests.