Skip to content

Commit 9b26268

Browse files
authored
Initial tables tests (#82)
* a bunch of tests work * couple minor test fixes
1 parent c8dc9fa commit 9b26268

File tree

16 files changed

+685
-32
lines changed

16 files changed

+685
-32
lines changed

DEVGUIDE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ The most prominent changes are the introduction of 5 new Mocha-API-esque functio
277277
- Some suite options to reduce boilerplate
278278
- `truncateColls: 'default'` - Does `deleteMany({})` on the default collection in the default namespace after each test case
279279
- `truncateColls: 'both'` - Does `deleteMany({})` on the default collection in both test namespaces after each test case
280-
- `dropEphemeral: 'after'` - Drops all non-default collections in both test namespaces after all the test cases in the suite
281-
- `dropEphemeral: 'afterEach'` - Drops all non-default collections in both test namespaces each test case
280+
- `drop: 'after'` - Drops all non-default collections in both test namespaces after all the test cases in the suite
281+
- `drop: 'afterEach'` - Drops all non-default collections in both test namespaces each test case
282282
- [`it`](https://github.com/datastax/astra-db-ts/blob/60fa445192b6a648b7a139a45986af8525a37ffb/tests/testlib/it.ts) - An overhaul to the existing `it` block
283283
- Performs "tag filtering" on the test names
284284
- Provides unique string keys for every test case

src/db/types/tables/table-schema.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,13 @@ interface CqlNonGenericType2TSTypeDict {
280280
uuid: UUID | null,
281281
varchar: string | null,
282282
varint: bigint | null,
283-
vector: DataAPIVector | null,
284283
}
285284

286285
interface CqlGenericType2TSTypeDict<Def> {
287286
map: CqlMapType2TsType<Def>,
288287
list: CqlListType2TsType<Def>,
289288
set: CqlSetType2TsType<Def>,
289+
vector: CqlVectorType2TsType<Def> | null,
290290
}
291291

292292
type CqlMapType2TsType<Def> =
@@ -303,3 +303,8 @@ type CqlSetType2TsType<Def> =
303303
Def extends { valueType: infer ValueType extends string }
304304
? Set<CqlType2TSType<ValueType, never> & {}>
305305
: TypeErr<'Invalid generics definition for \'set\'; should have valueType set as scalar CQL types (e.g. \'text\')'>;
306+
307+
type CqlVectorType2TsType<Def> =
308+
Def extends { service: unknown }
309+
? DataAPIVector | string
310+
: DataAPIVector;

src/documents/tables/ser-des.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,18 @@ const DefaultTableSerDesCfg = {
108108
return [[...value]];
109109
}
110110

111-
if (!ctx.bigNumsPresent && value instanceof BigNumber) {
111+
if (value instanceof BigNumber) {
112112
ctx.bigNumsPresent = true;
113+
return true;
113114
}
114115
} else if (!ctx.bigNumsPresent && typeof value === 'bigint') {
115116
ctx.bigNumsPresent = true;
117+
return true;
118+
} else if (typeof value === 'number') {
119+
if (!isFinite(value)) {
120+
return [value.toString(), true];
121+
}
122+
return [value, true];
116123
}
117124
},
118125
deserialize(key, _, ctx) {

tests/integration/db/db.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { DataAPIResponseError } from '@/src/documents';
2828
import { DataAPIClient } from '@/src/client';
2929
import { DEFAULT_DATA_API_PATHS, DEFAULT_KEYSPACE } from '@/src/lib/api/constants';
3030

31-
parallel('integration.db', { dropEphemeral: 'colls:after' }, ({ db }) => {
31+
parallel('integration.db', { drop: 'colls:after' }, ({ db }) => {
3232
describe('(LONG) createCollection', () => {
3333
it('should create a collections', async () => {
3434
const res = await db.createCollection('coll_1c', { indexing: { deny: ['*'] } });

tests/integration/documents/collections/options.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import { DEFAULT_COLLECTION_NAME, it, parallel } from '@/tests/testlib';
1616
import assert from 'assert';
1717

18-
parallel('integration.documents.collections.options', { dropEphemeral: 'colls:after' }, ({ db }) => {
18+
parallel('integration.documents.collections.options', { drop: 'colls:after' }, ({ db }) => {
1919
it('lists its own options', async () => {
2020
const coll = db.collection(DEFAULT_COLLECTION_NAME);
2121
const res = await coll.options();

tests/integration/documents/ids.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import assert from 'assert';
1717
import { ObjectId, UUID } from '@/src/documents';
1818
import { useSuiteResources, DEFAULT_COLLECTION_NAME, it, parallel } from '@/tests/testlib';
1919

20-
parallel('(LONG) integration.documents.ids', { dropEphemeral: 'colls:after' }, ({ db }) => {
20+
parallel('(LONG) integration.documents.ids', { drop: 'colls:after' }, ({ db }) => {
2121
const collections = useSuiteResources(() => ({
2222
default: db.collection(DEFAULT_COLLECTION_NAME).deleteMany({}).then(_ => db.collection(DEFAULT_COLLECTION_NAME)),
2323
uuid: db.createCollection(`${DEFAULT_COLLECTION_NAME}_uuid`, { defaultId: { type: 'uuid' } }),

tests/integration/documents/tables/cursor.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
import { DataAPIVector, SomeDoc } from '@/src/documents';
1717
import {
1818
DEFAULT_TABLE_NAME,
19-
parallel, EverythingTableSchema,
19+
describe,
20+
EverythingTableSchema,
2021
initCollectionWithFailingClient,
2122
it,
2223
OTHER_KEYSPACE,
23-
describe,
24+
parallel,
2425
} from '@/tests/testlib';
2526
import assert from 'assert';
2627

@@ -90,7 +91,7 @@ describe('integration.documents.table.cursor', { truncate: 'tables:before' }, ({
9091

9192
assert.ok(doc, 'Doc is not the 21st in the collections');
9293
assert.equal(cursor.state, 'started');
93-
// assert.strictEqual(cursor.buffered(), 19);
94+
assert.strictEqual(cursor.buffered(), 19);
9495
});
9596

9697
it('should return null if there are no more documents with next()', async () => {
@@ -251,7 +252,7 @@ describe('integration.documents.table.cursor', { truncate: 'tables:before' }, ({
251252
});
252253

253254
it('should return sort vector on only first API call if includeSortVector: true', async () => {
254-
const cursor = table_.find({}).sort({ vector: [1, 1, 1, 1, 1] }).includeSortVector();
255+
const cursor = table.find({}).sort({ vector: [1, 1, 1, 1, 1] }).includeSortVector();
255256
const start1 = performance.now();
256257
assert.deepStrictEqual(await cursor.getSortVector(), [1, 1, 1, 1, 1]);
257258
assert.ok(performance.now() - start1 > 5);
@@ -267,7 +268,7 @@ describe('integration.documents.table.cursor', { truncate: 'tables:before' }, ({
267268
});
268269

269270
it('should return null in getSortVector if includeSortVector: false', async () => {
270-
const cursor = table_.find({}).sort({ vector: [1, 1, 1, 1, 1] });
271+
const cursor = table.find({}).sort({ vector: [1, 1, 1, 1, 1] });
271272
await cursor.hasNext();
272273
assert.deepStrictEqual(await cursor.getSortVector(), null);
273274
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright DataStax, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
// noinspection DuplicatedCode
15+
16+
import { DataAPIResponseError } from '@/src/documents';
17+
import { it, parallel } from '@/tests/testlib';
18+
import assert from 'assert';
19+
20+
parallel('integration.documents.tables.delete-one', { truncate: 'colls:before' }, ({ table }) => {
21+
it('should error on sort being set', async (key) => {
22+
await assert.rejects(() => table.deleteOne({ text: key, int: 0 }, { sort: { int: 1 } }), DataAPIResponseError);
23+
await assert.rejects(() => table.deleteOne({ text: key, int: 0 }, { sort: { vector: [.1, .2, .3, .4, .5] } }), DataAPIResponseError);
24+
});
25+
26+
it('should delete one document with various filters', async (key1, key2) => {
27+
await table.insertOne({ text: key1, int: 0 });
28+
await table.deleteOne({ text: key1, int: { $gt: 0 } });
29+
assert.deepStrictEqual((await table.findOne({ text: key1, int: 0 }))?.int, 0);
30+
31+
await table.insertOne({ text: key2, int: 1 });
32+
await table.deleteOne({ text: key2, int: { $gt: 0 } });
33+
assert.deepStrictEqual(await table.findOne({ text: key2, int: 1 }), null);
34+
});
35+
36+
it('should delete many documents with $ne', async (key) => {
37+
await table.insertMany(Array.from({ length: 10 }, (_, i) => ({ text: key, int: i })));
38+
await table.deleteOne({ text: key, int: { $ne: 5 } });
39+
40+
for (let i = 0; i < 10; i++) {
41+
if (i === 5) {
42+
assert.deepStrictEqual(await table.findOne({ text: key, int: i }, { projection: { text: 1, int: 1 } }), { text: key, int: i });
43+
} else {
44+
assert.deepStrictEqual(await table.findOne({ text: key, int: i }), null);
45+
}
46+
}
47+
});
48+
});

tests/integration/documents/tables/find-one.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { EverythingTableSchema, it, parallel } from '@/tests/testlib';
2727
import assert from 'assert';
2828
import BigNumber from 'bignumber.js';
2929

30-
parallel('integration.documents.tables.find-one', { truncate: 'colls:before' }, ({ table }) => {
30+
parallel('integration.documents.tables.find-one', { truncate: 'colls:before', drop: 'tables:after' }, ({ table }) => {
3131
it('should find one partial row', async (key) => {
3232
const uuid = UUID.v4();
3333

0 commit comments

Comments
 (0)