From 869799a716ba6463d5135d0b55d7ed27ea16fa41 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 22 Feb 2018 11:33:03 +0100 Subject: [tests] Added a test for getting resources from the database This test would have flagged the Qt regression. --- test/storage/offline_database.test.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'test/storage/offline_database.test.cpp') diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp index 94daf59c02..23117173d1 100644 --- a/test/storage/offline_database.test.cpp +++ b/test/storage/offline_database.test.cpp @@ -38,6 +38,10 @@ void writeFile(const char* name, const std::string& data) { mbgl::util::write_file(name, data); } +void copyFile(const char* orig, const char* dest) { + mbgl::util::write_file(dest, mbgl::util::read_file(orig)); +} + } // namespace TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Create)) { @@ -143,6 +147,36 @@ TEST(OfflineDatabase, PutResource) { EXPECT_EQ("second", *updateGetResult->data); } +TEST(OfflineDatabase, TEST_REQUIRES_WRITE(GetResourceFromOfflineRegion)) { + using namespace mbgl; + + createDir("test/fixtures/offline_database"); + deleteFile("test/fixtures/offline_database/satellite.db"); + copyFile("test/fixtures/offline_database/satellite_test.db", "test/fixtures/offline_database/satellite.db"); + + OfflineDatabase db("test/fixtures/offline_database/satellite.db", mapbox::sqlite::ReadOnly); + + Resource resource = Resource::style("mapbox://styles/mapbox/satellite-v9"); + ASSERT_TRUE(db.get(resource)); +} + +TEST(OfflineDatabase, PutAndGetResource) { + using namespace mbgl; + + OfflineDatabase db(":memory:"); + + Response response1; + response1.data = std::make_shared("foobar"); + + Resource resource = Resource::style("mapbox://example.com/style"); + + db.put(resource, response1); + + auto response2 = db.get(resource); + + ASSERT_EQ(*response1.data, *(*response2).data); +} + TEST(OfflineDatabase, PutTile) { using namespace mbgl; -- cgit v1.2.1 From 136e536159a1e22aa4a92c4e6463893600b809d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 6 Feb 2018 17:55:50 +0100 Subject: [core, qt] move self-resetting Statement/Query object to shared header --- test/storage/offline_database.test.cpp | 47 +++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'test/storage/offline_database.test.cpp') diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp index 23117173d1..620e6eaa6d 100644 --- a/test/storage/offline_database.test.cpp +++ b/test/storage/offline_database.test.cpp @@ -66,7 +66,7 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(SchemaVersion)) { std::string path("test/fixtures/offline_database/offline.db"); { - mapbox::sqlite::Database db(path, mapbox::sqlite::Create | mapbox::sqlite::ReadWrite); + mapbox::sqlite::Database db{ path, mapbox::sqlite::Create | mapbox::sqlite::ReadWrite }; db.exec("PRAGMA user_version = 1"); } @@ -599,40 +599,45 @@ TEST(OfflineDatabase, OfflineMapboxTileCount) { } static int databasePageCount(const std::string& path) { - mapbox::sqlite::Database db(path, mapbox::sqlite::ReadOnly); - mapbox::sqlite::Statement stmt = db.prepare("pragma page_count"); - stmt.run(); - return stmt.get(0); + mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; + mapbox::sqlite::Statement stmt{ db, "pragma page_count" }; + mapbox::sqlite::Query query{ stmt }; + query.run(); + return query.get(0); } static int databaseUserVersion(const std::string& path) { - mapbox::sqlite::Database db(path, mapbox::sqlite::ReadOnly); - mapbox::sqlite::Statement stmt = db.prepare("pragma user_version"); - stmt.run(); - return stmt.get(0); + mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; + mapbox::sqlite::Statement stmt{ db, "pragma user_version" }; + mapbox::sqlite::Query query{ stmt }; + query.run(); + return query.get(0); } static std::string databaseJournalMode(const std::string& path) { - mapbox::sqlite::Database db(path, mapbox::sqlite::ReadOnly); - mapbox::sqlite::Statement stmt = db.prepare("pragma journal_mode"); - stmt.run(); - return stmt.get(0); + mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; + mapbox::sqlite::Statement stmt{ db, "pragma journal_mode" }; + mapbox::sqlite::Query query{ stmt }; + query.run(); + return query.get(0); } static int databaseSyncMode(const std::string& path) { - mapbox::sqlite::Database db(path, mapbox::sqlite::ReadOnly); - mapbox::sqlite::Statement stmt = db.prepare("pragma synchronous"); - stmt.run(); - return stmt.get(0); + mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; + mapbox::sqlite::Statement stmt{ db, "pragma synchronous" }; + mapbox::sqlite::Query query{ stmt }; + query.run(); + return query.get(0); } static std::vector databaseTableColumns(const std::string& path, const std::string& name) { - mapbox::sqlite::Database db(path, mapbox::sqlite::ReadOnly); + mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; const auto sql = std::string("pragma table_info(") + name + ")"; - mapbox::sqlite::Statement stmt = db.prepare(sql.c_str()); + mapbox::sqlite::Statement stmt{ db, sql.c_str() }; + mapbox::sqlite::Query query{ stmt }; std::vector columns; - while (stmt.run()) { - columns.push_back(stmt.get(1)); + while (query.run()) { + columns.push_back(query.get(1)); } return columns; } -- cgit v1.2.1 From a1195f6a9dc57910f7c3e6c9217e3041929a01fb Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 8 May 2018 10:40:35 -0700 Subject: Avoid exceptions for flow control during database creation Unfortuntely, it's difficult to avoid all exceptions, because sqlite3_open_v2 does not reliably return SQLITE_NOTADB if the file is not a database. However, this should avoid cases where developers misinterpret the SQLITE_CANTOPEN exception as a crash, which is the common case. --- test/storage/offline_database.test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'test/storage/offline_database.test.cpp') diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp index 620e6eaa6d..656231eebe 100644 --- a/test/storage/offline_database.test.cpp +++ b/test/storage/offline_database.test.cpp @@ -66,7 +66,7 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(SchemaVersion)) { std::string path("test/fixtures/offline_database/offline.db"); { - mapbox::sqlite::Database db{ path, mapbox::sqlite::Create | mapbox::sqlite::ReadWrite }; + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::Create | mapbox::sqlite::ReadWrite); db.exec("PRAGMA user_version = 1"); } @@ -599,7 +599,7 @@ TEST(OfflineDatabase, OfflineMapboxTileCount) { } static int databasePageCount(const std::string& path) { - mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); mapbox::sqlite::Statement stmt{ db, "pragma page_count" }; mapbox::sqlite::Query query{ stmt }; query.run(); @@ -607,7 +607,7 @@ static int databasePageCount(const std::string& path) { } static int databaseUserVersion(const std::string& path) { - mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); mapbox::sqlite::Statement stmt{ db, "pragma user_version" }; mapbox::sqlite::Query query{ stmt }; query.run(); @@ -615,7 +615,7 @@ static int databaseUserVersion(const std::string& path) { } static std::string databaseJournalMode(const std::string& path) { - mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); mapbox::sqlite::Statement stmt{ db, "pragma journal_mode" }; mapbox::sqlite::Query query{ stmt }; query.run(); @@ -623,7 +623,7 @@ static std::string databaseJournalMode(const std::string& path) { } static int databaseSyncMode(const std::string& path) { - mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); mapbox::sqlite::Statement stmt{ db, "pragma synchronous" }; mapbox::sqlite::Query query{ stmt }; query.run(); @@ -631,7 +631,7 @@ static int databaseSyncMode(const std::string& path) { } static std::vector databaseTableColumns(const std::string& path, const std::string& name) { - mapbox::sqlite::Database db{ path, mapbox::sqlite::ReadOnly }; + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); const auto sql = std::string("pragma table_info(") + name + ")"; mapbox::sqlite::Statement stmt{ db, sql.c_str() }; mapbox::sqlite::Query query{ stmt }; -- cgit v1.2.1