Skip to content

Commit 16e1659

Browse files
committed
overhaul of test filtering + some bug fixes
1 parent 8a5c556 commit 16e1659

34 files changed

+414
-184
lines changed

.env.example

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
# Astra API endpoint (THIS IS FOR RUNNING THE TESTS, NOT FOR BASIC CLIENT USAGE)
1+
# Astra API endpoint
22
ASTRA_URI=https://<db_id>-<region>.apps.astra.datastax.com
33

4-
# Application token, used to authenticate with the Astra API (THIS IS FOR RUNNING THE TESTS, NOT FOR BASIC CLIENT USAGE)
4+
# Application token, used to authenticate with the Astra API
55
APPLICATION_TOKEN=AstraCS:<rest_of_token>
6+
7+
# Set this to some value to enable running tests that require a dev environment (make sure you're actually using the dev environment!!)
8+
ASTRA_RUN_DEV_TESTS=
9+
10+
# Set this to some value to enable running long-running tests
11+
ASTRA_RUN_LONG_TESTS=
12+
13+
# Set this to some value to enable running admin tests
14+
ASTRA_RUN_ADMIN_TESTS=

DEVGUIDE.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,44 @@ npm run test:coverage
2626
npm run test:types
2727
```
2828

29-
I heavily recommend filtering out the tag `[long]` when you're just doing some smoke-ish testing.
29+
### Running tagged tests
30+
Tests can be given certain tags to allow for more granular control over which tests are run. These tags currently include:
31+
- `[long]`/`'LONG'`: Longer running tests that take more than a few seconds to run
32+
- `[admin]`/`'ADMIN'`: Tests that require admin permissions to run
33+
- `[dev]`/`'DEV'`: Tests that require the dev environment to run
34+
35+
To enable these tags, you can set the corresponding environment variables to some values. The env variables are in the
36+
`env.example` file, but they're repeated here for convenience:
37+
- `ASTRA_RUN_DEV_TESTS`
38+
- `ASTRA_RUN_LONG_TESTS`
39+
- `ASTRA_RUN_ADMIN_TESTS`
40+
41+
Or you can run the tests by doing something like
3042
```shell
31-
npm run test -- -f [long] -i
43+
env ASTRA_RUN_LONG_TESTS=1 npm run test
44+
```
45+
46+
### Adding your own tagged tests
47+
To enforce the tags, use the `assertTestsEnabled` function from `test/fixtures.ts`, which will skip the function if the
48+
given tag is not enabled.
49+
50+
It's also encouraged to add the corresponding tag to the test name, so that it's clear why the test is being skipped.
51+
52+
For example:
53+
```typescript
54+
describe('[long] createCollection + dropCollection', () => {
55+
// Note that it's important to use an actual function here, not an arrow function
56+
before(async function () {
57+
assertTestsEnabled(this, 'LONG');
58+
});
59+
60+
// ...
61+
});
3262
```
3363

34-
If you're adding a test, that isn't absolutely essential, that takes over ~2-3 seconds to run, please add the `[long]` tag to it.
64+
If a new tag really, really, needs to be added, it can be done by adding a new environment variable of the proper
65+
format, and updating the `assertTestsEnabled` function. However, this should be done sparingly, as it can make the
66+
test suite harder to manage.
3567

3668
### Linting
3769
Run `npm run lint` to run ESLint.

src/data-api/collection.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
177177
insertOne: { document },
178178
}
179179

180+
if (options?.vector && options?.vectorize) {
181+
throw new Error('Cannot set both vector and vectorize options');
182+
}
183+
180184
if (options?.vector) {
181185
command.insertOne.document = { ...command.insertOne.document, $vector: options.vector };
182186
}
@@ -268,6 +272,10 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
268272
async insertMany(documents: MaybeId<Schema>[], options?: InsertManyOptions): Promise<InsertManyResult<Schema>> {
269273
const chunkSize = options?.chunkSize ?? 20;
270274

275+
if (options?.vectors && options?.vectorize) {
276+
throw new Error('Cannot set both vectors and vectorize options');
277+
}
278+
271279
if (options?.vectors) {
272280
if (options.vectors.length !== documents.length) {
273281
throw new Error('The number of vectors must match the number of documents');

src/data-api/types/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import type { SomeId } from '@/src/data-api/types';
2121
* ```
2222
* IsNum<string | number> === true
2323
* ```
24+
*
25+
* @internal
2426
*/
2527
export type IsNum<T> = number extends T ? true : bigint extends T ? true : false
2628

@@ -31,9 +33,16 @@ export type IsNum<T> = number extends T ? true : bigint extends T ? true : false
3133
* ```
3234
* IsDate<string | Date> === boolean
3335
* ```
36+
*
37+
* @internal
3438
*/
3539
export type IsDate<T> = IsAny<T> extends true ? true : T extends Date | { $date: number } ? true : false
3640

41+
/**
42+
* Checks if a type is any
43+
*
44+
* @internal
45+
*/
3746
export type IsAny<T> = true extends false & T ? true : false
3847

3948
/**
@@ -81,6 +90,8 @@ export type IdOf<TSchema> =
8190

8291
/**
8392
* Makes all fields in a type mutable
93+
*
94+
* @internal
8495
*/
8596
export type Mutable<T> = {
8697
-readonly [P in keyof T]: T[P];

src/devops/astra-admin.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AdminBlockingOptions, DatabaseConfig, FullDatabaseInfo, ListDatabasesOptions } from '@/src/devops/types';
22
import { Db, mkDb } from '@/src/data-api';
3-
import { DEFAULT_DEVOPS_API_ENDPOINT, DevopsApiHttpClient, HTTP_METHODS } from '@/src/api';
3+
import { DEFAULT_DEVOPS_API_ENDPOINT, DEFAULT_NAMESPACE, DevopsApiHttpClient, HTTP_METHODS } from '@/src/api';
44
import { AstraDbAdmin } from '@/src/devops/astra-db-admin';
55
import { AdminSpawnOptions, DbSpawnOptions, RootClientOptsWithToken } from '@/src/client/types';
66

@@ -59,26 +59,29 @@ export class AstraAdmin {
5959
}
6060

6161
public async createDatabase(config: DatabaseConfig, options?: AdminBlockingOptions): Promise<AstraDbAdmin> {
62+
const definition = {
63+
capacityUnits: 1,
64+
tier: 'serverless',
65+
dbType: 'vector',
66+
keyspace: config.namespace || DEFAULT_NAMESPACE,
67+
...config,
68+
}
69+
6270
const resp = await this._httpClient.request({
6371
method: HTTP_METHODS.Post,
6472
path: '/databases',
65-
data: {
66-
capacityUnits: 1,
67-
tier: 'serverless',
68-
dbType: 'vector',
69-
...config,
70-
},
73+
data: definition,
7174
});
7275

73-
const db = mkDb(this.#defaultOpts, resp.headers.location, config.region, { namespace: resp.data.info.keyspace });
76+
const db = mkDb(this.#defaultOpts, resp.headers.location, definition.region, { namespace: definition.keyspace });
7477
const admin = db.admin(this.#defaultOpts.devopsOptions);
7578

7679
await this._httpClient.awaitStatus(db, 'ACTIVE', ['INITIALIZING', 'PENDING'], options, 10000);
7780
return admin;
7881
}
7982

80-
async dropDatabase(_db: Db | string, options?: AdminBlockingOptions): Promise<void> {
81-
const id = typeof _db === 'string' ? _db : _db.id;
83+
async dropDatabase(db: Db | string, options?: AdminBlockingOptions): Promise<void> {
84+
const id = typeof db === 'string' ? db : db.id;
8285

8386
await this._httpClient.request({
8487
method: HTTP_METHODS.Post,

src/devops/types/admin/create-database.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export interface DatabaseConfig {
3434
* The cloud region where the database is located.
3535
*/
3636
region: string,
37+
/**
38+
* The default namespace to use for the database.
39+
*/
40+
namespace?: string,
3741
}
3842

3943
export type AdminBlockingOptions =

src/devops/types/admin/list-databases.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface ListDatabasesOptions {
2626
/**
2727
* Allows filtering so that databases in listed states are returned.
2828
*/
29-
include?: Lowercase<DatabaseStatus>,
29+
include?: DatabaseStatus,
3030
/**
3131
* Allows filtering so that databases from a given provider are returned.
3232
*/
@@ -36,6 +36,8 @@ export interface ListDatabasesOptions {
3636
*
3737
* Should be between 1 and 100.
3838
*
39+
* Defaults to 25.
40+
*
3941
* @defaultValue 25
4042
*/
4143
limit?: number,

src/testing.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/e2e/.npmrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/e2e/index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)