Skip to content

Commit

Permalink
chore: add test for typescript gen tool (#4174)
Browse files Browse the repository at this point in the history
  • Loading branch information
connoratrug authored Aug 30, 2024
1 parent c874fd5 commit ad24652
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.molgenis.emx2;

import static java.lang.System.exit;

import org.molgenis.emx2.sql.SqlDatabase;
import org.molgenis.emx2.typescript.Generator;

Expand All @@ -22,7 +20,6 @@ public static void main(String[] args) {
} else {
System.out.println(
"Missing required arguments ( SchemaName ( example: 'Pet Store'), full-file-path (example: '/home/bob/app/types.ts')");
exit(1);
}
}

Expand All @@ -33,7 +30,6 @@ public static void generate(String schemaName, String fileFullPath) {
Schema schema = db.getSchema(schemaName);
if (schema == null) {
System.out.println("Schema " + schemaName + " not found");
exit(1);
}

Generator generator = new Generator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.molgenis.emx2;

import static org.junit.jupiter.api.Assertions.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import org.junit.jupiter.api.Test;

class AToolToGenerateTypeScriptTypesTest {

@Test
void runWithMissingArgs() {
PrintStream old = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream out = new PrintStream(baos);
System.setOut(out);

String[] args = new String[] {};
try {
AToolToGenerateTypeScriptTypes.main(args);
} catch (Exception e) {
e.printStackTrace();
}

System.out.flush();
System.setOut(old);
String s = new String(baos.toByteArray(), Charset.defaultCharset());
assertEquals(
"Generating TypeScript types\n"
+ "Missing required arguments ( SchemaName ( example: 'Pet Store'), full-file-path (example: '/home/bob/app/types.ts')\n",
s);
}

@Test
void runWithSingleArg() {
PrintStream old = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream out = new PrintStream(baos);
System.setOut(out);

String[] args = new String[] {"pet store"};
try {
AToolToGenerateTypeScriptTypes.main(args);
} catch (Exception e) {
e.printStackTrace();
}

System.out.flush();
System.setOut(old);
String s = new String(baos.toByteArray(), Charset.defaultCharset());
assertEquals(
"Generating TypeScript types\n"
+ "Missing required arguments ( SchemaName ( example: 'Pet Store'), full-file-path (example: '/home/bob/app/types.ts')\n",
s);
}

@Test
void runWithValidArgs() {
PrintStream old = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream out = new PrintStream(baos);
System.setOut(out);

String[] args = new String[] {"pet store", "'/home/bob/app/types.ts"};
try {
AToolToGenerateTypeScriptTypes.main(args);
} catch (Exception e) {
System.out.flush();
System.setOut(old);
String s = new String(baos.toByteArray(), Charset.defaultCharset());

assertTrue(
s.contains(
"Generating TypeScript types\n"
+ " Generate from Schema: pet store\n"
+ " Generate into file: '/home/bob/app/types.ts"));
}
}
}

0 comments on commit ad24652

Please sign in to comment.