def test_intermix_just_required_args_and_implicit_stdin(self): infile = StringIO("a,b,c\n1,2,3\n") with stdin_as_string(infile): self.assertLines( [ r"(\w)", r"\1!", ], [ "a,b,c", "1!,2!,3!", ], ) infile.close() # -c option in the middle of stuff infile = StringIO("a,b,c\n1,2,3\n") with stdin_as_string(infile): self.assertLines( [ r"(\w)", r"\1!", "-c", "a,c", ], [ "a,b,c", "1!,2,3!", ], ) infile.close()
def test_filtered_columns(self): """only selected columns are affected, AND filtered for""" with stdin_as_string(self.idata): self.assertLines( [ "-F", "-c", "id,name", r"([3-9])", r"\1@", ], [ "id,name,val", "3@,4@,5", "6@,7@y,8z", ], ) # variation... with stdin_as_string(self.idata): self.assertLines( [ "-Fc", "2", r"[a-z]", r"$", ], [ "id,name,val", "6,7$,8z", ], )
def test_slugify(self): input_file = StringIO( 'id, Hello ,World\nBond,Good-bye!,"E A R T H\n Ling!"') with stdin_as_string(input_file): self.assertLines( [ "--slugify", ], [ "id, Hello ,World", "bond,good_bye,e_a_r_t_h_ling", ], ) # verify alias works with stdin_as_string(StringIO("a,b,c\nD-D, E?E, !F! \n")): self.assertLines( [ "-S", ], [ "a,b,c", "d_d,e_e,f", ], )
def test_slugify_overrides_lower_and_upper(self): """ makes no sense for user to specify both slugify and upper/lower. But if they do, we silently handle it, as slugify's effects overrides lower/upper """ with stdin_as_string(self.ifile): self.assertLines( [ "-U", "-S", ], [ "A,B,C", "hello,wor_ld,good_bye", ], ) with stdin_as_string(self.ifile): self.assertLines( [ "-L", "-S", ], [ "A,B,C", "hello,wor_ld,good_bye", ], )
def test_single_index(self): with stdin_as_string(self.ifile): self.assertLines( ["-i", "0"], ["id", "0"], ) with stdin_as_string(self.ifile): self.assertLines( ["--indexes", "9"], ["id", "9"], )
def test_columns_by_index(self): """only specified columns are affected""" with stdin_as_string(self.idata): self.assertLines( ["-c", "3,1", r"(\w+)", r"\1!"], ["id,name,val", "1!,2,3x!", "3!,4,5!", "6!,7y,8z!"], )
def test_add_craploads_of_headers(self): txt = ",".join(str(i) for i in range(100)) infile = StringIO(f"{txt}\n") with stdin_as_string(infile): self.assertLines( ["-G"], [",".join(f"field_{i}" for i in range(1, 101)), txt], ) infile.close()
def test_opts_then_args(self, opts): with stdin_as_string(self.indata): self.assertLines( [ *opts, *self.margs, ], self.answer, )
def test_squeeze_then_strip(self): with stdin_as_string(self.ifile): self.assertLines( [], [ "a,b,c", "1,2 2,3", ], )
def test_slugify_intro_example(self): with stdin_as_string(self.idata): self.assertLines( ["-S"], [ "case,x,y,i_d", "1,2,3,4", "5,6,7,8", ], )
def test_rename_intro_example(self): with stdin_as_string(self.idata): self.assertLines( ["-R", "1|Case Num,4|ID,X|lat,Y|lng"], [ "Case Num,lat,lng,ID", "1,2,3,4", "5,6,7,8", ], )
def test_start_equals_end_is_fine(self): """why would user do this, who knows, but it's technically fine""" with stdin_as_string(self.ifile): self.assertLines( ["-i", "0-0"], [ "id", "0", ], ) with stdin_as_string(self.ifile): self.assertLines( ["-i", "3-3"], [ "id", "3", ], )
def test_several_indexes(self): with stdin_as_string(self.ifile): self.assertLines( ["-i", "2,0,9,2"], [ "id", "0", "2", "9", ], )
def test_lower_bound(self): with stdin_as_string(self.ifile): self.assertLines( ["-i", "7-"], [ "id", "7", "8", "9", ], )
def test_babynames_create_header(self): with stdin_as_string(self.idata): self.assertLines( ["-A", "name,sex,count"], [ "name,sex,count", "Mary,F,7065", "Anna,F,2604", "Emma,F,2003", ], )
def test_uppercase(self): input_file = StringIO("a,b,c\nHello,World !,Good-Bye\n") with stdin_as_string(input_file): self.assertLines( [ "--uppercase", ], ["a,b,c", "HELLO,WORLD !,GOOD-BYE"], ) with stdin_as_string(StringIO("a,b,c\nd,e,f\n")): self.assertLines( [ "-U", ], [ "a,b,c", "D,E,F", ], )
def test_lowercase(self): input_file = StringIO("A,B,C\nHello,World !,Good-Bye\n") with stdin_as_string(input_file): self.assertLines( [ "--lowercase", ], ["A,B,C", "hello,world !,good-bye"], ) with stdin_as_string(StringIO("A,B,C\nD,E,F\n")): self.assertLines( [ "-L", ], [ "A,B,C", "d,e,f", ], )
def test_intro(self): data = StringIO("id,val\n0,a\n1,b\n2,c\n3,d\n") with stdin_as_string(data): self.assertLines( ["-i", "0,2-3"], [ "id,val", "0,a", "2,c", "3,d", ], )
def test_slugify_helpdoc(self): ifile = StringIO('APPLES,"Date - Time "\n') with stdin_as_string(ifile): self.assertLines( [ "-S", ], [ "apples,date_time", ], )
def test_single_interval(self): with stdin_as_string(self.ifile): self.assertLines( ["-i", "3-5"], [ "id", "3", "4", "5", ], ) with stdin_as_string(self.ifile): self.assertLines( ["-i", "8-10000"], [ "id", "8", "9", ], )
def test_selective_norming(self): with stdin_as_string(self.ifile): self.assertLines( [ "-c", "2-3", ], [ "a,b,c,d", "hello,World,and Good?, bye ! ", ], )
def test_selective_slugifying(self): with stdin_as_string(self.ifile): self.assertLines( [ "--columns", "b,c", "-S", ], [ "a,b,c,d", "hello,world,and_good, bye ! ", ], )
def test_zero_range(self): """0 can be a special case because sometimes I forget how Python treats 0 as falsy/nonetype""" with stdin_as_string(self.ifile): self.assertLines( ["-i", "0-3"], [ "id", "0", "1", "2", "3", ], )
def test_usage_filter_example(self): """ $ csvsed -F '[a-z]' '%' data.csv """ with stdin_as_string(self.idata): self.assertLines( [ "-F", "[a-z]", "%", ], ["id,name,val", "1,2,3%", "6,7%,8%"], )
def test_supports_case_insensitivity(self): tdata = "\n".join(["a,b,c,d", "hey,Hello,hat,hEx"]) with stdin_as_string(StringIO(tdata)): self.assertLines( [ "(?i)h(?=e)", "M", ], [ "a,b,c,d", "Mey,Mello,hat,MEx", ], )
def test_keep_lines(self): r"""--keep-lines prevents squeeze's default conversion of \n to ' '""" input_file = StringIO('a, b ,c\n11,2 2,"3 \n\n 3"') with stdin_as_string(input_file): self.assertLines( [ "--keep-lines", ], [ "a, b ,c", '11,2 2,"3 ', ' 3"', ], )
def test_squeezing(self): """ consecutive whitespace and newlines \n are collapsed (and stripping is applied) """ input_file = StringIO('a, b ,c\n11,2 2,"3 \n\n 3"') with stdin_as_string(input_file): self.assertLines( [], [ "a, b ,c", "11,2 2,3 3", ], )
def test_edgy_lower_bound(self): """ for now, we don't warn user if there are overlapping/conflicting bounds """ with stdin_as_string(self.ifile): self.assertLines( ["-i", "9-,8,9,7-,8-"], [ "id", "7", "8", "9", ], )
def test_intro(self): datacsv = StringIO("id,name\n1,Mrs. Adams\n2,Miss Miller\n3,Mrs Smith\n") with stdin_as_string(datacsv): self.assertLines( [ r"(Mrs|Miss|Ms)\.?", "Ms.", ], [ "id,name", "1,Ms. Adams", "2,Ms. Miller", "3,Ms. Smith", ], )
def test_default(self): iof = StringIO("a,b,c\n0,12345,67890\n") with stdin_as_string(iof): self.assertLines( ["--max-length", "3"], [ "| field | value |", "| ----- | ----- |", "| a | 0 |", "| b | 123 |", "| | 45 |", "| c | 678 |", "| | 90 |", ], )