Skip to content

Commit

Permalink
unit tests around migration from monatomic project langs to multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
hatton committed Apr 22, 2024
1 parent c9622f3 commit 95ad70f
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 28 deletions.
5 changes: 0 additions & 5 deletions archive-configurations/ELAR/fields.json5
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
project: [
{ key: "vernacularIso3CodeAndName", visibility: "never" },
{ key: "analysisIso3CodeAndName", visibility: "never" },
{ key: "collectionSubjectLanguages", visibility: "always" },
{ key: "collectionWorkingLanguages", visibility: "always" },

{ key: "collectionName", visibility: "always" },
{ key: "collectionTitle", visibility: "always" },
{ key: "collectionDescription", visibility: "always" },
Expand Down
4 changes: 2 additions & 2 deletions archive-configurations/lameta/fields.json5
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@
key: "vernacularIso3CodeAndName",
englishLabel: "Subject Language",
xmlTag: "VernacularISO3CodeAndName",
visibility: "never", // replaced by collectionWorkingLanguages
type: "language",
tabIndex: 1
},
{
key: "analysisIso3CodeAndName",
englishLabel: "Working Language",
xmlTag: "AnalysisISO3CodeAndName",
visibility: "never", // replaced by collectionSubjectLanguages
type: "language",
tabIndex: 2
},
{
key: "collectionSubjectLanguages",
englishLabel: "Subject Languages",
xmlTag: "CollectionSubjectLanguages",
visibility: "never",
form: "primary",
type: "languageChoices",
controlProps: {
Expand All @@ -43,7 +44,6 @@
key: "collectionWorkingLanguages",
xmlTag: "CollectionWorkingLanguages",
englishLabel: "Working Languages",
visibility: "never",
form: "primary",
type: "languageChoices",
tabIndex: 2
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@
},
"//remember to match this with github actions in main.yml": "",
"engines": {
"node": "18.16.0"
"node": "20.12.2"
},
"debug": {
"env": {
"VITE_DEV_SERVER_URL": "http://127.0.0.1:7777"
}
},
"volta": {
"node": "18.16.0",
"node": "20.12.2",
"yarn": "1.22.19"
},
"packageManager": "[email protected]"
Expand Down
32 changes: 31 additions & 1 deletion src/model/Project/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ export class Project extends Folder {
return this[folderType + "s"];
}
public migrateFromPreviousVersions(): void {
//nothing to do, yet
// nothing here but see migrate() on ProjectMetadataFile
}

public findFolderById(
folderType: IFolderType,
id: string
Expand Down Expand Up @@ -1040,6 +1041,35 @@ export class ProjectMetadataFile extends FolderMetadataFile {
this.properties.removeProperty("AccessProtocol");
}
}

// Before lameta 3, we could store a single language for the vernacular. If we
// find a file using that but no modern collectionSubjectLanguage, collectionSubjectLanguage
// should be loaded with the value from vernacularIso3CodeAndName.
// If instead we do have a non-empty collectionSubjectLanguage, then we should set
// vernacularIso3CodeAndName to the first item in it.
this.migrateLanguageFields(
"vernacularIso3CodeAndName",
"collectionSubjectLanguages"
);
// same for the anlysis language
this.migrateLanguageFields(
"analysisIso3CodeAndName",
"collectionWorkingLanguages"
);
}

private migrateLanguageFields(legacySingle: string, modernMultiple: string) {
if (this.properties.getTextStringOrEmpty(modernMultiple).length === 0) {
this.properties.setText(
modernMultiple,
this.properties.getTextStringOrEmpty(legacySingle)
);
} else {
const parts = this.properties
.getTextStringOrEmpty(modernMultiple)
.split(";");
this.properties.setText(legacySingle, parts[0] || "");
}
}

// peek into the xml to get the configuration we're supposed to be using
Expand Down
39 changes: 39 additions & 0 deletions src/model/Project/ReadProject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,45 @@ describe("Project Read", () => {
"dde: Doondo"
);
});

// Before lameta 3, we could store a single language for the vernacular. If we
// find a file using that but no modern collectionSubjectLanguage, collectionSubjectLanguage
// should be loaded with the value from vernacularIso3CodeAndName.
it("should load vernacularIso3CodeAndName into collectionSubjectLanguage if missing or empty", () => {
const f = GetProjectFileWithOneField(
"VernacularISO3CodeAndName",
"abc:Abracadabra"
);
expect(
f.properties.getTextStringOrEmpty("collectionSubjectLanguages")
).toBe("abc:Abracadabra");
// do same for AnaylsisISO3CodeAndName and collectionWorkingLanguages
const f2 = GetProjectFileWithOneField(
"AnalysisISO3CodeAndName",
"abc:Abracadabra"
);
expect(
f2.properties.getTextStringOrEmpty("collectionWorkingLanguages")
).toBe("abc:Abracadabra");
});
it("fill legacy language fields from modern collectionLanguages", () => {
const f = GetProjectFileWithOneField(
"CollectionSubjectLanguages",
"foo:FooBar;abc:Abracadabra"
);
expect(f.properties.getTextStringOrEmpty("vernacularIso3CodeAndName")).toBe(
"foo:FooBar"
);
// do same for AnaylsisISO3CodeAndName and collectionWorkingLanguages
const f2 = GetProjectFileWithOneField(
"CollectionWorkingLanguages",
"foo:FooBar;aby:Abracadabra"
);
expect(f2.properties.getTextStringOrEmpty("analysisIso3CodeAndName")).toBe(
"foo:FooBar"
);
});

it("should read archiveConfigurationName", () => {
const f = GetProjectFileWithOneField("ArchiveConfigurationName", "ELAR");
expect(f.properties.getTextStringOrEmpty("archiveConfigurationName")).toBe(
Expand Down
37 changes: 19 additions & 18 deletions src/model/Project/WriteProject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,25 @@ describe("Project Write", () => {
)
).toBeGreaterThan(-1);
});
});

it("should write languages", () => {
const f = GetProjectFileWithOneField("unused", "");
f.properties.setText("collectionSubjectLanguages", "ab;cd;ef");
f.properties.setText("collectionWorkingLanguages", "gh;ij");
setResultXml(f.getXml());
xexpect("Project/CollectionSubjectLanguages").toHaveCount(1);
xexpect("Project/CollectionSubjectLanguages[text()='ab;cd;ef']").toHaveCount(
1
);
xexpect("Project/CollectionWorkingLanguages").toHaveCount(1);
xexpect("Project/CollectionWorkingLanguages[text()='gh;ij']").toHaveCount(1);
// and these we output for backwards compatibility
xexpect("Project/VernacularISO3CodeAndName").toHaveCount(1);
xexpect("Project/VernacularISO3CodeAndName[text()='ab']").toHaveCount(1);
xexpect("Project/AnalysisISO3CodeAndName").toHaveCount(1);
xexpect("Project/AnalysisISO3CodeAndName[text()='gh']").toHaveCount(1);
it("should write languages", () => {
const f = GetProjectFileWithOneField("unused", "x");
f.properties.setText("collectionSubjectLanguages", "ab;cd;ef");
f.properties.setText("collectionWorkingLanguages", "gh;ij");
setResultXml(f.getXml());
xexpect("Project/CollectionSubjectLanguages").toHaveCount(1);
xexpect(
"Project/CollectionSubjectLanguages[text()='ab;cd;ef']"
).toHaveCount(1);
xexpect("Project/CollectionWorkingLanguages").toHaveCount(1);
xexpect("Project/CollectionWorkingLanguages[text()='gh;ij']").toHaveCount(
1
);
// and these we output for backwards compatibility
xexpect("Project/VernacularISO3CodeAndName").toHaveCount(1);
xexpect("Project/VernacularISO3CodeAndName[text()='ab']").toHaveCount(1);
xexpect("Project/AnalysisISO3CodeAndName").toHaveCount(1);
xexpect("Project/AnalysisISO3CodeAndName[text()='gh']").toHaveCount(1);
});
});

function AttemptRoundTripOfOneField(
Expand Down
5 changes: 5 additions & 0 deletions src/model/file/File.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ describe("FolderMetadataFile", () => {
expect(newXml).toBe(originalXml);
});
it("can roundtrip sample project file", () => {
// TODO: this is broken because we are migrating from single language to multiple project languages.
// How to make a reasonable test? Maybe get
// all the properties of the original and then verify
// that after writing and reading back in, they are the same?

const originalPath = "./sample data/Edolo sample/Edolo sample.sprj";
const originalXml: string = fs.readFileSync(originalPath, "utf8");
const newDir = Path.join(temp.dir, "Edolo sample");
Expand Down

0 comments on commit 95ad70f

Please sign in to comment.