import unittest
from utils import TextLoader
import numpy as np

class TestUtilsMethods(unittest.TestCase):
    def setUp(self):
        self.data_loader = TextLoader("tests/test_data", batch_size=2, seq_length=5)

    def test_init(self):
      print (self.data_loader.vocab)
      print (self.data_loader.tensor)
      print (self.data_loader.vocab_size)

    def test_build_vocab(self):
        sentences = ["I", "love", "cat", "cat"]
        vocab, vocab_inv = self.data_loader.build_vocab(sentences)
        print (vocab, vocab_inv)

        # Must include I, love, and cat
        self.assertItemsEqual(vocab, ["I", "love", "cat"])
        self.assertDictEqual(vocab, {'I': 0, 'love': 2, 'cat': 1})

        self.assertItemsEqual(vocab_inv, ["I", "love", "cat"])

    def test_batch_vocab(self):
        print (np.array(self.data_loader.x_batches).shape)
        self.assertItemsEqual(self.data_loader.x_batches[0][0][1:],
                              self.data_loader.y_batches[0][0][:-1])
        self.assertItemsEqual(self.data_loader.x_batches[0][1][1:],
                              self.data_loader.y_batches[0][1][:-1])


if __name__ == '__main__':
    unittest.main()
import unittest
from utils import TextLoader
import numpy as np
from collections import Counter

class TestUtilsMethods(unittest.TestCase):
    def setUp(self):
        self.data_loader = TextLoader("tests/test_data", batch_size=2, seq_length=5)

    def test_init(self):
      print (self.data_loader.vocab)
      print (self.data_loader.tensor)
      print (self.data_loader.vocab_size)

    def test_build_vocab(self):
        sentences = ["I", "love", "cat", "cat"]
        vocab, vocab_inv = self.data_loader.build_vocab(sentences)
        print (vocab, vocab_inv)

        # Must include I, love, and cat
        self.assertEqual(Counter(list(vocab)), Counter(list(["I", "love", "cat"])))
        self.assertDictEqual(vocab, {'I': 0, 'love': 2, 'cat': 1})

        self.assertEqual(Counter(list(vocab_inv)), Counter(list(["I", "love", "cat"])))

    def test_batch_vocab(self):
        print (np.array(self.data_loader.x_batches).shape)
        self.assertEqual(Counter(list(self.data_loader.x_batches[0][0][1:])),
                              Counter(list(self.data_loader.y_batches[0][0][:-1])))
        self.assertEqual(Counter(list(self.data_loader.x_batches[0][1][1:])),
                              Counter(list(self.data_loader.y_batches[0][1][:-1])))


if __name__ == '__main__':
    unittest.main()

Generalize method names to be compatible with Python 2.7 and 3.4
Generalize method names to be compatible with Python 2.7 and 3.4

  import unittest
  from utils import TextLoader
  import numpy as np
+ from collections import Counter
  
  class TestUtilsMethods(unittest.TestCase):
      def setUp(self):
          self.data_loader = TextLoader("tests/test_data", batch_size=2, seq_length=5)
  
      def test_init(self):
        print (self.data_loader.vocab)
        print (self.data_loader.tensor)
        print (self.data_loader.vocab_size)
  
      def test_build_vocab(self):
          sentences = ["I", "love", "cat", "cat"]
          vocab, vocab_inv = self.data_loader.build_vocab(sentences)
          print (vocab, vocab_inv)
  
          # Must include I, love, and cat
-         self.assertItemsEqual(vocab, ["I", "love", "cat"])
+         self.assertEqual(Counter(list(vocab)), Counter(list(["I", "love", "cat"])))
          self.assertDictEqual(vocab, {'I': 0, 'love': 2, 'cat': 1})
  
-         self.assertItemsEqual(vocab_inv, ["I", "love", "cat"])
+         self.assertEqual(Counter(list(vocab_inv)), Counter(list(["I", "love", "cat"])))
  
      def test_batch_vocab(self):
          print (np.array(self.data_loader.x_batches).shape)
-         self.assertItemsEqual(self.data_loader.x_batches[0][0][1:],
+         self.assertEqual(Counter(list(self.data_loader.x_batches[0][0][1:])),
-                               self.data_loader.y_batches[0][0][:-1])
+                               Counter(list(self.data_loader.y_batches[0][0][:-1])))
-         self.assertItemsEqual(self.data_loader.x_batches[0][1][1:],
+         self.assertEqual(Counter(list(self.data_loader.x_batches[0][1][1:])),
-                               self.data_loader.y_batches[0][1][:-1])
+                               Counter(list(self.data_loader.y_batches[0][1][:-1])))
  
  
  if __name__ == '__main__':
      unittest.main()
+ 
Generalize method names to be compatible with Python 2.7 and 3.4
## Code Before:
import unittest
from utils import TextLoader
import numpy as np

class TestUtilsMethods(unittest.TestCase):
    def setUp(self):
        self.data_loader = TextLoader("tests/test_data", batch_size=2, seq_length=5)

    def test_init(self):
      print (self.data_loader.vocab)
      print (self.data_loader.tensor)
      print (self.data_loader.vocab_size)

    def test_build_vocab(self):
        sentences = ["I", "love", "cat", "cat"]
        vocab, vocab_inv = self.data_loader.build_vocab(sentences)
        print (vocab, vocab_inv)

        # Must include I, love, and cat
        self.assertItemsEqual(vocab, ["I", "love", "cat"])
        self.assertDictEqual(vocab, {'I': 0, 'love': 2, 'cat': 1})

        self.assertItemsEqual(vocab_inv, ["I", "love", "cat"])

    def test_batch_vocab(self):
        print (np.array(self.data_loader.x_batches).shape)
        self.assertItemsEqual(self.data_loader.x_batches[0][0][1:],
                              self.data_loader.y_batches[0][0][:-1])
        self.assertItemsEqual(self.data_loader.x_batches[0][1][1:],
                              self.data_loader.y_batches[0][1][:-1])


if __name__ == '__main__':
    unittest.main()
## Instruction:
Generalize method names to be compatible with Python 2.7 and 3.4
## Code After:
import unittest
from utils import TextLoader
import numpy as np
from collections import Counter

class TestUtilsMethods(unittest.TestCase):
    def setUp(self):
        self.data_loader = TextLoader("tests/test_data", batch_size=2, seq_length=5)

    def test_init(self):
      print (self.data_loader.vocab)
      print (self.data_loader.tensor)
      print (self.data_loader.vocab_size)

    def test_build_vocab(self):
        sentences = ["I", "love", "cat", "cat"]
        vocab, vocab_inv = self.data_loader.build_vocab(sentences)
        print (vocab, vocab_inv)

        # Must include I, love, and cat
        self.assertEqual(Counter(list(vocab)), Counter(list(["I", "love", "cat"])))
        self.assertDictEqual(vocab, {'I': 0, 'love': 2, 'cat': 1})

        self.assertEqual(Counter(list(vocab_inv)), Counter(list(["I", "love", "cat"])))

    def test_batch_vocab(self):
        print (np.array(self.data_loader.x_batches).shape)
        self.assertEqual(Counter(list(self.data_loader.x_batches[0][0][1:])),
                              Counter(list(self.data_loader.y_batches[0][0][:-1])))
        self.assertEqual(Counter(list(self.data_loader.x_batches[0][1][1:])),
                              Counter(list(self.data_loader.y_batches[0][1][:-1])))


if __name__ == '__main__':
    unittest.main()
