Skip to content

Commit

Permalink
test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Apr 12, 2024
1 parent e5c47f4 commit a3ef049
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class CandlePriceLevel {
public func toString() -> String {
return stringDescription
}

/// Returns full string representation of this candle price level attribute.
///
/// It is contains attribute key and its value.
Expand Down
2 changes: 1 addition & 1 deletion DXFeedFramework/Utils/StringUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ class StringUtil {

static func random(length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map{ _ in letters.randomElement()! })
return String((0..<length).map { _ in letters.randomElement()! })
}
}
5 changes: 2 additions & 3 deletions DXFeedFrameworkTests/CandleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ final class CandleTests: XCTestCase {
}

func testFetchingCandlesByAttrbuteSymbol() throws {
let exchange = CandleExchange.valueOf("A")
let period = CandlePeriod.valueOf(value: 100, type: .day)
let priceLevel = try CandlePriceLevel.valueOf(value: 999.35)
let priceLevel = try CandlePriceLevel.valueOf(value: 50)
let price = try CandlePrice.parse("mark")
let session = try CandleSession.parse("true")
let alignment = try CandleAlignment.parse("s")
let candleSymbol = CandleSymbol.valueOf("AAPL", [exchange, period, alignment, priceLevel, price, session])
let candleSymbol = CandleSymbol.valueOf("AAPL", [period, alignment, price, session])
let symbol = TimeSeriesSubscriptionSymbol(symbol: candleSymbol, fromTime: 1660125159)
try fetchCandles(symbol)
}
Expand Down
54 changes: 53 additions & 1 deletion DXFeedFrameworkTests/DXAsyncLastTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import XCTest
@testable import DXFeedFramework

final class DXAsyncLastTest: XCTestCase {
final class DXAsyncLocalLastTest: XCTestCase {
var endpoint: DXEndpoint!
var feed: DXFeed!
var publisher: DXPublisher!
Expand Down Expand Up @@ -60,3 +60,55 @@ final class DXAsyncLastTest: XCTestCase {
XCTAssertEqual(symbols, inputSymbols)
}
}

final class DXAsyncLastTest: XCTestCase {
var endpoint: DXEndpoint!
var feed: DXFeed!

override func setUpWithError() throws {
endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
feed = endpoint.getFeed()
}

override func tearDownWithError() throws {
try? endpoint.closeAndAwaitTermination()
}

func testTimeSeriesTask() async throws {
let date = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
guard let task = feed?.getTimeSeries(type: Candle.self,
symbol: "AAPL{=1d}",
fromTime: Long(date.millisecondsSince1970),
toTime: Long.max) else {
XCTAssert(false, "Async task is nil")
return
}
let result = await task.result
switch result {
case .success(let value):
XCTAssert((value?.count ?? 0) > 0)
case .failure(let value):
XCTAssert(false, "\(value)")
}
}

func testIndexedEventTask() async throws {
throw XCTSkip("""
Skiped
""")
let date = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
guard let task = feed?.getIndexedEvents(type: Series.self,
symbol: "ETH/USD:GDAX",
source: OrderSource.agregateAsk!) else {
XCTAssert(false, "Async task is nil")
return
}
let result = await task.result
switch result {
case .success(let value):
XCTAssert((value?.count ?? 0) > 0)
case .failure(let value):
XCTAssert(false, "\(value)")
}
}
}
115 changes: 16 additions & 99 deletions DXFeedFrameworkTests/DXPromiseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import XCTest

// swiftlint:disable type_body_length
final class DXPromiseTest: XCTestCase {
var endpoint: DXEndpoint!
var feed: DXFeed!

override func setUpWithError() throws {
endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
feed = endpoint.getFeed()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
try? endpoint.closeAndAwaitTermination()
}

private func eventPromise(type: IEventType.Type, symbol: String, feed: DXFeed) throws -> Promise {
let promise = try feed.getLastEventPromise(type: type, symbol: symbol)
let promise = try feed.getLastEventPromise(type: type, symbol: symbol)
return promise
}

Expand All @@ -38,11 +42,7 @@ final class DXPromiseTest: XCTestCase {

func getAsyncResult(timeOut: Int32?, withException: Bool = true) {
do {
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

let promise = try eventPromise(type: Trade.self,
symbol: "ETH/USD:GDAX",
feed: feed!)
Expand Down Expand Up @@ -73,11 +73,7 @@ final class DXPromiseTest: XCTestCase {

func testGetAsyncResult() {
do {
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

let promise = try eventPromise(type: Trade.self,
symbol: "ETH/USD:GDAX",
feed: feed!)
Expand All @@ -97,11 +93,7 @@ final class DXPromiseTest: XCTestCase {

func testGetResultWithException() {
do {
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

let promise = try eventPromise(type: Trade.self,
symbol: "ETH/USD:GDAX_TEST",
feed: feed!)
Expand All @@ -117,11 +109,7 @@ final class DXPromiseTest: XCTestCase {

func testGetIndexedEventResult() {
do {
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

guard let promise = try feed?.getIndexedEventsPromise(type: Trade.self,
symbol: "ETH/USD:GDAX",
source: OrderSource.agregateAsk!) else {
Expand All @@ -137,11 +125,7 @@ final class DXPromiseTest: XCTestCase {

func testGetIndexedEventResultWithException() {
do {
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

guard let promise = try feed?.getIndexedEventsPromise(type: Trade.self,
symbol: "ETH/USD:GDAX_TEST",
source: OrderSource.agregateAsk!) else {
Expand All @@ -158,11 +142,7 @@ final class DXPromiseTest: XCTestCase {

func testGetMultipleResults() {
do {
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

let promises = try feed?.getLastEventPromises(type: Quote.self, symbols: ["ETH/USD:GDAX", "AAPL"])
if promises?.isEmpty != false {
XCTAssert(false, "Promises is empty")
Expand All @@ -182,11 +162,7 @@ final class DXPromiseTest: XCTestCase {
func testGetAyncMultipleResults() {
do {
let symbols = ["ETH/USD:GDAX", "AAPL"]
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

let promises = try feed?.getLastEventPromises(type: Quote.self, symbols: symbols)
if promises?.isEmpty != false {
XCTAssert(false, "Promises is empty")
Expand All @@ -209,11 +185,7 @@ final class DXPromiseTest: XCTestCase {

func testGetMultipleResultsWithException() {
do {
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

let promises = try feed?.getLastEventPromises(type: Quote.self, symbols: ["ETH/USD:GDAX_TEST", "AAPL_TEST"])
if promises?.isEmpty != false {
XCTAssert(false, "Promises is empty")
Expand All @@ -235,11 +207,7 @@ final class DXPromiseTest: XCTestCase {
func testGetTimeSeriesResult() {
do {
let date = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

guard let promise = try feed?.getTimeSeriesPromise(type: Candle.self,
symbol: "AAPL{=1d}",
fromTime: Long(date.millisecondsSince1970),
Expand All @@ -257,11 +225,7 @@ final class DXPromiseTest: XCTestCase {

func testAllOffPromises() {
do {
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()

let promise = try eventPromise(type: Profile.self, symbol: "IBM", feed: feed!)
guard var promises = try feed?.getLastEventPromises(type: Quote.self,
symbols: ["ETH/USD:GDAX", "AAPL"]) else {
Expand Down Expand Up @@ -384,52 +348,5 @@ final class DXPromiseTest: XCTestCase {
}
}

func testTimeSeriesTask() async throws {
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()
let date = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
guard let task = feed?.getTimeSeries(type: Candle.self,
symbol: "AAPL{=1d}",
fromTime: Long(date.millisecondsSince1970),
toTime: Long.max) else {
XCTAssert(false, "Async task is nil")
return
}
let result = await task.result
switch result {
case .success(let value):
XCTAssert((value?.count ?? 0) > 0)
case .failure(let value):
XCTAssert(false, "\(value)")
}
}

func testIndexedEventTask() async throws {
throw XCTSkip("""
Skiped
""")
let endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
defer {
try? endpoint.closeAndAwaitTermination()
}
let feed = endpoint.getFeed()
let date = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
guard let task = feed?.getIndexedEvents(type: Series.self,
symbol: "ETH/USD:GDAX",
source: OrderSource.agregateAsk!) else {
XCTAssert(false, "Async task is nil")
return
}
let result = await task.result
switch result {
case .success(let value):
XCTAssert((value?.count ?? 0) > 0)
case .failure(let value):
XCTAssert(false, "\(value)")
}
}
}
// swiftlint:enable type_body_length
2 changes: 1 addition & 1 deletion Samples/QuoteTableApp/QuoteTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extension QuoteTableViewController: DXEventListener {
func receiveEvents(_ events: [MarketEvent]) {
events.forEach { event in
switch event.type {
case .quote:
case .quote:
dataSource[event.eventSymbol]?.update(event.quote)
case .profile:
dataSource[event.eventSymbol]?.update(event.profile.descriptionStr ?? "")
Expand Down

0 comments on commit a3ef049

Please sign in to comment.