Skip to content

Commit

Permalink
tests and removing truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Jan 13, 2025
1 parent 4a6deda commit e0f23b2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
28 changes: 28 additions & 0 deletions app/test/testmerginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2942,3 +2942,31 @@ void TestMerginApi::testParseVersion()
QCOMPARE( major, 2024 );
QCOMPARE( minor, 4 );
}

void TestMerginApi::testUpdateProjectMetadataRole()
{
QString projectName = "testUpdateProjectMetadataRole";

createRemoteProject( mApiExtra, mWorkspaceName, projectName, mTestDataPath + "/" + TEST_PROJECT_NAME + "/" );
downloadRemoteProject( mApi, mWorkspaceName, projectName );

LocalProject projectInfo = mApi->localProjectsManager().projectFromMerginName( mWorkspaceName, projectName );
QVERIFY( projectInfo.isValid() );
QString projectDir = projectInfo.projectDir;

// Test 1: Initial role should be 'owner'
QString cachedRole = mApi->getCachedProjectRole( MerginApi::getFullProjectName( mWorkspaceName, projectName ) );
QCOMPARE( cachedRole, QString( "owner" ) );

// Test 2: Update cached role to 'reader'
QString newRole = "reader";
bool updateSuccess = mApi->updateCachedProjectRole( MerginApi::getFullProjectName( mWorkspaceName, projectName ), newRole );
QVERIFY( updateSuccess );

// Verify role was updated in cache
cachedRole = mApi->getCachedProjectRole( MerginApi::getFullProjectName( mWorkspaceName, projectName ) );
QCOMPARE( cachedRole, newRole );

// Clean up
deleteRemoteProjectNow( mApi, mWorkspaceName, projectName );
}
1 change: 1 addition & 0 deletions app/test/testmerginapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class TestMerginApi: public QObject
void testSynchronizationViaManager();
void testAutosync();
void testAutosyncFailure();
void testUpdateProjectMetadataRole();

void testRegisterAndDelete();
void testCreateWorkspace();
Expand Down
24 changes: 18 additions & 6 deletions core/merginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3478,7 +3478,7 @@ bool MerginApi::updateCachedProjectRole( const QString &projectFullName, const Q
obj["role"] = newRole;
doc.setObject( obj );

if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
if ( !file.open( QIODevice::WriteOnly ) )
{
return false;
}
Expand Down Expand Up @@ -3991,9 +3991,7 @@ void MerginApi::updateProjectMetadataRole( const QString &projectFullName )
return;
}

QString projectDir = mLocalProjects.projectFromMerginName( projectFullName ).projectDir;
MerginProjectMetadata cachedProjectMetadata = MerginProjectMetadata::fromCachedJson( projectDir + "/" + sMetadataFile );
QString cachedRole = cachedProjectMetadata.role;
QString cachedRole = MerginApi::getCachedProjectRole( projectFullName );

QNetworkReply *reply = getProjectInfo( projectFullName );
if ( !reply )
Expand All @@ -4003,7 +4001,6 @@ void MerginApi::updateProjectMetadataRole( const QString &projectFullName )
}

reply->request().setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );
reply->request().setAttribute( static_cast<QNetworkRequest::Attribute>( AttrCachedRole ), cachedRole );
connect( reply, &QNetworkReply::finished, this, &MerginApi::updateProjectMetadataRoleReplyFinished );
}

Expand All @@ -4013,7 +4010,7 @@ void MerginApi::updateProjectMetadataRoleReplyFinished()
Q_ASSERT( r );

QString projectFullName = r->request().attribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ) ).toString();
QString cachedRole = r->request().attribute( static_cast<QNetworkRequest::Attribute>( AttrCachedRole ) ).toString();
QString cachedRole = MerginApi::getCachedProjectRole( projectFullName );

if ( r->error() == QNetworkReply::NoError )
{
Expand All @@ -4040,3 +4037,18 @@ void MerginApi::updateProjectMetadataRoleReplyFinished()

r->deleteLater();
}

QString MerginApi::getCachedProjectRole( const QString &projectFullName ) const
{
if ( projectFullName.isEmpty() )
return QString();

QString projectDir = mLocalProjects.projectFromMerginName( projectFullName ).projectDir;

if ( projectDir.isEmpty() )
return QString();

MerginProjectMetadata cachedProjectMetadata = MerginProjectMetadata::fromCachedJson( projectDir + "/" + sMetadataFile );

return cachedProjectMetadata.role;
}
5 changes: 3 additions & 2 deletions core/merginapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,8 @@ class MerginApi: public QObject

bool updateCachedProjectRole( const QString &projectFullName, const QString &newRole );

QString getCachedProjectRole( const QString &projectFullName ) const;

QNetworkAccessManager mManager;
QString mApiRoot;
LocalProjectsManager &mLocalProjects;
Expand All @@ -814,8 +816,7 @@ class MerginApi: public QObject
AttrProjectFullName = QNetworkRequest::User,
AttrTempFileName = QNetworkRequest::User + 1,
AttrWorkspaceName = QNetworkRequest::User + 2,
AttrAcceptFlag = QNetworkRequest::User + 3,
AttrCachedRole = QNetworkRequest::User + 4
AttrAcceptFlag = QNetworkRequest::User + 3
};

Transactions mTransactionalStatus; //projectFullname -> transactionStatus
Expand Down

1 comment on commit e0f23b2

@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 25.1.698111 just submitted!

Please sign in to comment.