from unittest import TestCase
from ppp_datamodel.communication import Request
from ppp_datamodel import Triple, Resource, Missing, Sentence
from ppp_libmodule.tests import PPPTestCase

from ppp_spell_checker import app

class RequestHandlerTest(PPPTestCase(app)):
    def testCorrectSentence(self):
        original = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': original}}
        answer = self.request(j)
        self.assertEquals(len(answer), 0)

    def testWrongSentence(self):
        original = 'Who is the pesident of the United States'
        expected = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': original}}
        answer = self.request(j)
        self.assertEquals(len(answer), 1)
        self.assertIsInstance(answer[0].tree, Sentence)
        result = answer[0].tree.__getattr__('value')
        self.assertEqual(result, expected)

    def testIrrelevantInput(self):
        original = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'resource', 'value': original}}
        answer = self.request(j)
        self.assertEquals(len(answer), 0)

from unittest import TestCase
from ppp_datamodel.communication import Request
from ppp_datamodel import Triple, Resource, Missing, Sentence
from ppp_libmodule.tests import PPPTestCase

from ppp_spell_checker import app

class RequestHandlerTest(PPPTestCase(app)):
    def testCorrectSentence(self):
        original = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': original}}
        answer = self.request(j)
        self.assertEqual(len(answer), 0, answer)

    def testWrongSentence(self):
        original = 'Who is the pesident of the United States'
        expected = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': original}}
        answer = self.request(j)
        self.assertEqual(len(answer), 1, answer)
        self.assertEqual(answer[0].tree, Sentence(value=expected))

    def testIrrelevantInput(self):
        original = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'resource', 'value': original}}
        answer = self.request(j)
        self.assertEqual(len(answer), 0, answer)

Write tests in a better way
Write tests in a better way

  from unittest import TestCase
  from ppp_datamodel.communication import Request
  from ppp_datamodel import Triple, Resource, Missing, Sentence
  from ppp_libmodule.tests import PPPTestCase
  
  from ppp_spell_checker import app
  
  class RequestHandlerTest(PPPTestCase(app)):
      def testCorrectSentence(self):
          original = 'Who is the president of the United States'
          j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
               'tree': {'type': 'sentence', 'value': original}}
          answer = self.request(j)
-         self.assertEquals(len(answer), 0)
+         self.assertEqual(len(answer), 0, answer)
  
      def testWrongSentence(self):
          original = 'Who is the pesident of the United States'
          expected = 'Who is the president of the United States'
          j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
               'tree': {'type': 'sentence', 'value': original}}
          answer = self.request(j)
-         self.assertEquals(len(answer), 1)
+         self.assertEqual(len(answer), 1, answer)
-         self.assertIsInstance(answer[0].tree, Sentence)
+         self.assertEqual(answer[0].tree, Sentence(value=expected))
-         result = answer[0].tree.__getattr__('value')
-         self.assertEqual(result, expected)
  
      def testIrrelevantInput(self):
          original = 'Who is the president of the United States'
          j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
               'tree': {'type': 'resource', 'value': original}}
          answer = self.request(j)
-         self.assertEquals(len(answer), 0)
+         self.assertEqual(len(answer), 0, answer)
  
Write tests in a better way
## Code Before:
from unittest import TestCase
from ppp_datamodel.communication import Request
from ppp_datamodel import Triple, Resource, Missing, Sentence
from ppp_libmodule.tests import PPPTestCase

from ppp_spell_checker import app

class RequestHandlerTest(PPPTestCase(app)):
    def testCorrectSentence(self):
        original = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': original}}
        answer = self.request(j)
        self.assertEquals(len(answer), 0)

    def testWrongSentence(self):
        original = 'Who is the pesident of the United States'
        expected = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': original}}
        answer = self.request(j)
        self.assertEquals(len(answer), 1)
        self.assertIsInstance(answer[0].tree, Sentence)
        result = answer[0].tree.__getattr__('value')
        self.assertEqual(result, expected)

    def testIrrelevantInput(self):
        original = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'resource', 'value': original}}
        answer = self.request(j)
        self.assertEquals(len(answer), 0)

## Instruction:
Write tests in a better way
## Code After:
from unittest import TestCase
from ppp_datamodel.communication import Request
from ppp_datamodel import Triple, Resource, Missing, Sentence
from ppp_libmodule.tests import PPPTestCase

from ppp_spell_checker import app

class RequestHandlerTest(PPPTestCase(app)):
    def testCorrectSentence(self):
        original = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': original}}
        answer = self.request(j)
        self.assertEqual(len(answer), 0, answer)

    def testWrongSentence(self):
        original = 'Who is the pesident of the United States'
        expected = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'sentence', 'value': original}}
        answer = self.request(j)
        self.assertEqual(len(answer), 1, answer)
        self.assertEqual(answer[0].tree, Sentence(value=expected))

    def testIrrelevantInput(self):
        original = 'Who is the president of the United States'
        j = {'id': '1', 'language': 'en', 'measures': {}, 'trace': [],
             'tree': {'type': 'resource', 'value': original}}
        answer = self.request(j)
        self.assertEqual(len(answer), 0, answer)
