44
55namespace Flow \ETL \Adapter \GoogleSheet \Tests \Integration ;
66
7- use function Flow \ETL \DSL \{config , flow_context };
8- use Flow \ETL \Adapter \GoogleSheet \{Columns , GoogleSheetExtractor , Tests \GoogleSheetsContext };
7+ use function Flow \ETL \Adapter \GoogleSheet \from_google_sheet ;
8+ use function Flow \ETL \DSL \{df , int_schema , schema , string_schema };
9+ use Flow \ETL \Adapter \GoogleSheet \{Tests \GoogleSheetsContext };
910use Flow \ETL \Exception \InvalidArgumentException ;
1011use Flow \ETL \Tests \FlowTestCase ;
1112
@@ -18,30 +19,62 @@ protected function setUp() : void
1819 $ this ->context = new GoogleSheetsContext ();
1920 }
2021
21- public function test_extract_skip_extra_empty_rows () : void
22+ public function test_extract_puts_null_in_not_matching_schema_rows () : void
2223 {
23- $ extractor = new GoogleSheetExtractor (
24- $ this ->context ->sheets (__DIR__ . '/../Fixtures/extra-empty-rows.json ' ),
25- '1234567890 ' ,
26- new Columns ('Sheet ' , 'A ' , 'Z ' ),
27- );
24+ $ rows = df ()
25+ ->extract (
26+ from_google_sheet (
27+ $ this ->context ->sheets (__DIR__ . '/../Fixtures/extra-empty-rows.json ' ),
28+ '1234567890 ' ,
29+ 'Sheet ' ,
30+ )->withSchema (
31+ schema (
32+ string_schema ('Header 1 ' ),
33+ string_schema ('Header 2 ' ),
34+ int_schema ('id ' ),
35+ )
36+ )
37+ )
38+ ->fetch ()
39+ ->toArray ();
2840
29- $ rows = $ extractor ->extract (flow_context (config ()));
41+ foreach ($ rows as $ row ) {
42+ self ::assertNotSame ([], $ row );
43+ self ::assertArrayNotHasKey ('Header 3 ' , $ row );
44+ self ::assertNull ($ row ['id ' ]);
45+ }
46+ }
47+
48+ public function test_extract_skip_extra_empty_rows () : void
49+ {
50+ $ rows = df ()
51+ ->extract (
52+ from_google_sheet (
53+ $ this ->context ->sheets (__DIR__ . '/../Fixtures/extra-empty-rows.json ' ),
54+ '1234567890 ' ,
55+ 'Sheet ' ,
56+ )
57+ )
58+ ->fetch ()
59+ ->toArray ();
3060
3161 foreach ($ rows as $ row ) {
32- self ::assertNotSame ([], $ row-> toArray () );
62+ self ::assertNotSame ([], $ row );
3363 }
3464 }
3565
3666 public function test_extract_with_cut_extra_columns () : void
3767 {
38- $ extractor = new GoogleSheetExtractor (
39- $ this ->context ->sheets (__DIR__ . '/../Fixtures/extra-columns.json ' ),
40- '1234567890 ' ,
41- new Columns ('Sheet ' , 'A ' , 'Z ' ),
42- );
43-
44- $ rows = $ extractor ->extract (flow_context (config ()));
68+ $ rows = df ()
69+ ->extract (
70+ from_google_sheet (
71+ $ this ->context ->sheets (__DIR__ . '/../Fixtures/extra-columns.json ' ),
72+ '1234567890 ' ,
73+ 'Sheet ' ,
74+ )
75+ )
76+ ->fetch ()
77+ ->toArray ();
4578
4679 foreach ($ rows as $ row ) {
4780 self ::assertNotNull ($ row );
@@ -50,20 +83,18 @@ public function test_extract_with_cut_extra_columns() : void
5083
5184 public function test_extract_without_cut_extra_columns () : void
5285 {
53- $ extractor = new GoogleSheetExtractor (
54- $ this ->context ->sheets (__DIR__ . '/../Fixtures/extra-columns.json ' ),
55- '1234567890 ' ,
56- new Columns ('Sheet ' , 'A ' , 'Z ' ),
57- );
58- $ extractor ->withDropExtraColumns (false );
59-
60- $ rows = $ extractor ->extract (flow_context (config ()));
61-
6286 $ this ->expectException (InvalidArgumentException::class);
6387 $ this ->expectExceptionMessage ('Row has more columns (4) than headers (3) ' );
6488
65- foreach ($ rows as $ row ) {
66- self ::assertNotNull ($ row );
67- }
89+ df ()
90+ ->extract (
91+ from_google_sheet (
92+ $ this ->context ->sheets (__DIR__ . '/../Fixtures/extra-columns.json ' ),
93+ '1234567890 ' ,
94+ 'Sheet ' ,
95+ )->withDropExtraColumns (false )
96+ )
97+ ->fetch ()
98+ ->toArray ();
6899 }
69100}
0 commit comments