-1

I have a folder structure as below. I am trying to have a test folder and import app inside

(base) xadr-a01:redis-example adrianlee$ tree
.
├── Dockerfile
├── README.md
├── __pycache__
│   └── app.cpython-37.pyc
├── config
├── deployment.yaml
├── redis.yaml
├── requirements.txt
├── server
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   └── app.cpython-37.pyc
│   ├── app.py
│   ├── config.py
│   └── templates
│       └── index.html
└── tests
    └── app.test

in my server/app.py, this is my code

#!/usr/bin/python

from flask import Flask, render_template, jsonify
from redis import Redis
import os
import json

app = Flask(__name__)
host = os.environ['REDIS_SERVICE']
redis = Redis(host=host, port=6379)


@app.route("/", methods=["GET"])
@app.route("/index", methods=["GET", "POST"])
def index():
    counter = redis.incr("hits")
    return render_template("index.html",counter=counter)

@app.route("/getdata", methods=["GET"])
def get_data():
    data = redis.get("hits")
    result = int(data)
    return jsonify({'count': result})


if __name__ == "__main__":
    app.run(
        host="0.0.0.0",
        port=8000,
        debug=True
    )

in my tests/app.test, I am trying to import server/app.py.

import unittest
import fakeredis
import json
from server.app import app
import app
from mock import patch, MagicMock

class BasicTestCase(unittest.TestCase):
    counter = 1
    def setUp(self):
        
        self.app = app.app.test_client()
        app.redis = fakeredis.FakeStrictRedis()
        app.redis.set('hits',BasicTestCase.counter)
    
    def create_redis(self):
        return fakeredis.FakeStrictRedis()   

    def test_getdata(self):
        response = self.app.get('/getdata', content_type='html/text')
        data = json.loads(response.get_data(as_text=True))
        expected = {'count': BasicTestCase.counter}
        assert expected == data

    def test_increment(self):
        response = self.app.get('/', content_type='html/text')
        result = self.app.get('/getdata', content_type='html/text')
        data = json.loads(result.get_data(as_text=True))
        expected = {'count': BasicTestCase.counter +1 }
        assert expected == data

    def test_index(self):
        response = self.app.get('/', content_type='html/text')
        self.assertEqual(response.status_code, 200)

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

However, I keep getting a modulenotfounderror. I am unsure why cant i import my app inside the subfolder

  File "app.test", line 4, in <module>
    from server.app import app
ModuleNotFoundError: No module named 'server'

What am I doung wrong?

Adam
  • 1,157
  • 6
  • 19
  • 40
  • Does this answer your question? [How to fix "Attempted relative import in non-package" even with \_\_init\_\_.py](https://stackoverflow.com/questions/11536764/how-to-fix-attempted-relative-import-in-non-package-even-with-init-py) – Equinox Aug 15 '20 at 08:10

1 Answers1

0

I guess you need a relative import. If you write module.app Python tries to import from the same directory. I your case you need to import from the above directory.

Try this:

from ..server.app import app
hedy
  • 1,160
  • 5
  • 23