/// <summary> /// Save the mock results to a file. /// </summary> /// <param name="mockResult">The results to save</param> private void SaveMockResult(MockQueryResult mockResult) { string[] parts = mockResult.MockId.Split(new char[] { '.' }); string fileName = Path.Combine(this.settings.OutputPath, parts[0] + ".xml"); MockQueryResultSet mockResultSet = null; if (File.Exists(fileName)) { string fileText = File.ReadAllText(fileName).Trim(); if (fileText != String.Empty) { using (Stream stream = new MemoryStream(System.Text.UnicodeEncoding.UTF8.GetBytes(fileText))) { mockResultSet = MockQueryResultSet.Deserialize(stream); } } } if (mockResultSet == null) { mockResultSet = new MockQueryResultSet(); } string mockResultKey = NormalizeCommandText(mockResult.CommandText); int matchIdx = -1; for (int idx = 0; idx < mockResultSet.CommandResults.Count; idx++) { MockQueryResult currentResult = mockResultSet.CommandResults[idx]; if (NormalizeCommandText(currentResult.CommandText) == mockResultKey && currentResult.DatabaseName == mockResult.DatabaseName && currentResult.MockId == mockResult.MockId) { matchIdx = idx; break; } } if (matchIdx >= 0) { mockResultSet.CommandResults[matchIdx] = mockResult; } else { mockResultSet.CommandResults.Add(mockResult); } using (Stream stream = File.Open(fileName, FileMode.Create, FileAccess.Write)) { MockQueryResultSet.Serialize(stream, mockResultSet); } AddMockResult(mockResult); }
/// <summary> /// Serializes the provided <see cref="MockQueryResultSet"/> into the stream /// </summary> /// <param name="stream">Where to output the serialization</param> /// <param name="value">What to serialize</param> public static void Serialize(Stream stream, MockQueryResultSet value) { XmlSerializer serializer = new XmlSerializer(typeof(MockQueryResultSet)); using (StreamWriter writer = new StreamWriter(stream)) { serializer.Serialize(writer, value); } }
/// <summary> /// Used to initialize the mock results /// </summary> private static void InitializeMockResults() { string path = "./TSqlMockSessions"; var files = Directory.GetFiles(path); foreach (var file in files) { using (FileStream stream = new FileStream(file, FileMode.Open)) { MockQueryResultSet mockResultSet = MockQueryResultSet.Deserialize(stream); if (mockResultSet.CommandResults != null) { foreach (MockQueryResult mockResult in mockResultSet.CommandResults) { AddMockResult(mockResult); } } } } }