def test_partial_derivative_calculation(self): equation = sympy.sympify('x+y') plotter = FieldPlotter(equation) X, Y, U, V = plotter._calc_partials() for i, x in enumerate(plotter.xrange): for j, y in enumerate(plotter.yrange): self.assertEqual(x + y < 0, V[j][i] < 0)
def test_make_data(self): equation = sympy.sympify('x*y') plotter = FieldPlotter(equation) data = plotter.make_data() self.assertIsInstance(data, dict) self.assertEqual(set(data.keys()), set(plotter.xrange)) for x in plotter.xrange: self.assertEqual(set(data[x].keys()), set(plotter.yrange))
def data(): equation_string = flask.request.args.get('equation') plotter = FieldPlotter() plotter.set_equation_from_string(equation_string) json_data = plotter.json_data() response = flask.make_response(json_data) response.mimetype = 'text/json' return response
def test_set_equation_from_string_fail(self): equation_str = 'x***y' plotter = FieldPlotter() # Test that error is raised when not told to fail silently with self.assertRaises(sympy.SympifyError): plotter.set_equation_from_string(equation_str) # Test silent failure plotter.set_equation_from_string(equation_str, fail_silently=True) self.assertIsNone(plotter.equation)
def test_set_equation_from_string(self): equations = [ 'x', 'y', 'x+y', '2*x', 'x*y', 'x/y', ] plotter = FieldPlotter() for equation_str in equations: plotter.set_equation_from_string(equation_str) sympy_eq = sympy.sympify(equation_str) self.assertEqual(plotter.equation, sympy_eq)
def plot(): equation_string = flask.request.args.get('equation') diff_equation = FieldPlotter() diff_equation.set_equation_from_string(equation_string) diff_equation.make_plot() # If plotting was successful, write plot out if diff_equation.figure: # Write output to memory and add to response object output = StringIO.StringIO() response = flask.make_response(base64.b64encode(diff_equation.write_data(output))) response.mimetype = 'image/png' return response else: return flask.make_response('')
def test_make_plot(self): equation = sympy.sympify('x+y') plotter = FieldPlotter(equation) plotter.make_plot() self.assertIsNotNone(plotter.figure)
def test_set_equation_from_string_timeout(self): equation_str = '9**9**9**9**9**9' plotter = FieldPlotter() plotter.set_equation_from_string(equation_str) self.assertIsNone(plotter.equation)
def test_set_equation(self): plotter = FieldPlotter() equation = sympy.sympify('2*x + 3*y') plotter.set_equation(equation) self.assertEqual(equation, plotter.equation)
def test_json_data(self): equation = sympy.sympify('x*y') plotter = FieldPlotter(equation) json_data = plotter.json_data()
def test_missing_equation(self): plotter = FieldPlotter() with self.assertRaises(FieldPlotter.MissingEquationError): plotter.make_plot()