mirror of
https://github.com/QuasarApp/SecretService.git
synced 2025-04-27 22:34:31 +00:00
fix db and tets of database
This commit is contained in:
parent
e846a4b104
commit
19dd73e3fa
@ -32,7 +32,7 @@ QString Record::table() const {
|
|||||||
|
|
||||||
QH::PKG::DBVariantMap Record::variantMap() const {
|
QH::PKG::DBVariantMap Record::variantMap() const {
|
||||||
return {
|
return {
|
||||||
{"hash", {_hash, QH::PKG::MemberType::Unique}},
|
{"hash", {_hash, QH::PKG::MemberType::PrimaryKey}},
|
||||||
{"alias", {_alias, static_cast<QH::PKG::MemberType>(static_cast<int>(QH::PKG::MemberType::InsertUpdate) |
|
{"alias", {_alias, static_cast<QH::PKG::MemberType>(static_cast<int>(QH::PKG::MemberType::InsertUpdate) |
|
||||||
static_cast<int>(QH::PKG::MemberType::Unique))}},
|
static_cast<int>(QH::PKG::MemberType::Unique))}},
|
||||||
{"data", {_data, QH::PKG::MemberType::Insert}}
|
{"data", {_data, QH::PKG::MemberType::Insert}}
|
||||||
@ -41,11 +41,17 @@ QH::PKG::DBVariantMap Record::variantMap() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString Record::primaryKey() const {
|
QString Record::primaryKey() const {
|
||||||
|
if (_hash.size())
|
||||||
return "hash";
|
return "hash";
|
||||||
|
|
||||||
|
return "alias";
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant Record::primaryValue() const {
|
QVariant Record::primaryValue() const {
|
||||||
|
if (_hash.size())
|
||||||
return _hash;
|
return _hash;
|
||||||
|
|
||||||
|
return _alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString &Record::getAlias() const {
|
const QString &Record::getAlias() const {
|
||||||
@ -70,7 +76,7 @@ const QByteArray &Record::getData() const {
|
|||||||
|
|
||||||
const QByteArray &Record::setData(const QByteArray &newData) {
|
const QByteArray &Record::setData(const QByteArray &newData) {
|
||||||
_data = newData;
|
_data = newData;
|
||||||
setHash(QCryptographicHash::hash(_data, QCryptographicHash::Sha256));
|
setHash(QCryptographicHash::hash(_data, QCryptographicHash::Sha256).toBase64(QByteArray::Base64UrlEncoding));
|
||||||
return getHash();
|
return getHash();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
//#
|
//#
|
||||||
|
|
||||||
#include "secretdatabase.h"
|
#include "secretdatabase.h"
|
||||||
|
#include "record.h"
|
||||||
#include <QCryptographicHash>
|
#include <QCryptographicHash>
|
||||||
#include <dbobjectsrequest.h>
|
#include <dbobjectsrequest.h>
|
||||||
#include <sqldb.h>
|
#include <sqldb.h>
|
||||||
@ -26,16 +27,16 @@ SecretDataBase::SecretDataBase() {
|
|||||||
|
|
||||||
QSharedPointer<iRecord>
|
QSharedPointer<iRecord>
|
||||||
SecretDataBase::getRecordByAlias(const QString &alias, bool ifNotExistsCreate) {
|
SecretDataBase::getRecordByAlias(const QString &alias, bool ifNotExistsCreate) {
|
||||||
|
return getById<Record>(alias, &Record::setAlias, ifNotExistsCreate);
|
||||||
}
|
}
|
||||||
|
|
||||||
QSharedPointer<iRecord>
|
QSharedPointer<iRecord>
|
||||||
SecretDataBase::getRecordByHash(const QByteArray &hash, bool ifNotExistsCreate) {
|
SecretDataBase::getRecordByHash(const QByteArray &hash, bool ifNotExistsCreate) {
|
||||||
|
return getById<Record>(hash, &Record::setHash, ifNotExistsCreate);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SecretDataBase::saveRecord(const QSharedPointer<iRecord> &record) {
|
bool SecretDataBase::saveRecord(const QSharedPointer<iRecord> &record) {
|
||||||
|
return db()->replaceObject(record.staticCast<Record>(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,7 @@
|
|||||||
#include "idatabase.h"
|
#include "idatabase.h"
|
||||||
namespace DBSecret {
|
namespace DBSecret {
|
||||||
|
|
||||||
IDataBase::IDataBase()
|
IDataBase::IDataBase() {
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "dbtest.h"
|
#include "dbtest.h"
|
||||||
|
#include "heart.h"
|
||||||
#include <SecretDB.h>
|
#include <SecretDB.h>
|
||||||
|
|
||||||
DBTest::DBTest() {
|
DBTest::DBTest() {
|
||||||
@ -18,6 +19,7 @@ DBTest::~DBTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DBTest::test() {
|
void DBTest::test() {
|
||||||
|
QH::init();
|
||||||
DBSecret::init();
|
DBSecret::init();
|
||||||
auto database = DBSecret::database();
|
auto database = DBSecret::database();
|
||||||
|
|
||||||
@ -29,7 +31,7 @@ void DBTest::test() {
|
|||||||
QSharedPointer<DBSecret::iRecord> result;
|
QSharedPointer<DBSecret::iRecord> result;
|
||||||
QVERIFY2((result = database->getRecordByAlias("test", true)), "must creat a new empty record");
|
QVERIFY2((result = database->getRecordByAlias("test", true)), "must creat a new empty record");
|
||||||
|
|
||||||
QVERIFY2(database->getRecordByAlias("test"),
|
QVERIFY2(!database->getRecordByAlias("test"),
|
||||||
"still should be empty because the getRecordByAlias with creation option do not save object into database");
|
"still should be empty because the getRecordByAlias with creation option do not save object into database");
|
||||||
|
|
||||||
QVERIFY2(result->getAlias() == "test", "verify alias");
|
QVERIFY2(result->getAlias() == "test", "verify alias");
|
||||||
@ -39,7 +41,7 @@ void DBTest::test() {
|
|||||||
auto dataHash = result->setData("secret");
|
auto dataHash = result->setData("secret");
|
||||||
|
|
||||||
QVERIFY2(result->getAlias() == "test", "verify configured record");
|
QVERIFY2(result->getAlias() == "test", "verify configured record");
|
||||||
QVERIFY2(result->getData() == "test", "verify configured record");
|
QVERIFY2(result->getData() == "secret", "verify configured record");
|
||||||
QVERIFY2(result->getHash() == QCryptographicHash::hash("secret",
|
QVERIFY2(result->getHash() == QCryptographicHash::hash("secret",
|
||||||
QCryptographicHash::Sha256).toBase64(QByteArray::Base64UrlEncoding),
|
QCryptographicHash::Sha256).toBase64(QByteArray::Base64UrlEncoding),
|
||||||
"verify configured record");
|
"verify configured record");
|
||||||
@ -50,9 +52,13 @@ void DBTest::test() {
|
|||||||
|
|
||||||
auto result2 = database->getRecordByAlias("test");
|
auto result2 = database->getRecordByAlias("test");
|
||||||
|
|
||||||
QVERIFY2(result2.data() == result.data(), "should be some as a prev object");
|
QVERIFY2(result2->getAlias() == result->getAlias(), "should be some as a prev object");
|
||||||
|
QVERIFY2(result2->getHash() == result->getHash(), "should be some as a prev object");
|
||||||
|
QVERIFY2(result2->getData() == result->getData(), "should be some as a prev object");
|
||||||
|
|
||||||
result2 = database->getRecordByHash(dataHash);
|
result2 = database->getRecordByHash(dataHash);
|
||||||
|
|
||||||
QVERIFY2(result2.data() == result.data(), "should be some as a prev object");
|
QVERIFY2(result2->getAlias() == result->getAlias(), "should be some as a prev object");
|
||||||
|
QVERIFY2(result2->getHash() == result->getHash(), "should be some as a prev object");
|
||||||
|
QVERIFY2(result2->getData() == result->getData(), "should be some as a prev object");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user