Skip to content

Commit

Permalink
mock QNetworkAccessManager and QNetworkReply, testDownloadWithNetwork…
Browse files Browse the repository at this point in the history
…Error
  • Loading branch information
VitorVieiraZ committed Nov 5, 2024
1 parent be2075d commit 7b10df2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/test/testmerginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2942,3 +2942,32 @@ void TestMerginApi::testParseVersion()
QCOMPARE( major, 2024 );
QCOMPARE( minor, 4 );
}

void TestMerginApi::testDownloadWithNetworkError()
{
MockNetworkManager *mockManager = new MockNetworkManager( mApi );
mApi->setNetworkManager( mockManager );

QString projectName = "testDownloadWithNetworkError";
QString projectNamespace = mWorkspaceName;
createRemoteProject( mApiExtra, projectNamespace, projectName, mTestDataPath + "/" + TEST_PROJECT_NAME + "/" );

mockManager->setShouldFail( true );

QSignalSpy spy( mApi, &MerginApi::syncProjectFinished );
QSignalSpy errorSpy( mApi, &MerginApi::networkErrorOccurred );

mApi->pullProject( projectNamespace, projectName );

QVERIFY( spy.wait( TestUtils::LONG_REPLY * 5 ) );

QCOMPARE( spy.count(), 1 );
QList<QVariant> arguments = spy.takeFirst();
QVERIFY( !arguments.at( 1 ).toBool() );

QCOMPARE( errorSpy.count(), 1 );

mApi->setNetworkManager( new QNetworkAccessManager( mApi ) );

deleteRemoteProjectNow( mApi, projectNamespace, projectName );
}
39 changes: 39 additions & 0 deletions app/test/testmerginapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,44 @@

#include <qgsapplication.h>

class MockReply : public QNetworkReply
{
public:
explicit MockReply( QObject *parent = nullptr ) : QNetworkReply( parent )
{
QNetworkReply::setError( QNetworkReply::ConnectionRefusedError, "Mock network failure" );
QMetaObject::invokeMethod( this, "finished", Qt::QueuedConnection );
}

void abort() override {}
qint64 readData( char *, qint64 ) override { return -1; }
qint64 writeData( const char *, qint64 ) override { return -1; }
};

class MockNetworkManager : public QNetworkAccessManager
{
public:
explicit MockNetworkManager( QObject *parent = nullptr )
: QNetworkAccessManager( parent )
, mShouldFail( false )
{}

void setShouldFail( bool shouldFail ) { mShouldFail = shouldFail; }
bool shouldFail() const { return mShouldFail; }

QNetworkReply *post( const QNetworkRequest &request, const QByteArray &data )
{
if ( mShouldFail )
{
return new MockReply( this );
}
return QNetworkAccessManager::post( request, data );
}

private:
bool mShouldFail;
};

class TestMerginApi: public QObject
{
Q_OBJECT
Expand All @@ -40,6 +78,7 @@ class TestMerginApi: public QObject
void testListProject();
void testListProjectsByName();
void testDownloadProject();
void testDownloadWithNetworkError();
void testDownloadProjectSpecChars();
void testCancelDownloadProject();
void testCreateProjectTwice();
Expand Down

1 comment on commit 7b10df2

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 24.11.692411 just submitted!

Please sign in to comment.