From d646b47877e59112b7ade88a17ce8ddabb8e62f3 Mon Sep 17 00:00:00 2001 From: Adam Rocska Date: Fri, 5 Jun 2020 16:09:44 +0200 Subject: [PATCH] System tests ready for execution. Before coverage I'll take another round in the php rat hole to make sure all models are ok. Signed-off-by: Adam Rocska --- Sources/Api/Reader.swift | 2 +- Sources/Api/ReaderFactory.swift | 11 +++--- .../System/FileBased/CountryLookupTest.swift | 36 +++++++++++++++++++ Tests/System/FileBased/Wut.swift | 25 ------------- smart.sh | 2 ++ 5 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 Tests/System/FileBased/CountryLookupTest.swift delete mode 100644 Tests/System/FileBased/Wut.swift diff --git a/Sources/Api/Reader.swift b/Sources/Api/Reader.swift index bd06cfe..20a503e 100644 --- a/Sources/Api/Reader.swift +++ b/Sources/Api/Reader.swift @@ -7,7 +7,7 @@ public class Reader where Model: DictionaryInitialisable { init(dbReader: DBReader.Reader) { self.dbReader = dbReader } - func lookup(_ ip: IpAddress) -> Model? { + public func lookup(_ ip: IpAddress) -> Model? { guard let dictionary = dbReader.get(ip) else { return nil } return Model.init(dictionary) } diff --git a/Sources/Api/ReaderFactory.swift b/Sources/Api/ReaderFactory.swift index 0a31344..1807fe9 100644 --- a/Sources/Api/ReaderFactory.swift +++ b/Sources/Api/ReaderFactory.swift @@ -18,12 +18,11 @@ public class ReaderFactory { public func makeReader(source: URL, type: DatabaseType) throws -> Reader? { if !source.isFileURL { return nil } - - return Reader( - dbReader: try fileReaderFactory.makeInMemoryReader { - InputStream(url: source) ?? InputStream() - } - ) + let reader = try fileReaderFactory.makeInMemoryReader { + InputStream(url: source) ?? InputStream() + } + precondition(reader.metadata.databaseType.contains(type.rawValue)) + return Reader(dbReader: reader) } } diff --git a/Tests/System/FileBased/CountryLookupTest.swift b/Tests/System/FileBased/CountryLookupTest.swift new file mode 100644 index 0000000..64d0903 --- /dev/null +++ b/Tests/System/FileBased/CountryLookupTest.swift @@ -0,0 +1,36 @@ +import Foundation +import XCTest +import TestResources +import class Api.Reader +import struct Api.CountryModel +import class Api.ReaderFactory + +class CountryLookupTest: XCTestCase { + + private static var reader: Reader! + private var reader: Reader { get { return CountryLookupTest.reader } } + + override static func setUp() { + super.setUp() + let factory = ReaderFactory() + guard let url = bundle.url( + forResource: "GeoLite2-Country", + withExtension: "mmdb", + subdirectory: "GeoLite2-Country_20200421" + ) else { + XCTFail("Could not load country database.") + return + } + + do { + try reader = factory.makeReader(source: url, type: .country) + } catch { + XCTFail("Couldn't initialize reader.") + } + } + + func testSuccessfulLookup() { + + } + +} diff --git a/Tests/System/FileBased/Wut.swift b/Tests/System/FileBased/Wut.swift deleted file mode 100644 index a253d63..0000000 --- a/Tests/System/FileBased/Wut.swift +++ /dev/null @@ -1,25 +0,0 @@ -import Foundation -import XCTest -import TestResources - -class Wut: XCTestCase { - - func testWut() { - print(bundle.path( - forResource: "GeoLite2-ASN_20200526/GeoLite2-ASN", - ofType: "mmdb" - )) -// print(Bundle.main.bundlePath) -// print(Bundle.main.resourcePath) -// print(Bundle.main.privateFrameworksPath) -// print(Bundle.main.builtInPlugInsPath) -// print(Bundle.main.executablePath) -// print(Bundle.main.sharedFrameworksPath) -// print(Bundle.main.sharedSupportPath) -// XCTAssertTrue(false) -// let bundle = Bundle(for: Wut.self) -// bundle.path(forResource: <#T##String?##Swift.String?#>, ofType: <#T##String?##Swift.String?#>) -// print(bundle) - } - -} diff --git a/smart.sh b/smart.sh index a614d59..9fe3917 100755 --- a/smart.sh +++ b/smart.sh @@ -1,5 +1,7 @@ #!/bin/sh +set -e + /Library/Developer/Toolchains/swift-5.0.3-RELEASE.xctoolchain/usr/bin/swift test \ --enable-code-coverage \ --parallel \