|
3 | 3 | use Yajra\SQLLoader\ControlFileBuilder;
|
4 | 4 | use Yajra\SQLLoader\SQLLoader;
|
5 | 5 |
|
6 |
| -test('it can build a control file', function () { |
7 |
| - $loader = new SQLLoader(['skip=1', 'load=2']); |
8 |
| - $loader->inFile(__DIR__.'/../data/users.dat') |
9 |
| - ->as('users.ctl') |
10 |
| - ->into( |
11 |
| - table: 'users', |
12 |
| - columns: ['id', 'name', 'email'], |
13 |
| - trailing: 'TRAILING NULLCOLS' |
14 |
| - ); |
15 |
| - |
16 |
| - $ctl = new ControlFileBuilder($loader); |
17 |
| - $controlFile = $ctl->build(); |
18 |
| - |
19 |
| - expect($controlFile)->toBeString() |
20 |
| - ->and($controlFile)->toContain('OPTIONS(skip=1, load=2)') |
21 |
| - ->and($controlFile)->toContain("INFILE '".__DIR__."/../data/users.dat'") |
22 |
| - ->and($controlFile)->toContain('APPEND') |
23 |
| - ->and($controlFile)->toContain('INTO TABLE users') |
24 |
| - ->and($controlFile)->toContain("FIELDS TERMINATED BY ','") |
25 |
| - ->and($controlFile)->toContain('OPTIONALLY') |
26 |
| - ->and($controlFile)->toContain("ENCLOSED BY '\"'") |
27 |
| - ->and($controlFile)->toContain('TRAILING NULLCOLS') |
28 |
| - ->and($controlFile)->toContain('(') |
29 |
| - ->and($controlFile)->toContain('id,') |
30 |
| - ->and($controlFile)->toContain('name,') |
31 |
| - ->and($controlFile)->toContain('email') |
32 |
| - ->and($controlFile)->toContain(')'); |
33 |
| -}); |
34 |
| - |
35 |
| -test('it can build multiple input files', function () { |
36 |
| - $loader = new SQLLoader(['skip=1', 'load=2']); |
37 |
| - $loader->inFile(__DIR__.'/../data/users.dat') |
38 |
| - ->inFile(__DIR__.'/../data/roles.dat') |
39 |
| - ->as('users.ctl') |
40 |
| - ->into( |
41 |
| - table: 'users', |
42 |
| - columns: ['id', 'name', 'email'], |
43 |
| - trailing: 'TRAILING NULLCOLS' |
44 |
| - ); |
45 |
| - |
46 |
| - $ctl = new ControlFileBuilder($loader); |
47 |
| - $controlFile = $ctl->build(); |
48 |
| - |
49 |
| - expect($controlFile)->toBeString() |
50 |
| - ->and($controlFile)->toContain("INFILE '".__DIR__."/../data/users.dat'") |
51 |
| - ->and($controlFile)->toContain("INFILE '".__DIR__."/../data/roles.dat'"); |
52 |
| -}); |
53 |
| - |
54 |
| -test('it can build with bad file, discard file and discard max', function () { |
55 |
| - $loader = new SQLLoader(['skip=1', 'load=2']); |
56 |
| - $loader->inFile(__DIR__.'/../data/users.dat', badFile: 'users.bad', discardFile: 'users.dis', discardMax: '1') |
57 |
| - ->as('users.ctl') |
58 |
| - ->into( |
59 |
| - table: 'users', |
60 |
| - columns: ['id', 'name', 'email'], |
61 |
| - trailing: 'TRAILING NULLCOLS' |
62 |
| - ); |
63 |
| - |
64 |
| - $ctl = new ControlFileBuilder($loader); |
65 |
| - $controlFile = $ctl->build(); |
66 |
| - |
67 |
| - expect($controlFile)->toBeString() |
68 |
| - ->and($controlFile)->toContain("BADFILE 'users.bad'") |
69 |
| - ->and($controlFile)->toContain("DISCARDFILE 'users.dis'") |
70 |
| - ->and($controlFile)->toContain('DISCARDMAX 1'); |
71 |
| -}); |
72 |
| - |
73 |
| -test('it can build table with formatting options', function () { |
74 |
| - $loader = new SQLLoader(['skip=1', 'load=2']); |
75 |
| - $loader->inFile(__DIR__.'/../data/users.dat') |
76 |
| - ->as('users.ctl') |
77 |
| - ->into( |
78 |
| - table: 'users', |
79 |
| - columns: ['id', 'name', 'email'], |
80 |
| - trailing: 'TRAILING NULLCOLS', |
81 |
| - formatOptions: [ |
82 |
| - 'DATE_FORMAT "YYYY-MM-DD"', |
83 |
| - 'TIMESTAMP FORMAT "YYYY-MM-DD HH24:MI:SS"', |
84 |
| - 'TIMESTAMP WITH TIME ZONE "YYYY-MM-DD HH24:MI:SS TZH:TZM"', |
85 |
| - 'TIMESTAMP WITH LOCAL TIME ZONE "YYYY-MM-DD HH24:MI:SS"', |
86 |
| - ] |
87 |
| - ); |
88 |
| - |
89 |
| - $ctl = new ControlFileBuilder($loader); |
90 |
| - $controlFile = $ctl->build(); |
91 |
| - |
92 |
| - expect($controlFile)->toBeString() |
93 |
| - ->and($controlFile)->toContain('DATE_FORMAT "YYYY-MM-DD"') |
94 |
| - ->and($controlFile)->toContain('TIMESTAMP FORMAT "YYYY-MM-DD HH24:MI:SS') |
95 |
| - ->and($controlFile)->toContain('TIMESTAMP WITH TIME ZONE "YYYY-MM-DD HH24:MI:SS TZH:TZM"') |
96 |
| - ->and($controlFile)->toContain('TIMESTAMP WITH LOCAL TIME ZONE "YYYY-MM-DD HH24:MI:SS"'); |
97 |
| -}); |
98 |
| - |
99 |
| -test('it can build using begin data', function () { |
100 |
| - $loader = new SQLLoader(['skip=1', 'load=2']); |
101 |
| - $loader->as('users.ctl') |
102 |
| - ->into( |
103 |
| - table: 'users', |
104 |
| - columns: ['id', 'name', 'email'], |
105 |
| - ) |
106 |
| - ->beginData([ |
107 |
| - ['1', 'name-1', 'email-1'], |
108 |
| - ['2', 'name-2', 'email-2'], |
109 |
| - ['3', 'name,-2', 'email"-2'], |
110 |
| - ]); |
111 |
| - |
112 |
| - $ctl = new ControlFileBuilder($loader); |
113 |
| - $controlFile = $ctl->build(); |
114 |
| - |
115 |
| - expect($controlFile)->toBeString() |
116 |
| - ->and($controlFile)->toContain('INFILE *') |
117 |
| - ->and($controlFile)->toContain("BEGINDATA\n") |
118 |
| - ->and($controlFile)->toContain('1,name-1,email-1') |
119 |
| - ->and($controlFile)->toContain('2,name-2,email-2') |
120 |
| - ->and($controlFile)->toContain('3,"name,-2","email""-2"'); |
| 6 | +describe('Control File Builder', function () { |
| 7 | + test('it can build a control file', function () { |
| 8 | + $loader = new SQLLoader(['skip=1', 'load=2']); |
| 9 | + $loader->inFile(__DIR__.'/../data/users.dat') |
| 10 | + ->as('users.ctl') |
| 11 | + ->into( |
| 12 | + table: 'users', |
| 13 | + columns: ['id', 'name', 'email'], |
| 14 | + trailing: 'TRAILING NULLCOLS' |
| 15 | + ); |
| 16 | + |
| 17 | + $ctl = new ControlFileBuilder($loader); |
| 18 | + $controlFile = $ctl->build(); |
| 19 | + |
| 20 | + expect($controlFile)->toBeString() |
| 21 | + ->and($controlFile)->toContain('OPTIONS(skip=1, load=2)') |
| 22 | + ->and($controlFile)->toContain("INFILE '".__DIR__."/../data/users.dat'") |
| 23 | + ->and($controlFile)->toContain('APPEND') |
| 24 | + ->and($controlFile)->toContain('INTO TABLE "USERS"') |
| 25 | + ->and($controlFile)->toContain("FIELDS TERMINATED BY ','") |
| 26 | + ->and($controlFile)->toContain('OPTIONALLY') |
| 27 | + ->and($controlFile)->toContain("ENCLOSED BY '\"'") |
| 28 | + ->and($controlFile)->toContain('TRAILING NULLCOLS') |
| 29 | + ->and($controlFile)->toContain('(') |
| 30 | + ->and($controlFile)->toContain('id,') |
| 31 | + ->and($controlFile)->toContain('name,') |
| 32 | + ->and($controlFile)->toContain('email') |
| 33 | + ->and($controlFile)->toContain(')'); |
| 34 | + }); |
| 35 | + |
| 36 | + test('it can build multiple input files', function () { |
| 37 | + $loader = new SQLLoader(['skip=1', 'load=2']); |
| 38 | + $loader->inFile(__DIR__.'/../data/users.dat') |
| 39 | + ->inFile(__DIR__.'/../data/roles.dat') |
| 40 | + ->as('users.ctl') |
| 41 | + ->into( |
| 42 | + table: 'users', |
| 43 | + columns: ['id', 'name', 'email'], |
| 44 | + trailing: 'TRAILING NULLCOLS' |
| 45 | + ); |
| 46 | + |
| 47 | + $ctl = new ControlFileBuilder($loader); |
| 48 | + $controlFile = $ctl->build(); |
| 49 | + |
| 50 | + expect($controlFile)->toBeString() |
| 51 | + ->and($controlFile)->toContain("INFILE '".__DIR__."/../data/users.dat'") |
| 52 | + ->and($controlFile)->toContain("INFILE '".__DIR__."/../data/roles.dat'"); |
| 53 | + }); |
| 54 | + |
| 55 | + test('it can build with bad file, discard file and discard max', function () { |
| 56 | + $loader = new SQLLoader(['skip=1', 'load=2']); |
| 57 | + $loader->inFile(__DIR__.'/../data/users.dat', badFile: 'users.bad', discardFile: 'users.dis', discardMax: '1') |
| 58 | + ->as('users.ctl') |
| 59 | + ->into( |
| 60 | + table: 'users', |
| 61 | + columns: ['id', 'name', 'email'], |
| 62 | + trailing: 'TRAILING NULLCOLS' |
| 63 | + ); |
| 64 | + |
| 65 | + $ctl = new ControlFileBuilder($loader); |
| 66 | + $controlFile = $ctl->build(); |
| 67 | + |
| 68 | + expect($controlFile)->toBeString() |
| 69 | + ->and($controlFile)->toContain("BADFILE 'users.bad'") |
| 70 | + ->and($controlFile)->toContain("DISCARDFILE 'users.dis'") |
| 71 | + ->and($controlFile)->toContain('DISCARDMAX 1'); |
| 72 | + }); |
| 73 | + |
| 74 | + test('it can build table with formatting options', function () { |
| 75 | + $loader = new SQLLoader(['skip=1', 'load=2']); |
| 76 | + $loader->inFile(__DIR__.'/../data/users.dat') |
| 77 | + ->as('users.ctl') |
| 78 | + ->into( |
| 79 | + table: 'users', |
| 80 | + columns: ['id', 'name', 'email'], |
| 81 | + trailing: 'TRAILING NULLCOLS', |
| 82 | + formatOptions: [ |
| 83 | + 'DATE_FORMAT "YYYY-MM-DD"', |
| 84 | + 'TIMESTAMP FORMAT "YYYY-MM-DD HH24:MI:SS"', |
| 85 | + 'TIMESTAMP WITH TIME ZONE "YYYY-MM-DD HH24:MI:SS TZH:TZM"', |
| 86 | + 'TIMESTAMP WITH LOCAL TIME ZONE "YYYY-MM-DD HH24:MI:SS"', |
| 87 | + ] |
| 88 | + ); |
| 89 | + |
| 90 | + $ctl = new ControlFileBuilder($loader); |
| 91 | + $controlFile = $ctl->build(); |
| 92 | + |
| 93 | + expect($controlFile)->toBeString() |
| 94 | + ->and($controlFile)->toContain('DATE_FORMAT "YYYY-MM-DD"') |
| 95 | + ->and($controlFile)->toContain('TIMESTAMP FORMAT "YYYY-MM-DD HH24:MI:SS') |
| 96 | + ->and($controlFile)->toContain('TIMESTAMP WITH TIME ZONE "YYYY-MM-DD HH24:MI:SS TZH:TZM"') |
| 97 | + ->and($controlFile)->toContain('TIMESTAMP WITH LOCAL TIME ZONE "YYYY-MM-DD HH24:MI:SS"'); |
| 98 | + }); |
| 99 | + |
| 100 | + test('it can build using begin data', function () { |
| 101 | + $loader = new SQLLoader(['skip=1', 'load=2']); |
| 102 | + $loader->as('users.ctl') |
| 103 | + ->into( |
| 104 | + table: 'users', |
| 105 | + columns: ['id', 'name', 'email'], |
| 106 | + ) |
| 107 | + ->beginData([ |
| 108 | + ['1', 'name-1', 'email-1'], |
| 109 | + ['2', 'name-2', 'email-2'], |
| 110 | + ['3', 'name,-2', 'email"-2'], |
| 111 | + ]); |
| 112 | + |
| 113 | + $ctl = new ControlFileBuilder($loader); |
| 114 | + $controlFile = $ctl->build(); |
| 115 | + |
| 116 | + expect($controlFile)->toBeString() |
| 117 | + ->and($controlFile)->toContain('INFILE *') |
| 118 | + ->and($controlFile)->toContain("BEGINDATA\n") |
| 119 | + ->and($controlFile)->toContain('1,name-1,email-1') |
| 120 | + ->and($controlFile)->toContain('2,name-2,email-2') |
| 121 | + ->and($controlFile)->toContain('3,"name,-2","email""-2"'); |
| 122 | + }); |
121 | 123 | });
|
0 commit comments