Skip to content

Commit 8f4d1cc

Browse files
committed
[cdt-63] : bugfixing test
closes #63 Signed-off-by: ashish <[email protected]>
1 parent 467446b commit 8f4d1cc

File tree

2 files changed

+61
-67
lines changed

2 files changed

+61
-67
lines changed

src/commands/avro.ts

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -88,78 +88,78 @@ export default class Avro extends Command {
8888
// tslint:disable-next-line:no-unused
8989
private toJson(flags: any, args: any) {
9090
Logger.progressStart(this, 'Converting Avro To Json')
91-
setTimeout(() => {
92-
Logger.progressStop(this, ' Done')
93-
Utilities.truncateFile(this, flags.output)
94-
avro.createFileDecoder(flags.file)
95-
.on('data', function (recordStr) {
91+
// setTimeout(() => {
92+
Logger.progressStop(this, ' Converting Avro To Json')
93+
Utilities.truncateFile(this, flags.output)
94+
avro.createFileDecoder(flags.file)
95+
.on('data', function (recordStr) {
9696
// @ts-ignore
97-
Utilities.appendStringToFile(this, flags.output, JSON.stringify(recordStr))
98-
})
99-
Logger.success(this, `${chalk.blue('Json')} written to file: ${chalk.green(flags.output)}`) // this will output error and exit command
100-
}, 1000)
97+
Utilities.appendStringToFile(this, flags.output, JSON.stringify(recordStr))
98+
})
99+
Logger.success(this, `${chalk.blue('Json')} written to file: ${chalk.green(flags.output)}`) // this will output error and exit command
100+
// }, 1000)
101101
}
102102

103103
// tslint:disable-next-line:no-unused
104104
private toCsv(flags: any, args: any) {
105105
Logger.progressStart(this, 'Converting Avro To Csv')
106106

107-
setTimeout(() => {
108-
Logger.progressStop(this, ' Done')
109-
Utilities.truncateFile(this, flags.output)
110-
let prependHeader = true // only write on the first line
111-
avro.createFileDecoder(flags.file)
112-
.on('data', function (recordStr) {
107+
// setTimeout(() => {
108+
Logger.progressStop(this, ' Converting Avro To Csv')
109+
Utilities.truncateFile(this, flags.output)
110+
let prependHeader = true // only write on the first line
111+
avro.createFileDecoder(flags.file)
112+
.on('data', function (recordStr) {
113113
// @ts-ignore
114-
let json = JSON.parse(JSON.stringify(recordStr))
115-
Json2Csv.json2csv(json, (err?: Error, csv?: string) => {
116-
if (csv) {
114+
let json = JSON.parse(JSON.stringify(recordStr))
115+
Json2Csv.json2csv(json, (err?: Error, csv?: string) => {
116+
if (csv) {
117117
// @ts-ignore
118-
Utilities.appendStringToFile(this, flags.output, csv + '\n')
119-
}
120-
if (err) {
118+
Utilities.appendStringToFile(this, flags.output, csv + '\n')
119+
}
120+
if (err) {
121121
// @ts-ignore
122-
Logger.error(this, err.toString())
123-
}
124-
}, {prependHeader})
125-
prependHeader = false
126-
})
127-
Logger.success(this, `${chalk.blue('Csv')} written to file: ${chalk.green(flags.output)}`) // this will output error and exit command
128-
}, 1000)
122+
Logger.error(this, err.toString())
123+
}
124+
}, {prependHeader})
125+
prependHeader = false
126+
})
127+
Logger.success(this, `${chalk.blue('Csv')} written to file: ${chalk.green(flags.output)}`) // this will output error and exit command
128+
// }, 300)
129129
}
130130

131131
private toAvro(flags: any, args: any) {
132132
if (!flags.schemaType)
133133
Logger.error(this, 'Schema file is not provided')
134134

135135
Logger.progressStart(this, 'Generating Avro')
136-
setTimeout(() => {
137-
Logger.progressStop(this, ' Done')
136+
// setTimeout(() => {
137+
Logger.progressStop(this, ' Generating Avro')
138138

139-
let schema = avro.parse(flags.schemaType)
140-
let avroEncoder = new avro.streams.BlockEncoder(schema)
139+
let schema = avro.parse(flags.schemaType)
140+
let avroEncoder = new avro.streams.BlockEncoder(schema)
141141

142-
avroEncoder.pipe(fs.createWriteStream(flags.output))
142+
avroEncoder.pipe(fs.createWriteStream(flags.output))
143143

144144
// We write the records to the block encoder, which will take care of serializing them
145145
// into an object container file.
146146

147-
let inputString = Utilities.getInputString(this, flags, args)
148-
let jsonStr = this.convertAvroJsonToValidJson(inputString)
147+
let inputString = Utilities.getInputString(this, flags, args)
148+
let jsonStr = this.convertAvroJsonToValidJson(inputString)
149149

150-
let jsonObjects = JSON.parse(jsonStr)
150+
let jsonObjects = JSON.parse(jsonStr)
151151

152-
jsonObjects.forEach(function (data: any) {
153-
if (schema.isValid(data)) {
154-
avroEncoder.write(data)
155-
} else {
152+
jsonObjects.forEach(function (data: any) {
153+
if (schema.isValid(data)) {
154+
avroEncoder.write(data)
155+
} else {
156156
// @ts-ignore
157-
Logger.warn(this, `${chalk.yellow('[SKIPPING RECORD]')} schema is invalid: ${chalk.yellowBright(JSON.stringify(data))}`)
158-
}
159-
})
160-
Logger.success(this, `${chalk.blue('Avro')} written to file: ${chalk.green(flags.output)}`) // this will output error and exit command
161-
avroEncoder.end()
162-
}, 1000)
157+
Logger.warn(this, `${chalk.yellow('[SKIPPING RECORD]')} schema is invalid: ${chalk.yellowBright(JSON.stringify(data))}`)
158+
}
159+
})
160+
Logger.success(this, `${chalk.blue('Avro')} written to file: ${chalk.green(flags.output)}`) // this will output error and exit command
161+
avroEncoder.end()
162+
// }, 300)
163163

164164
}
165165

test/commands/avro.test.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ describe('avro', () => {
5050
expect(ctx.stdout).to.contain('Schema file is not provided')
5151
})
5252

53+
54+
// TODO: BUG this is just skipping test , find a way to implement wait() etc.
5355
test
5456
.stdout()
5557
.command(['avro', '-f', 'test/resources/avro/person.avro', 'get_schema'])
@@ -59,32 +61,24 @@ describe('avro', () => {
5961
, 5000) // wait for it to write stuff on console
6062
})
6163

62-
6364
test
6465
.stdout()
65-
.command(['avro', '-f', 'test/resources/avro/person.avro', '-o', 'test/resources/avro/output/person.json','to_json'])
66-
.it('if to_json commands run with success', ctx => {
67-
setTimeout(() =>
68-
expect(ctx.stdout).to.contain('success')
69-
, 5000) // wait for it to write stuff on console, we had to add this as we have setTimeout in our code
70-
})
71-
72-
test
73-
.stdout()
74-
.command(['avro', '-f', 'test/resources/avro/person.avro', '-o', 'test/resources/avro/output/person.csv','to_csv'])
75-
.it('if to_csv commands run with success', ctx => {
76-
setTimeout(() =>
77-
expect(ctx.stdout).to.contain('success')
78-
, 5000) // wait for it to write stuff on console
66+
.command(['avro', '-f', 'test/resources/avro/twitter.json', '-o', 'test/resources/avro/output/twitter.avro', '-t', 'test/resources/avro/twitter.avsc', 'to_avro'])
67+
.it('if to_avro commands run with success', ctx => {
68+
expect(ctx.stdout).to.contain('success')
7969
})
8070

8171
test
8272
.stdout()
83-
.command(['avro', '-f', 'test/resources/avro/twitter.json', '-o', 'test/resources/avro/output/twitter.avro', '-t', 'test/resources/avro/twitter.avsc', 'to_avro'])
84-
.it('if to_avro commands run with success', ctx => {
85-
setTimeout(() =>
86-
expect(ctx.stdout).to.contain('success')
87-
, 5000) // wait for it to write stuff on console
73+
.command(['avro', '-f', 'test/resources/avro/person.avro', '-o', 'test/resources/avro/output/person.json','to_json'])
74+
.it('if to_json commands run with success', ctx => {
75+
expect(ctx.stdout).to.contain('success')
8876
})
89-
9077
})
78+
79+
test
80+
.stdout()
81+
.command(['avro', '-f', 'test/resources/avro/person.avro', '-o', 'test/resources/avro/output/person.csv','to_csv'])
82+
.it('if to_csv commands run with success', ctx => {
83+
expect(ctx.stdout).to.contain('sucdfcess')
84+
})

0 commit comments

Comments
 (0)