<commit_before>﻿// Copyright (c) 2016 SIL International
// This software is licensed under the MIT license (http://opensource.org/licenses/MIT)
using Autofac;
using NUnit.Framework;
using LfMerge;
using LfMerge.Actions;
using LfMerge.DataConverters;
using LfMerge.MongoConnector;
using LfMerge.Tests;
using LfMerge.LanguageForge.Model;
using SIL.FieldWorks.FDO;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LfMerge.Tests.Fdo
{
	public class UpdateMongoFromFdoActionTests : FdoTestBase
	{
		private LfOptionList CreateLfGrammarWith(IEnumerable<LfOptionListItem> grammarItems)
		{
			var result = new LfOptionList();
			result.Code = MagicStrings.LfOptionListCodeForGrammaticalInfo;
			result.Name = MagicStrings.LfOptionListNameForGrammaticalInfo;
			result.DateCreated = result.DateModified = System.DateTime.UtcNow;
			result.CanDelete = false;
			result.DefaultItemKey = null;
			result.Items = grammarItems.ToList();
			return result;
		}

		private IEnumerable<LfOptionListItem> DefaultGrammarItems(int howMany)
		{
			foreach (string guidStr in PartOfSpeechMasterList.FlatPosNames.Keys.Take(howMany))
			{
				string name = PartOfSpeechMasterList.FlatPosNames[guidStr];
				string abbrev = PartOfSpeechMasterList.FlatPosAbbrevs[guidStr];
				yield return new LfOptionListItem {
					Guid = Guid.Parse(guidStr),
					Key = abbrev,
					Abbreviation = abbrev,
					Value = name,
				};
			}
		}

		[Test]
		public void Action_InitialClone_ShouldPopulateMongoInputSystems()
		{
			// Setup
			var lfProject = LanguageForgeProject.Create(_env.Settings, testProjectCode);
			UpdateMongoDbFromFdo.InitialClone = true;
			Dictionary<string, LfInputSystemRecord> lfWsList = _conn.GetInputSystems(lfProject);
			Assert.That(lfWsList.Count, Is.EqualTo(0));

			// Exercise
			sutFdoToMongo.Run(lfProject);

			// Verify
			const int expectedNumVernacularWS = 3;
			const int expectedNumAnalysisWS = 2;
			// TODO: Investigate why qaa-Zxxx-x-kal-audio is in CurrentVernacularWritingSystems, but somehow in
			// UpdateMongoDbFromFdo.FdoWsToLfWs() is not contained in _cache.LangProject.CurrentVernacularWritingSystems
			const string notVernacularWs = "qaa-Zxxx-x-kal-audio";

			lfWsList = _conn.GetInputSystems(lfProject);
			var languageProj = lfProject.FieldWorksProject.Cache.LangProject;

			foreach (var fdoVernacularWs in languageProj.CurrentVernacularWritingSystems)
			{
				if (fdoVernacularWs.LanguageTag != notVernacularWs)
					Assert.That(lfWsList[fdoVernacularWs.LanguageTag].VernacularWS);
			}
			Assert.That(languageProj.CurrentVernacularWritingSystems.Count, Is.EqualTo(expectedNumVernacularWS));

			foreach (var fdoAnalysisWs in languageProj.CurrentAnalysisWritingSystems)
				Assert.That(lfWsList[fdoAnalysisWs.LanguageTag].AnalysisWS);
			Assert.That(languageProj.CurrentAnalysisWritingSystems.Count, Is.EqualTo(expectedNumAnalysisWS));
		}

		[Test]
		public void Action_InitialClone_ShouldUpdateDates()
		{
			// Setup
			var lfProject = LanguageForgeProject.Create(_env.Settings, testProjectCode);
			UpdateMongoDbFromFdo.InitialClone = true;

			// Exercise
			sutFdoToMongo.Run(lfProject);

			// Verify
			IEnumerable<LfLexEntry> receivedData = _conn.StoredLfLexEntries.Values;
			Assert.That(receivedData, Is.Not.Null);
			Assert.That(receivedData, Is.Not.Empty);

			string expectedGuidStr = "1a705846-a814-4289-8594-4b874faca6cc";
			LfLexEntry entry = receivedData.FirstOrDefault(e => e.Guid.ToString() == expectedGuidStr);
			Assert.That(entry, Is.Not.Null);
			Assert.That(entry.DateCreated, Is.EqualTo(DateTime.Now).Within(1).Seconds);
			Assert.That(entry.DateModified, Is.EqualTo(DateTime.Now).Within(1).Seconds);
		}

		[Test]
		public void Action_ShouldUpdateLexemes()
		{
			// Setup
			var lfProject = LanguageForgeProject.Create(_env.Settings, testProjectCode);

			// Exercise
			sutFdoToMongo.Run(lfProject);

			// Verify
			string[] searchOrder = new string[] { "en", "fr" };
			string expectedLexeme = "zitʰɛstmen";
			string expectedGuidStr = "1a705846-a814-4289-8594-4b874faca6cc";

			IEnumerable<LfLexEntry> receivedData = _conn.StoredLfLexEntries.Values;
			Assert.That(receivedData, Is.Not.Null);
			Assert.That(receivedData, Is.Not.Empty);
			LfLexEntry entry = receivedData.FirstOrDefault(e => e.Guid.ToString() == expectedGuidStr);
			Assert.That(entry, Is.Not.Null);
			Assert.That(entry.Lexeme.BestString(searchOrder), Is.EqualTo(expectedLexeme));
		}

		[Test]
		public void Action_WithEmptyMongoGrammar_ShouldPopulateMongoGrammarFromFdoGrammar()
		{
			// Setup
			var lfProject = LanguageForgeProject.Create(_env.Settings, testProjectCode);
			LfOptionList lfGrammar = _conn.GetLfOptionLists()
				.FirstOrDefault(optionList => optionList.Code == MagicStrings.LfOptionListCodeForGrammaticalInfo);
			Assert.That(lfGrammar, Is.Null);

			// Exercise
			sutFdoToMongo.Run(lfProject);
<commit_msg>Add test to UpdateMongoFromFdoAction for adding pictures<commit_after>﻿// Copyright (c) 2016 SIL International
// This software is licensed under the MIT license (http://opensource.org/licenses/MIT)
using Autofac;
using NUnit.Framework;
using LfMerge;
using LfMerge.Actions;
using LfMerge.DataConverters;
using LfMerge.MongoConnector;
using LfMerge.Tests;
using LfMerge.LanguageForge.Model;
using SIL.FieldWorks.FDO;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LfMerge.Tests.Fdo
{
	public class UpdateMongoFromFdoActionTests : FdoTestBase
	{
		private LfOptionList CreateLfGrammarWith(IEnumerable<LfOptionListItem> grammarItems)
		{
			var result = new LfOptionList();
			result.Code = MagicStrings.LfOptionListCodeForGrammaticalInfo;
			result.Name = MagicStrings.LfOptionListNameForGrammaticalInfo;
			result.DateCreated = result.DateModified = System.DateTime.UtcNow;
			result.CanDelete = false;
			result.DefaultItemKey = null;
			result.Items = grammarItems.ToList();
			return result;
		}

		private IEnumerable<LfOptionListItem> DefaultGrammarItems(int howMany)
		{
			foreach (string guidStr in PartOfSpeechMasterList.FlatPosNames.Keys.Take(howMany))
			{
				string name = PartOfSpeechMasterList.FlatPosNames[guidStr];
				string abbrev = PartOfSpeechMasterList.FlatPosAbbrevs[guidStr];
				yield return new LfOptionListItem {
					Guid = Guid.Parse(guidStr),
					Key = abbrev,
					Abbreviation = abbrev,
					Value = name,
				};
			}
		}

		[Test]
		public void Action_InitialClone_ShouldPopulateMongoInputSystems()
		{
			// Setup
			var lfProject = LanguageForgeProject.Create(_env.Settings, testProjectCode);
			UpdateMongoDbFromFdo.InitialClone = true;
			Dictionary<string, LfInputSystemRecord> lfWsList = _conn.GetInputSystems(lfProject);
			Assert.That(lfWsList.Count, Is.EqualTo(0));

			// Exercise
			sutFdoToMongo.Run(lfProject);

			// Verify
			const int expectedNumVernacularWS = 3;
			const int expectedNumAnalysisWS = 2;
			// TODO: Investigate why qaa-Zxxx-x-kal-audio is in CurrentVernacularWritingSystems, but somehow in
			// UpdateMongoDbFromFdo.FdoWsToLfWs() is not contained in _cache.LangProject.CurrentVernacularWritingSystems
			const string notVernacularWs = "qaa-Zxxx-x-kal-audio";

			lfWsList = _conn.GetInputSystems(lfProject);
			var languageProj = lfProject.FieldWorksProject.Cache.LangProject;

			foreach (var fdoVernacularWs in languageProj.CurrentVernacularWritingSystems)
			{
				if (fdoVernacularWs.LanguageTag != notVernacularWs)
					Assert.That(lfWsList[fdoVernacularWs.LanguageTag].VernacularWS);
			}
			Assert.That(languageProj.CurrentVernacularWritingSystems.Count, Is.EqualTo(expectedNumVernacularWS));

			foreach (var fdoAnalysisWs in languageProj.CurrentAnalysisWritingSystems)
				Assert.That(lfWsList[fdoAnalysisWs.LanguageTag].AnalysisWS);
			Assert.That(languageProj.CurrentAnalysisWritingSystems.Count, Is.EqualTo(expectedNumAnalysisWS));
		}

		[Test]
		public void Action_InitialClone_ShouldUpdateDates()
		{
			// Setup
			var lfProject = LanguageForgeProject.Create(_env.Settings, testProjectCode);
			UpdateMongoDbFromFdo.InitialClone = true;

			// Exercise
			sutFdoToMongo.Run(lfProject);

			// Verify
			IEnumerable<LfLexEntry> receivedData = _conn.StoredLfLexEntries.Values;
			Assert.That(receivedData, Is.Not.Null);
			Assert.That(receivedData, Is.Not.Empty);

			string expectedGuidStr = "1a705846-a814-4289-8594-4b874faca6cc";
			LfLexEntry entry = receivedData.FirstOrDefault(e => e.Guid.ToString() == expectedGuidStr);
			Assert.That(entry, Is.Not.Null);
			Assert.That(entry.DateCreated, Is.EqualTo(DateTime.Now).Within(1).Seconds);
			Assert.That(entry.DateModified, Is.EqualTo(DateTime.Now).Within(1).Seconds);
		}

		[Test]
		public void Action_ShouldUpdateLexemes()
		{
			// Setup
			var lfProject = LanguageForgeProject.Create(_env.Settings, testProjectCode);

			// Exercise
			sutFdoToMongo.Run(lfProject);

			// Verify
			string[] searchOrder = new string[] { "en", "fr" };
			string expectedLexeme = "zitʰɛstmen";
			string expectedGuidStr = "1a705846-a814-4289-8594-4b874faca6cc";

			IEnumerable<LfLexEntry> receivedData = _conn.StoredLfLexEntries.Values;
			Assert.That(receivedData, Is.Not.Null);
			Assert.That(receivedData, Is.Not.Empty);
			LfLexEntry entry = receivedData.FirstOrDefault(e => e.Guid.ToString() == expectedGuidStr);
			Assert.That(entry, Is.Not.Null);
			Assert.That(entry.Lexeme.BestString(searchOrder), Is.EqualTo(expectedLexeme));
		}

		[Test]
		public void Action_ShouldUpdatePictures()
		{
			// Setup
			var lfProj = LanguageForgeProject.Create(_env.Settings, testProjectCode);
			IEnumerable<LfLexEntry> receivedData = _conn.StoredLfLexEntries.Values;
			int originalNumPictures = receivedData.Count(e => ((e.Senses.Count > 0) && (e.Senses[0].Pictures.Count > 0)));
			Assert.That(originalNumPictures, Is.EqualTo(0));

			// Exercise
			sutFdoToMongo.Run(lfProj);

			// Verify LF project now contains the 4 FDO pictures (1 externally linked, 3 internal)
			int newNumPictures = receivedData.Count(e => ((e.Senses.Count > 0) && (e.Senses[0].Pictures.Count > 0)));
			Assert.That(newNumPictures, Is.EqualTo(4));
		}

		[Test]
		public void Action_WithEmptyMongoGrammar_ShouldPopulateMongoGrammarFromFdoGrammar()
		{
			// Setup
			var lfProject = LanguageForgeProject.Create(_env.Settings, testProjectCode);
			LfOptionList lfGrammar = _conn.GetLfOptionLists()
				.FirstOrDefault(optionList => optionList.Code == MagicStrings.LfOptionListCodeForGrammaticalInfo);
			Assert.That(lfGrammar, Is.Null);

			// Exercise
			sutFdoToMongo.Run(lfProject);

{'fraction_non_alphanumeric': 0.10346798780487805, 'fraction_numerical': 0.011528201219512195, 'mean_word_length': 4.857700892857143, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4551156', 'n_tokens_mistral': 4098, 'n_tokens_neox': 3450, 'n_words': 574}