Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Introduce test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bfollington committed Mar 20, 2024
1 parent 44b9bcb commit e5212fc
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 4 deletions.
3 changes: 2 additions & 1 deletion xcode/Subconscious/Shared/Components/Deck/DeckView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ struct DeckModel: ModelProtocol {

guard let entry = environment.database.readRandomUnseenEntry(
owner: us,
seen: state.seen.map { entry in entry.address }
seen: state.seen.map { entry in entry.address },
minimumBodyLength: 64
) else {
return .topupDeck
}
Expand Down
13 changes: 10 additions & 3 deletions xcode/Subconscious/Shared/Services/DatabaseService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,11 @@ final class DatabaseService {
.first
}

func readRandomUnseenEntry(owner: Did?, seen: [Slashlink]) -> EntryStub? {
func readRandomUnseenEntry(
owner: Did?,
seen: [Slashlink],
minimumBodyLength: Int = 0
) -> EntryStub? {
guard self.state == .ready else {
return nil
}
Expand All @@ -1440,12 +1444,15 @@ final class DatabaseService {
SELECT memo.did, peer.petname, memo.slug, memo.excerpt, memo.headers FROM memo
LEFT JOIN peer ON memo.did = peer.did
WHERE substr(memo.slug, 1, 1) != '_'
AND length(memo.excerpt) > 64
AND length(memo.excerpt) > ?
AND memo.slashlink NOT IN (SELECT value FROM json_each(?))
ORDER BY RANDOM()
LIMIT 1
""",
parameters: [.json(seen.map { s in s.description }, or: "[]")]
parameters: [
.integer(minimumBodyLength),
.json(seen.map { s in s.description }, or: "[]")
]
)
.compactMap({ row in
guard
Expand Down
225 changes: 225 additions & 0 deletions xcode/Subconscious/SubconsciousTests/Tests_DatabaseService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,231 @@ class Tests_DatabaseService: XCTestCase {
)
}

func testListAllMemos() throws {
let service = try createDatabaseService()
_ = try service.migrate()

// Add some entries to DB
let now = Date.now

let foo = Memo(
contentType: "text/subtext",
created: now,
modified: now,
fileExtension: "subtext",
additionalHeaders: [],
body: "Foo"
)
try service.writeMemo(
MemoRecord(
did: Did.local,
petname: nil,
slug: Slug("foo")!,
memo: foo,
size: foo.toHeaderSubtext().size()!
)
)


let did = Did("did:key:abc123")!
let did2 = Did("did:key:def456")!
try service.writePeer(PeerRecord(petname: Petname("abc")!, identity: did))

let bar = Memo(
contentType: "text/subtext",
created: now,
modified: now,
fileExtension: "subtext",
additionalHeaders: [],
body: "Bar"
)
try service.writeMemo(
MemoRecord(
did: did,
petname: Petname("abc")!,
slug: Slug("bar")!,
memo: bar
)
)

let qux = Memo(
contentType: "text/subtext",
created: now,
modified: now,
fileExtension: "subtext",
additionalHeaders: [],
body: "Qux"
)
try service.writeMemo(
MemoRecord(
did: did2,
petname: nil,
slug: Slug("qux")!,
memo: qux,
size: qux.toHeaderSubtext().size()!
)
)

let baz = Memo(
contentType: "text/subtext",
created: now,
modified: now,
fileExtension: "subtext",
additionalHeaders: [],
body: "Baz"
)
try service.writeMemo(
MemoRecord(
did: Did.local,
petname: nil,
slug: Slug("baz")!,
memo: baz,
size: baz.toHeaderSubtext().size()!
)
)

let recent = try service.listAll(owner: did, limit: 3)

XCTAssertEqual(recent.count, 3)

XCTAssertEqual(recent[0].did, Did.local)
XCTAssertEqual(recent[1].did, did)
XCTAssertEqual(recent[2].did, did2)

let slashlinks = Set(
recent.compactMap({ stub in
stub.address
})
)
XCTAssertEqual(slashlinks.count, 3)
XCTAssertTrue(
slashlinks.contains(
Slashlink(
peer: Peer.did(did2),
slug: Slug("qux")!
)
)
)
XCTAssertTrue(
slashlinks.contains(
Slashlink(
peer: Peer.did(Did.local),
slug: Slug("foo")!
)
)
)
XCTAssertTrue(
slashlinks.contains(
Slashlink(
petname: Petname("abc")!,
slug: Slug("bar")!
)
)
)
}

func testListUnseen() throws {
let service = try createDatabaseService()
_ = try service.migrate()

// Add some entries to DB
let now = Date.now

let foo = Memo(
contentType: "text/subtext",
created: now,
modified: now,
fileExtension: "subtext",
additionalHeaders: [],
body: "Foo"
)
try service.writeMemo(
MemoRecord(
did: Did.local,
petname: nil,
slug: Slug("foo")!,
memo: foo,
size: foo.toHeaderSubtext().size()!
)
)


let did = Did("did:key:abc123")!
let did2 = Did("did:key:def456")!
try service.writePeer(PeerRecord(petname: Petname("abc")!, identity: did))

let bar = Memo(
contentType: "text/subtext",
created: now,
modified: now,
fileExtension: "subtext",
additionalHeaders: [],
body: "Bar"
)
try service.writeMemo(
MemoRecord(
did: did,
petname: Petname("abc")!,
slug: Slug("bar")!,
memo: bar
)
)

let qux = Memo(
contentType: "text/subtext",
created: now,
modified: now,
fileExtension: "subtext",
additionalHeaders: [],
body: "Qux"
)
try service.writeMemo(
MemoRecord(
did: did2,
petname: nil,
slug: Slug("qux")!,
memo: qux,
size: qux.toHeaderSubtext().size()!
)
)

let baz = Memo(
contentType: "text/subtext",
created: now,
modified: now,
fileExtension: "subtext",
additionalHeaders: [],
body: "Baz"
)
try service.writeMemo(
MemoRecord(
did: Did.local,
petname: nil,
slug: Slug("baz")!,
memo: baz,
size: baz.toHeaderSubtext().size()!
)
)

let recent = service.readRandomUnseenEntry(
owner: did,
seen: [
Slashlink(
slug: Slug("baz")!
),
Slashlink(
slug: Slug("foo")!
),
Slashlink(
petname: Petname("abc")!,
slug: Slug("foo")!
)
]
)

XCTAssert(recent?.address.slug == Slug("qux"))
}

func testReadRandomEntryInDateRange() throws {
let service = try createDatabaseService()
_ = try service.migrate()
Expand Down

0 comments on commit e5212fc

Please sign in to comment.