File indexing completed on 2024-05-12 05:26:21

0001 #include <QTest>
0002 #include <QDebug>
0003 #include <QSignalSpy>
0004 #include <QAbstractItemModel>
0005 #include <functional>
0006 
0007 #include <test.h>
0008 #include <store.h>
0009 #include <log.h>
0010 #include <configstore.h>
0011 #include "test.h"
0012 
0013 class AccountsTest : public QObject
0014 {
0015     Q_OBJECT
0016 private slots:
0017 
0018     void initTestCase()
0019     {
0020         Sink::Test::initTest();
0021     }
0022 
0023     void init()
0024     {
0025         ConfigStore("accounts", "type").clear();
0026         ConfigStore("resources", Sink::ApplicationDomain::SinkResource::ResourceType::name).clear();
0027     }
0028 
0029     void testLoad()
0030     {
0031         using namespace Sink;
0032         using namespace Sink::ApplicationDomain;
0033 
0034         QString accountName("name");
0035         QString accountIcon("icon");
0036         auto account = ApplicationDomainType::createEntity<SinkAccount>();
0037         account.setAccountType("maildir");
0038         account.setName(accountName);
0039         account.setIcon(accountIcon);
0040         Store::create(account).exec().waitForFinished();
0041 
0042         Store::fetchAll<SinkAccount>(Query()).then([&](const QList<SinkAccount::Ptr> &accounts) {
0043             QCOMPARE(accounts.size(), 1);
0044             auto account = accounts.first();
0045             QCOMPARE(account->getAccountType(), QString("maildir"));
0046             QCOMPARE(account->getName(), accountName);
0047             QCOMPARE(account->getIcon(), accountIcon);
0048         })
0049         .exec().waitForFinished();
0050 
0051         QString smtpServer("smtpServer");
0052         QString smtpUsername("smtpUsername");
0053         QString smtpPassword("smtpPassword");
0054         auto resource = ApplicationDomainType::createEntity<SinkResource>();
0055         resource.setResourceType("sink.mailtransport");
0056         resource.setAccount(account);
0057         resource.setProperty("server", smtpServer);
0058         resource.setProperty("username", smtpUsername);
0059         resource.setProperty("password", smtpPassword);
0060         Store::create(resource).exec().waitForFinished();
0061 
0062 
0063         Store::fetchAll<SinkResource>(Query()).then([&](const QList<SinkResource::Ptr> &resources) {
0064             QCOMPARE(resources.size(), 1);
0065             auto resource = resources.first();
0066             QCOMPARE(resource->getResourceType(), QByteArray("sink.mailtransport"));
0067             QCOMPARE(resource->getProperty("server").toString(), smtpServer);
0068         })
0069         .exec().waitForFinished();
0070 
0071         auto identity = ApplicationDomainType::createEntity<Identity>();
0072         identity.setName(smtpServer);
0073         identity.setAddress(smtpUsername);
0074         identity.setAccount(account.identifier());
0075         Store::create(identity).exec().waitForFinished();
0076 
0077         Store::fetchAll<Identity>(Query()).then([&](const QList<Identity::Ptr> &identities) {
0078             QCOMPARE(identities.size(), 1);
0079             QCOMPARE(identities.first()->getName(), smtpServer);
0080             QCOMPARE(identities.first()->getAddress(), smtpUsername);
0081             QCOMPARE(identities.first()->getAccount(), account.identifier());
0082         })
0083         .exec().waitForFinished();
0084 
0085 
0086         Store::remove(resource).exec().waitForFinished();
0087 
0088         Store::fetchAll<SinkResource>(Query()).then([](const QList<SinkResource::Ptr> &resources) {
0089             QCOMPARE(resources.size(), 0);
0090         })
0091         .exec().waitForFinished();
0092     }
0093 
0094     void testLiveQuery()
0095     {
0096         using namespace Sink;
0097         using namespace Sink::ApplicationDomain;
0098 
0099         auto account = ApplicationDomainType::createEntity<SinkAccount>();
0100         account.setAccountType("maildir");
0101         account.setName("name");
0102         Store::create(account).exec().waitForFinished();
0103 
0104         Query query;
0105         query.setFlags(Query::LiveQuery);
0106         auto model = Store::loadModel<SinkAccount>(query);
0107         QTRY_COMPARE(model->rowCount(QModelIndex()), 1);
0108 
0109         auto account2 = ApplicationDomainType::createEntity<SinkAccount>();
0110         account2.setAccountType("maildir");
0111         account2.setName("name");
0112         Store::create(account2).exec().waitForFinished();
0113         QTRY_COMPARE(model->rowCount(QModelIndex()), 2);
0114 
0115         //Ensure the notifier only affects one type
0116         auto resource = ApplicationDomainType::createEntity<SinkResource>();
0117         resource.setResourceType("sink.mailtransport");
0118         Store::create(resource).exec().waitForFinished();
0119         QTRY_COMPARE(model->rowCount(QModelIndex()), 2);
0120     }
0121 
0122     void testLoadAccountStatus()
0123     {
0124         using namespace Sink;
0125         using namespace Sink::ApplicationDomain;
0126 
0127         auto account = ApplicationDomainType::createEntity<SinkAccount>();
0128         account.setAccountType("dummy");
0129         account.setName("name");
0130         VERIFYEXEC(Store::create(account));
0131 
0132         auto res = Sink::ApplicationDomain::DummyResource::create(account.identifier());
0133         VERIFYEXEC(Sink::Store::create(res));
0134         {
0135             Sink::Query query;
0136             query.setFlags(Query::LiveQuery);
0137             query.request<Sink::ApplicationDomain::SinkAccount::Status>();
0138 
0139             auto model = Sink::Store::loadModel<Sink::ApplicationDomain::SinkAccount>(query);
0140             QTRY_COMPARE(model->rowCount(QModelIndex()), 1);
0141             auto account = model->data(model->index(0, 0, QModelIndex()), Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::SinkAccount::Ptr>();
0142             QCOMPARE(account->getStatus(), static_cast<int>(Sink::ApplicationDomain::NoStatus));
0143 
0144             //Synchronize to connect
0145             VERIFYEXEC(Sink::Store::synchronize(Query().resourceFilter(res.identifier())));
0146 
0147             QTRY_COMPARE_WITH_TIMEOUT(model->data(model->index(0, 0, QModelIndex()), Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::SinkAccount::Ptr>()->getStatus(), static_cast<int>(Sink::ApplicationDomain::ConnectedStatus), 1000);
0148         }
0149     }
0150 
0151     void testLoadAccountStatusLive()
0152     {
0153         using namespace Sink;
0154         using namespace Sink::ApplicationDomain;
0155 
0156         {
0157             //Create a live query for all accounts
0158             Sink::Query query;
0159             query.setFlags(Query::LiveQuery);
0160             query.request<Sink::ApplicationDomain::SinkAccount::Status>();
0161 
0162             auto model = Sink::Store::loadModel<Sink::ApplicationDomain::SinkAccount>(query);
0163 
0164             //Create the account
0165             auto account = ApplicationDomainType::createEntity<SinkAccount>();
0166             account.setAccountType("dummy");
0167             account.setName("name");
0168             VERIFYEXEC(Store::create(account));
0169 
0170             auto res = Sink::ApplicationDomain::DummyResource::create(account.identifier());
0171             VERIFYEXEC(Sink::Store::create(res));
0172 
0173             //Ensure the account was created
0174             QTRY_COMPARE(model->rowCount(QModelIndex()), 1);
0175             auto retrievedAccount = model->data(model->index(0, 0, QModelIndex()), Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::SinkAccount::Ptr>();
0176             QCOMPARE(retrievedAccount->getStatus(), static_cast<int>(Sink::ApplicationDomain::NoStatus));
0177 
0178             //Synchronize to connect and ensure we receive the update
0179             VERIFYEXEC(Sink::Store::synchronize(Query().resourceFilter(res.identifier())));
0180 
0181             QTRY_COMPARE_WITH_TIMEOUT(model->data(model->index(0, 0, QModelIndex()), Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::SinkAccount::Ptr>()->getStatus(), static_cast<int>(Sink::ApplicationDomain::ConnectedStatus), 1000);
0182         }
0183     }
0184 
0185 };
0186 
0187 QTEST_GUILESS_MAIN(AccountsTest)
0188 #include "accountstest.moc"