#include "gtest/gtest.h"
// #include "leveldb/env.h"
// #include "leveldb/db.h"
#include "fielddb/field_db.h"
#include "test/helper.cc"
using namespace fielddb;


TEST(TestLab1, Basic) {
    fielddb::DestroyDB("testdb1.1",Options()); //每个测试前，先把对应名称的之前的数据库删了
    FieldDB *db = new FieldDB();

    if(OpenDB("testdb1.1", &db).ok() == false) {
        std::cerr << "open db failed" << std::endl;
        abort();
    }
    // ClearDB(db);
    InsertFieldData(db);
    bool allowNotFound = false;
    GetFieldData(db, allowNotFound);    
    findKeysByCity(db);

    DeleteFieldData(db);
    GetDeleteData(db);

    delete db;
}

TEST(TestLab2, Basic) {
    fielddb::DestroyDB("testdb1.2",Options());
    FieldDB *db = new FieldDB();

    if(OpenDB("testdb1.2", &db).ok() == false) {
        std::cerr << "open db failed" << std::endl;
        abort();
    }
    shanghaiKeys.clear();
    age20Keys.clear();
    InsertFieldData(db);
    // GetFieldData(db);    
    // findKeysByCity(db);
    db->CreateIndexOnField("address", op);
    db->CreateIndexOnField("age", op);
    findKeysByCityIndex(db, true);
    findKeysByAgeIndex(db, true);

    db->DeleteIndex("address", op);
    findKeysByCityIndex(db, false);
    findKeysByAgeIndex(db, true);

    DeleteFieldData(db);
    // GetDeleteData(db);
    //helper太长不再封装函数了，这里因为数据都被delete了，但索引还在，所以能QueryByIndex但返回key数量0
    Field field = {"age", "20"};
    Status s;
    std::vector<std::string> resKeys = db->QueryByIndex(field, &s);
    ASSERT_EQ(resKeys.size(), 0);

    WriteFieldData(db);
    GetFieldData(db, false);
    findKeysByAgeIndex(db, true);
    delete db;
}


int main(int argc, char** argv) {
  // All tests currently run with the same read-only file limits.
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}