Sling Academy
Home/Golang/Writing Unit Tests for Complex Map-Based Logic in Go

Writing Unit Tests for Complex Map-Based Logic in Go

Last updated: November 26, 2024

Unit testing is essential to verify that your code behaves as expected, especially when dealing with complex map-based logic in Go. This article guides you through the process of writing unit tests for map-based logic, starting with basic examples and advancing to more sophisticated scenarios.

Introduction to Unit Testing in Go

Go comes with a robust testing framework out of the box, located in the testing package. A basic understanding of this package will help you write unit tests efficiently.

Basic Unit Testing with Maps

Let's start by creating a simple function that uses a map and write a unit test for it.

package main

type CityPopulation map[string]int

func TotalPopulation(cities CityPopulation) int {
    total := 0
    for _, population := range cities {
        total += population
    }
    return total
}

Now, let's write a basic test for this function.

package main

import (
    "testing"
)

func TestTotalPopulation(t *testing.T) {
    cities := CityPopulation{"New York": 8175133, "Los Angeles": 3792621}
    expected := 8175133 + 3792621
    result := TotalPopulation(cities)
    if result != expected {
        t.Errorf("Expected %d but got %d", expected, result)
    }
}

Intermediate Map-Based Logic Testing

Next, consider a situation where map logic includes specific business rules, such as filtering cities by a minimum population threshold.

package main

func FilterCitiesByPopulation(cities CityPopulation, minPopulation int) CityPopulation {
    result := CityPopulation{}
    for city, population := range cities {
        if population >= minPopulation {
            result[city] = population
        }
    }
    return result
}

Now we test this refined logic.

package main

func TestFilterCitiesByPopulation(t *testing.T) {
    cities := CityPopulation{"New York": 8175133, "Los Angeles": 3792621, "Columbia": 500000}
    minPopulation := 1000000
    expected := CityPopulation{"New York": 8175133, "Los Angeles": 3792621}
    result := FilterCitiesByPopulation(cities, minPopulation)
    if len(result) != len(expected) {
        t.Errorf("Expected length %d but got %d", len(expected), len(result))
    }
    for city, population := range expected {
        if result[city] != population {
            t.Errorf("Expected population of %s to be %d but got %d", city, population, result[city])
        }
    }
}

Advanced Testing Scenarios

For more advanced scenarios, consider the possibility of concurrent map access or handling maps with nested data structures. Let's look at an advanced example.

package main

type ContinentMap map[string]CityPopulation

func TotalContinentPopulation(continent ContinentMap) int {
    total := 0
    for _, countries := range continent {
        total += TotalPopulation(countries)
    }
    return total
}

Then, you would test such logic, possibly using go test with additional tools to check for race conditions in more complex cases.

package main

func TestTotalContinentPopulation(t *testing.T) {
    europe := ContinentMap{
        "Germany": CityPopulation{"Berlin": 3644826},
        "France": CityPopulation{"Paris": 2140526}
    }
    expected := 3644826 + 2140526
    result := TotalContinentPopulation(europe)
    if result != expected {
        t.Errorf("Expected %d but got %d", expected, result)
    }
}

Conclusion

Testing complex map-based logic in Go requires attention to detail and an understanding of the data structures involved. By following the examples laid out in this article, you will be better equipped to write effective tests for any map-based logic you implement in your Go projects.

Next Article: Designing Custom Sorting Functions for Map Keys in Go

Previous Article: Using Maps for Multi-Language Support in Go Applications

Series: Working with Maps in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant