Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import sys 

2import math 

3import os 

4sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../../") 

5from sdc_etl_libs.sdc_dataframe.Dataframe import * 

6import pandas as pd 

7import numpy as np 

8import json 

9import pytest 

10 

11 

12def test_convert_columns_to_json_replace_newline_characters(): 

13 """ 

14 Ensure when converting a string to JSON that incidental newline / carriage 

15 return characters in values are double-escaped. 

16 

17 """ 

18 

19 test_schema = """ 

20 { 

21 "namespace": "", 

22 "type": "object", 

23 "name": "", 

24 "country_code": "USA", 

25 "data_sink": {"type":"snowflake", "database": "", "table_name": "", "schema": ""}, 

26 "data_source": {"type": "api", "base_url": ""}, 

27 "fields": [ 

28 {"name":"COL1","type":{"type":"string","logical_type":"json"}} 

29 ] 

30 }""" 

31 

32 df = Dataframe(SDCDFTypes.PANDAS, test_schema) 

33 df.load_data([ 

34 {'COL1': {'Notes': 'Microsoft EA meeting; (Wed)\nFriday - FireDrill'}}, 

35 {'COL1': {'Notes': 'Microsoft EA meeting; (Wed)Friday\r - FireDrill'}}, 

36 {'COL1': {'Notes': 'Microsoft EA meeting; (Wed)\n\nFriday - FireDrill'}}]) 

37 

38 df.convert_columns_to_json(['COL1']) 

39 

40 assert df.df["COL1"][0] == '{"Notes": "Microsoft EA meeting; (Wed)\\\\nFriday - FireDrill"}' 

41 assert df.df["COL1"][1] == '{"Notes": "Microsoft EA meeting; (Wed)Friday\\\\r - FireDrill"}' 

42 assert df.df["COL1"][2] == '{"Notes": "Microsoft EA meeting; (Wed)\\\\n\\\\nFriday - FireDrill"}'