示例#1
0
def test_checkTypeError():

	fileWriter1: CSVFileWriter = CSVFileWriter(StringIO())
	
	with raises(TypeError) as e:
		fileWriter2: CSVFileWriter = CSVFileWriter(BytesIO())

	assert CSVFileWriter.TYPE_ERROR_EXCEPTION in str(e.value)
示例#2
0
def test_createFileWriterDefaults():

	output_file = StringIO()
	fixture_value = '1555110000, "whatsapp", "@username", "content"\n'

	fileWriter: CSVFileWriter = CSVFileWriter(output_file)

	fileWriter.write(Line(datetime(2019, 4, 13), "username", "content"))

	output_value = output_file.getvalue()

	assert output_value == fixture_value
示例#3
0
def process() -> "html":

    fileToProcess = None
    original_file = None
    slack_file = StringIO()

    delimiter = request.form["delimiter"]
    channel = request.form["channel"]

    if "whatsapp_file" in request.files:

        file = request.files["whatsapp_file"]

        if file.filename != "":
            fileToProcess = file

    if fileToProcess is None:

        whatsapp_text = request.form["whatsapp_text"]

        if whatsapp_text != "":

            fileToProcess = BytesIO(whatsapp_text.encode("utf-8"))

    if fileToProcess is not None:
        original_file = fileToProcess.read().decode("utf-8")

        fileToProcess.seek(0, 0)

        if isinstance(fileToProcess, FileStorage):
            reader = WhatsAppFileReader(fileToProcess.stream)
        else:
            reader = WhatsAppFileReader(fileToProcess)

        writer = CSVFileWriter(slack_file,
                               channel=channel,
                               delimiter=delimiter)

        try:
            reader.process(writer)
        except Exception as e:
            slack_file = StringIO("ERROR: " + str(e))

    return render_template("results.html",
                           the_title="Welcome to whatsapp_slack on the web!",
                           original_file=original_file,
                           slack_file=slack_file.getvalue(),
                           the_delimiter=delimiter,
                           the_channel=channel)
示例#4
0
def test_close():

	output_file = StringIO()
	fixture_value = '1555110000, "whatsapp", "@username", "content"\n'

	fileWriter: CSVFileWriter = CSVFileWriter(output_file)

	fileWriter.write(Line(datetime(2019, 4, 13), "username", "content"))

	output_value = output_file.getvalue()

	assert output_value == fixture_value

	fileWriter.close()

	with raises(ValueError):
		fileWriter.write(Line(datetime(2019, 4, 13), "username", "content"))
示例#5
0
def test_createFileWriterOverrideUsername(fixture_csv_file):

	output_file = StringIO()

	fileWriter: CSVFileWriter = CSVFileWriter(
		output_file, 
		delimiter="!", 
		channel="test", 
		overrideUsername=True)

	fileWriter.write(Line(datetime(2019, 4, 13), "username", "content"))

	hash_reader = hashlib.md5()
	hash_reader.update(output_file.getvalue().encode("utf-8"))
	output_hash = hash_reader.hexdigest()

	hash_reader = hashlib.md5()
	hash_reader.update(fixture_csv_file.read())
	fixture_hash = hash_reader.hexdigest()

	assert output_hash == fixture_hash
def test_processWithContent(absolute_path, whatsapp_file_path):

    csvFile = open(absolute_path + "/../utilities/slack_original.csv", "r")

    fileReader: Reader = WhatsAppFileReader(whatsapp_file_path)

    contents: StringIO = StringIO()

    fileWriter: Writer = CSVFileWriter(contents,
                                       channel="test-channel",
                                       delimiter="|")

    fileReader.process(fileWriter)

    hash_reader = hashlib.md5()
    hash_reader.update(csvFile.read().encode("utf-8"))
    existing_file_hash = hash_reader.hexdigest()

    hash_reader = hashlib.md5()
    hash_reader.update(contents.getvalue().encode("utf-8"))
    output_file_hash = hash_reader.hexdigest()

    assert existing_file_hash == output_file_hash
示例#7
0
def test_fileNotInWriteMode(fixture_csv_file):

	output_file = open(fixture_csv_file.name, "r")

	with raises(IOError):
		fileWriter: CSVFileWriter = CSVFileWriter(output_file)