'Test that setting a field in a missing StudentModule creates the student module'

def test_set_field_in_missing_student_module(self):

    self.assertEquals(0, len(self.field_data_cache))
    self.assertEquals(0, StudentModule.objects.all().count())
    with self.assertNumQueries(4, using='default'):
        with self.assertNumQueries(1, using='student_module_history'):
            self.kvs.set(user_state_key('a_field'), 'a_value')
    self.assertEquals(1, sum((len(cache) for cache in self.field_data_cache.cache.values())))
    self.assertEquals(1, StudentModule.objects.all().count())
    student_module = StudentModule.objects.all()[0]
    self.assertEquals({'a_field': 'a_value'}, json.loads(student_module.state))
    self.assertEquals(self.user, student_module.student)
    self.assertEquals(location('usage_id').replace(run=None), student_module.module_state_key)
    self.assertEquals(course_id, student_module.course_id)
