-
Notifications
You must be signed in to change notification settings - Fork 46
initial public api spec #299
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { mockSuperThis } from "../helpers.js"; | ||
import { CID } from "multiformats/cid"; | ||
|
||
import { Database, DatabaseFactory, DocResponse, DocWithId, IndexRows, bs, fireproof, index, isDatabase } from "@fireproof/core"; | ||
|
||
export function carLogIncludesGroup(list: bs.AnyLink[], cid: CID) { | ||
return list.some((c) => c.equals(cid)); | ||
} | ||
|
||
interface FooType { | ||
readonly foo: string; | ||
} | ||
|
||
describe("public API", function () { | ||
interface Doc { | ||
foo: string; | ||
} | ||
let db: Database; | ||
let ok: DocResponse; | ||
let doc: DocWithId<Doc>; | ||
let query: IndexRows<string, Doc>; | ||
const sthis = mockSuperThis(); | ||
|
||
afterEach(async () => { | ||
await db.close(); | ||
await db.destroy(); | ||
}); | ||
|
||
beforeEach(async function () { | ||
await sthis.start(); | ||
db = fireproof("test-api"); | ||
// index = index(db, 'test-index', (doc) => doc.foo) | ||
ok = await db.put({ _id: "test", foo: "bar" }); | ||
doc = await db.get("test"); | ||
query = await db.query<string, Doc>((doc) => doc.foo); | ||
}); | ||
it("should be a database instance", function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. function => |
||
expect(db).toBeTruthy(); | ||
expect(isDatabase(db)).toBeTruthy(); | ||
}); | ||
it("should put", function () { | ||
expect(ok).toBeTruthy(); | ||
expect(ok.id).toBe("test"); | ||
}); | ||
it("should get", function () { | ||
expect(doc.foo).toBe("bar"); | ||
}); | ||
it("should query", function () { | ||
expect(query).toBeTruthy(); | ||
expect(query.rows).toBeTruthy(); | ||
expect(query.rows.length).toBe(1); | ||
expect(query.rows[0].key).toBe("bar"); | ||
}); | ||
}); | ||
|
||
describe("basic database", function () { | ||
interface Doc { | ||
foo: string; | ||
} | ||
let db: Database<Doc>; | ||
const sthis = mockSuperThis(); | ||
afterEach(async function () { | ||
await db.close(); | ||
await db.destroy(); | ||
}); | ||
beforeEach(async function () { | ||
await sthis.start(); | ||
db = DatabaseFactory("test-basic"); | ||
}); | ||
it("can put with id", async function () { | ||
const ok = await db.put({ _id: "test", foo: "bar" }); | ||
expect(ok).toBeTruthy(); | ||
expect(ok.id).toBe("test"); | ||
}); | ||
it("can put without id", async function () { | ||
const ok = await db.put({ foo: "bam" }); | ||
expect(ok).toBeTruthy(); | ||
const got = await db.get<Doc>(ok.id); | ||
expect(got.foo).toBe("bam"); | ||
}); | ||
it("can define an index", async function () { | ||
const ok = await db.put({ _id: "test", foo: "bar" }); | ||
expect(ok).toBeTruthy(); | ||
const idx = index<string, { foo: string }>(db, "test-index", (doc) => doc.foo); | ||
const result = await idx.query(); | ||
expect(result).toBeTruthy(); | ||
expect(result.rows).toBeTruthy(); | ||
expect(result.rows.length).toBe(1); | ||
expect(result.rows[0].key).toBe("bar"); | ||
}); | ||
it("can define an index with a default function", async function () { | ||
const ok = await db.put({ _id: "test", foo: "bar" }); | ||
expect(ok).toBeTruthy(); | ||
const idx = index(db, "foo"); | ||
const result = await idx.query(); | ||
expect(result).toBeTruthy(); | ||
expect(result.rows).toBeTruthy(); | ||
expect(result.rows.length).toBe(1); | ||
expect(result.rows[0].key).toBe("bar"); | ||
}); | ||
}); | ||
|
||
describe("Reopening a database", function () { | ||
interface Doc { | ||
foo: string; | ||
} | ||
let db: Database; | ||
const sthis = mockSuperThis(); | ||
afterEach(async function () { | ||
await db.close().catch(() => true); | ||
await db.destroy(); | ||
}); | ||
beforeEach(async function () { | ||
// erase the existing test data | ||
await sthis.start(); | ||
|
||
db = DatabaseFactory("test-reopen-spec", { autoCompact: 100000 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not fireproof? |
||
const ok = await db.put({ _id: "test", foo: "bar" }); | ||
expect(ok).toBeTruthy(); | ||
expect(ok.id).toBe("test"); | ||
}); | ||
|
||
it("should persist data", async function () { | ||
const doc = await db.get<Doc>("test"); | ||
expect(doc.foo).toBe("bar"); | ||
}); | ||
|
||
it("should have the same data on reopen", async function () { | ||
await db.close(); | ||
const db2 = DatabaseFactory("test-reopen-spec"); | ||
const doc = await db2.get<FooType>("test"); | ||
expect(doc.foo).toBe("bar"); | ||
await db2.close(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async () =>