Skip to content

Commit

Permalink
Add tests for file read and convert xml to record
Browse files Browse the repository at this point in the history
  • Loading branch information
prakanth97 committed Mar 22, 2024
1 parent 57e9cc7 commit b18cb8d
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ballerina/tests/fromXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function testXmlStringToRecord1() returns error? {
<C>5</C>
</A>
</Data>`;
Data rec1 = check parseString(xmlStr);
Data rec1 = check parseString(xmlStr, {});

test:assertEquals(rec1.A.length(), 2);
test:assertEquals(rec1.A[0].B.length(), 2);
Expand All @@ -62,7 +62,7 @@ function testXmlStringToRecord1() returns error? {
}
function testXmlToRecord1() returns error? {
xml xmlVal = xml `<Data><A><B>1</B><B>2</B><C>6</C></A><D>5</D><A><B>3</B><B>4</B><C>5</C></A></Data>`;
Data rec1 = check parseAsType(xmlVal);
Data rec1 = check parseAsType(xmlVal, {});

test:assertEquals(rec1.A.length(), 2);
test:assertEquals(rec1.A[0].B.length(), 2);
Expand Down Expand Up @@ -93,7 +93,7 @@ type Data2 record {|
}
function testXmlStringToRecord2() returns error? {
string xmlStr1 = "<Data1><A>1</A></Data1>";
Data1 rec1 = check parseString(xmlStr1);
Data1 rec1 = check parseString(xmlStr1, {});
test:assertEquals(rec1.A.\#content, "1");

string xmlStr2 = "<Data2><A>1</A></Data2>";
Expand Down Expand Up @@ -142,7 +142,7 @@ type Data5 record {|
}
function testXmlStringToRecord3() returns error? {
string xmlStr1 = "<Data3><A>1</A><B>2</B></Data3>";
Data3 rec1 = check parseString(xmlStr1);
Data3 rec1 = check parseString(xmlStr1, {});
test:assertEquals(rec1.A.\#content, "1");
test:assertEquals(rec1.B.\#content, 2);

Expand Down
20 changes: 20 additions & 0 deletions ballerina/tests/resources/source_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<library>
<book>
<title>Harry Potter and the Philosopher's Stone</title>
<author>J.K. Rowling</author>
<genre>Fantasy</genre>
<publication_year>1997</publication_year>
</book>
<book>
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<genre>Classic</genre>
<publication_year>1925</publication_year>
</book>
<book>
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
<genre>Fiction</genre>
<publication_year>1960</publication_year>
</book>
</library>
20 changes: 20 additions & 0 deletions ballerina/tests/resources/source_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<movies>
<movie id="1">
<title>American Beauty</title>
<director>Sam Mendes</director>
<genre>Drama</genre>
<release_year>1999</release_year>
</movie>
<movie id="2">
<title>The Shawshank Redemption</title>
<director>Frank Darabont</director>
<genre>Drama</genre>
<release_year>1994</release_year>
</movie>
<movie id="3">
<title>Forrest Gump</title>
<director>Robert Zemeckis</director>
<genre>Drama</genre>
<release_year>1994</release_year>
</movie>
</movies>
29 changes: 29 additions & 0 deletions ballerina/tests/resources/source_3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<library xmlns:bk="http://example.com/books" xmlns:auth="http://example.com/authors">
<bk:book id="1">
<bk:title>The Catcher in the Rye</bk:title>
<bk:author id="101" auth:nationality="American">
<auth:first_name>J.D.</auth:first_name>
<auth:last_name>Salinger</auth:last_name>
</bk:author>
<bk:genre>Fiction</bk:genre>
<bk:publication_year>1951</bk:publication_year>
</bk:book>
<bk:book id="2">
<bk:title>1984</bk:title>
<bk:author id="102" auth:nationality="British">
<auth:first_name>George</auth:first_name>
<auth:last_name>Orwell</auth:last_name>
</bk:author>
<bk:genre>Dystopian</bk:genre>
<bk:publication_year>1949</bk:publication_year>
</bk:book>
<bk:book id="3">
<bk:title>The Lord of the Rings</bk:title>
<bk:author id="103" auth:nationality="British">
<auth:first_name>J.R.R.</auth:first_name>
<auth:last_name>Tolkien</auth:last_name>
</bk:author>
<bk:genre>Fantasy</bk:genre>
<bk:publication_year>1954</bk:publication_year>
</bk:book>
</library>
70 changes: 70 additions & 0 deletions ballerina/tests/resources/source_4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<orders xmlns="http://example.com/orders" xmlns:cust="http://example.com/customers" xmlns:prod="http://example.com/products">
<order order_id="123456" date="2024-03-22T10:30:00" cust:customer_id="987654">
<cust:customer_info>
<cust:name>John Doe</cust:name>
<cust:email>john.doe@example.com</cust:email>
<cust:address>
<cust:street>123 Main St</cust:street>
<cust:city>Anytown</cust:city>
<cust:state>CA</cust:state>
<cust:zip>12345</cust:zip>
<cust:country>USA</cust:country>
</cust:address>
</cust:customer_info>
<products>
<prod:product prod:product_id="789" prod:quantity="2">
<prod:name>Laptop</prod:name>
<prod:price currency="USD">999.99</prod:price>
</prod:product>
<prod:product prod:product_id="456" prod:quantity="1">
<prod:name>Printer</prod:name>
<prod:price currency="USD">199.99</prod:price>
</prod:product>
</products>
<payment>
<method>credit_card</method>
<amount currency="USD">2199.97</amount>
<card>
<card_type>VISA</card_type>
<card_number>1234 5678 9012 3456</card_number>
<expiration_date>2026-12</expiration_date>
</card>
</payment>
<status>completed</status>
</order>
<order order_id="789012" date="2024-03-22T11:45:00" cust:customer_id="543210">
<cust:customer_info>
<cust:name>Jane Smith</cust:name>
<cust:email>jane.smith@example.com</cust:email>
<cust:address>
<cust:street>456 Oak Ave</cust:street>
<cust:city>Smalltown</cust:city>
<cust:state>NY</cust:state>
<cust:zip>54321</cust:zip>
<cust:country>USA</cust:country>
</cust:address>
</cust:customer_info>
<products>
<prod:product prod:product_id="123" prod:quantity="3">
<prod:name>Smartphone</prod:name>
<prod:price currency="USD">799.99</prod:price>
</prod:product>
<prod:product prod:product_id="234" prod:quantity="2">
<prod:name>Tablet</prod:name>
<prod:price currency="USD">499.99</prod:price>
</prod:product>
<prod:product prod:product_id="345" prod:quantity="1">
<prod:name>Smart Watch</prod:name>
<prod:price currency="USD">299.99</prod:price>
</prod:product>
</products>
<payment>
<method>paypal</method>
<amount currency="USD">3499.94</amount>
<paypal>
<email>jane.smith@example.com</email>
</paypal>
</payment>
<status>pending</status>
</order>
</orders>
Loading

0 comments on commit b18cb8d

Please sign in to comment.