Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This assignment has you working with a SQLight database and unittest code. Write a Python module, first, in which three functions are defined to create

image text in transcribed
image text in transcribed
This assignment has you working with a SQLight database and unittest code. Write a Python module, first, in which three functions are defined to create or access a database file myda tabase.db. Function create_database () makes a connection to mydatabase.dh, creates a cursor, and makes a table mytable with columns employee (type TEXT) and amount (type REAL). It populates the table with data from file results.txt (available from the Blackboard page for this assignment), whose first few lines are Fred,5.73 Sue, 3.56 Ed, 7.43 Ann, 9.23 Each line has the employee value, a comma, then the amount value. You can use the following to remove the new line at the end of a line and to split the resulting string into the employee value, emp, and the string version of the amount value, amt. amp, amt = line.replace(" ', '').split(",) Note that amt must be converted to a float before it is inserted into the table. To insert these values, use a prepared statement that is executed for each employee-amount pair. INSERT INTO mytable (employee, amount) VALUES (?, ?); Function aggregate_amount (emps), where emps is a list of employees, returns the sum of the amounts associated with the employees in this list. You loop over the employees in this list, fetching one amount value on each iteration and adding it to the accumulating sum, which is returned once we have gone through the list of employees. Use a prepared SELECT statement with a WHERE clause having "employee = ?". When this is executed, it expects one value, which is provided by a singleton tuple of the form (e.). The value fetched from the execution is a single tuple; the number is accessed by indexing to position 0. Function average_amount() returns the average of the values in the amount column of mytable. You will have a SELECT command that lets us fetch the list of all amount values as singleton tuples. To calculate the average, you must extract the values from the tuples, sum them, and divide the result by the length of the list. Include the following test code at the end of you file. 11 _main__': create_database 0) print (aggregate_amount('Fred', 'Ed', 'Peg'])) print("{0:28)". format(average_amount())) Manually remove mydatabase.db after each run. Write a test file, test_my_db.py, for your database functions. Call the testcase TestMyDataBase. The setUp() method can simply call first.create_database (). The tear Down() method can use the remove() function from the os module to delete myda tabase.db. And it should write an entry in the file log.txt. This file (also available from the Blackboard page) starts out with a single line Log of executions of tests name . The entry produced by teardown() starts with the id of the method (use self.id()), followed by a comma, followed by the date and time in the format YYYY-MM-DD HH:MM:SS. The following is an example of the content of this file after the test file has been executed once. Log of executions of tests test_my_db. TestMyDatabase. test_aggregate_amount, 2020-02-24 22:24 test_my_db. TestMyDatabase. test_average_amount, 2020-02-24 22:26 (We describe how to get current values for the data and time components below.) Note that file log.txt should be opened for appending, mode a. Write test methods for functions aggregate_amount() and average_amount(). For the former, pass the list ['Fred', 'Ed', 'Peg'] to aggregate_amount() (the list used in the test code attached to first.py), and compare the results to 20.08. Since these are floating-point values, assertAlmostEqual() with places=2. For testing average_amount(), compare the value returned to 5.59, again using assertAlmostEqual() with places=2. Class datetime in Module datetime To have unqualified access to the class datetime in module datetime, use from datetime import datetime A class method of this class is now(), which returns a datetime object with the components of the current date and time as attributes. These attributes have self-explanatory names: year, month, day, hour (0-23), minute, second, microsecond. The following shows how to get a date-time in the desired format. >>> print (f' (datetime.now(): Y-tm-id :)) use 2021-03-03 23:55

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

More Books

Students also viewed these Databases questions