Skip to content

Latest commit

 

History

History
166 lines (109 loc) · 5.48 KB

File metadata and controls

166 lines (109 loc) · 5.48 KB

Code Smell 117 - Unrealistic Data

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.cart.emailAddress('sarsasa')
        # This is not a real email

       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)    
        # You 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')

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.cart.emailAddress('seller@example.com')
       
       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')

    # You don't reuse same example.
    # You 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')

Detection 🔍

[X] Manual

This is a semantic smell.

Exceptions 🛑

On some domains and under regulation we can't use real data.

We should fake it with meaningful data.

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.

Relations 👩‍❤️‍💋‍👨

Code Smell 05 - Comment Abusers

More Information 📕

Credits 🙏

Photo by Hofmann Natalia on Unsplash

Thanks to Curtis Einsmann


You don't really understand something unless you can explain it to your grandmother.

Albert Einstein

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of Your Code