private byte[] GetMetadataFileAsBytes(IndicatorMetadata indicatorMetadata, 
            IList<IndicatorMetadataTextProperty> properties, IDictionary<string, string> descriptive)
        {
            // Create CSV writer
            var csvWriter = new CsvWriter();
            csvWriter.AddHeader("Field", "Text");

            // Add descriptive metadata properties
            foreach (var property in properties)
            {
                if (descriptive.ContainsKey(property.ColumnName))
                {
                    var text = descriptive[property.ColumnName];

                    if (string.IsNullOrWhiteSpace(text) == false)
                    {
                        text = htmlCleaner.RemoveHtml(text);
                        csvWriter.AddLine(property.DisplayName, text);
                    }
                }
            }

            // Add other properties
            csvWriter.AddLine("Unit", indicatorMetadata.Unit.Label);
            csvWriter.AddLine("Value type", indicatorMetadata.ValueType.Name);
            csvWriter.AddLine("Year type", indicatorMetadata.YearType.Name);

            var bytes = csvWriter.WriteAsBytes();
            return bytes;
        }
        public void TestAddingBothHeaderAndLineIncreasesSizeOfFile()
        {
            var writer = new CsvWriter();
            writer.AddHeader("a");
            var bytesWithOnlyHeader = writer.WriteAsBytes();

            writer = new CsvWriter();
            writer.AddHeader("a");
            writer.AddLine("b");
            var bytesWithHeaderAndLine = writer.WriteAsBytes();

            Assert.AreNotEqual(0, bytesWithOnlyHeader);
            Assert.IsTrue(bytesWithOnlyHeader.Length < bytesWithHeaderAndLine.Length);
        }
        private byte[] GetCoreDataFileAsBytes(IList<CkanCoreDataSet> dataList)
        {
            // Create CSV writer
            var csvWriter = new CsvWriter();
            csvWriter.AddHeader("Area Code", "Area Name", "Area Type", "Sex", "Age",
                "Category Type", "Category",
                "Time period", "Value", "Lower CI limit", "Upper CI limit",
                "Count", "Denominator",
                "Value note");

            // Add descriptive metadata properties
            foreach (CkanCoreDataSet data in dataList)
            {
                var coreDataSet = data.Data;

                string categoryTypeId = coreDataSet.CategoryTypeId != -1
                    ? lookUpManager.GetCategoryTypeName(coreDataSet.CategoryTypeId)
                    : "";

                string categoryId = coreDataSet.CategoryId != -1
                    ? lookUpManager.GetCategoryName(coreDataSet.CategoryTypeId, coreDataSet.CategoryId)
                    : "";

                var val = coreDataSet.IsValueValid
                    ? coreDataSet.Value.ToString()
                    : "";

                string lowerCI;
                string upperCI;
                if (coreDataSet.AreCIsValid)
                {
                    lowerCI = coreDataSet.LowerCI.ToString();
                    upperCI = coreDataSet.UpperCI.ToString();
                }
                else
                {
                    lowerCI = string.Empty;
                    upperCI = string.Empty;
                }

                var count = coreDataSet.IsCountValid
                    ? coreDataSet.Count.ToString()
                    : "";

                var denominator = coreDataSet.IsDenominatorValid
                    ? coreDataSet.Denominator.ToString()
                    : "";

                var valueNote = coreDataSet.ValueNoteId > 0
                    ? lookUpManager.GetValueNoteText(coreDataSet.ValueNoteId)
                    : "";

                var areaCode = coreDataSet.AreaCode;
                csvWriter.AddLine(
                    areaCode,
                    lookUpManager.GetAreaName(areaCode),
                    lookUpManager.GetAreaTypeName(areaCode),
                    lookUpManager.GetSexName(coreDataSet.SexId),
                    lookUpManager.GetAgeName(coreDataSet.AgeId),
                    categoryTypeId,
                    categoryId,
                    data.TimePeriodString,
                    val,
                    lowerCI,
                    upperCI,
                    count,
                    denominator,
                    valueNote);
            }

            byte[] bytes = csvWriter.WriteAsBytes();
            return bytes;
        }
 public void TestWriteEmptyFile()
 {
     var writer = new CsvWriter();
     Assert.IsNotNull(writer.WriteAsBytes());
 }