Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 53 additions & 12 deletions src/LfMerge.Core.Tests/Lcm/TransferMongoToLcmActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,8 @@ public void Action_EntryDeletedInFieldWorksSinceLastSync_IsNotRecreated()
Assert.That(_counts.Added, Is.EqualTo(0));
Assert.That(_counts.Modified, Is.EqualTo(0));
Assert.That(_counts.Deleted, Is.EqualTo(0));
Assert.That(_env.Logger.GetMessages(), Does.Not.Contain("has never synced"),
"after a sync, an entry FieldWorks lacks was certainly deleted in FLEx, so it is not reported one by one");
}

[Test]
Expand Down Expand Up @@ -745,25 +747,34 @@ public void Action_NeverSyncedProjectWhoseEntriesLfMergeWrote_KeepsFieldWorksVer
Assert.That(_counts.Modified, Is.EqualTo(0));
}

[Test]
public void Action_NeverSyncedProjectWithEntryImportedIntoLf_SendsItToFieldWorks()
// An entry as a LIFT import into LF leaves it: no user references, the same as LfMerge's own
// writes, so it is stored with UpdateRecord rather than as an LF user's edit.
private LfLexEntry StoreImportedLfEntry(ILfProject lfProj, string lexeme)
{
// Setup: an LF project's first Send/Receive, as when one is migrated. A LIFT import into
// LF leaves no user references on its entries, the same as LfMerge's own writes do, so
// the entry is stored with UpdateRecord rather than as an LF user's edit.
var lfProj = _lfProj;
_conn.SetLastSyncedDate(lfProj, MagicValues.UnixEpoch);
FwProject fwProject = lfProj.FieldWorksProject;
string vernacularWS = fwProject.Cache.LanguageProject.DefaultVernacularWritingSystem.Id;
Guid importedGuid = Guid.NewGuid();
string vernacularWS = lfProj.FieldWorksProject.Cache.LanguageProject.DefaultVernacularWritingSystem.Id;
var imported = new LfLexEntry();
imported.Guid = importedGuid;
imported.Lexeme = LfMultiText.FromSingleStringMapping(vernacularWS, "imported into LF");
imported.Guid = Guid.NewGuid();
imported.Lexeme = LfMultiText.FromSingleStringMapping(vernacularWS, lexeme);
imported.AuthorInfo = new LfAuthorInfo();
imported.AuthorInfo.CreatedDate = new DateTime(2016, 4, 10, 0, 0, 0, DateTimeKind.Utc);
imported.AuthorInfo.ModifiedDate = imported.AuthorInfo.CreatedDate;
imported.DateModified = new DateTime(2016, 7, 25, 0, 0, 0, DateTimeKind.Utc);
_conn.UpdateRecord(lfProj, imported);
return imported;
}

[Test]
public void Action_NeverSyncedProjectIntoEmptyFieldWorks_SendsEntriesImportedIntoLf()
{
// Setup: an LF project's first Send/Receive into a new, empty FieldWorks project, as when
// one is migrated
var lfProj = _lfProj;
_conn.SetLastSyncedDate(lfProj, MagicValues.UnixEpoch);
FwProject fwProject = lfProj.FieldWorksProject;
foreach (Guid guid in LcmTestHelper.GetEntries(fwProject).Select(entry => entry.Guid).ToList())
LcmTestHelper.DeleteEntry(fwProject, guid);
Assert.That(LcmTestHelper.CountEntries(fwProject), Is.EqualTo(0));
Guid importedGuid = StoreImportedLfEntry(lfProj, "imported into LF").Guid.Value;

// Exercise
SutMongoToLcm.Run(lfProj);
Expand All @@ -778,6 +789,36 @@ public void Action_NeverSyncedProjectWithEntryImportedIntoLf_SendsItToFieldWorks
Assert.That(_counts.Deleted, Is.EqualTo(0));
}

[Test]
public void Action_NeverSyncedProjectWhoseFieldWorksHasEntries_DoesNotRecreateEntryDeletedInFieldWorks()
{
// Setup: an initial clone whose transfer to Mongo never got as far as recording the sync,
// after which FLEx deleted one of the entries LfMerge had written to Mongo
var lfProj = _lfProj;
SutLcmToMongo.Run(lfProj);
_conn.SetLastSyncedDate(lfProj, MagicValues.UnixEpoch);
FwProject fwProject = lfProj.FieldWorksProject;
Guid kenGuid = Guid.Parse(KenEntryGuidStr);
LcmTestHelper.DeleteEntry(fwProject, kenGuid);
Assert.That(LcmTestHelper.CountEntries(fwProject), Is.GreaterThan(0));

// Exercise
SutMongoToLcm.Run(lfProj);

// Verify
ILexEntry ken;
Assert.That(fwProject.ServiceLocator.GetInstance<ILexEntryRepository>().TryGetObject(kenGuid, out ken), Is.False,
"an entry FLEx deleted must stay deleted, though LF still has it");
Assert.That(_counts.Added, Is.EqualTo(0));
Assert.That(_counts.Modified, Is.EqualTo(0));
Assert.That(_counts.Deleted, Is.EqualTo(0));
// The data cannot tell this from an entry imported into LF after the clone, which would be
// marked deleted the same way, so each one is logged where it can be found again
string messages = _env.Logger.GetMessages();
Assert.That(messages, Does.Contain($"MongoToLcm: entry {kenGuid} ("));
Assert.That(messages, Does.Contain("has never synced"));
}

// TODO: Move custom field tests to their own test class, and move these helper functions with them
public void AddCustomFieldToSense(LfSense sense, string fieldName, BsonValue fieldValue)
{
Expand Down
43 changes: 32 additions & 11 deletions src/LfMerge.Core/DataConverters/ConvertMongoToLcmLexicon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ public class ConvertMongoToLcmLexicon

// Entries where LF's copy differs from FieldWorks, or FieldWorks lacks it, but no LF user has
// touched it since the last sync, so FieldWorks holds the newer version (see
// EditedInLfSinceLastSync). An entry FieldWorks lacks in a project that has never synced is
// not counted: it is LF's own, and is sent (see LfLexEntryToLcmLexEntry).
// EditedInLfSinceLastSync). Not counted are the entries of a never-synced project whose
// FieldWorks lexicon started out empty: those are LF's own, and are sent (see
// LfLexEntryToLcmLexEntry).
private int _changedOnlyInFieldWorks;
private int _absentOnlyFromFieldWorks;

// Whether this is an LF project's first Send/Receive into an empty FieldWorks project, as when
// one is migrated: never synced, and FieldWorks had no entries before this conversion began.
private bool _firstSyncIntoEmptyFieldWorks;

// Shorter names to use in this class since MagicStrings.LfOptionListCodeForGrammaticalInfo
// (etc.) are real mouthfuls
private const string GrammarListCode = MagicStrings.LfOptionListCodeForGrammaticalInfo;
Expand Down Expand Up @@ -159,6 +164,8 @@ public ConversionError<LfLexEntry> RunConversion()
IEnumerable<LfLexEntry> lexicon = GetLexicon(LfProject);
_changedOnlyInFieldWorks = 0;
_absentOnlyFromFieldWorks = 0;
// Decided once, before any entry is created: it is about where the project started.
_firstSyncIntoEmptyFieldWorks = LastSyncedDate == null && GetInstance<ILexEntryRepository>().Count == 0;
UndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW("undo", "redo", Cache.ActionHandlerAccessor, () =>
{
#if false // Once we allow LanguageForge to create optionlist items with "canonical" values (parts of speech, semantic domains, etc.), uncomment this block
Expand Down Expand Up @@ -1061,8 +1068,8 @@ private DateTime? LastSyncedDate
/// sync. LfMerge clears the user references on every entry it writes, and LF sets one on every
/// edit and creation; a deletion sets none, but deleting what FieldWorks lacks is harmless.
/// A LIFT import into LF sets none either, so this says "not edited" of every imported entry
/// in a never-synced LF project; LfLexEntryToLcmLexEntry sends such an entry anyway when
/// FieldWorks does not have it.
/// in a never-synced LF project; LfLexEntryToLcmLexEntry sends such entries anyway when
/// FieldWorks started out with no entries at all.
/// </summary>
private bool EditedInLfSinceLastSync(LfLexEntry lfEntry)
{
Expand Down Expand Up @@ -1092,17 +1099,31 @@ private void LfLexEntryToLcmLexEntry(LfLexEntry lfEntry)
_changedOnlyInFieldWorks++;
return;
}
if (LastSyncedDate != null)
if (!_firstSyncIntoEmptyFieldWorks)
{
// FieldWorks is taken to be right not to have it. After a sync that is certain: LF's
// copy is what FieldWorks had then, untouched since, so FLEx has deleted it.
//
// In a never-synced project whose FieldWorks has entries it is an assumption, which
// the data cannot check. Such a project's entries normally came from its FieldWorks
// at the initial clone, so one FieldWorks now lacks was deleted in FLEx since, or
// dropped when the repository was begun afresh. But an entry imported into LF from
// LIFT after the clone has no user references either, and looks just the same: it
// is deliberately treated as deleted in FLEx too, and the transfer back to Mongo
// marks it deleted. Marked, not removed -- it stays in Mongo, and clearing isDeleted
// restores it -- and logged here, so that it can be found if it was LF's own.
if (LastSyncedDate == null)
Logger.Info("MongoToLcm: entry {0} ({1}) is in LF but not in FieldWorks, and the project has never synced; " +
"taking it to have been deleted in FLEx, so the transfer back to Mongo will mark it deleted",
guid, ConvertUtilities.EntryNameForDebugging(lfEntry));
_absentOnlyFromFieldWorks++;
return;
}
// Never synced, and FieldWorks does not have it: an LF project's own entry, going to
// FieldWorks for the first time. A LIFT import into LF sets no user references either,
// so EditedInLfSinceLastSync cannot tell it from one LfMerge wrote; but FieldWorks
// cannot hold a newer version of an entry it does not have, so there is nothing to
// keep. Leaving it out would also lose it: the transfer back to Mongo marks every
// entry FieldWorks lacks as deleted.
// An LF project's own entry, on its first Send/Receive into an empty FieldWorks
// project: a migration. A LIFT import into LF sets no user references, so
// EditedInLfSinceLastSync cannot tell it from one LfMerge wrote, but an empty
// FieldWorks project never had any entry to delete. Leaving it out would lose it, as
// the transfer back to Mongo marks every entry FieldWorks lacks as deleted.
}
bool createdEntry = false;
bool wantCreation = !lfEntry.IsDeleted;
Expand Down
Loading