diff --git a/tests/assets/1.jpg b/tests/assets/1.jpg new file mode 100644 index 0000000..471fce0 Binary files /dev/null and b/tests/assets/1.jpg differ diff --git a/tests/assets/2.jpg b/tests/assets/2.jpg new file mode 100644 index 0000000..a9055c7 Binary files /dev/null and b/tests/assets/2.jpg differ diff --git a/tests/testdata/sample_neighborhoods.json b/tests/assets/sample_neighborhoods.json similarity index 100% rename from tests/testdata/sample_neighborhoods.json rename to tests/assets/sample_neighborhoods.json diff --git a/tests/testdata/sample_places.json b/tests/assets/sample_places.json similarity index 100% rename from tests/testdata/sample_places.json rename to tests/assets/sample_places.json diff --git a/tests/cases/06_gridfs.ts b/tests/cases/06_gridfs.ts index 1154401..cb0860f 100644 --- a/tests/cases/06_gridfs.ts +++ b/tests/cases/06_gridfs.ts @@ -1,9 +1,17 @@ import { GridFSBucket } from "../../mod.ts"; import { testWithClient } from "../common.ts"; -import { assert, assertEquals, bytesEquals } from "../test.deps.ts"; - -function bufferEquals(a: ArrayBuffer, b: ArrayBuffer) { - return bytesEquals(new Uint8Array(a), new Uint8Array(b)); +import { + assert, + assertEquals, + assertNotEquals, + readAll, + readerFromStreamReader, +} from "../test.deps.ts"; + +async function streamReadAll(readable: ReadableStream): Promise { + const reader = readerFromStreamReader(readable.getReader()); + const result = await readAll(reader); + return result; } testWithClient("GridFS: Echo small Hello World", async (client) => { @@ -27,50 +35,46 @@ testWithClient("GridFS: Echo small Hello World", async (client) => { testWithClient("GridFS: Echo large Image", async (client) => { const bucket = new GridFSBucket(client.database("test"), { - bucketName: "deno_logo", + bucketName: "A", }); // Set an impractically low chunkSize to test chunking algorithm - const upstream = await bucket.openUploadStream("deno_logo.png", { + const upstream = await bucket.openUploadStream("1.jpg", { chunkSizeBytes: 255 * 8, }); - await (await fetch("https://deno.land/images/deno_logo.png")).body!.pipeTo( - upstream, - ); + const image = await Deno.open("tests/assets/1.jpg", { read: true }); + await image.readable.pipeTo(upstream); - const getId = - (await bucket.find({ filename: "deno_logo.png" }).toArray())[0]._id; + const [{ _id }] = await bucket.find({ filename: "1.jpg" }).toArray(); - const expected = await fetch("https://deno.land/images/deno_logo.png"); - const actual = await new Response(await bucket.openDownloadStream(getId)) - .arrayBuffer(); + const expected = await Deno.readFile("tests/assets/1.jpg"); + const actual = await streamReadAll(await bucket.openDownloadStream(_id)); - assert(bufferEquals(actual, await expected.arrayBuffer())); + assertEquals(actual, expected); }); testWithClient( "GridFS: Echo large Image (compare with different Image)", async (client) => { const bucket = new GridFSBucket(client.database("test"), { - bucketName: "deno_logo", + bucketName: "A", }); - const upstream = await bucket.openUploadStream("deno_logo.png"); + // Set an impractically low chunkSize to test chunking algorithm + const upstream = await bucket.openUploadStream("1.jpg", { + chunkSizeBytes: 255 * 8, + }); - await (await fetch("https://deno.land/images/deno_logo.png")).body! - .pipeTo( - upstream, - ); + const image = await Deno.open("tests/assets/1.jpg", { read: true }); + await image.readable.pipeTo(upstream); - const getId = - (await bucket.find({ filename: "deno_logo.png" }).toArray())[0]._id; + const [{ _id }] = await bucket.find({ filename: "1.jpg" }).toArray(); - const expected = await fetch("https://deno.land/images/deno_logo_4.gif"); - const actual = await new Response(await bucket.openDownloadStream(getId)) - .arrayBuffer(); + const notExpected = await Deno.readFile("tests/assets/2.jpg"); + const actual = await streamReadAll(await bucket.openDownloadStream(_id)); - assert(!bufferEquals(actual, await expected.arrayBuffer())); + assertNotEquals(actual, notExpected); }, ); diff --git a/tests/cases/09_geospatial_types.ts b/tests/cases/09_geospatial_types.ts index 0bfc919..e1a821a 100644 --- a/tests/cases/09_geospatial_types.ts +++ b/tests/cases/09_geospatial_types.ts @@ -61,7 +61,7 @@ type LegacyNearSphereDocumentQuery = { } & DistanceConstraint; const placeDataString = await Deno.readTextFile( - "tests/testdata/sample_places.json", + "tests/assets/sample_places.json", ); // deno-lint-ignore no-explicit-any @@ -78,7 +78,7 @@ const placeData: IPlace[] = (JSON.parse(placeDataString) as any[]) })); const neighborhoodsDataString = await Deno.readTextFile( - "tests/testdata/sample_neighborhoods.json", + "tests/assets/sample_neighborhoods.json", ); const neighborhoodsData: INeighborhoods[] = diff --git a/tests/test.deps.ts b/tests/test.deps.ts index fef9216..fe4b169 100644 --- a/tests/test.deps.ts +++ b/tests/test.deps.ts @@ -7,3 +7,7 @@ export { } from "https://deno.land/std@0.149.0/testing/asserts.ts"; export { equals as bytesEquals } from "https://deno.land/std@0.149.0/bytes/equals.ts"; export * as semver from "https://deno.land/x/semver@v1.4.0/mod.ts"; +export { + readAll, + readerFromStreamReader, +} from "https://deno.land/std@0.149.0/streams/mod.ts";