Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Feb 7, 2025
1 parent 6dd1a18 commit 9a4c110
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 74 deletions.
52 changes: 48 additions & 4 deletions RPC/Tests/RPCTests/ChainHandlesTests.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,67 @@
import Blockchain
@testable import Node
@testable import RPC
import Testing
import TracingUtils
@testable import Utils
import Vapor
import XCTVapor

public final class DummyNodeDataSource: Sendable {
public let chainDataProvider: BlockchainDataProvider
public init(
chainDataProvider: BlockchainDataProvider
) {
self.chainDataProvider = chainDataProvider
}
}

extension DummyNodeDataSource: ChainDataSource {
public func getKeys(prefix _: Data32, count _: UInt32, startKey _: Data32?, blockHash _: Data32?) async throws
-> [String]
{
["key1", "key2", "key3"]
}

public func getStorage(key _: Data32, blockHash _: Data32?) async throws -> [String] {
["value1", "value2"]
}

public func getFinalizedHead() async throws -> Data32? {
try await chainDataProvider.getFinalizedHead()
}

public func getBestBlock() async throws -> BlockRef {
try await chainDataProvider.getBlock(hash: chainDataProvider.bestHead.hash)
}

Check warning on line 35 in RPC/Tests/RPCTests/ChainHandlesTests.swift

View check run for this annotation

Codecov / codecov/patch

RPC/Tests/RPCTests/ChainHandlesTests.swift#L33-L35

Added lines #L33 - L35 were not covered by tests

public func getBlock(hash: Data32) async throws -> BlockRef? {
try await chainDataProvider.getBlock(hash: hash)
}

public func getState(blockHash: Data32, key: Data32) async throws -> Data? {
let state = try await chainDataProvider.getState(hash: blockHash)
return try await state.value.read(key: key)
}

Check warning on line 44 in RPC/Tests/RPCTests/ChainHandlesTests.swift

View check run for this annotation

Codecov / codecov/patch

RPC/Tests/RPCTests/ChainHandlesTests.swift#L41-L44

Added lines #L41 - L44 were not covered by tests

public func getBlockHash(byTimeslot timeslot: TimeslotIndex) async throws -> Set<Data32> {
try await chainDataProvider.getBlockHash(byTimeslot: timeslot)
}

public func getHeader(hash: Data32) async throws -> HeaderRef? {
try await chainDataProvider.getHeader(hash: hash)
}
}

final class ChainRPCControllerTests {
var app: Application!
var dataProvider: BlockchainDataProvider!

func setUp() async throws {
app = try await Application.make(.testing)
let dummyNodeDataSource = DummyNodeDataSource(genesis: .minimal)
dataProvider = dummyNodeDataSource.dataProvider
let (genesisState, genesisBlock) = try! State.devGenesis(config: .minimal)
dataProvider = try! await BlockchainDataProvider(InMemoryDataProvider(genesisState: genesisState, genesisBlock: genesisBlock))
let rpcController = JSONRPCController(handlers: ChainHandlers
.getHandlers(source: dummyNodeDataSource))
.getHandlers(source: DummyNodeDataSource(chainDataProvider: dataProvider)))
try app.register(collection: rpcController)
}

Expand Down
65 changes: 0 additions & 65 deletions RPC/Tests/RPCTests/DummyNodeDataSource.swift

This file was deleted.

8 changes: 3 additions & 5 deletions RPC/Tests/RPCTests/StateHandlersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ final class StateHandlersTests {

func setUp() async throws {
app = try await Application.make(.testing)
let dummyNodeDataSource = DummyNodeDataSource(genesis: .minimal)
dataProvider = dummyNodeDataSource.dataProvider
let (genesisState, genesisBlock) = try! State.devGenesis(config: .minimal)
let rpcController = JSONRPCController(handlers: ChainHandlers
.getHandlers(source: dummyNodeDataSource))
dataProvider = try! await BlockchainDataProvider(InMemoryDataProvider(genesisState: genesisState, genesisBlock: genesisBlock))
let rpcController = JSONRPCController(handlers: StateHandlers
.getHandlers(source: DummyNodeDataSource(chainDataProvider: dataProvider)))
try app.register(collection: rpcController)
}

Expand All @@ -37,7 +36,6 @@ final class StateHandlersTests {
try buffer.writeJSONEncodable(req)
try await app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res async in
#expect(res.status == .ok)
print("res body \(res.body.string)")
let resp = try! res.content.decode(JSONResponse.self, using: JSONDecoder())
#expect(resp.result!.value != nil)
}
Expand Down

0 comments on commit 9a4c110

Please sign in to comment.