Testing your Swift codables with Python

Testing your Swift codables example image
Hallo collega’s, Leo hier. The topic is Testing your Swift codables with Python to fast check if your codable objects work.
The problem we will face today is something that every iOS Developer eventually encounters. You start your sprint (if you still do sprints and not a kanban-like development), and the backend is not ready yet. So you sit down with them and hopefully can outline what the JSON will look like. So you go back to your desk and think: How I can be sure that this codable parse the JSON correctly?
Today we will check how can we do this with Python! And without Python too.
The next two weeks will be pretty hard/intense for me. So maybe or not I’ll have time/energy/willpower to post something here, I hope everything is back to normal from mid-November and so on.
That’s said, let’s code! But first… The painting.

Painting of The Day

This painting is called “Coal cars” an 1821 art piece by Théodore Géricault. Géricault’s fiery, daring personality and short life, fit the mold of Romantic artists of his era and, along with his controversial paintings, profoundly influenced 19th-century art.
I choose this painting because it demonstrates hard-working people in the coal mines, and my next weeks will be intense, so I found that fits into my actual situation.

The Problem – Testing your Swift codables

You have a JSON structure and need to test decoding them with your codables.
Imagine that you have a JSON structure to try to decode, like this one:
  "id": "1",
  "title": "Star Wars!",
  "genre": "Syfi"
For the sake of simplicity, this is not a complex JSON. But the idea is the same for a big one.
And you write a codable movie object like this:
struct Movie: Codable {
    let id: Int 
    let title: String
    let genre: String
How can you quickly know this is wrong?

Python Solution

In the Python solution, we will use a pretty little command in Terminal. We will first create a file, and serve it with a Python server, so any Swift code can read it!
First, create a file called index.json and put this into it:
   "id": "1",
   "title": "Star Wars!",
   "genre": "Syfi"
Open the Terminal and navigate to the folder you create the index.json file.
Now type this in Terminal: python -m SimpleHTTPServer 8100. But wait a minute, what is this doing? It’s literally saying:
Hey Mr. Python can you use your module SimpleHTTPServer to create a HTTP server, serving everything in my folder on the port 8100, please?
And that’s exactly what it does. With this simple statement, you now have an HTTP server that you can reach with your Swift code and test your codable against it!
So now, in the Playgrounds you can just test:
let url = URL(string: "http://localhost:8100/index.json")!
let data = try! Data(contentsOf: url)
let decoder = JSONDecoder()

let movie = try! decoder.decode(Movie.self, from: data)

And the result is…. oh wait:
Testing your codables error example
It’s saying our model is wrong in codingKey “id”, it expected to decode Int but found a string/data instead. Let’s fix it by changing from Int to String!
struct Movie: Codable {
    let id: String
    let title: String
    let genre: String
Now it’s perfect!
Testing your codables success image example
This way when the backend is ready to test, you already know that at least the JSON both sides agreed is parsing correctly. Awesome right?
The python solution is great when you need to implement something into legacy code that is hard to modify the network layer, so you will need to add this new HTTP request to the localhost and the codable, and you are ready to test. But of course, if you have an easy networking layer, you can go with a simpler Swift solution that you will check below.

Only Swift Solution

If you can easily access the networking layer of your app you can just modify the data received like this:
let json = """
    "id": "1",
    "title": "Star Waors!",
    "genre": "Sci-Fi"

let data = json.data(using: .utf8)
let movie = try? JSONDecoder().decode(Movie.self, from: data!)

And the result is pretty much the same!
Testing your codables success 2 image example
And that’s it!

Summary – Testing your Swift codables

Today we study a fast way to create an HTTP Server in Python and how this can be useful to test your HTTP requests. Also, we checked a fully Swift way to check and parse your codable to be sure that your models are up to date with the backend.

That’s all my people, I hope you liked reading this article as much as I enjoyed writing it. If you want to support this blog you can Buy Me a Coffee or leave a comment saying hello. You can also sponsor posts and I’m open to freelance writing! You can reach me on LinkedIn or Twitter and send me an e-mail through the contact page.

Thanks for reading and… That’s all folks.


title image

Share this post:

Related posts