@@ -28,7 +28,6 @@ def test_connect_already_connected(self):
2828 assert connection is self .email_handler .connection , "The connection must be the same as the one in the handler."
2929
3030 def test_check_connection (self ):
31-
3231 response = self .email_handler .check_connection ()
3332 assert response .success is True , "The response success must be True."
3433
@@ -37,30 +36,38 @@ def test_select(self):
3736 Test the select method of EmailsTable Class
3837 """
3938
40- mock_df = pd .DataFrame ({
41- 'date' : ["Wed, 02 Feb 2022 15:30:00 +0000" ,
42- "Thu, 10 Mar 2022 10:45:15 +0530" ,
43- "Fri, 16 Dec 2022 20:15:30 -0400"
44- ],
45- 'body_content_type' : ['html' , 'html' , 'text' ],
46- "body" : ["<html><body><p>Hello, World!</p></body></html>" , "<html><body><p>Hello, World!</p></body></html>" , "Hello, World!" ],
47- "from_field" : ["" , "" , "" ],
48- "id" : ["" , "" , "" ],
49- "to_field" : ["" , "" , "" ],
50- "subject" : ["" , "" , "" ],
51- })
39+ mock_df = pd .DataFrame (
40+ {
41+ "date" : [
42+ "Wed, 02 Feb 2022 15:30:00 +0000" ,
43+ "Thu, 10 Mar 2022 10:45:15 +0530" ,
44+ "Fri, 16 Dec 2022 20:15:30 -0400" ,
45+ ],
46+ "body_content_type" : ["html" , "html" , "text" ],
47+ "body" : [
48+ "<html><body><p>Hello, World!</p></body></html>" ,
49+ "<html><body><p>Hello, World!</p></body></html>" ,
50+ "Hello, World!" ,
51+ ],
52+ "from_field" : ["" , "" , "" ],
53+ "id" : ["" , "" , "" ],
54+ "to_field" : ["" , "" , "" ],
55+ "subject" : ["" , "" , "" ],
56+ }
57+ )
5258
5359 self .emails_table_instance .handler .connection .search_email = MagicMock (return_value = mock_df )
5460
55- query = parse_sql (' SELECT * FROM emails limit 1' )
61+ query = parse_sql (" SELECT * FROM emails limit 1" )
5662
5763 self .emails_table_instance .select (query )
5864
59- assert self .emails_table_instance .handler .connection .search_email .called , ("The search_email "
60- "method must be called." )
65+ assert self .emails_table_instance .handler .connection .search_email .called , (
66+ "The search_email method must be called."
67+ )
6168
6269 # select using invalid column should raise Exception
63- query = parse_sql (' SELECT invalid_column FROM emails limit 1' )
70+ query = parse_sql (" SELECT invalid_column FROM emails limit 1" )
6471
6572 with pytest .raises (Exception ):
6673 self .emails_table_instance .select (query )
@@ -73,16 +80,18 @@ def test_insert(self):
7380 self .emails_table_instance .handler .connection .send_email = MagicMock ()
7481
7582 query = parse_sql (
76- 'INSERT INTO email_datasource.emails(to_field, subject, body) '
77- 'VALUES ("toemail@email.com", "MindsDB", "Hello from MindsDB!")' )
83+ "INSERT INTO email_datasource.emails(to_field, subject, body) "
84+ 'VALUES ("toemail@email.com", "MindsDB", "Hello from MindsDB!")'
85+ )
7886
7987 self .emails_table_instance .insert (query )
8088 assert self .emails_table_instance .handler .connection .send_email .called , "The send_email method must be called."
8189
8290 # insert using invalid column should raise Exception
8391 query = parse_sql (
84- 'INSERT INTO email_datasource.emails(to_field, subject, body, invalid_column) '
85- 'VALUES ("toemail@email.com", "MindsDB", "blaha" , "invalid")' )
92+ "INSERT INTO email_datasource.emails(to_field, subject, body, invalid_column) "
93+ 'VALUES ("toemail@email.com", "MindsDB", "blaha" , "invalid")'
94+ )
8695
8796 with pytest .raises (Exception ):
8897 self .emails_table_instance .insert (query )
@@ -94,9 +103,36 @@ def test_get_columns(self):
94103
95104 columns = self .emails_table_instance .get_columns ()
96105 assert isinstance (columns , list ), "The returned value must be a list."
97- assert 'id' in columns , "Column 'id' must be in the columns list."
98- assert 'body' in columns , "Column 'body' must be in the columns list."
99- assert 'subject' in columns , "Column 'subject' must be in the columns list."
100- assert 'to_field' in columns , "Column 'to_field' must be in the columns list."
101- assert 'from_field' in columns , "Column 'from_field' must be in the columns list."
102- assert 'datetime' in columns , "Column 'datetime' must be in the columns list."
106+ assert "id" in columns , "Column 'id' must be in the columns list."
107+ assert "body" in columns , "Column 'body' must be in the columns list."
108+ assert "subject" in columns , "Column 'subject' must be in the columns list."
109+ assert "to_field" in columns , "Column 'to_field' must be in the columns list."
110+ assert "from_field" in columns , "Column 'from_field' must be in the columns list."
111+ assert "datetime" in columns , "Column 'datetime' must be in the columns list."
112+
113+ def test_undetectable_encoding_handling (self ):
114+ """
115+ Test that the email handler can process emails with undetectable encodings
116+ without raising exceptions.
117+ """
118+
119+ undetectable_content = b"\x80 \x81 \x82 \x83 \x84 \x85 \x86 \x87 "
120+ mock_df = pd .DataFrame (
121+ {
122+ "date" : ["Wed, 02 Feb 2022 15:30:00 +0000" ],
123+ "body_content_type" : ["text" ],
124+ "body" : [undetectable_content ],
125+ "from_field" : ["test@example.com" ],
126+ "id" : ["test1" ],
127+ "to_field" : ["recipient@example.com" ],
128+ "subject" : ["Test email with undetectable encoding" ],
129+ }
130+ )
131+
132+ self .emails_table_instance .handler .connection .search_email = MagicMock (return_value = mock_df )
133+ query = parse_sql ("SELECT * FROM emails limit 1" )
134+ result = self .emails_table_instance .select (query )
135+
136+ assert result is not None , "The result must not be None."
137+ assert "body" in result .columns , "The body should be in the result columns."
138+ assert len (result ) > 0 , "The result should not be empty."
0 commit comments