DEV Community

Cover image for Code Smell 117 - Unrealistic Data
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

Code Smell 117 - Unrealistic Data

Programmers are lazy and seldom try to learn from real business domains

TL;DR: Use real case scenarios and real data (when possible)

Problems

  • Bijection Violation

  • Bad test use cases

  • Readability

Solutions

  1. Change test data for a real one.

  2. Use MAPPER to map real entities and real data.

Context

In the past, developers used to fake domain data.

We considered Hello Word a good practice and we tested with abstract data.

We developed using a waterfall model very far from real users.

With bijection and MAPPER techniques, DDD and TDD, user acceptance testing became more important.

Using Agile methodologies, we need to test with real-world data.

If we find an error in a production system, we need to add a case covering the exact mistake with real data.

Sample Code

Wrong

class BookCartTestCase(unittest.TestCase):
    def setUp(self):
        self.cart = Cart()

    def test_add_book(self):
       self.cart.add_item('xxxxx', 3, 10)
        #This is not a real example

       self.assertEqual(self.cart.total, 30, msg='Book Cart total not correct after adding books')
       self.assertEqual(self.cart.items['xxxxx'], 3, msg='Quantity of items not correct after adding book')

    def test_remove_item(self):
        self.cart.add_item('fgdfhhfhhh', 3, 10)
        self.cart.remove_item('fgdfhhfhrhh', 2, 10)    
        #We made a typo since example is not a real one
        self.assertEqual(self.cart.total, 10, msg='Book Cart total not correct after removing book')
        self.assertEqual(self.cart.items['fgdfhhfhhh'], 1, msg='Quantity of books not correct after removing book')
Enter fullscreen mode Exit fullscreen mode

Right

class BookCartTestCase(unittest.TestCase):
    def setUp(self):
        self.cart = Cart()

    def test_add_book(self):
       self.cart.add_item('Harry Potter', 3, 10)

       self.assertEqual(self.cart.total, 30, msg='Book Cart total not correct after adding books')
       self.assertEqual(self.cart.items['Harry Potter'], 3, msg='Quantity of items not correct after adding book')

    #We don't reuse same example. 
    #We use a new REAL book
    def test_remove_item(self):
        self.cart.add_item('Divergent', 3, 10)
        self.cart.remove_item('Divergent', 2, 10)    
        self.assertEqual(self.cart.total, 10, msg='Book Cart total not correct after removing book')
        self.assertEqual(self.cart.items['Divergent'], 1, msg='Quantity of books not correct after removing book')

Enter fullscreen mode Exit fullscreen mode

Detection

[X] Manual

This is a semantic smell.

Tags

  • Testing

Conclusion

Code comments are a code smell.

Reading tests is the only way to learn how the software behaves.

We need to be extra explicit on our tests.

Exceptions

On some domains and under regulation we cannot use real data.

We should fake it with meaningful data.

Relations

More Info

Credits

Photo by Hofmann Natalia on Unsplash

Thanks to Curtis Einsmann


You do not really understand something unless you can explain it to your grandmother.

Albert Einstein


This article is part of the CodeSmell Series.

Top comments (0)