Skip to content

Commit

Permalink
refactor(test): localize test assets (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
erfanium authored Aug 10, 2022
1 parent d5445a4 commit ed42dc1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 29 deletions.
Binary file added tests/assets/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
58 changes: 31 additions & 27 deletions tests/cases/06_gridfs.ts
Original file line number Diff line number Diff line change
@@ -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<Uint8Array> {
const reader = readerFromStreamReader(readable.getReader());
const result = await readAll(reader);
return result;
}

testWithClient("GridFS: Echo small Hello World", async (client) => {
Expand All @@ -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);
},
);

Expand Down
4 changes: 2 additions & 2 deletions tests/cases/09_geospatial_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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[] =
Expand Down
4 changes: 4 additions & 0 deletions tests/test.deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export {
} from "https://deno.land/[email protected]/testing/asserts.ts";
export { equals as bytesEquals } from "https://deno.land/[email protected]/bytes/equals.ts";
export * as semver from "https://deno.land/x/[email protected]/mod.ts";
export {
readAll,
readerFromStreamReader,
} from "https://deno.land/[email protected]/streams/mod.ts";

0 comments on commit ed42dc1

Please sign in to comment.