Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store #436

Merged
merged 7 commits into from
Sep 16, 2024
Merged

Store #436

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
with:
deno-version: vx.x.x

- run: deno lint --unstable http
- run: deno lint --unstable examples
- run: deno lint --unstable-kv core
- run: deno lint --unstable-kv examples

- name: Run tests
run: deno task test
Expand Down
2 changes: 1 addition & 1 deletion components/button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComponentChildren } from "https://esm.sh/preact@10.23.2";
import type { ComponentChildren } from "https://esm.sh/preact@10.24.0";

export default function Button(props: { children: ComponentChildren }) {
return (
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
83 changes: 83 additions & 0 deletions core/map/map.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { assert, assertEquals } from "../server/deps.ts";
import { Store } from "./map.ts";

Deno.test("ExpiringMap: should set and get a value", async () => {
const expiringMap = new Store<string, string>(1000); // 1 second TTL
expiringMap.set("key1", "value1");
assertEquals(await expiringMap.get("key1"), "value1");
});

Deno.test("ExpiringMap: should return undefined for expired keys", async () => {
const expiringMap = new Store<string, string>(100);
expiringMap.set("key1", "value1");
await new Promise((resolve) => setTimeout(resolve, 200));
assertEquals(await expiringMap.get("key1"), undefined);
});

Deno.test("ExpiringMap: should check if a key exists", async () => {
const expiringMap = new Store<string, string>(100);
expiringMap.set("key1", "value1");
assert(expiringMap.has("key1"));
await new Promise((resolve) => setTimeout(resolve, 200));
assert(!expiringMap.has("key1"));
});

Deno.test("ExpiringMap: should delete a key", async () => {
const expiringMap = new Store<string, string>();
expiringMap.set("key1", "value1");
assert(expiringMap.delete("key1"));
assertEquals(await expiringMap.get("key1"), undefined);
});

Deno.test("ExpiringMap: should clear all entries", () => {
const expiringMap = new Store<string, string>();
expiringMap.set("key1", "value1");
expiringMap.set("key2", "value2");
expiringMap.clear();
assertEquals(expiringMap.size(), 0);
});

Deno.test("ExpiringMap: should get the size of the map", () => {
const expiringMap = new Store<string, string>();
expiringMap.set("key1", "value1");
expiringMap.set("key2", "value2");
assertEquals(expiringMap.size(), 2);
});

Deno.test("ExpiringMap: should iterate over valid entries using forEach", () => {
const expiringMap = new Store<string, string>(1000);
expiringMap.set("key1", "value1");
expiringMap.set("key2", "value2");

const entries: [string, string][] = [];
expiringMap.forEach((value, key) => {
entries.push([key, value]);
});

assertEquals(entries.length, 2);
assertEquals(entries, [["key1", "value1"], ["key2", "value2"]]);
});

Deno.test("ExpiringMap: should set a key with custom TTL", async () => {
const expiringMap = new Store<string, string>(2000);
expiringMap.set("key1", "value1", 1000);
assertEquals(await expiringMap.get("key1"), "value1");
await new Promise((resolve) => setTimeout(resolve, 1100));
assertEquals(await expiringMap.get("key1"), undefined);
});

Deno.test("ExpiringMap: save it to github", async () => {
const token = Deno.env.get("GITHUB_TOKEN");
if (!token) return;
const expiringMap = new Store<string, number>(2000, {
owner: "fastrodev",
repo: "fastro",
path: "modules/store/records.json",
branch: "store",
token,
});
const d = new Date();
expiringMap.set("key1", d.getTime(), 1000);
const c = await expiringMap.commit();
assertEquals(c?.data.content?.name, "records.json");
});
Loading