11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
4+ using Bogus ;
5+ using FluentAssertions ;
36using Xunit ;
47
5- namespace ICG . NetCore . Utilities . Spreadsheet . Tests
8+ namespace ICG . NetCore . Utilities . Spreadsheet . Tests ;
9+
10+ // ReSharper disable once InconsistentNaming
11+ public class OpenXmlSpreadsheetGeneratorTests
612{
7- // ReSharper disable once InconsistentNaming
8- public class OpenXmlSpreadsheetGeneratorTests
13+ private readonly ISpreadsheetGenerator _spreadsheetGenerator ;
14+
15+ public OpenXmlSpreadsheetGeneratorTests ( )
916 {
10- private readonly ISpreadsheetGenerator _spreadsheetGenerator ;
17+ _spreadsheetGenerator = new OpenXmlSpreadsheetGenerator ( ) ;
18+ }
1119
12- public OpenXmlSpreadsheetGeneratorTests ( )
13- {
14- _spreadsheetGenerator = new OpenXmlSpreadsheetGenerator ( ) ;
15- }
20+ [ Fact ]
21+ public void CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenWorksheetNameIsMissing ( )
22+ {
23+ //Arrange
24+ var configuration = new SpreadsheetConfiguration < SampleExportRecord > ( ) ;
1625
17- [ Fact ]
18- public void CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenWorksheetNameIsMissing ( )
19- {
20- //Arrange
21- var configuration = new SpreadsheetConfiguration < SampleExportRecord > ( ) ;
22-
23- //Act/Assert
24- //Assert that it throws the exception
25- var result = Assert . Throws < ArgumentException > ( ( ) =>
26- _spreadsheetGenerator . CreateSingleSheetSpreadsheet < SampleExportRecord > ( configuration ) ) ;
27- //Assert for the proper property
28- Assert . Equal ( "WorksheetName" , result . ParamName ) ;
29- }
30-
31- [ Fact ]
32- public void CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenWorksheetNameWhiteSpace ( )
33- {
34- //Arrange
35- var configuration = new SpreadsheetConfiguration < SampleExportRecord > { WorksheetName = " " } ;
36-
37- //Act/Assert
38- //Assert that it throws the exception
39- var result = Assert . Throws < ArgumentException > ( ( ) =>
40- _spreadsheetGenerator . CreateSingleSheetSpreadsheet < SampleExportRecord > ( configuration ) ) ;
41- //Assert for the proper property
42- Assert . Equal ( "WorksheetName" , result . ParamName ) ;
43- }
44-
45- [ Fact ]
46- public void CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenExportDataIsNull ( )
47- {
48- //Arrange
49- var configuration = new SpreadsheetConfiguration < SampleExportRecord > { WorksheetName = "TestSheet" } ;
50-
51- //Act/Assert
52- //Assert that it throws the exception
53- var result = Assert . Throws < ArgumentException > ( ( ) =>
54- _spreadsheetGenerator . CreateSingleSheetSpreadsheet < SampleExportRecord > ( configuration ) ) ;
55- //Assert for the proper property
56- Assert . Equal ( "ExportData" , result . ParamName ) ;
57- }
58-
59- [ Fact ]
60- public void CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenRenderTitleIsTrueAndDocumentTitleIsNull ( )
26+ //Act/Assert
27+ //Assert that it throws the exception
28+ var result = Assert . Throws < ArgumentException > ( ( ) =>
29+ _spreadsheetGenerator . CreateSingleSheetSpreadsheet ( configuration ) ) ;
30+ //Assert for the proper property
31+ Assert . Equal ( "WorksheetName" , result . ParamName ) ;
32+ }
33+
34+ [ Fact ]
35+ public void CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenWorksheetNameWhiteSpace ( )
36+ {
37+ //Arrange
38+ var configuration = new SpreadsheetConfiguration < SampleExportRecord > { WorksheetName = " " } ;
39+
40+ //Act/Assert
41+ //Assert that it throws the exception
42+ var result = Assert . Throws < ArgumentException > ( ( ) =>
43+ _spreadsheetGenerator . CreateSingleSheetSpreadsheet ( configuration ) ) ;
44+ //Assert for the proper property
45+ Assert . Equal ( "WorksheetName" , result . ParamName ) ;
46+ }
47+
48+ [ Fact ]
49+ public void CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenExportDataIsNull ( )
50+ {
51+ //Arrange
52+ var configuration = new SpreadsheetConfiguration < SampleExportRecord > { WorksheetName = "TestSheet" } ;
53+
54+ //Act/Assert
55+ //Assert that it throws the exception
56+ var result = Assert . Throws < ArgumentException > ( ( ) =>
57+ _spreadsheetGenerator . CreateSingleSheetSpreadsheet ( configuration ) ) ;
58+ //Assert for the proper property
59+ Assert . Equal ( "ExportData" , result . ParamName ) ;
60+ }
61+
62+ [ Fact ]
63+ public void CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenRenderTitleIsTrueAndDocumentTitleIsNull ( )
64+ {
65+ //Arrange
66+ var configuration = new SpreadsheetConfiguration < SampleExportRecord >
6167 {
62- //Arrange
63- var configuration = new SpreadsheetConfiguration < SampleExportRecord >
64- {
65- WorksheetName = "TestSheet" , ExportData = new List < SampleExportRecord > ( ) , RenderTitle = true
66- } ;
67-
68- //Act/Assert
69- //Assert that it throws the exception
70- var result = Assert . Throws < ArgumentException > ( ( ) =>
71- _spreadsheetGenerator . CreateSingleSheetSpreadsheet < SampleExportRecord > ( configuration ) ) ;
72- //Assert for the proper property
73- Assert . Equal ( "DocumentTitle" , result . ParamName ) ;
74- }
75-
76- [ Fact ]
77- public void CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenRenderSubTitleIsTrueAndDocumentSubTitleIsNull ( )
68+ WorksheetName = "TestSheet" , ExportData = new List < SampleExportRecord > ( ) , RenderTitle = true
69+ } ;
70+
71+ //Act/Assert
72+ //Assert that it throws the exception
73+ var result = Assert . Throws < ArgumentException > ( ( ) =>
74+ _spreadsheetGenerator . CreateSingleSheetSpreadsheet ( configuration ) ) ;
75+ //Assert for the proper property
76+ Assert . Equal ( "DocumentTitle" , result . ParamName ) ;
77+ }
78+
79+ [ Fact ]
80+ public void
81+ CreateSingleSheetWorksheet_ShouldThrowArgumentException_WhenRenderSubTitleIsTrueAndDocumentSubTitleIsNull ( )
82+ {
83+ //Arrange
84+ var configuration = new SpreadsheetConfiguration < SampleExportRecord >
7885 {
79- //Arrange
80- var configuration = new SpreadsheetConfiguration < SampleExportRecord >
81- {
82- WorksheetName = "TestSheet" ,
83- ExportData = new List < SampleExportRecord > ( ) ,
84- RenderSubTitle = true
85- } ;
86-
87- //Act/Assert
88- //Assert that it throws the exception
89- var result = Assert . Throws < ArgumentException > ( ( ) =>
90- _spreadsheetGenerator . CreateSingleSheetSpreadsheet < SampleExportRecord > ( configuration ) ) ;
91- //Assert for the proper property
92- Assert . Equal ( "DocumentSubTitle" , result . ParamName ) ;
93- }
94-
95- [ Fact ]
96- public void CreateMultiSheetSpreadsheet_ShouldThrowError_WhenProvidedSheetsIsNull ( )
86+ WorksheetName = "TestSheet" ,
87+ ExportData = new List < SampleExportRecord > ( ) ,
88+ RenderSubTitle = true
89+ } ;
90+
91+ //Act/Assert
92+ //Assert that it throws the exception
93+ var result = Assert . Throws < ArgumentException > ( ( ) =>
94+ _spreadsheetGenerator . CreateSingleSheetSpreadsheet ( configuration ) ) ;
95+ //Assert for the proper property
96+ Assert . Equal ( "DocumentSubTitle" , result . ParamName ) ;
97+ }
98+
99+ [ Fact ]
100+ public void CreateMultiSheetSpreadsheet_ShouldThrowError_WhenProvidedSheetsIsNull ( )
101+ {
102+ //Arrange
103+
104+ //Act
105+ var actualResult =
106+ Assert . Throws < ArgumentNullException > ( ( ) => _spreadsheetGenerator . CreateMultiSheetSpreadsheet ( null ) ) ;
107+
108+ //Assert
109+ Assert . Equal ( "exportSheets" , actualResult . ParamName ) ;
110+ }
111+
112+ [ Fact ]
113+ public void CreateSingleWorksheet_Should_Work ( )
114+ {
115+ var id = 1 ;
116+ var testRecord = new Faker < TestExportRecord > ( )
117+ . UseSeed ( 42 )
118+ . RuleFor ( o => o . Id , f => id ++ )
119+ . RuleFor ( o => o . Name , f => f . Name . FullName ( ) )
120+ . RuleFor ( o => o . Amount , f => f . Random . Double ( 0 , 10 ) )
121+ . RuleFor ( o => o . Date , f => f . Date . Soon ( ) ) ;
122+
123+ var result = _spreadsheetGenerator . CreateSingleSheetSpreadsheet ( new SpreadsheetConfiguration < TestExportRecord >
97124 {
98- //Arrange
125+ WorksheetName = "Test Sheet" ,
126+ AutoSizeColumns = false ,
127+ ExportData = testRecord . Generate ( 10 )
128+ } ) ;
99129
100- //Act
101- var actualResult =
102- Assert . Throws < ArgumentNullException > ( ( ) => _spreadsheetGenerator . CreateMultiSheetSpreadsheet ( null ) ) ;
130+ result . Length . Should ( ) . BeGreaterThan ( 0 ) ;
103131
104- //Assert
105- Assert . Equal ( "exportSheets" , actualResult . ParamName ) ;
106- }
132+ var sheetPath = Path . Join ( Path . GetTempPath ( ) ,
133+ $ "createsingleworksheet_should_work_ { DateTime . Now : yyyyMMddHHmmssfff } .xlsx" ) ;
134+ File . WriteAllBytes ( sheetPath , result ) ;
107135 }
108- }
136+ }
0 commit comments